summaryrefslogtreecommitdiffabout
path: root/libkcal/calendarlocal.cpp
authorzautrix <zautrix>2005-06-08 10:34:22 (UTC)
committer zautrix <zautrix>2005-06-08 10:34:22 (UTC)
commit793d117812b4da36c9c11d90cccba347cbc6e208 (patch) (unidiff)
tree5bc77e2b7a32a541c2a3b3c3d6d50f873216deef /libkcal/calendarlocal.cpp
parent39d84e2fc3099bd5d7596e8be5dc6783826cec01 (diff)
downloadkdepimpi-793d117812b4da36c9c11d90cccba347cbc6e208.zip
kdepimpi-793d117812b4da36c9c11d90cccba347cbc6e208.tar.gz
kdepimpi-793d117812b4da36c9c11d90cccba347cbc6e208.tar.bz2
changed incidence type to a faster access method
Diffstat (limited to 'libkcal/calendarlocal.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendarlocal.cpp6
1 files changed, 1 insertions, 5 deletions
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index bc76c0b..fe74052 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -77,654 +77,650 @@ bool CalendarLocal::load( const QString &fileName )
77bool CalendarLocal::save( const QString &fileName, CalFormat *format ) 77bool CalendarLocal::save( const QString &fileName, CalFormat *format )
78{ 78{
79 FileStorage storage( this, fileName, format ); 79 FileStorage storage( this, fileName, format );
80 return storage.save(); 80 return storage.save();
81} 81}
82 82
83void CalendarLocal::close() 83void CalendarLocal::close()
84{ 84{
85 85
86 Todo * i; 86 Todo * i;
87 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false); 87 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false);
88 88
89 mEventList.setAutoDelete( true ); 89 mEventList.setAutoDelete( true );
90 mTodoList.setAutoDelete( true ); 90 mTodoList.setAutoDelete( true );
91 mJournalList.setAutoDelete( false ); 91 mJournalList.setAutoDelete( false );
92 92
93 mEventList.clear(); 93 mEventList.clear();
94 mTodoList.clear(); 94 mTodoList.clear();
95 mJournalList.clear(); 95 mJournalList.clear();
96 96
97 mEventList.setAutoDelete( false ); 97 mEventList.setAutoDelete( false );
98 mTodoList.setAutoDelete( false ); 98 mTodoList.setAutoDelete( false );
99 mJournalList.setAutoDelete( false ); 99 mJournalList.setAutoDelete( false );
100 100
101 setModified( false ); 101 setModified( false );
102} 102}
103 103
104bool CalendarLocal::addAnniversaryNoDup( Event *event ) 104bool CalendarLocal::addAnniversaryNoDup( Event *event )
105{ 105{
106 QString cat; 106 QString cat;
107 bool isBirthday = true; 107 bool isBirthday = true;
108 if( event->categoriesStr() == i18n( "Anniversary" ) ) { 108 if( event->categoriesStr() == i18n( "Anniversary" ) ) {
109 isBirthday = false; 109 isBirthday = false;
110 cat = i18n( "Anniversary" ); 110 cat = i18n( "Anniversary" );
111 } else if( event->categoriesStr() == i18n( "Birthday" ) ) { 111 } else if( event->categoriesStr() == i18n( "Birthday" ) ) {
112 isBirthday = true; 112 isBirthday = true;
113 cat = i18n( "Birthday" ); 113 cat = i18n( "Birthday" );
114 } else { 114 } else {
115 qDebug("addAnniversaryNoDup called without fitting category! "); 115 qDebug("addAnniversaryNoDup called without fitting category! ");
116 return false; 116 return false;
117 } 117 }
118 Event * eve; 118 Event * eve;
119 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 119 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
120 if ( !(eve->categories().contains( cat ) )) 120 if ( !(eve->categories().contains( cat ) ))
121 continue; 121 continue;
122 // now we have an event with fitting category 122 // now we have an event with fitting category
123 if ( eve->dtStart().date() != event->dtStart().date() ) 123 if ( eve->dtStart().date() != event->dtStart().date() )
124 continue; 124 continue;
125 // now we have an event with fitting category+date 125 // now we have an event with fitting category+date
126 if ( eve->summary() != event->summary() ) 126 if ( eve->summary() != event->summary() )
127 continue; 127 continue;
128 // now we have an event with fitting category+date+summary 128 // now we have an event with fitting category+date+summary
129 return false; 129 return false;
130 } 130 }
131 return addEvent( event ); 131 return addEvent( event );
132 132
133} 133}
134bool CalendarLocal::addEventNoDup( Event *event ) 134bool CalendarLocal::addEventNoDup( Event *event )
135{ 135{
136 Event * eve; 136 Event * eve;
137 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 137 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
138 if ( *eve == *event ) { 138 if ( *eve == *event ) {
139 //qDebug("CalendarLocal::Duplicate event found! Not inserted! "); 139 //qDebug("CalendarLocal::Duplicate event found! Not inserted! ");
140 return false; 140 return false;
141 } 141 }
142 } 142 }
143 return addEvent( event ); 143 return addEvent( event );
144} 144}
145 145
146bool CalendarLocal::addEvent( Event *event ) 146bool CalendarLocal::addEvent( Event *event )
147{ 147{
148 insertEvent( event ); 148 insertEvent( event );
149 149
150 event->registerObserver( this ); 150 event->registerObserver( this );
151 151
152 setModified( true ); 152 setModified( true );
153 153
154 return true; 154 return true;
155} 155}
156 156
157void CalendarLocal::deleteEvent( Event *event ) 157void CalendarLocal::deleteEvent( Event *event )
158{ 158{
159 if ( mUndoIncidence ) delete mUndoIncidence; 159 if ( mUndoIncidence ) delete mUndoIncidence;
160 mUndoIncidence = event->clone(); 160 mUndoIncidence = event->clone();
161 if ( mEventList.removeRef( event ) ) { 161 if ( mEventList.removeRef( event ) ) {
162 setModified( true ); 162 setModified( true );
163 } 163 }
164} 164}
165 165
166 166
167Event *CalendarLocal::event( const QString &uid ) 167Event *CalendarLocal::event( const QString &uid )
168{ 168{
169 169
170 Event *event; 170 Event *event;
171 171
172 for ( event = mEventList.first(); event; event = mEventList.next() ) { 172 for ( event = mEventList.first(); event; event = mEventList.next() ) {
173 if ( event->uid() == uid ) { 173 if ( event->uid() == uid ) {
174 return event; 174 return event;
175 } 175 }
176 } 176 }
177 177
178 return 0; 178 return 0;
179} 179}
180bool CalendarLocal::addTodoNoDup( Todo *todo ) 180bool CalendarLocal::addTodoNoDup( Todo *todo )
181{ 181{
182 Todo * eve; 182 Todo * eve;
183 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) { 183 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) {
184 if ( *eve == *todo ) { 184 if ( *eve == *todo ) {
185 //qDebug("duplicate todo found! not inserted! "); 185 //qDebug("duplicate todo found! not inserted! ");
186 return false; 186 return false;
187 } 187 }
188 } 188 }
189 return addTodo( todo ); 189 return addTodo( todo );
190} 190}
191bool CalendarLocal::addTodo( Todo *todo ) 191bool CalendarLocal::addTodo( Todo *todo )
192{ 192{
193 mTodoList.append( todo ); 193 mTodoList.append( todo );
194 194
195 todo->registerObserver( this ); 195 todo->registerObserver( this );
196 196
197 // Set up subtask relations 197 // Set up subtask relations
198 setupRelations( todo ); 198 setupRelations( todo );
199 199
200 setModified( true ); 200 setModified( true );
201 201
202 return true; 202 return true;
203} 203}
204 204
205void CalendarLocal::deleteTodo( Todo *todo ) 205void CalendarLocal::deleteTodo( Todo *todo )
206{ 206{
207 // Handle orphaned children 207 // Handle orphaned children
208 if ( mUndoIncidence ) delete mUndoIncidence; 208 if ( mUndoIncidence ) delete mUndoIncidence;
209 removeRelations( todo ); 209 removeRelations( todo );
210 mUndoIncidence = todo->clone(); 210 mUndoIncidence = todo->clone();
211 211
212 if ( mTodoList.removeRef( todo ) ) { 212 if ( mTodoList.removeRef( todo ) ) {
213 setModified( true ); 213 setModified( true );
214 } 214 }
215} 215}
216 216
217QPtrList<Todo> CalendarLocal::rawTodos() 217QPtrList<Todo> CalendarLocal::rawTodos()
218{ 218{
219 return mTodoList; 219 return mTodoList;
220} 220}
221Todo *CalendarLocal::todo( QString syncProf, QString id ) 221Todo *CalendarLocal::todo( QString syncProf, QString id )
222{ 222{
223 Todo *todo; 223 Todo *todo;
224 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 224 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
225 if ( todo->getID( syncProf ) == id ) return todo; 225 if ( todo->getID( syncProf ) == id ) return todo;
226 } 226 }
227 227
228 return 0; 228 return 0;
229} 229}
230void CalendarLocal::removeSyncInfo( QString syncProfile) 230void CalendarLocal::removeSyncInfo( QString syncProfile)
231{ 231{
232 QPtrList<Incidence> all = rawIncidences() ; 232 QPtrList<Incidence> all = rawIncidences() ;
233 Incidence *inc; 233 Incidence *inc;
234 for ( inc = all.first(); inc; inc = all.next() ) { 234 for ( inc = all.first(); inc; inc = all.next() ) {
235 inc->removeID( syncProfile ); 235 inc->removeID( syncProfile );
236 } 236 }
237 if ( syncProfile.isEmpty() ) { 237 if ( syncProfile.isEmpty() ) {
238 QPtrList<Event> el; 238 QPtrList<Event> el;
239 Event *todo; 239 Event *todo;
240 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 240 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
241 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 241 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
242 el.append( todo ); 242 el.append( todo );
243 } 243 }
244 for ( todo = el.first(); todo; todo = el.next() ) { 244 for ( todo = el.first(); todo; todo = el.next() ) {
245 deleteIncidence ( todo ); 245 deleteIncidence ( todo );
246 } 246 }
247 } else { 247 } else {
248 Event *lse = event( "last-syncEvent-"+ syncProfile); 248 Event *lse = event( "last-syncEvent-"+ syncProfile);
249 if ( lse ) 249 if ( lse )
250 deleteIncidence ( lse ); 250 deleteIncidence ( lse );
251 } 251 }
252} 252}
253QPtrList<Event> CalendarLocal::getExternLastSyncEvents() 253QPtrList<Event> CalendarLocal::getExternLastSyncEvents()
254{ 254{
255 QPtrList<Event> el; 255 QPtrList<Event> el;
256 Event *todo; 256 Event *todo;
257 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 257 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
258 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 258 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
259 if ( todo->summary().left(3) == "E: " ) 259 if ( todo->summary().left(3) == "E: " )
260 el.append( todo ); 260 el.append( todo );
261 } 261 }
262 262
263 return el; 263 return el;
264 264
265} 265}
266Event *CalendarLocal::event( QString syncProf, QString id ) 266Event *CalendarLocal::event( QString syncProf, QString id )
267{ 267{
268 Event *todo; 268 Event *todo;
269 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 269 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
270 if ( todo->getID( syncProf ) == id ) return todo; 270 if ( todo->getID( syncProf ) == id ) return todo;
271 } 271 }
272 272
273 return 0; 273 return 0;
274} 274}
275Todo *CalendarLocal::todo( const QString &uid ) 275Todo *CalendarLocal::todo( const QString &uid )
276{ 276{
277 Todo *todo; 277 Todo *todo;
278 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 278 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
279 if ( todo->uid() == uid ) return todo; 279 if ( todo->uid() == uid ) return todo;
280 } 280 }
281 281
282 return 0; 282 return 0;
283} 283}
284QString CalendarLocal::nextSummary() const 284QString CalendarLocal::nextSummary() const
285{ 285{
286 return mNextSummary; 286 return mNextSummary;
287} 287}
288QDateTime CalendarLocal::nextAlarmEventDateTime() const 288QDateTime CalendarLocal::nextAlarmEventDateTime() const
289{ 289{
290 return mNextAlarmEventDateTime; 290 return mNextAlarmEventDateTime;
291} 291}
292void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted) 292void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted)
293{ 293{
294 //mNextAlarmIncidence 294 //mNextAlarmIncidence
295 //mNextAlarmDateTime 295 //mNextAlarmDateTime
296 //return mNextSummary; 296 //return mNextSummary;
297 //return mNextAlarmEventDateTime; 297 //return mNextAlarmEventDateTime;
298 bool newNextAlarm = false; 298 bool newNextAlarm = false;
299 bool computeNextAlarm = false; 299 bool computeNextAlarm = false;
300 bool ok; 300 bool ok;
301 int offset; 301 int offset;
302 QDateTime nextA; 302 QDateTime nextA;
303 // QString nextSum; 303 // QString nextSum;
304 //QDateTime nextEvent; 304 //QDateTime nextEvent;
305 if ( mNextAlarmIncidence == 0 || incidence == 0 ) { 305 if ( mNextAlarmIncidence == 0 || incidence == 0 ) {
306 computeNextAlarm = true; 306 computeNextAlarm = true;
307 } else { 307 } else {
308 if ( ! deleted ) { 308 if ( ! deleted ) {
309 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 309 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
310 if ( ok ) { 310 if ( ok ) {
311 if ( nextA < mNextAlarmDateTime ) { 311 if ( nextA < mNextAlarmDateTime ) {
312 deRegisterAlarm(); 312 deRegisterAlarm();
313 mNextAlarmDateTime = nextA; 313 mNextAlarmDateTime = nextA;
314 mNextSummary = incidence->summary(); 314 mNextSummary = incidence->summary();
315 mNextAlarmEventDateTime = nextA.addSecs(offset ) ; 315 mNextAlarmEventDateTime = nextA.addSecs(offset ) ;
316 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 316 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
317 newNextAlarm = true; 317 newNextAlarm = true;
318 mNextAlarmIncidence = incidence; 318 mNextAlarmIncidence = incidence;
319 } else { 319 } else {
320 if ( incidence == mNextAlarmIncidence ) { 320 if ( incidence == mNextAlarmIncidence ) {
321 computeNextAlarm = true; 321 computeNextAlarm = true;
322 } 322 }
323 } 323 }
324 } else { 324 } else {
325 if ( mNextAlarmIncidence == incidence ) { 325 if ( mNextAlarmIncidence == incidence ) {
326 computeNextAlarm = true; 326 computeNextAlarm = true;
327 } 327 }
328 } 328 }
329 } else { // deleted 329 } else { // deleted
330 if ( incidence == mNextAlarmIncidence ) { 330 if ( incidence == mNextAlarmIncidence ) {
331 computeNextAlarm = true; 331 computeNextAlarm = true;
332 } 332 }
333 } 333 }
334 } 334 }
335 if ( computeNextAlarm ) { 335 if ( computeNextAlarm ) {
336 deRegisterAlarm(); 336 deRegisterAlarm();
337 nextA = nextAlarm( 1000 ); 337 nextA = nextAlarm( 1000 );
338 if (! mNextAlarmIncidence ) { 338 if (! mNextAlarmIncidence ) {
339 return; 339 return;
340 } 340 }
341 newNextAlarm = true; 341 newNextAlarm = true;
342 } 342 }
343 if ( newNextAlarm ) 343 if ( newNextAlarm )
344 registerAlarm(); 344 registerAlarm();
345} 345}
346QString CalendarLocal:: getAlarmNotification() 346QString CalendarLocal:: getAlarmNotification()
347{ 347{
348 QString ret; 348 QString ret;
349 // this should not happen 349 // this should not happen
350 if (! mNextAlarmIncidence ) 350 if (! mNextAlarmIncidence )
351 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString; 351 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString;
352 Alarm* alarm = mNextAlarmIncidence->alarms().first(); 352 Alarm* alarm = mNextAlarmIncidence->alarms().first();
353 if ( alarm->type() == Alarm::Procedure ) { 353 if ( alarm->type() == Alarm::Procedure ) {
354 ret = "proc_alarm" + alarm->programFile()+"+++"; 354 ret = "proc_alarm" + alarm->programFile()+"+++";
355 } else { 355 } else {
356 ret = "audio_alarm" +alarm->audioFile() +"+++"; 356 ret = "audio_alarm" +alarm->audioFile() +"+++";
357 } 357 }
358 ret += "cal_alarm"+ mNextSummary.left( 25 ); 358 ret += "cal_alarm"+ mNextSummary.left( 25 );
359 if ( mNextSummary.length() > 25 ) 359 if ( mNextSummary.length() > 25 )
360 ret += "\n" + mNextSummary.mid(25, 25 ); 360 ret += "\n" + mNextSummary.mid(25, 25 );
361 ret+= "\n"+mNextAlarmEventDateTimeString; 361 ret+= "\n"+mNextAlarmEventDateTimeString;
362 return ret; 362 return ret;
363} 363}
364void CalendarLocal::registerAlarm() 364void CalendarLocal::registerAlarm()
365{ 365{
366 mLastAlarmNotificationString = getAlarmNotification(); 366 mLastAlarmNotificationString = getAlarmNotification();
367 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() ); 367 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() );
368 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 368 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
369// #ifndef DESKTOP_VERSION 369// #ifndef DESKTOP_VERSION
370// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() ); 370// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() );
371// #endif 371// #endif
372} 372}
373void CalendarLocal::deRegisterAlarm() 373void CalendarLocal::deRegisterAlarm()
374{ 374{
375 if ( mLastAlarmNotificationString.isNull() ) 375 if ( mLastAlarmNotificationString.isNull() )
376 return; 376 return;
377 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() ); 377 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() );
378 378
379 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 379 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
380 mNextAlarmEventDateTime = QDateTime(); 380 mNextAlarmEventDateTime = QDateTime();
381// #ifndef DESKTOP_VERSION 381// #ifndef DESKTOP_VERSION
382// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() ); 382// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() );
383// #endif 383// #endif
384} 384}
385 385
386QPtrList<Todo> CalendarLocal::todos( const QDate &date ) 386QPtrList<Todo> CalendarLocal::todos( const QDate &date )
387{ 387{
388 QPtrList<Todo> todos; 388 QPtrList<Todo> todos;
389 389
390 Todo *todo; 390 Todo *todo;
391 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 391 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
392 if ( todo->hasDueDate() && todo->dtDue().date() == date ) { 392 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
393 todos.append( todo ); 393 todos.append( todo );
394 } 394 }
395 } 395 }
396 396
397 filter()->apply( &todos ); 397 filter()->apply( &todos );
398 return todos; 398 return todos;
399} 399}
400void CalendarLocal::reInitAlarmSettings() 400void CalendarLocal::reInitAlarmSettings()
401{ 401{
402 if ( !mNextAlarmIncidence ) { 402 if ( !mNextAlarmIncidence ) {
403 nextAlarm( 1000 ); 403 nextAlarm( 1000 );
404 } 404 }
405 deRegisterAlarm(); 405 deRegisterAlarm();
406 mNextAlarmIncidence = 0; 406 mNextAlarmIncidence = 0;
407 checkAlarmForIncidence( 0, false ); 407 checkAlarmForIncidence( 0, false );
408 408
409} 409}
410 410
411 411
412 412
413QDateTime CalendarLocal::nextAlarm( int daysTo ) 413QDateTime CalendarLocal::nextAlarm( int daysTo )
414{ 414{
415 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo ); 415 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo );
416 QDateTime start = QDateTime::currentDateTime().addSecs( 30 ); 416 QDateTime start = QDateTime::currentDateTime().addSecs( 30 );
417 QDateTime next; 417 QDateTime next;
418 Event *e; 418 Event *e;
419 bool ok; 419 bool ok;
420 bool found = false; 420 bool found = false;
421 int offset; 421 int offset;
422 mNextAlarmIncidence = 0; 422 mNextAlarmIncidence = 0;
423 for( e = mEventList.first(); e; e = mEventList.next() ) { 423 for( e = mEventList.first(); e; e = mEventList.next() ) {
424 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 424 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
425 if ( ok ) { 425 if ( ok ) {
426 if ( next < nextA ) { 426 if ( next < nextA ) {
427 nextA = next; 427 nextA = next;
428 found = true; 428 found = true;
429 mNextSummary = e->summary(); 429 mNextSummary = e->summary();
430 mNextAlarmEventDateTime = next.addSecs(offset ) ; 430 mNextAlarmEventDateTime = next.addSecs(offset ) ;
431 mNextAlarmIncidence = (Incidence *) e; 431 mNextAlarmIncidence = (Incidence *) e;
432 } 432 }
433 } 433 }
434 } 434 }
435 Todo *t; 435 Todo *t;
436 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 436 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
437 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 437 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
438 if ( ok ) { 438 if ( ok ) {
439 if ( next < nextA ) { 439 if ( next < nextA ) {
440 nextA = next; 440 nextA = next;
441 found = true; 441 found = true;
442 mNextSummary = t->summary(); 442 mNextSummary = t->summary();
443 mNextAlarmEventDateTime = next.addSecs(offset ); 443 mNextAlarmEventDateTime = next.addSecs(offset );
444 mNextAlarmIncidence = (Incidence *) t; 444 mNextAlarmIncidence = (Incidence *) t;
445 } 445 }
446 } 446 }
447 } 447 }
448 if ( mNextAlarmIncidence ) { 448 if ( mNextAlarmIncidence ) {
449 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 449 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
450 mNextAlarmDateTime = nextA; 450 mNextAlarmDateTime = nextA;
451 } 451 }
452 return nextA; 452 return nextA;
453} 453}
454Alarm::List CalendarLocal::alarmsTo( const QDateTime &to ) 454Alarm::List CalendarLocal::alarmsTo( const QDateTime &to )
455{ 455{
456 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to ); 456 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to );
457} 457}
458 458
459Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to ) 459Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to )
460{ 460{
461 kdDebug(5800) << "CalendarLocal::alarms(" << from.toString() << " - " 461
462 << to.toString() << ")\n";
463
464 Alarm::List alarms; 462 Alarm::List alarms;
465 463
466 Event *e; 464 Event *e;
467 465
468 for( e = mEventList.first(); e; e = mEventList.next() ) { 466 for( e = mEventList.first(); e; e = mEventList.next() ) {
469 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to ); 467 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to );
470 else appendAlarms( alarms, e, from, to ); 468 else appendAlarms( alarms, e, from, to );
471 } 469 }
472 470
473 Todo *t; 471 Todo *t;
474 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 472 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
475 appendAlarms( alarms, t, from, to ); 473 appendAlarms( alarms, t, from, to );
476 } 474 }
477 475
478 return alarms; 476 return alarms;
479} 477}
480 478
481void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence, 479void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence,
482 const QDateTime &from, const QDateTime &to ) 480 const QDateTime &from, const QDateTime &to )
483{ 481{
484 QPtrList<Alarm> alarmList = incidence->alarms(); 482 QPtrList<Alarm> alarmList = incidence->alarms();
485 Alarm *alarm; 483 Alarm *alarm;
486 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 484 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
487// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text() 485// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text()
488// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl; 486// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl;
489 if ( alarm->enabled() ) { 487 if ( alarm->enabled() ) {
490 if ( alarm->time() >= from && alarm->time() <= to ) { 488 if ( alarm->time() >= from && alarm->time() <= to ) {
491 kdDebug(5800) << "CalendarLocal::appendAlarms() '" << incidence->summary()
492 << "': " << alarm->time().toString() << endl;
493 alarms.append( alarm ); 489 alarms.append( alarm );
494 } 490 }
495 } 491 }
496 } 492 }
497} 493}
498 494
499void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms, 495void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms,
500 Incidence *incidence, 496 Incidence *incidence,
501 const QDateTime &from, 497 const QDateTime &from,
502 const QDateTime &to ) 498 const QDateTime &to )
503{ 499{
504 500
505 QPtrList<Alarm> alarmList = incidence->alarms(); 501 QPtrList<Alarm> alarmList = incidence->alarms();
506 Alarm *alarm; 502 Alarm *alarm;
507 QDateTime qdt; 503 QDateTime qdt;
508 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 504 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
509 if (incidence->recursOn(from.date())) { 505 if (incidence->recursOn(from.date())) {
510 qdt.setTime(alarm->time().time()); 506 qdt.setTime(alarm->time().time());
511 qdt.setDate(from.date()); 507 qdt.setDate(from.date());
512 } 508 }
513 else qdt = alarm->time(); 509 else qdt = alarm->time();
514 // qDebug("1 %s %s %s", qdt.toString().latin1(), from.toString().latin1(), to.toString().latin1()); 510 // qDebug("1 %s %s %s", qdt.toString().latin1(), from.toString().latin1(), to.toString().latin1());
515 if ( alarm->enabled() ) { 511 if ( alarm->enabled() ) {
516 if ( qdt >= from && qdt <= to ) { 512 if ( qdt >= from && qdt <= to ) {
517 alarms.append( alarm ); 513 alarms.append( alarm );
518 } 514 }
519 } 515 }
520 } 516 }
521} 517}
522 518
523 519
524/****************************** PROTECTED METHODS ****************************/ 520/****************************** PROTECTED METHODS ****************************/
525 521
526// after changes are made to an event, this should be called. 522// after changes are made to an event, this should be called.
527void CalendarLocal::update( IncidenceBase *incidence ) 523void CalendarLocal::update( IncidenceBase *incidence )
528{ 524{
529 incidence->setSyncStatus( Event::SYNCMOD ); 525 incidence->setSyncStatus( Event::SYNCMOD );
530 incidence->setLastModified( QDateTime::currentDateTime() ); 526 incidence->setLastModified( QDateTime::currentDateTime() );
531 // we should probably update the revision number here, 527 // we should probably update the revision number here,
532 // or internally in the Event itself when certain things change. 528 // or internally in the Event itself when certain things change.
533 // need to verify with ical documentation. 529 // need to verify with ical documentation.
534 530
535 setModified( true ); 531 setModified( true );
536} 532}
537 533
538void CalendarLocal::insertEvent( Event *event ) 534void CalendarLocal::insertEvent( Event *event )
539{ 535{
540 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event ); 536 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event );
541} 537}
542 538
543 539
544QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted ) 540QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted )
545{ 541{
546 QPtrList<Event> eventList; 542 QPtrList<Event> eventList;
547 543
548 Event *event; 544 Event *event;
549 for( event = mEventList.first(); event; event = mEventList.next() ) { 545 for( event = mEventList.first(); event; event = mEventList.next() ) {
550 if ( event->doesRecur() ) { 546 if ( event->doesRecur() ) {
551 if ( event->isMultiDay() ) { 547 if ( event->isMultiDay() ) {
552 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() ); 548 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() );
553 int i; 549 int i;
554 for ( i = 0; i <= extraDays; i++ ) { 550 for ( i = 0; i <= extraDays; i++ ) {
555 if ( event->recursOn( qd.addDays( -i ) ) ) { 551 if ( event->recursOn( qd.addDays( -i ) ) ) {
556 eventList.append( event ); 552 eventList.append( event );
557 break; 553 break;
558 } 554 }
559 } 555 }
560 } else { 556 } else {
561 if ( event->recursOn( qd ) ) 557 if ( event->recursOn( qd ) )
562 eventList.append( event ); 558 eventList.append( event );
563 } 559 }
564 } else { 560 } else {
565 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) { 561 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) {
566 eventList.append( event ); 562 eventList.append( event );
567 } 563 }
568 } 564 }
569 } 565 }
570 566
571 if ( !sorted ) { 567 if ( !sorted ) {
572 return eventList; 568 return eventList;
573 } 569 }
574 570
575 // kdDebug(5800) << "Sorting events for date\n" << endl; 571 // kdDebug(5800) << "Sorting events for date\n" << endl;
576 // now, we have to sort it based on dtStart.time() 572 // now, we have to sort it based on dtStart.time()
577 QPtrList<Event> eventListSorted; 573 QPtrList<Event> eventListSorted;
578 Event *sortEvent; 574 Event *sortEvent;
579 for ( event = eventList.first(); event; event = eventList.next() ) { 575 for ( event = eventList.first(); event; event = eventList.next() ) {
580 sortEvent = eventListSorted.first(); 576 sortEvent = eventListSorted.first();
581 int i = 0; 577 int i = 0;
582 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() ) 578 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() )
583 { 579 {
584 i++; 580 i++;
585 sortEvent = eventListSorted.next(); 581 sortEvent = eventListSorted.next();
586 } 582 }
587 eventListSorted.insert( i, event ); 583 eventListSorted.insert( i, event );
588 } 584 }
589 return eventListSorted; 585 return eventListSorted;
590} 586}
591 587
592 588
593QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end, 589QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end,
594 bool inclusive ) 590 bool inclusive )
595{ 591{
596 Event *event = 0; 592 Event *event = 0;
597 593
598 QPtrList<Event> eventList; 594 QPtrList<Event> eventList;
599 595
600 // Get non-recurring events 596 // Get non-recurring events
601 for( event = mEventList.first(); event; event = mEventList.next() ) { 597 for( event = mEventList.first(); event; event = mEventList.next() ) {
602 if ( event->doesRecur() ) { 598 if ( event->doesRecur() ) {
603 QDate rStart = event->dtStart().date(); 599 QDate rStart = event->dtStart().date();
604 bool found = false; 600 bool found = false;
605 if ( inclusive ) { 601 if ( inclusive ) {
606 if ( rStart >= start && rStart <= end ) { 602 if ( rStart >= start && rStart <= end ) {
607 // Start date of event is in range. Now check for end date. 603 // Start date of event is in range. Now check for end date.
608 // if duration is negative, event recurs forever, so do not include it. 604 // if duration is negative, event recurs forever, so do not include it.
609 if ( event->recurrence()->duration() == 0 ) { // End date set 605 if ( event->recurrence()->duration() == 0 ) { // End date set
610 QDate rEnd = event->recurrence()->endDate(); 606 QDate rEnd = event->recurrence()->endDate();
611 if ( rEnd >= start && rEnd <= end ) { // End date within range 607 if ( rEnd >= start && rEnd <= end ) { // End date within range
612 found = true; 608 found = true;
613 } 609 }
614 } else if ( event->recurrence()->duration() > 0 ) { // Duration set 610 } else if ( event->recurrence()->duration() > 0 ) { // Duration set
615 // TODO: Calculate end date from duration. Should be done in Event 611 // TODO: Calculate end date from duration. Should be done in Event
616 // For now exclude all events with a duration. 612 // For now exclude all events with a duration.
617 } 613 }
618 } 614 }
619 } else { 615 } else {
620 bool founOne; 616 bool founOne;
621 QDate next = event->getNextOccurence( start, &founOne ).date(); 617 QDate next = event->getNextOccurence( start, &founOne ).date();
622 if ( founOne ) { 618 if ( founOne ) {
623 if ( next <= end ) { 619 if ( next <= end ) {
624 found = true; 620 found = true;
625 } 621 }
626 } 622 }
627 623
628 /* 624 /*
629 // crap !!! 625 // crap !!!
630 if ( rStart <= end ) { // Start date not after range 626 if ( rStart <= end ) { // Start date not after range
631 if ( rStart >= start ) { // Start date within range 627 if ( rStart >= start ) { // Start date within range
632 found = true; 628 found = true;
633 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever 629 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever
634 found = true; 630 found = true;
635 } else if ( event->recurrence()->duration() == 0 ) { // End date set 631 } else if ( event->recurrence()->duration() == 0 ) { // End date set
636 QDate rEnd = event->recurrence()->endDate(); 632 QDate rEnd = event->recurrence()->endDate();
637 if ( rEnd >= start && rEnd <= end ) { // End date within range 633 if ( rEnd >= start && rEnd <= end ) { // End date within range
638 found = true; 634 found = true;
639 } 635 }
640 } else { // Duration set 636 } else { // Duration set
641 // TODO: Calculate end date from duration. Should be done in Event 637 // TODO: Calculate end date from duration. Should be done in Event
642 // For now include all events with a duration. 638 // For now include all events with a duration.
643 found = true; 639 found = true;
644 } 640 }
645 } 641 }
646 */ 642 */
647 643
648 } 644 }
649 645
650 if ( found ) eventList.append( event ); 646 if ( found ) eventList.append( event );
651 } else { 647 } else {
652 QDate s = event->dtStart().date(); 648 QDate s = event->dtStart().date();
653 QDate e = event->dtEnd().date(); 649 QDate e = event->dtEnd().date();
654 650
655 if ( inclusive ) { 651 if ( inclusive ) {
656 if ( s >= start && e <= end ) { 652 if ( s >= start && e <= end ) {
657 eventList.append( event ); 653 eventList.append( event );
658 } 654 }
659 } else { 655 } else {
660 if ( ( s >= start && s <= end ) || ( e >= start && e <= end ) ) { 656 if ( ( s >= start && s <= end ) || ( e >= start && e <= end ) ) {
661 eventList.append( event ); 657 eventList.append( event );
662 } 658 }
663 } 659 }
664 } 660 }
665 } 661 }
666 662
667 return eventList; 663 return eventList;
668} 664}
669 665
670QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt ) 666QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt )
671{ 667{
672 return rawEventsForDate( qdt.date() ); 668 return rawEventsForDate( qdt.date() );
673} 669}
674 670
675QPtrList<Event> CalendarLocal::rawEvents() 671QPtrList<Event> CalendarLocal::rawEvents()
676{ 672{
677 return mEventList; 673 return mEventList;
678} 674}
679 675
680bool CalendarLocal::addJournal(Journal *journal) 676bool CalendarLocal::addJournal(Journal *journal)
681{ 677{
682 if ( journal->dtStart().isValid()) 678 if ( journal->dtStart().isValid())
683 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl; 679 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
684 else 680 else
685 kdDebug(5800) << "Adding Journal without a DTSTART" << endl; 681 kdDebug(5800) << "Adding Journal without a DTSTART" << endl;
686 682
687 mJournalList.append(journal); 683 mJournalList.append(journal);
688 684
689 journal->registerObserver( this ); 685 journal->registerObserver( this );
690 686
691 setModified( true ); 687 setModified( true );
692 688
693 return true; 689 return true;
694} 690}
695 691
696void CalendarLocal::deleteJournal( Journal *journal ) 692void CalendarLocal::deleteJournal( Journal *journal )
697{ 693{
698 if ( mUndoIncidence ) delete mUndoIncidence; 694 if ( mUndoIncidence ) delete mUndoIncidence;
699 mUndoIncidence = journal->clone(); 695 mUndoIncidence = journal->clone();
700 mUndoIncidence->setSummary( mUndoIncidence->description().left(25)); 696 mUndoIncidence->setSummary( mUndoIncidence->description().left(25));
701 if ( mJournalList.removeRef(journal) ) { 697 if ( mJournalList.removeRef(journal) ) {
702 setModified( true ); 698 setModified( true );
703 } 699 }
704} 700}
705 701
706Journal *CalendarLocal::journal( const QDate &date ) 702Journal *CalendarLocal::journal( const QDate &date )
707{ 703{
708// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl; 704// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl;
709 705
710 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 706 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
711 if ( it->dtStart().date() == date ) 707 if ( it->dtStart().date() == date )
712 return it; 708 return it;
713 709
714 return 0; 710 return 0;
715} 711}
716 712
717Journal *CalendarLocal::journal( const QString &uid ) 713Journal *CalendarLocal::journal( const QString &uid )
718{ 714{
719 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 715 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
720 if ( it->uid() == uid ) 716 if ( it->uid() == uid )
721 return it; 717 return it;
722 718
723 return 0; 719 return 0;
724} 720}
725 721
726QPtrList<Journal> CalendarLocal::journals() 722QPtrList<Journal> CalendarLocal::journals()
727{ 723{
728 return mJournalList; 724 return mJournalList;
729} 725}
730 726