summaryrefslogtreecommitdiff
path: root/library/backend/event.h
blob: 118523fdd76ffad4b3b06e3fff2fe210692735da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**********************************************************************
** Copyright (C) 2000-2002 Trolltech AS.  All rights reserved.
**
** This file is part of the Qtopia Environment.
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
** See http://www.trolltech.com/gpl/ for GPL licensing information.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
**********************************************************************/

#ifndef __EVENT_H__
#define __EVENT_H__

#include <qdatetime.h>
#include <qvaluelist.h>
#include <qcolor.h>

#ifdef PALMTOPCENTER
#include <qpc/qsorter.h>
#endif
#include <qtopia/private/palmtoprecord.h>

#include <qpe/timeconversion.h>

static const QColor colorNormal      = QColor(255, 0  , 0  );
static const QColor colorRepeat      = QColor(0  , 0  , 255);
static const QColor colorNormalLight = QColor(255, 220, 220);
static const QColor colorRepeatLight = QColor(200, 200, 255);

class EventPrivate;
class QPC_EXPORT Event : public Qtopia::Record
{
public:
    enum RepeatType { NoRepeat = -1, Daily, Weekly, MonthlyDay,
                      MonthlyDate, Yearly };

    // Don't use this.
    enum Days { MON = 0x01, TUE = 0x02, WED = 0x04, THU = 0x08,
                FRI = 0x10, SAT = 0x20, SUN = 0x40 };
    // Don't use this.
    struct QPC_EXPORT RepeatPattern
    {
	RepeatPattern() {
	    type = NoRepeat; frequency = -1; days = 0;  position = 0; createTime = -1;
	    hasEndDate = FALSE; endDateUTC = 0; }
	bool operator ==( const RepeatPattern &right ) const;

	RepeatType type;
	int frequency;
	int position;	// the posistion in the month (e.g. the first sunday, etc) positive, count from the front negative count from the end...
	char days;  // a mask for days OR in your days!
	bool hasEndDate;
	QDate endDate() const { return TimeConversion::fromUTC( endDateUTC ).date(); }
	void setEndDate( const QDate &dt ) { endDateUTC = TimeConversion::toUTC( dt ); }
	time_t endDateUTC;
	time_t createTime;
    };

    Event();
    Event( const QMap<int, QString > & map );
    virtual ~Event();

    QMap<int, QString> toMap() const;

    static void writeVCalendar( const QString &filename, const QValueList<Event> &events);
    static void writeVCalendar( const QString &filename, const Event &event);
    static QValueList<Event> readVCalendar( const QString &filename );

    enum Type { Normal, AllDay };
    enum SoundTypeChoice { Silent, Loud };

    // Don't use these, there are essentially meaningless.
    bool operator<( const Event &e1) const { return start() < e1.start(); };
    bool operator<=( const Event &e1 ) const { return start() <= e1.start(); };
    bool operator!=( const Event &e1 ) const { return !( *this == e1 ); };
    bool operator>( const Event &e1 ) const { return start() > e1.start(); };
    bool operator>=(const Event &e1 ) const { return start() >= e1.start(); };
    bool operator==( const Event &e ) const;

    void setDescription( const QString &s );
    const QString &description() const;

    void setLocation( const QString &s );
    const QString &location() const;

    void setNotes( const QString &n );
    const QString &notes() const;

    void setType( Type t ); // Don't use me.
    Type type() const; // Don't use me.

    void setAllDay(bool);
    bool isAllDay() const;

    void setStart( const QDateTime &d );
    void setStart( time_t time ); // don't use me.
    QDateTime start( bool actual = FALSE) const; // don't use me.
    time_t startTime() const { return startUTC; } // don't use me.
    void setEnd( const QDateTime &e );
    void setEnd( time_t time ); // don't use me
    QDateTime end( bool actual = FALSE ) const; // don't use me.
    time_t endTime() const { return endUTC; } // don't use me.
    void setTimeZone( const QString & );
    const QString &timeZone() const;
    void setAlarm( int minutes, SoundTypeChoice );
    void clearAlarm();
    void setAlarm( bool b, int minutes, SoundTypeChoice ); // Don't use me.
    bool hasAlarm() const;
    int alarmDelay() const;
    int alarmTime() const; // Don't use me.
    SoundTypeChoice alarmSound() const;

    RepeatType repeatType() const;
    int frequency() const;
    int weekOffset() const;
    QDate repeatTill() const;
    bool repeatForever() const;
    bool repeatOnWeekDay(int day) const;

    void setRepeatType(RepeatType);
    void setFrequency(int);
    void setRepeatTill(const QDate &);
    void setRepeatForever(bool);
    void setRepeatOnWeekDay(int day, bool enable);

    // Don't use any of these.
    void setRepeat( bool b, const RepeatPattern &p );
    void setRepeat( const RepeatPattern &p );
    bool hasRepeat() const;
    const RepeatPattern &repeatPattern() const;
    RepeatPattern &repeatPattern();
    bool doRepeat() const { return pattern.type != NoRepeat; }

    void save( QString& buf );
    //void load( Node *n );

    bool match( const QRegExp &r ) const;

    // Don't use these either.  Functionality will be moved elsewhere.

