00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <qdatetime.h>
00025 #include <qstring.h>
00026 #include <qptrlist.h>
00027
00028 #include <kdebug.h>
00029 #include <klocale.h>
00030 #include <kmessagebox.h>
00031
00032 #include "vcaldrag.h"
00033 #include "vcalformat.h"
00034 #include "icalformat.h"
00035 #include "exceptions.h"
00036 #include "incidence.h"
00037 #include "journal.h"
00038 #include "filestorage.h"
00039
00040 #include "calendarlocal.h"
00041
00042 using namespace KCal;
00043
00044 CalendarLocal::CalendarLocal( const QString &timeZoneId )
00045 : Calendar( timeZoneId ), mEvents( 47 )
00046 {
00047 init();
00048 }
00049
00050 void CalendarLocal::init()
00051 {
00052 mDeletedIncidences.setAutoDelete( true );
00053 mFileName = QString::null;
00054 }
00055
00056
00057 CalendarLocal::~CalendarLocal()
00058 {
00059 close();
00060 }
00061
00062 bool CalendarLocal::load( const QString &fileName, CalFormat *format )
00063 {
00064 mFileName = fileName;
00065 FileStorage storage( this, fileName, format );
00066 return storage.load();
00067 }
00068
00069 bool CalendarLocal::reload( const QString &tz )
00070 {
00071 const QString filename = mFileName;
00072 save();
00073 close();
00074 mFileName = filename;
00075 setTimeZoneId( tz );
00076 FileStorage storage( this, mFileName );
00077 return storage.load();
00078 }
00079
00080 bool CalendarLocal::save( const QString &fileName, CalFormat *format )
00081 {
00082
00083
00084 if ( mFileName != fileName || isModified() ) {
00085 FileStorage storage( this, fileName, format );
00086 return storage.save();
00087 } else {
00088 return true;
00089 }
00090 }
00091
00092 void CalendarLocal::close()
00093 {
00094 setObserversEnabled( false );
00095 mFileName = QString::null;
00096
00097 deleteAllEvents();
00098 deleteAllTodos();
00099 deleteAllJournals();
00100
00101 mDeletedIncidences.clear();
00102 setModified( false );
00103
00104 setObserversEnabled( true );
00105 }
00106
00107
00108 bool CalendarLocal::addEvent( Event *event )
00109 {
00110 insertEvent( event );
00111
00112 event->registerObserver( this );
00113
00114 setModified( true );
00115
00116 notifyIncidenceAdded( event );
00117
00118 return true;
00119 }
00120
00121 bool CalendarLocal::deleteEvent( Event *event )
00122 {
00123
00124
00125 if ( mEvents.remove( event->uid() ) ) {
00126 setModified( true );
00127 notifyIncidenceDeleted( event );
00128 mDeletedIncidences.append( event );
00129 return true;
00130 } else {
00131 kdWarning() << "CalendarLocal::deleteEvent(): Event not found." << endl;
00132 return false;
00133 }
00134 }
00135
00136 void CalendarLocal::deleteAllEvents()
00137 {
00138
00139 QDictIterator<Event> it( mEvents );
00140 while( it.current() ) {
00141 notifyIncidenceDeleted( it.current() );
00142 ++it;
00143 }
00144
00145 mEvents.setAutoDelete( true );
00146 mEvents.clear();
00147 mEvents.setAutoDelete( false );
00148 }
00149
00150 Event *CalendarLocal::event( const QString &uid )
00151 {
00152
00153 return mEvents[ uid ];
00154 }
00155
00156 bool CalendarLocal::addTodo( Todo *todo )
00157 {
00158 mTodoList.append( todo );
00159
00160 todo->registerObserver( this );
00161
00162
00163 setupRelations( todo );
00164
00165 setModified( true );
00166
00167 notifyIncidenceAdded( todo );
00168
00169 return true;
00170 }
00171
00172 bool CalendarLocal::deleteTodo( Todo *todo )
00173 {
00174
00175 removeRelations( todo );
00176
00177 if ( mTodoList.removeRef( todo ) ) {
00178 setModified( true );
00179 notifyIncidenceDeleted( todo );
00180 mDeletedIncidences.append( todo );
00181 return true;
00182 } else {
00183 kdWarning() << "CalendarLocal::deleteTodo(): Todo not found." << endl;
00184 return false;
00185 }
00186 }
00187
00188 void CalendarLocal::deleteAllTodos()
00189 {
00190
00191 Todo::List::ConstIterator it;
00192 for( it = mTodoList.begin(); it != mTodoList.end(); ++it ) {
00193 notifyIncidenceDeleted( *it );
00194 }
00195
00196 mTodoList.setAutoDelete( true );
00197 mTodoList.clearAll();
00198 mTodoList.setAutoDelete( false );
00199 }
00200
00201 Todo::List CalendarLocal::rawTodos( TodoSortField sortField,
00202 SortDirection sortDirection )
00203 {
00204 return sortTodos( &mTodoList, sortField, sortDirection );
00205 }
00206
00207 Todo *CalendarLocal::todo( const QString &uid )
00208 {
00209 Todo::List::ConstIterator it;
00210 for ( it = mTodoList.begin(); it != mTodoList.end(); ++it ) {
00211 if ( (*it)->uid() == uid ) return *it;
00212 }
00213
00214 return 0;
00215 }
00216
00217 Todo::List CalendarLocal::rawTodosForDate( const QDate &date )
00218 {
00219 Todo::List todos;
00220
00221 Todo::List::ConstIterator it;
00222 for ( it = mTodoList.begin(); it != mTodoList.end(); ++it ) {
00223 Todo *todo = *it;
00224 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
00225 todos.append( todo );
00226 }
00227 }
00228
00229 return todos;
00230 }
00231
00232 Alarm::List CalendarLocal::alarmsTo( const QDateTime &to )
00233 {
00234 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to );
00235 }
00236
00237 Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to )
00238 {
00239
00240
00241
00242 Alarm::List alarms;
00243
00244 EventDictIterator it( mEvents );
00245 for( ; it.current(); ++it ) {
00246 Event *e = *it;
00247 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to );
00248 else appendAlarms( alarms, e, from, to );
00249 }
00250
00251 Todo::List::ConstIterator it2;
00252 for( it2 = mTodoList.begin(); it2 != mTodoList.end(); ++it2 ) {
00253 Todo *t = *it2;
00254 if ( t->isCompleted() ) {
00255 continue;
00256 }
00257 if ( t->doesRecur() ) appendRecurringAlarms( alarms, t, from, to );
00258 else appendAlarms( alarms, t, from, to );
00259 }
00260
00261 return alarms;
00262 }
00263
00264 void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence,
00265 const QDateTime &from, const QDateTime &to )
00266 {
00267 QDateTime preTime = from.addSecs(-1);
00268 Alarm::List::ConstIterator it;
00269 for( it = incidence->alarms().begin(); it != incidence->alarms().end();
00270 ++it ) {
00271 Alarm *alarm = *it;
00272 if ( alarm->enabled() ) {
00273 QDateTime dt = alarm->nextRepetition( preTime );
00274 if ( dt.isValid() && dt <= to ) {
00275 kdDebug(5800) << "CalendarLocal::appendAlarms() '"
00276 << incidence->summary() << "': "
00277 << dt.toString() << endl;
00278 alarms.append( alarm );
00279 }
00280 }
00281 }
00282 }
00283
00284 void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms,
00285 Incidence *incidence,
00286 const QDateTime &from,
00287 const QDateTime &to )
00288 {
00289 QDateTime dt;
00290 Duration endOffset( 0 );
00291 bool endOffsetValid = false;
00292 Duration period( from, to );
00293
00294 Event *e = static_cast<Event *>( incidence );
00295 Todo *t = static_cast<Todo *>( incidence );
00296
00297 Alarm::List::ConstIterator it;
00298 for( it = incidence->alarms().begin(); it != incidence->alarms().end();
00299 ++it ) {
00300 Alarm *alarm = *it;
00301 if ( alarm->enabled() ) {
00302 if ( alarm->hasTime() ) {
00303
00304 dt = alarm->nextRepetition( from.addSecs(-1) );
00305 if ( !dt.isValid() || dt > to ) {
00306 continue;
00307 }
00308 } else {
00309
00310
00311
00312 Duration offset( 0 );
00313 if ( alarm->hasStartOffset() ) {
00314 offset = alarm->startOffset().asSeconds();
00315 } else if ( alarm->hasEndOffset() ) {
00316 offset = alarm->endOffset().asSeconds();
00317 if ( !endOffsetValid ) {
00318 if ( incidence->type() == "Event" ) {
00319 endOffset = Duration( e->dtStart(), e->dtEnd() );
00320 endOffsetValid = true;
00321 } else if ( incidence->type() == "Todo" ) {
00322 endOffset = Duration( t->dtStart(), t->dtDue() );
00323 endOffsetValid = true;
00324 }
00325 }
00326 }
00327
00328
00329 QDateTime alarmStart;
00330 if ( incidence->type() == "Event" ) {
00331 alarmStart =
00332 offset.end( alarm->hasEndOffset() ? e->dtEnd() : e->dtStart() );
00333 } else if ( incidence->type() == "Todo" ) {
00334 alarmStart =
00335 offset.end( alarm->hasEndOffset() ? t->dtDue() : t->dtStart() );
00336 }
00337
00338 if ( alarmStart.isValid() && alarmStart > to ) {
00339 continue;
00340 }
00341
00342 QDateTime baseStart = incidence->dtStart();
00343 if ( from > alarmStart ) {
00344 alarmStart = from;
00345 baseStart = (-offset).end( (-endOffset).end( alarmStart ) );
00346 }
00347
00348
00349
00350
00351 dt = incidence->recurrence()->getNextDateTime( baseStart.addSecs(-1) );
00352 if ( !dt.isValid() ||
00353 ( dt = endOffset.end( offset.end( dt ) ) ) > to )
00354 {
00355
00356 if ( !alarm->repeatCount() ) {
00357 continue;
00358 }
00359
00360
00361
00362 bool found = false;
00363 Duration alarmDuration = alarm->duration();
00364 for ( QDateTime base = baseStart;
00365 ( dt = incidence->recurrence()->getPreviousDateTime( base ) ).isValid();
00366 base = dt ) {
00367 if ( alarm->duration().end( dt ) < base ) {
00368 break;
00369 }
00370
00371
00372
00373
00374 int snooze = alarm->snoozeTime().value();
00375 if ( alarm->snoozeTime().isDaily() ) {
00376 Duration toFromDuration( dt, base );
00377 int toFrom = toFromDuration.asDays();
00378 if ( alarm->snoozeTime().end( from ) <= to ||
00379 ( toFromDuration.isDaily() && toFrom % snooze == 0 ) ||
00380 ( toFrom / snooze + 1 ) * snooze <= toFrom + period.asDays() ) {
00381 found = true;
00382 #ifndef NDEBUG
00383
00384 dt = offset.end( dt ).addDays( ( ( toFrom - 1 ) / snooze + 1 ) * snooze );
00385 #endif
00386 break;
00387 }
00388 } else {
00389 int toFrom = dt.secsTo( base );
00390 if ( period.asSeconds() >= snooze ||
00391 toFrom % snooze == 0 ||
00392 ( toFrom / snooze + 1 ) * snooze <= toFrom + period.asSeconds() )
00393 {
00394 found = true;
00395 #ifndef NDEBUG
00396
00397 dt = offset.end( dt ).addSecs( ( ( toFrom - 1 ) / snooze + 1 ) * snooze );
00398 #endif
00399 break;
00400 }
00401 }
00402 }
00403 if ( !found ) {
00404 continue;
00405 }
00406 }
00407 }
00408 kdDebug(5800) << "CalendarLocal::appendAlarms() '" << incidence->summary()
00409 << "': " << dt.toString() << endl;
00410 alarms.append( alarm );
00411 }
00412 }
00413 }
00414
00415
00416 void CalendarLocal::incidenceUpdated( IncidenceBase *incidence )
00417 {
00418 incidence->setSyncStatusSilent( Event::SYNCMOD );
00419 incidence->setLastModified( QDateTime::currentDateTime() );
00420
00421
00422
00423
00424
00425 notifyIncidenceChanged( static_cast<Incidence *>( incidence ) );
00426
00427 setModified( true );
00428 }
00429
00430 void CalendarLocal::insertEvent( Event *event )
00431 {
00432 QString uid = event->uid();
00433 if ( mEvents[ uid ] == 0 ) {
00434 mEvents.insert( uid, event );
00435 }
00436 #ifndef NDEBUG
00437 else
00438
00439 Q_ASSERT( mEvents[uid] == event );
00440 #endif
00441 }
00442
00443 Event::List CalendarLocal::rawEventsForDate( const QDate &qd,
00444 EventSortField sortField,
00445 SortDirection sortDirection )
00446 {
00447 Event::List eventList;
00448
00449
00450
00451 if ( !qd.isValid() ) {
00452 return eventList;
00453 }
00454
00455 EventDictIterator it( mEvents );
00456 for( ; it.current(); ++it ) {
00457 Event *event = *it;
00458
00459 if ( event->doesRecur() ) {
00460 if ( event->isMultiDay() ) {
00461 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() );
00462 int i;
00463 for ( i = 0; i <= extraDays; i++ ) {
00464 if ( event->recursOn( qd.addDays( -i ) ) ) {
00465 eventList.append( event );
00466 break;
00467 }
00468 }
00469 } else {
00470 if ( event->recursOn( qd ) )
00471 eventList.append( event );
00472 }
00473 } else {
00474 if ( event->dtStart().date() <= qd && event->dateEnd() >= qd ) {
00475 eventList.append( event );
00476 }
00477 }
00478 }
00479
00480 return sortEventsForDate( &eventList, qd, sortField, sortDirection );
00481 }
00482
00483 Event::List CalendarLocal::rawEvents( const QDate &start, const QDate &end,
00484 bool inclusive )
00485 {
00486 Event::List eventList;
00487 QDate yesterStart = start.addDays(-1);
00488
00489
00490 EventDictIterator it( mEvents );
00491 for( ; it.current(); ++it ) {
00492 Event *event = *it;
00493
00494 QDate rStart = event->dtStart().date();
00495 if (end < rStart) {
00496
00497 continue;
00498 }
00499 if ( inclusive && rStart < start) {
00500
00501 continue;
00502 }
00503
00504 if ( ! event->doesRecur() ) {
00505 QDate rEnd = event->dtEnd().date();
00506 if (rEnd < start) {
00507
00508 continue;
00509 }
00510 if ( inclusive && end < rEnd ) {
00511
00512 continue;
00513 }
00514 } else {
00515 switch ( event->recurrence()->duration() ) {
00516 case -1:
00517 if ( inclusive ) {
00518
00519 continue;
00520 }
00521 break;
00522 case 0:
00523 default:
00524 QDate rEnd = event->recurrence()->endDate();
00525 if ( ! rEnd.isValid() ) {
00526
00527 continue;
00528 }
00529 if ( rEnd < start ) {
00530
00531 continue;
00532 }
00533 if ( inclusive && end < rEnd ) {
00534
00535 continue;
00536 }
00537
00538
00539
00540
00541
00542
00543 #if 0
00544 int durationBeforeStart = event->recurrence()->durationTo(yesterStart);
00545 int durationUntilEnd = event->recurrence()->durationTo(end);
00546 if (durationBeforeStart == durationUntilEnd) {
00547 kdDebug(5800) << "Skipping recurring event without occurences in TOI" << endl;
00548 continue;
00549 }
00550 #endif
00551 break;
00552 }
00553 }
00554
00555 eventList.append( event );
00556 }
00557
00558 return eventList;
00559 }
00560
00561 Event::List CalendarLocal::rawEventsForDate( const QDateTime &qdt )
00562 {
00563 return rawEventsForDate( qdt.date() );
00564 }
00565
00566 Event::List CalendarLocal::rawEvents( EventSortField sortField, SortDirection sortDirection )
00567 {
00568 Event::List eventList;
00569 EventDictIterator it( mEvents );
00570 for( ; it.current(); ++it )
00571 eventList.append( *it );
00572 return sortEvents( &eventList, sortField, sortDirection );
00573 }
00574
00575 bool CalendarLocal::addJournal(Journal *journal)
00576 {
00577
00578
00579
00580
00581
00582 mJournalList.append(journal);
00583
00584 journal->registerObserver( this );
00585
00586 setModified( true );
00587
00588 notifyIncidenceAdded( journal );
00589
00590 return true;
00591 }
00592
00593 bool CalendarLocal::deleteJournal( Journal *journal )
00594 {
00595 if ( mJournalList.removeRef( journal ) ) {
00596 setModified( true );
00597 notifyIncidenceDeleted( journal );
00598 mDeletedIncidences.append( journal );
00599 return true;
00600 } else {
00601 kdWarning() << "CalendarLocal::deleteJournal(): Journal not found." << endl;
00602 return false;
00603 }
00604 }
00605
00606 void CalendarLocal::deleteAllJournals()
00607 {
00608 Journal::List::ConstIterator it;
00609 for( it = mJournalList.begin(); it != mJournalList.end(); ++it ) {
00610 notifyIncidenceDeleted( *it );
00611 }
00612
00613 mJournalList.setAutoDelete( true );
00614 mJournalList.clearAll();
00615 mJournalList.setAutoDelete( false );
00616 }
00617
00618 Journal *CalendarLocal::journal( const QString &uid )
00619 {
00620 Journal::List::ConstIterator it;
00621 for ( it = mJournalList.begin(); it != mJournalList.end(); ++it )
00622 if ( (*it)->uid() == uid )
00623 return *it;
00624
00625 return 0;
00626 }
00627
00628 Journal::List CalendarLocal::rawJournals( JournalSortField sortField, SortDirection sortDirection )
00629 {
00630 return sortJournals( &mJournalList, sortField, sortDirection );
00631 }
00632
00633 Journal::List CalendarLocal::rawJournalsForDate( const QDate &date )
00634 {
00635 Journal::List journals;
00636
00637 Journal::List::ConstIterator it;
00638 for ( it = mJournalList.begin(); it != mJournalList.end(); ++it ) {
00639 Journal *journal = *it;
00640 if ( journal->dtStart().date() == date ) {
00641 journals.append( journal );
00642 }
00643 }
00644
00645 return journals;
00646 }
00647
00648 void CalendarLocal::setTimeZoneIdViewOnly( const QString& tz )
00649 {
00650 const QString question( i18n("The timezone setting was changed. In order to display the calendar "
00651 "you are looking at in the new timezone, it needs to be saved. Do you want to save the pending "
00652 "changes or rather wait and apply the new timezone on the next reload?" ) );
00653 int rc = KMessageBox::Yes;
00654 if ( isModified() ) {
00655 rc = KMessageBox::questionYesNo( 0, question,
00656 i18n("Save before applying timezones?"),
00657 KStdGuiItem::save(),
00658 KGuiItem(i18n("Apply Timezone Change on Next Reload")),
00659 "calendarLocalSaveBeforeTimezoneShift");
00660 }
00661 if ( rc == KMessageBox::Yes ) {
00662 reload( tz );
00663 }
00664 }