-rw-r--r-- | libopie/pim/oevent.cpp | 717 |
1 files changed, 0 insertions, 717 deletions
diff --git a/libopie/pim/oevent.cpp b/libopie/pim/oevent.cpp deleted file mode 100644 index 9b31957..0000000 --- a/libopie/pim/oevent.cpp +++ b/dev/null | |||
@@ -1,717 +0,0 @@ | |||
1 | #include <qshared.h> | ||
2 | #include <qarray.h> | ||
3 | |||
4 | #include <qpe/palmtopuidgen.h> | ||
5 | #include <qpe/categories.h> | ||
6 | #include <qpe/stringutil.h> | ||
7 | |||
8 | #include "orecur.h" | ||
9 | #include "opimresolver.h" | ||
10 | #include "opimnotifymanager.h" | ||
11 | |||
12 | #include "oevent.h" | ||
13 | |||
14 | int OCalendarHelper::week( const QDate& date) { | ||
15 | // Calculates the week this date is in within that | ||
16 | // month. Equals the "row" is is in in the month view | ||
17 | int week = 1; | ||
18 | QDate tmp( date.year(), date.month(), 1 ); | ||
19 | if ( date.dayOfWeek() < tmp.dayOfWeek() ) | ||
20 | ++week; | ||
21 | |||
22 | week += ( date.day() - 1 ) / 7; | ||
23 | |||
24 | return week; | ||
25 | } | ||
26 | int OCalendarHelper::ocurrence( const QDate& date) { | ||
27 | // calculates the number of occurrances of this day of the | ||
28 | // week till the given date (e.g 3rd Wednesday of the month) | ||
29 | return ( date.day() - 1 ) / 7 + 1; | ||
30 | } | ||
31 | int OCalendarHelper::dayOfWeek( char day ) { | ||
32 | int dayOfWeek = 1; | ||
33 | char i = ORecur::MON; | ||
34 | while ( !( i & day ) && i <= ORecur::SUN ) { | ||
35 | i <<= 1; | ||
36 | ++dayOfWeek; | ||
37 | } | ||
38 | return dayOfWeek; | ||
39 | } | ||
40 | int OCalendarHelper::monthDiff( const QDate& first, const QDate& second ) { | ||
41 | return ( second.year() - first.year() ) * 12 + | ||
42 | second.month() - first.month(); | ||
43 | } | ||
44 | |||
45 | struct OEvent::Data : public QShared { | ||
46 | Data() : QShared() { | ||
47 | child = 0; | ||
48 | recur = 0; | ||
49 | manager = 0; | ||
50 | isAllDay = false; | ||
51 | parent = 0; | ||
52 | } | ||
53 | ~Data() { | ||
54 | delete manager; | ||
55 | delete recur; | ||
56 | } | ||
57 | QString description; | ||
58 | QString location; | ||
59 | OPimNotifyManager* manager; | ||
60 | ORecur* recur; | ||
61 | QString note; | ||
62 | QDateTime created; | ||
63 | QDateTime start; | ||
64 | QDateTime end; | ||
65 | bool isAllDay : 1; | ||
66 | QString timezone; | ||
67 | QArray<int>* child; | ||
68 | int parent; | ||
69 | }; | ||
70 | |||
71 | OEvent::OEvent( int uid ) | ||
72 | : OPimRecord( uid ) { | ||
73 | data = new Data; | ||
74 | } | ||
75 | OEvent::OEvent( const OEvent& ev) | ||
76 | : OPimRecord( ev ), data( ev.data ) | ||
77 | { | ||
78 | data->ref(); | ||
79 | } | ||
80 | |||
81 | OEvent::OEvent( const QMap<int, QString> map ) | ||
82 | : OPimRecord( 0 ) | ||
83 | { | ||
84 | data = new Data; | ||
85 | |||
86 | fromMap( map ); | ||
87 | } | ||
88 | |||
89 | OEvent::~OEvent() { | ||
90 | if ( data->deref() ) { | ||
91 | delete data; | ||
92 | data = 0; | ||
93 | } | ||
94 | } | ||
95 | OEvent& OEvent::operator=( const OEvent& ev) { | ||
96 | if ( this == &ev ) return *this; | ||
97 | |||
98 | OPimRecord::operator=( ev ); | ||
99 | ev.data->ref(); | ||
100 | deref(); | ||
101 | data = ev.data; | ||
102 | |||
103 | |||
104 | return *this; | ||
105 | } | ||
106 | QString OEvent::description()const { | ||
107 | return data->description; | ||
108 | } | ||
109 | void OEvent::setDescription( const QString& description ) { | ||
110 | changeOrModify(); | ||
111 | data->description = description; | ||
112 | } | ||
113 | void OEvent::setLocation( const QString& loc ) { | ||
114 | changeOrModify(); | ||
115 | data->location = loc; | ||
116 | } | ||
117 | QString OEvent::location()const { | ||
118 | return data->location; | ||
119 | } | ||
120 | OPimNotifyManager &OEvent::notifiers()const { | ||
121 | // I hope we can skip the changeOrModify here | ||
122 | // the notifier should take care of it | ||
123 | // and OPimNotify is shared too | ||
124 | if (!data->manager ) | ||
125 | data->manager = new OPimNotifyManager; | ||
126 | |||
127 | return *data->manager; | ||
128 | } | ||
129 | bool OEvent::hasNotifiers()const { | ||
130 | if (!data->manager ) | ||
131 | return false; | ||
132 | if (data->manager->reminders().isEmpty() && | ||
133 | data->manager->alarms().isEmpty() ) | ||
134 | return false; | ||
135 | |||
136 | return true; | ||
137 | } | ||
138 | ORecur OEvent::recurrence()const { | ||
139 | if (!data->recur) | ||
140 | data->recur = new ORecur; | ||
141 | |||
142 | return *data->recur; | ||
143 | } | ||
144 | void OEvent::setRecurrence( const ORecur& rec) { | ||
145 | changeOrModify(); | ||
146 | if (data->recur ) | ||
147 | (*data->recur) = rec; | ||
148 | else | ||
149 | data->recur = new ORecur( rec ); | ||
150 | } | ||
151 | bool OEvent::hasRecurrence()const { | ||
152 | if (!data->recur ) return false; | ||
153 | return data->recur->doesRecur(); | ||
154 | } | ||
155 | QString OEvent::note()const { | ||
156 | return data->note; | ||
157 | } | ||
158 | void OEvent::setNote( const QString& note ) { | ||
159 | changeOrModify(); | ||
160 | data->note = note; | ||
161 | } | ||
162 | QDateTime OEvent::createdDateTime()const { | ||
163 | return data->created; | ||
164 | } | ||
165 | void OEvent::setCreatedDateTime( const QDateTime& time ) { | ||
166 | changeOrModify(); | ||
167 | data->created = time; | ||
168 | } | ||
169 | QDateTime OEvent::startDateTime()const { | ||
170 | if ( data->isAllDay ) | ||
171 | return QDateTime( data->start.date(), QTime(0, 0, 0 ) ); | ||
172 | return data->start; | ||
173 | } | ||
174 | QDateTime OEvent::startDateTimeInZone()const { | ||
175 | /* if no timezone, or all day event or if the current and this timeZone match... */ | ||
176 | if (data->timezone.isEmpty() || data->isAllDay || data->timezone == OTimeZone::current().timeZone() ) return startDateTime(); | ||
177 | |||
178 | OTimeZone zone(data->timezone ); | ||
179 | return zone.toDateTime( data->start, OTimeZone::current() ); | ||
180 | } | ||
181 | void OEvent::setStartDateTime( const QDateTime& dt ) { | ||
182 | changeOrModify(); | ||
183 | data->start = dt; | ||
184 | } | ||
185 | QDateTime OEvent::endDateTime()const { | ||
186 | /* | ||
187 | * if all Day event the end time needs | ||
188 | * to be on the same day as the start | ||
189 | */ | ||
190 | if ( data->isAllDay ) | ||
191 | return QDateTime( data->start.date(), QTime(23, 59, 59 ) ); | ||
192 | return data->end; | ||
193 | } | ||
194 | QDateTime OEvent::endDateTimeInZone()const { | ||
195 | /* if no timezone, or all day event or if the current and this timeZone match... */ | ||
196 | if (data->timezone.isEmpty() || data->isAllDay || data->timezone == OTimeZone::current().timeZone() ) return endDateTime(); | ||
197 | |||
198 | OTimeZone zone(data->timezone ); | ||
199 | return zone.toDateTime( data->end, OTimeZone::current() ); | ||
200 | } | ||
201 | void OEvent::setEndDateTime( const QDateTime& dt ) { | ||
202 | changeOrModify(); | ||
203 | data->end = dt; | ||
204 | } | ||
205 | bool OEvent::isMultipleDay()const { | ||
206 | return data->end.date().day() - data->start.date().day(); | ||
207 | } | ||
208 | bool OEvent::isAllDay()const { | ||
209 | return data->isAllDay; | ||
210 | } | ||
211 | void OEvent::setAllDay( bool allDay ) { | ||
212 | changeOrModify(); | ||
213 | data->isAllDay = allDay; | ||
214 | if (allDay ) data->timezone = "UTC"; | ||
215 | } | ||
216 | void OEvent::setTimeZone( const QString& tz ) { | ||
217 | changeOrModify(); | ||
218 | data->timezone = tz; | ||
219 | } | ||
220 | QString OEvent::timeZone()const { | ||
221 | if (data->isAllDay ) return QString::fromLatin1("UTC"); | ||
222 | return data->timezone; | ||
223 | } | ||
224 | bool OEvent::match( const QRegExp& re )const { | ||
225 | if ( re.match( data->description ) != -1 ){ | ||
226 | setLastHitField( Qtopia::DatebookDescription ); | ||
227 | return true; | ||
228 | } | ||
229 | if ( re.match( data->note ) != -1 ){ | ||
230 | setLastHitField( Qtopia::Note ); | ||
231 | return true; | ||
232 | } | ||
233 | if ( re.match( data->location ) != -1 ){ | ||
234 | setLastHitField( Qtopia::Location ); | ||
235 | return true; | ||
236 | } | ||
237 | if ( re.match( data->start.toString() ) != -1 ){ | ||
238 | setLastHitField( Qtopia::StartDateTime ); | ||
239 | return true; | ||
240 | } | ||
241 | if ( re.match( data->end.toString() ) != -1 ){ | ||
242 | setLastHitField( Qtopia::EndDateTime ); | ||
243 | return true; | ||
244 | } | ||
245 | return false; | ||
246 | } | ||
247 | QString OEvent::toRichText()const { | ||
248 | QString text, value; | ||
249 | |||
250 | // description | ||
251 | text += "<b><h3><img src=\"datebook/DateBook\">"; | ||
252 | if ( !description().isEmpty() ) { | ||
253 | text += Qtopia::escapeString(description() ).replace(QRegExp( "[\n]"), "" ); | ||
254 | } | ||
255 | text += "</h3></b><br><hr><br>"; | ||
256 | |||
257 | // location | ||
258 | if ( !(value = location()).isEmpty() ) { | ||
259 | text += "<b>" + QObject::tr( "Location:" ) + "</b> "; | ||
260 | text += Qtopia::escapeString(value) + "<br>"; | ||
261 | } | ||
262 | |||
263 | // all day event | ||
264 | if ( isAllDay() ) { | ||
265 | text += "<b><i>" + QObject::tr( "This is an all day event" ) + "</i></b><br>"; | ||
266 | } | ||
267 | // multiple day event | ||
268 | else if ( isMultipleDay () ) { | ||
269 | text += "<b><i>" + QObject::tr( "This is a multiple day event" ) + "</i></b><br>"; | ||
270 | } | ||
271 | // start & end times | ||
272 | else { | ||
273 | // start time | ||
274 | if ( startDateTime().isValid() ) { | ||
275 | text += "<b>" + QObject::tr( "Start:") + "</b> "; | ||
276 | text += Qtopia::escapeString(startDateTime().toString() ). | ||
277 | replace(QRegExp( "[\n]"), "<br>" ) + "<br>"; | ||
278 | } | ||
279 | |||
280 | // end time | ||
281 | if ( endDateTime().isValid() ) { | ||
282 | text += "<b>" + QObject::tr( "End:") + "</b> "; | ||
283 | text += Qtopia::escapeString(endDateTime().toString() ). | ||
284 | replace(QRegExp( "[\n]"), "<br>" ) + "<br>"; | ||
285 | } | ||
286 | } | ||
287 | |||
288 | // categories | ||
289 | if ( categoryNames("Calendar").count() ){ | ||
290 | text += "<b>" + QObject::tr( "Category:") + "</b> "; | ||
291 | text += categoryNames("Calendar").join(", "); | ||
292 | text += "<br>"; | ||
293 | } | ||
294 | |||
295 | //notes | ||
296 | if ( !note().isEmpty() ) { | ||
297 | text += "<b>" + QObject::tr( "Note:") + "</b><br>"; | ||
298 | text += note(); | ||
299 | // text += Qtopia::escapeString(note() ). | ||
300 | // replace(QRegExp( "[\n]"), "<br>" ) + "<br>"; | ||
301 | } | ||
302 | return text; | ||
303 | } | ||
304 | QString OEvent::toShortText()const { | ||
305 | QString text; | ||
306 | text += QString::number( startDateTime().date().day() ); | ||
307 | text += "."; | ||
308 | text += QString::number( startDateTime().date().month() ); | ||
309 | text += "."; | ||
310 | text += QString::number( startDateTime().date().year() ); | ||
311 | text += " "; | ||
312 | text += QString::number( startDateTime().time().hour() ); | ||
313 | text += ":"; | ||
314 | text += QString::number( startDateTime().time().minute() ); | ||
315 | text += " - "; | ||
316 | text += description(); | ||
317 | return text; | ||
318 | } | ||
319 | QString OEvent::type()const { | ||
320 | return QString::fromLatin1("OEvent"); | ||
321 | } | ||
322 | QString OEvent::recordField( int /*id */ )const { | ||
323 | return QString::null; | ||
324 | } | ||
325 | int OEvent::rtti() { | ||
326 | return OPimResolver::DateBook; | ||
327 | } | ||
328 | bool OEvent::loadFromStream( QDataStream& ) { | ||
329 | return true; | ||
330 | } | ||
331 | bool OEvent::saveToStream( QDataStream& )const { | ||
332 | return true; | ||
333 | } | ||
334 | void OEvent::changeOrModify() { | ||
335 | if ( data->count != 1 ) { | ||
336 | data->deref(); | ||
337 | Data* d2 = new Data; | ||
338 | d2->description = data->description; | ||
339 | d2->location = data->location; | ||
340 | |||
341 | if (data->manager ) | ||
342 | d2->manager = new OPimNotifyManager( *data->manager ); | ||
343 | |||
344 | if ( data->recur ) | ||
345 | d2->recur = new ORecur( *data->recur ); | ||
346 | |||
347 | d2->note = data->note; | ||
348 | d2->created = data->created; | ||
349 | d2->start = data->start; | ||
350 | d2->end = data->end; | ||
351 | d2->isAllDay = data->isAllDay; | ||
352 | d2->timezone = data->timezone; | ||
353 | d2->parent = data->parent; | ||
354 | |||
355 | if ( data->child ) { | ||
356 | d2->child = new QArray<int>( *data->child ); | ||
357 | d2->child->detach(); | ||
358 | } | ||
359 | |||
360 | data = d2; | ||
361 | } | ||
362 | } | ||
363 | void OEvent::deref() { | ||
364 | if ( data->deref() ) { | ||
365 | delete data; | ||
366 | data = 0; | ||
367 | } | ||
368 | } | ||
369 | // Exporting Event data to map. Using the same | ||
370 | // encoding as ODateBookAccessBackend_xml does.. | ||
371 | // Thus, we could remove the stuff there and use this | ||
372 | // for it and for all other places.. | ||
373 | // Encoding should happen at one place, only ! (eilers) | ||
374 | QMap<int, QString> OEvent::toMap()const { | ||
375 | QMap<int, QString> retMap; | ||
376 | |||
377 | retMap.insert( OEvent::FUid, QString::number( uid() ) ); | ||
378 | retMap.insert( OEvent::FCategories, Qtopia::escapeString( Qtopia::Record::idsToString( categories() ) )); | ||
379 | retMap.insert( OEvent::FDescription, Qtopia::escapeString( description() ) ); | ||
380 | retMap.insert( OEvent::FLocation, Qtopia::escapeString( location() ) ); | ||
381 | retMap.insert( OEvent::FType, isAllDay() ? "AllDay" : "" ); | ||
382 | OPimAlarm alarm = notifiers().alarms()[0]; | ||
383 | retMap.insert( OEvent::FAlarm, QString::number( alarm.dateTime().secsTo( startDateTime() ) / 60 ) ); | ||
384 | retMap.insert( OEvent::FSound, (alarm.sound() == OPimAlarm::Loud) ? "loud" : "silent" ); | ||
385 | |||
386 | OTimeZone zone( timeZone().isEmpty() ? OTimeZone::current() : timeZone() ); | ||
387 | retMap.insert( OEvent::FStart, QString::number( zone.fromUTCDateTime( zone.toDateTime( startDateTime(), OTimeZone::utc() ) ) ) ); | ||
388 | retMap.insert( OEvent::FEnd, QString::number( zone.fromUTCDateTime( zone.toDateTime( endDateTime(), OTimeZone::utc() ) ) ) ); | ||
389 | retMap.insert( OEvent::FNote, Qtopia::escapeString( note() ) ); | ||
390 | retMap.insert( OEvent::FTimeZone, timeZone().isEmpty() ? QString( "None" ) : timeZone() ); | ||
391 | if( parent() ) | ||
392 | retMap.insert( OEvent::FRecParent, QString::number( parent() ) ); | ||
393 | if( children().count() ){ | ||
394 | QArray<int> childr = children(); | ||
395 | QString buf; | ||
396 | for ( uint i = 0; i < childr.count(); i++ ) { | ||
397 | if ( i != 0 ) buf += " "; | ||
398 | buf += QString::number( childr[i] ); | ||
399 | } | ||
400 | retMap.insert( OEvent::FRecChildren, buf ); | ||
401 | } | ||
402 | |||
403 | // Add recurrence stuff | ||
404 | if( hasRecurrence() ){ | ||
405 | ORecur recur = recurrence(); | ||
406 | QMap<int, QString> recFields = recur.toMap(); | ||
407 | retMap.insert( OEvent::FRType, recFields[ORecur::RType] ); | ||
408 | retMap.insert( OEvent::FRWeekdays, recFields[ORecur::RWeekdays] ); | ||
409 | retMap.insert( OEvent::FRPosition, recFields[ORecur::RPosition] ); | ||
410 | retMap.insert( OEvent::FRFreq, recFields[ORecur::RFreq] ); | ||
411 | retMap.insert( OEvent::FRHasEndDate, recFields[ORecur::RHasEndDate] ); | ||
412 | retMap.insert( OEvent::FREndDate, recFields[ORecur::EndDate] ); | ||
413 | retMap.insert( OEvent::FRCreated, recFields[ORecur::Created] ); | ||
414 | retMap.insert( OEvent::FRExceptions, recFields[ORecur::Exceptions] ); | ||
415 | } else { | ||
416 | ORecur recur = recurrence(); | ||
417 | QMap<int, QString> recFields = recur.toMap(); | ||
418 | retMap.insert( OEvent::FRType, recFields[ORecur::RType] ); | ||
419 | } | ||
420 | |||
421 | return retMap; | ||
422 | } | ||
423 | |||
424 | void OEvent::fromMap( const QMap<int, QString>& map ) | ||
425 | { | ||
426 | |||
427 | // We just want to set the UID if it is really stored. | ||
428 | if ( !map[OEvent::FUid].isEmpty() ) | ||
429 | setUid( map[OEvent::FUid].toInt() ); | ||
430 | |||
431 | setCategories( idsFromString( map[OEvent::FCategories] ) ); | ||
432 | setDescription( map[OEvent::FDescription] ); | ||
433 | setLocation( map[OEvent::FLocation] ); | ||
434 | |||
435 | if ( map[OEvent::FType] == "AllDay" ) | ||
436 | setAllDay( true ); | ||
437 | else | ||
438 | setAllDay( false ); | ||
439 | |||
440 | int alarmTime = -1; | ||
441 | if( !map[OEvent::FAlarm].isEmpty() ) | ||
442 | alarmTime = map[OEvent::FAlarm].toInt(); | ||
443 | |||
444 | int sound = ( ( map[OEvent::FSound] == "loud" ) ? OPimAlarm::Loud : OPimAlarm::Silent ); | ||
445 | if ( ( alarmTime != -1 ) ){ | ||
446 | QDateTime dt = startDateTime().addSecs( -1*alarmTime*60 ); | ||
447 | OPimAlarm al( sound , dt ); | ||
448 | notifiers().add( al ); | ||
449 | } | ||
450 | if ( !map[OEvent::FTimeZone].isEmpty() && ( map[OEvent::FTimeZone] != "None" ) ){ | ||
451 | setTimeZone( map[OEvent::FTimeZone] ); | ||
452 | } | ||
453 | |||
454 | time_t start = (time_t) map[OEvent::FStart].toLong(); | ||
455 | time_t end = (time_t) map[OEvent::FEnd].toLong(); | ||
456 | |||
457 | /* AllDay is always in UTC */ | ||
458 | if ( isAllDay() ) { | ||
459 | OTimeZone utc = OTimeZone::utc(); | ||
460 | setStartDateTime( utc.fromUTCDateTime( start ) ); | ||
461 | setEndDateTime ( utc.fromUTCDateTime( end ) ); | ||
462 | setTimeZone( "UTC"); // make sure it is really utc | ||
463 | }else { | ||
464 | /* to current date time */ | ||
465 | // qWarning(" Start is %d", start ); | ||
466 | OTimeZone zone( timeZone().isEmpty() ? OTimeZone::current() : timeZone() ); | ||
467 | QDateTime date = zone.toDateTime( start ); | ||
468 | qWarning(" Start is %s", date.toString().latin1() ); | ||
469 | setStartDateTime( zone.toDateTime( date, OTimeZone::current() ) ); | ||
470 | |||
471 | date = zone.toDateTime( end ); | ||
472 | setEndDateTime ( zone.toDateTime( date, OTimeZone::current() ) ); | ||
473 | } | ||
474 | |||
475 | if ( !map[OEvent::FRecParent].isEmpty() ) | ||
476 | setParent( map[OEvent::FRecParent].toInt() ); | ||
477 | |||
478 | if ( !map[OEvent::FRecChildren].isEmpty() ){ | ||
479 | QStringList list = QStringList::split(' ', map[OEvent::FRecChildren] ); | ||
480 | for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it ) { | ||
481 | addChild( (*it).toInt() ); | ||
482 | } | ||
483 | } | ||
484 | |||
485 | // Fill recurrence stuff and put it directly into the ORecur-Object using fromMap.. | ||
486 | if( !map[OEvent::FRType].isEmpty() ){ | ||
487 | QMap<int, QString> recFields; | ||
488 | recFields.insert( ORecur::RType, map[OEvent::FRType] ); | ||
489 | recFields.insert( ORecur::RWeekdays, map[OEvent::FRWeekdays] ); | ||
490 | recFields.insert( ORecur::RPosition, map[OEvent::FRPosition] ); | ||
491 | recFields.insert( ORecur::RFreq, map[OEvent::FRFreq] ); | ||
492 | recFields.insert( ORecur::RHasEndDate, map[OEvent::FRHasEndDate] ); | ||
493 | recFields.insert( ORecur::EndDate, map[OEvent::FREndDate] ); | ||
494 | recFields.insert( ORecur::Created, map[OEvent::FRCreated] ); | ||
495 | recFields.insert( ORecur::Exceptions, map[OEvent::FRExceptions] ); | ||
496 | ORecur recur( recFields ); | ||
497 | setRecurrence( recur ); | ||
498 | } | ||
499 | |||
500 | } | ||
501 | |||
502 | |||
503 | int OEvent::parent()const { | ||
504 | return data->parent; | ||
505 | } | ||
506 | void OEvent::setParent( int uid ) { | ||
507 | changeOrModify(); | ||
508 | data->parent = uid; | ||
509 | } | ||
510 | QArray<int> OEvent::children() const{ | ||
511 | if (!data->child) return QArray<int>(); | ||
512 | else | ||
513 | return data->child->copy(); | ||
514 | } | ||
515 | void OEvent::setChildren( const QArray<int>& arr ) { | ||
516 | changeOrModify(); | ||
517 | if (data->child) delete data->child; | ||
518 | |||
519 | data->child = new QArray<int>( arr ); | ||
520 | data->child->detach(); | ||
521 | } | ||
522 | void OEvent::addChild( int uid ) { | ||
523 | changeOrModify(); | ||
524 | if (!data->child ) { | ||
525 | data->child = new QArray<int>(1); | ||
526 | (*data->child)[0] = uid; | ||
527 | }else{ | ||
528 | int count = data->child->count(); | ||
529 | data->child->resize( count + 1 ); | ||
530 | (*data->child)[count] = uid; | ||
531 | } | ||
532 | } | ||
533 | void OEvent::removeChild( int uid ) { | ||
534 | if (!data->child || !data->child->contains( uid ) ) return; | ||
535 | changeOrModify(); | ||
536 | QArray<int> newAr( data->child->count() - 1 ); | ||
537 | int j = 0; | ||
538 | uint count = data->child->count(); | ||
539 | for ( uint i = 0; i < count; i++ ) { | ||
540 | if ( (*data->child)[i] != uid ) { | ||
541 | newAr[j] = (*data->child)[i]; | ||
542 | j++; | ||
543 | } | ||
544 | } | ||
545 | (*data->child) = newAr; | ||
546 | } | ||
547 | struct OEffectiveEvent::Data : public QShared { | ||
548 | Data() : QShared() { | ||
549 | } | ||
550 | OEvent event; | ||
551 | QDate date; | ||
552 | QTime start, end; | ||
553 | QDate startDate, endDate; | ||
554 | bool dates : 1; | ||
555 | }; | ||
556 | |||
557 | OEffectiveEvent::OEffectiveEvent() { | ||
558 | data = new Data; | ||
559 | data->date = QDate::currentDate(); | ||
560 | data->start = data->end = QTime::currentTime(); | ||
561 | data->dates = false; | ||
562 | } | ||
563 | OEffectiveEvent::OEffectiveEvent( const OEvent& ev, const QDate& startDate, | ||
564 | Position pos ) { | ||
565 | data = new Data; | ||
566 | data->event = ev; | ||
567 | data->date = startDate; | ||
568 | if ( pos & Start ) | ||
569 | data->start = ev.startDateTime().time(); | ||
570 | else | ||
571 | data->start = QTime( 0, 0, 0 ); | ||
572 | |||
573 | if ( pos & End ) | ||
574 | data->end = ev.endDateTime().time(); | ||
575 | else | ||
576 | data->end = QTime( 23, 59, 59 ); | ||
577 | |||
578 | data->dates = false; | ||
579 | } | ||
580 | OEffectiveEvent::OEffectiveEvent( const OEffectiveEvent& ev) { | ||
581 | data = ev.data; | ||
582 | data->ref(); | ||
583 | } | ||
584 | OEffectiveEvent::~OEffectiveEvent() { | ||
585 | if ( data->deref() ) { | ||
586 | delete data; | ||
587 | data = 0; | ||
588 | } | ||
589 | } | ||
590 | OEffectiveEvent& OEffectiveEvent::operator=( const OEffectiveEvent& ev ) { | ||
591 | if ( *this == ev ) return *this; | ||
592 | |||
593 | ev.data->ref(); | ||
594 | deref(); | ||
595 | data = ev.data; | ||
596 | |||
597 | return *this; | ||
598 | } | ||
599 | |||
600 | void OEffectiveEvent::setStartTime( const QTime& ti) { | ||
601 | changeOrModify(); | ||
602 | data->start = ti; | ||
603 | } | ||
604 | void OEffectiveEvent::setEndTime( const QTime& en) { | ||
605 | changeOrModify(); | ||
606 | data->end = en; | ||
607 | } | ||
608 | void OEffectiveEvent::setEvent( const OEvent& ev) { | ||
609 | changeOrModify(); | ||
610 | data->event = ev; | ||
611 | } | ||
612 | void OEffectiveEvent::setDate( const QDate& da) { | ||
613 | changeOrModify(); | ||
614 | data->date = da; | ||
615 | } | ||
616 | void OEffectiveEvent::setEffectiveDates( const QDate& from, | ||
617 | const QDate& to ) { | ||
618 | if (!from.isValid() ) { | ||
619 | data->dates = false; | ||
620 | return; | ||
621 | } | ||
622 | |||
623 | data->startDate = from; | ||
624 | data->endDate = to; | ||
625 | } | ||
626 | QString OEffectiveEvent::description()const { | ||
627 | return data->event.description(); | ||
628 | } | ||
629 | QString OEffectiveEvent::location()const { | ||
630 | return data->event.location(); | ||
631 | } | ||
632 | QString OEffectiveEvent::note()const { | ||
633 | return data->event.note(); | ||
634 | } | ||
635 | OEvent OEffectiveEvent::event()const { | ||
636 | return data->event; | ||
637 | } | ||
638 | QTime OEffectiveEvent::startTime()const { | ||
639 | return data->start; | ||
640 | } | ||
641 | QTime OEffectiveEvent::endTime()const { | ||
642 | return data->end; | ||
643 | } | ||
644 | QDate OEffectiveEvent::date()const { | ||
645 | return data->date; | ||
646 | } | ||
647 | int OEffectiveEvent::length()const { | ||
648 | return (data->end.hour() * 60 - data->start.hour() * 60) | ||
649 | + QABS(data->start.minute() - data->end.minute() ); | ||
650 | } | ||
651 | int OEffectiveEvent::size()const { | ||
652 | return ( data->end.hour() - data->start.hour() ) * 3600 | ||
653 | + (data->end.minute() - data->start.minute() * 60 | ||
654 | + data->end.second() - data->start.second() ); | ||
655 | } | ||
656 | QDate OEffectiveEvent::startDate()const { | ||
657 | if ( data->dates ) | ||
658 | return data->startDate; | ||
659 | else if ( data->event.hasRecurrence() ) // single day, since multi-day should have a d pointer | ||
660 | return data->date; | ||
661 | else | ||
662 | return data->event.startDateTime().date(); | ||
663 | } | ||
664 | QDate OEffectiveEvent::endDate()const { | ||
665 | if ( data->dates ) | ||
666 | return data->endDate; | ||
667 | else if ( data->event.hasRecurrence() ) | ||
668 | return data->date; | ||
669 | else | ||
670 | return data->event.endDateTime().date(); | ||
671 | } | ||
672 | void OEffectiveEvent::deref() { | ||
673 | if ( data->deref() ) { | ||
674 | delete data; | ||
675 | data = 0; | ||
676 | } | ||
677 | } | ||
678 | void OEffectiveEvent::changeOrModify() { | ||
679 | if ( data->count != 1 ) { | ||
680 | data->deref(); | ||
681 | Data* d2 = new Data; | ||
682 | d2->event = data->event; | ||
683 | d2->date = data->date; | ||
684 | d2->start = data->start; | ||
685 | d2->end = data->end; | ||
686 | d2->startDate = data->startDate; | ||
687 | d2->endDate = data->endDate; | ||
688 | d2->dates = data->dates; | ||
689 | data = d2; | ||
690 | } | ||
691 | } | ||
692 | bool OEffectiveEvent::operator<( const OEffectiveEvent &e ) const{ | ||
693 | if ( data->date < e.date() ) | ||
694 | return TRUE; | ||
695 | if ( data->date == e.date() ) | ||
696 | return ( startTime() < e.startTime() ); | ||
697 | else | ||
698 | return FALSE; | ||
699 | } | ||
700 | bool OEffectiveEvent::operator<=( const OEffectiveEvent &e ) const{ | ||
701 | return (data->date <= e.date() ); | ||
702 | } | ||
703 | bool OEffectiveEvent::operator==( const OEffectiveEvent &e ) const { | ||
704 | return ( date() == e.date() | ||
705 | && startTime() == e.startTime() | ||
706 | && endTime()== e.endTime() | ||
707 | && event() == e.event() ); | ||
708 | } | ||
709 | bool OEffectiveEvent::operator!=( const OEffectiveEvent &e ) const { | ||
710 | return !(*this == e ); | ||
711 | } | ||
712 | bool OEffectiveEvent::operator>( const OEffectiveEvent &e ) const { | ||
713 | return !(*this <= e ); | ||
714 | } | ||
715 | bool OEffectiveEvent::operator>= ( const OEffectiveEvent &e ) const { | ||
716 | return !(*this < e); | ||
717 | } | ||