    // helper function to calculate the week of the given date
    static int week( const QDate& date );
    // calculates the number of occurrences of the week day of
    // the given date from the start of the month
    static int occurrence( const QDate& date );
    // returns a proper days-char for a given dayOfWeek()
    static char day( int dayOfWeek ) { return 1 << ( dayOfWeek - 1 ); }
    // returns the dayOfWeek for the *first* day it finds (ignores
    // any further days!). Returns 1 (Monday) if there isn't any day found
    static int dayOfWeek( char day );
    // returns the difference of months from first to second.
    static int monthDiff( const QDate& first, const QDate& second );

private:
    Qtopia::UidGen &uidGen() { return sUidGen; }
    static Qtopia::UidGen sUidGen;

    QString descript, locat, categ;
    Type typ : 4;
    bool startTimeDirty : 1;
    bool endTimeDirty : 1;
    time_t startUTC, endUTC;
    QString tz;
    bool hAlarm, hRepeat;
    int aMinutes;
    SoundTypeChoice aSound;
    RepeatPattern pattern;
    QString note;
    // ADDITION
    int mRid;	// Recode ID
    int mRinfo;	// Recode Info
    //
    EventPrivate *d;

};

// Since an event spans multiple day, it is better to have this
// class to represent a day instead of creating many
// dummy events...

class EffectiveEventPrivate;
class QPC_EXPORT EffectiveEvent
{
public:
    // If we calculate the effective event of a multi-day event
    // we have to figure out whether we are at the first day,
    // at the end, or anywhere else ("middle"). This is important
    // for the start/end times (00:00/23:59)
    // MidWay: 00:00 -> 23:59, as we are "in the middle" of a multi-
    //         day event
    // Start: start time -> 23:59
    // End: 00:00 -> end time
    // Start | End == StartEnd: for single-day events (default)
    //                          here we draw start time -> end time
    enum Position { MidWay = 0, Start = 1, End = 2, StartEnd = 3 };

    EffectiveEvent();
    EffectiveEvent( const Event &event, const QDate &startDate, Position pos = StartEnd );
    EffectiveEvent( const EffectiveEvent & );
    EffectiveEvent& operator=( const EffectiveEvent & );
    ~EffectiveEvent();


    bool operator<( const EffectiveEvent &e ) const;
    bool operator<=( const EffectiveEvent &e ) const;
    bool operator==( const EffectiveEvent &e ) const;
    bool operator!=( const EffectiveEvent &e ) const;
    bool operator>( const EffectiveEvent &e ) const;
    bool operator>= ( const EffectiveEvent &e ) const;

    void setStart( const QTime &start );
    void setEnd( const QTime &end );
    void setEvent( Event e );
    void setDate( const QDate &date );
    void setEffectiveDates( const QDate &from, const QDate &to );

    //    QString category() const;
    const QString &description() const;
    const QString &location() const;
    const QString &notes() const;
    const Event &event() const;
    const QTime &start() const;
    const QTime &end() const;
    const QDate &date() const;
    int length() const;
    int size() const;

    QDate startDate() const;
    QDate endDate() const;

private:
    class EffectiveEventPrivate *d;
    Event mEvent;
    QDate mDate;
    QTime mStart,
	  mEnd;

};

inline void Event::setAlarm( int minutes, SoundTypeChoice s )
{
    setAlarm(TRUE, minutes, s);
}

inline void Event::clearAlarm()
{
    setAlarm(FALSE, 0, Silent);
}

inline int Event::alarmDelay() const
{
    return alarmTime();
}

inline void Event::setAllDay(bool enable)
{
    if (enable)
	setType(AllDay);
    else
	setType(Normal);
};

inline bool Event::isAllDay() const
{
    return type() == AllDay;
}

inline Event::RepeatType Event::repeatType() const
{
    return repeatPattern().type;
}

inline int Event::frequency() const
{
    return repeatPattern().frequency;
}

inline int Event::weekOffset() const
{
    if (start().date().day() == 1)
	return 1;
    return (start().date().day() - 1) / 7 + 1;
}

inline QDate Event::repeatTill() const
{
    return repeatPattern().endDate();
}

inline bool Event::repeatForever() const
{
    return !repeatPattern().hasEndDate;
}

inline void Event::setRepeatType(RepeatType t)
{
    pattern.type = t;
}

inline void Event::setFrequency(int f)
{
    pattern.frequency = f;
}

inline void Event::setRepeatTill(const QDate &d)
{
    pattern.setEndDate(d);
    pattern.hasEndDate = TRUE;
}

inline void Event::setRepeatForever(bool b)
{
    if (!b == pattern.hasEndDate)
	return;
    if (!b && !pattern.hasEndDate)
	pattern.setEndDate(end().date());
    pattern.hasEndDate = !b;
}

inline bool Event::repeatOnWeekDay(int day) const
{
    if (pattern.type != Weekly)
	return FALSE;
    return ( (1 << (day - 1)) & pattern.days ) != 0;
}

inline void Event::setRepeatOnWeekDay(int day, bool enable)
{
    if ( repeatOnWeekDay( day ) != enable )
	pattern.days ^= 1 << (day - 1);
}
#ifdef PALMTOPCENTER
class QPC_EXPORT EffectiveEventSizeSorter : public QSorter<EffectiveEvent>
{
public:
    int compare( const EffectiveEvent& a, const EffectiveEvent& b ) const
    {
	return a.size() - b.size();
    }
};

class QPC_EXPORT EffectiveEventTimeSorter : public QSorter<EffectiveEvent>
{
public:
    int compare( const EffectiveEvent& a, const EffectiveEvent& b ) const
    {
	return a.start().secsTo( b.start() );
    }
};
#endif

#endif