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