summaryrefslogtreecommitdiffabout
path: root/libkcal
Unidiff
Diffstat (limited to 'libkcal') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendarlocal.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index 5c889c3..00a43e5 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -1,726 +1,726 @@
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 if ( mUndoIncidence ) delete mUndoIncidence; 155 if ( mUndoIncidence ) delete mUndoIncidence;
156 mUndoIncidence = event->clone(); 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 if ( mUndoIncidence ) delete mUndoIncidence; 204 if ( mUndoIncidence ) delete mUndoIncidence;
205 removeRelations( todo ); 205 removeRelations( todo );
206 mUndoIncidence = todo->clone(); 206 mUndoIncidence = todo->clone();
207 207
208 if ( mTodoList.removeRef( todo ) ) { 208 if ( mTodoList.removeRef( todo ) ) {
209 setModified( true ); 209 setModified( true );
210 } 210 }
211} 211}
212 212
213QPtrList<Todo> CalendarLocal::rawTodos() 213QPtrList<Todo> CalendarLocal::rawTodos()
214{ 214{
215 return mTodoList; 215 return mTodoList;
216} 216}
217Todo *CalendarLocal::todo( QString syncProf, QString id ) 217Todo *CalendarLocal::todo( QString syncProf, QString id )
218{ 218{
219 Todo *todo; 219 Todo *todo;
220 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 220 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
221 if ( todo->getID( syncProf ) == id ) return todo; 221 if ( todo->getID( syncProf ) == id ) return todo;
222 } 222 }
223 223
224 return 0; 224 return 0;
225} 225}
226void CalendarLocal::removeSyncInfo( QString syncProfile) 226void CalendarLocal::removeSyncInfo( QString syncProfile)
227{ 227{
228 QPtrList<Incidence> all = rawIncidences() ; 228 QPtrList<Incidence> all = rawIncidences() ;
229 Incidence *inc; 229 Incidence *inc;
230 for ( inc = all.first(); inc; inc = all.next() ) { 230 for ( inc = all.first(); inc; inc = all.next() ) {
231 inc->removeID( syncProfile ); 231 inc->removeID( syncProfile );
232 } 232 }
233 if ( syncProfile.isEmpty() ) { 233 if ( syncProfile.isEmpty() ) {
234 QPtrList<Event> el; 234 QPtrList<Event> el;
235 Event *todo; 235 Event *todo;
236 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 236 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
237 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 237 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
238 el.append( todo ); 238 el.append( todo );
239 } 239 }
240 for ( todo = el.first(); todo; todo = el.next() ) { 240 for ( todo = el.first(); todo; todo = el.next() ) {
241 deleteIncidence ( todo ); 241 deleteIncidence ( todo );
242 } 242 }
243 } else { 243 } else {
244 Event *lse = event( "last-syncEvent-"+ syncProfile); 244 Event *lse = event( "last-syncEvent-"+ syncProfile);
245 if ( lse ) 245 if ( lse )
246 deleteIncidence ( lse ); 246 deleteIncidence ( lse );
247 } 247 }
248} 248}
249QPtrList<Event> CalendarLocal::getExternLastSyncEvents() 249QPtrList<Event> CalendarLocal::getExternLastSyncEvents()
250{ 250{
251 QPtrList<Event> el; 251 QPtrList<Event> el;
252 Event *todo; 252 Event *todo;
253 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 253 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
254 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 254 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
255 if ( todo->summary().left(3) == "E: " ) 255 if ( todo->summary().left(3) == "E: " )
256 el.append( todo ); 256 el.append( todo );
257 } 257 }
258 258
259 return el; 259 return el;
260 260
261} 261}
262Event *CalendarLocal::event( QString syncProf, QString id ) 262Event *CalendarLocal::event( QString syncProf, QString id )
263{ 263{
264 Event *todo; 264 Event *todo;
265 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 265 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
266 if ( todo->getID( syncProf ) == id ) return todo; 266 if ( todo->getID( syncProf ) == id ) return todo;
267 } 267 }
268 268
269 return 0; 269 return 0;
270} 270}
271Todo *CalendarLocal::todo( const QString &uid ) 271Todo *CalendarLocal::todo( const QString &uid )
272{ 272{
273 Todo *todo; 273 Todo *todo;
274 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 274 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
275 if ( todo->uid() == uid ) return todo; 275 if ( todo->uid() == uid ) return todo;
276 } 276 }
277 277
278 return 0; 278 return 0;
279} 279}
280QString CalendarLocal::nextSummary() const 280QString CalendarLocal::nextSummary() const
281{ 281{
282 return mNextSummary; 282 return mNextSummary;
283} 283}
284QDateTime CalendarLocal::nextAlarmEventDateTime() const 284QDateTime CalendarLocal::nextAlarmEventDateTime() const
285{ 285{
286 return mNextAlarmEventDateTime; 286 return mNextAlarmEventDateTime;
287} 287}
288void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted) 288void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted)
289{ 289{
290 //mNextAlarmIncidence 290 //mNextAlarmIncidence
291 //mNextAlarmDateTime 291 //mNextAlarmDateTime
292 //return mNextSummary; 292 //return mNextSummary;
293 //return mNextAlarmEventDateTime; 293 //return mNextAlarmEventDateTime;
294 bool newNextAlarm = false; 294 bool newNextAlarm = false;
295 bool computeNextAlarm = false; 295 bool computeNextAlarm = false;
296 bool ok; 296 bool ok;
297 int offset; 297 int offset;
298 QDateTime nextA; 298 QDateTime nextA;
299 // QString nextSum; 299 // QString nextSum;
300 //QDateTime nextEvent; 300 //QDateTime nextEvent;
301 mNextAlarmEventDateTime = QDateTime();
302 if ( mNextAlarmIncidence == 0 || incidence == 0 ) { 301 if ( mNextAlarmIncidence == 0 || incidence == 0 ) {
303 computeNextAlarm = true; 302 computeNextAlarm = true;
304 } else { 303 } else {
305 if ( ! deleted ) { 304 if ( ! deleted ) {
306 nextA = incidence->getNextAlarmDateTime(& ok, &offset ) ; 305 nextA = incidence->getNextAlarmDateTime(& ok, &offset ) ;
307 if ( ok ) { 306 if ( ok ) {
308 if ( nextA < mNextAlarmDateTime ) { 307 if ( nextA < mNextAlarmDateTime ) {
309 deRegisterAlarm(); 308 deRegisterAlarm();
310 mNextAlarmDateTime = nextA; 309 mNextAlarmDateTime = nextA;
311 mNextSummary = incidence->summary(); 310 mNextSummary = incidence->summary();
312 mNextAlarmEventDateTime = nextA.addSecs(offset ) ; 311 mNextAlarmEventDateTime = nextA.addSecs(offset ) ;
313 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 312 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
314 newNextAlarm = true; 313 newNextAlarm = true;
315 mNextAlarmIncidence = incidence; 314 mNextAlarmIncidence = incidence;
316 } else { 315 } else {
317 if ( incidence == mNextAlarmIncidence ) { 316 if ( incidence == mNextAlarmIncidence ) {
318 computeNextAlarm = true; 317 computeNextAlarm = true;
319 } 318 }
320 } 319 }
321 } else { 320 } else {
322 if ( mNextAlarmIncidence == incidence ) { 321 if ( mNextAlarmIncidence == incidence ) {
323 computeNextAlarm = true; 322 computeNextAlarm = true;
324 } 323 }
325 } 324 }
326 } else { // deleted 325 } else { // deleted
327 if ( incidence == mNextAlarmIncidence ) { 326 if ( incidence == mNextAlarmIncidence ) {
328 computeNextAlarm = true; 327 computeNextAlarm = true;
329 } 328 }
330 } 329 }
331 } 330 }
332 if ( computeNextAlarm ) { 331 if ( computeNextAlarm ) {
333 deRegisterAlarm(); 332 deRegisterAlarm();
334 nextA = nextAlarm( 1000 ); 333 nextA = nextAlarm( 1000 );
335 if (! mNextAlarmIncidence ) { 334 if (! mNextAlarmIncidence ) {
336 return; 335 return;
337 } 336 }
338 newNextAlarm = true; 337 newNextAlarm = true;
339 } 338 }
340 if ( newNextAlarm ) 339 if ( newNextAlarm )
341 registerAlarm(); 340 registerAlarm();
342} 341}
343QString CalendarLocal:: getAlarmNotification() 342QString CalendarLocal:: getAlarmNotification()
344{ 343{
345 QString ret; 344 QString ret;
346 // this should not happen 345 // this should not happen
347 if (! mNextAlarmIncidence ) 346 if (! mNextAlarmIncidence )
348 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString; 347 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString;
349 Alarm* alarm = mNextAlarmIncidence->alarms().first(); 348 Alarm* alarm = mNextAlarmIncidence->alarms().first();
350 if ( alarm->type() == Alarm::Procedure ) { 349 if ( alarm->type() == Alarm::Procedure ) {
351 ret = "proc_alarm" + alarm->programFile()+"+++"; 350 ret = "proc_alarm" + alarm->programFile()+"+++";
352 } else { 351 } else {
353 ret = "audio_alarm" +alarm->audioFile() +"+++"; 352 ret = "audio_alarm" +alarm->audioFile() +"+++";
354 } 353 }
355 ret += "cal_alarm"+ mNextSummary.left( 25 ); 354 ret += "cal_alarm"+ mNextSummary.left( 25 );
356 if ( mNextSummary.length() > 25 ) 355 if ( mNextSummary.length() > 25 )
357 ret += "\n" + mNextSummary.mid(25, 25 ); 356 ret += "\n" + mNextSummary.mid(25, 25 );
358 ret+= "\n"+mNextAlarmEventDateTimeString; 357 ret+= "\n"+mNextAlarmEventDateTimeString;
359 return ret; 358 return ret;
360} 359}
361void CalendarLocal::registerAlarm() 360void CalendarLocal::registerAlarm()
362{ 361{
363 mLastAlarmNotificationString = getAlarmNotification(); 362 mLastAlarmNotificationString = getAlarmNotification();
364 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() ); 363 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() );
365 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 364 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
366// #ifndef DESKTOP_VERSION 365// #ifndef DESKTOP_VERSION
367// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() ); 366// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() );
368// #endif 367// #endif
369} 368}
370void CalendarLocal::deRegisterAlarm() 369void CalendarLocal::deRegisterAlarm()
371{ 370{
372 if ( mLastAlarmNotificationString.isNull() ) 371 if ( mLastAlarmNotificationString.isNull() )
373 return; 372 return;
374 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() ); 373 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() );
375 374
376 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 375 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
376 mNextAlarmEventDateTime = QDateTime();
377// #ifndef DESKTOP_VERSION 377// #ifndef DESKTOP_VERSION
378// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() ); 378// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() );
379// #endif 379// #endif
380} 380}
381 381
382QPtrList<Todo> CalendarLocal::todos( const QDate &date ) 382QPtrList<Todo> CalendarLocal::todos( const QDate &date )
383{ 383{
384 QPtrList<Todo> todos; 384 QPtrList<Todo> todos;
385 385
386 Todo *todo; 386 Todo *todo;
387 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 387 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
388 if ( todo->hasDueDate() && todo->dtDue().date() == date ) { 388 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
389 todos.append( todo ); 389 todos.append( todo );
390 } 390 }
391 } 391 }
392 392
393 filter()->apply( &todos ); 393 filter()->apply( &todos );
394 return todos; 394 return todos;
395} 395}
396void CalendarLocal::reInitAlarmSettings() 396void CalendarLocal::reInitAlarmSettings()
397{ 397{
398 if ( !mNextAlarmIncidence ) { 398 if ( !mNextAlarmIncidence ) {
399 nextAlarm( 1000 ); 399 nextAlarm( 1000 );
400 } 400 }
401 deRegisterAlarm(); 401 deRegisterAlarm();
402 mNextAlarmIncidence = 0; 402 mNextAlarmIncidence = 0;
403 checkAlarmForIncidence( 0, false ); 403 checkAlarmForIncidence( 0, false );
404 404
405} 405}
406 406
407 407
408 408
409QDateTime CalendarLocal::nextAlarm( int daysTo ) 409QDateTime CalendarLocal::nextAlarm( int daysTo )
410{ 410{
411 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo ); 411 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo );
412 QDateTime start = QDateTime::currentDateTime().addSecs( 30 ); 412 QDateTime start = QDateTime::currentDateTime().addSecs( 30 );
413 QDateTime next; 413 QDateTime next;
414 Event *e; 414 Event *e;
415 bool ok; 415 bool ok;
416 bool found = false; 416 bool found = false;
417 int offset; 417 int offset;
418 mNextAlarmIncidence = 0; 418 mNextAlarmIncidence = 0;
419 for( e = mEventList.first(); e; e = mEventList.next() ) { 419 for( e = mEventList.first(); e; e = mEventList.next() ) {
420 next = e->getNextAlarmDateTime(& ok, &offset ) ; 420 next = e->getNextAlarmDateTime(& ok, &offset ) ;
421 if ( ok ) { 421 if ( ok ) {
422 if ( next < nextA ) { 422 if ( next < nextA ) {
423 nextA = next; 423 nextA = next;
424 found = true; 424 found = true;
425 mNextSummary = e->summary(); 425 mNextSummary = e->summary();
426 mNextAlarmEventDateTime = next.addSecs(offset ) ; 426 mNextAlarmEventDateTime = next.addSecs(offset ) ;
427 mNextAlarmIncidence = (Incidence *) e; 427 mNextAlarmIncidence = (Incidence *) e;
428 } 428 }
429 } 429 }
430 } 430 }
431 Todo *t; 431 Todo *t;
432 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 432 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
433 next = t->getNextAlarmDateTime(& ok, &offset ) ; 433 next = t->getNextAlarmDateTime(& ok, &offset ) ;
434 if ( ok ) { 434 if ( ok ) {
435 if ( next < nextA ) { 435 if ( next < nextA ) {
436 nextA = next; 436 nextA = next;
437 found = true; 437 found = true;
438 mNextSummary = t->summary(); 438 mNextSummary = t->summary();
439 mNextAlarmEventDateTime = next.addSecs(offset ); 439 mNextAlarmEventDateTime = next.addSecs(offset );
440 mNextAlarmIncidence = (Incidence *) t; 440 mNextAlarmIncidence = (Incidence *) t;
441 } 441 }
442 } 442 }
443 } 443 }
444 if ( mNextAlarmIncidence ) { 444 if ( mNextAlarmIncidence ) {
445 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 445 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
446 mNextAlarmDateTime = nextA; 446 mNextAlarmDateTime = nextA;
447 } 447 }
448 return nextA; 448 return nextA;
449} 449}
450Alarm::List CalendarLocal::alarmsTo( const QDateTime &to ) 450Alarm::List CalendarLocal::alarmsTo( const QDateTime &to )
451{ 451{
452 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to ); 452 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to );
453} 453}
454 454
455Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to ) 455Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to )
456{ 456{
457 kdDebug(5800) << "CalendarLocal::alarms(" << from.toString() << " - " 457 kdDebug(5800) << "CalendarLocal::alarms(" << from.toString() << " - "
458 << to.toString() << ")\n"; 458 << to.toString() << ")\n";
459 459
460 Alarm::List alarms; 460 Alarm::List alarms;
461 461
462 Event *e; 462 Event *e;
463 463
464 for( e = mEventList.first(); e; e = mEventList.next() ) { 464 for( e = mEventList.first(); e; e = mEventList.next() ) {
465 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to ); 465 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to );
466 else appendAlarms( alarms, e, from, to ); 466 else appendAlarms( alarms, e, from, to );
467 } 467 }
468 468
469 Todo *t; 469 Todo *t;
470 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 470 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
471 appendAlarms( alarms, t, from, to ); 471 appendAlarms( alarms, t, from, to );
472 } 472 }
473 473
474 return alarms; 474 return alarms;
475} 475}
476 476
477void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence, 477void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence,
478 const QDateTime &from, const QDateTime &to ) 478 const QDateTime &from, const QDateTime &to )
479{ 479{
480 QPtrList<Alarm> alarmList = incidence->alarms(); 480 QPtrList<Alarm> alarmList = incidence->alarms();
481 Alarm *alarm; 481 Alarm *alarm;
482 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 482 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
483// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text() 483// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text()
484// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl; 484// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl;
485 if ( alarm->enabled() ) { 485 if ( alarm->enabled() ) {
486 if ( alarm->time() >= from && alarm->time() <= to ) { 486 if ( alarm->time() >= from && alarm->time() <= to ) {
487 kdDebug(5800) << "CalendarLocal::appendAlarms() '" << incidence->summary() 487 kdDebug(5800) << "CalendarLocal::appendAlarms() '" << incidence->summary()
488 << "': " << alarm->time().toString() << endl; 488 << "': " << alarm->time().toString() << endl;
489 alarms.append( alarm ); 489 alarms.append( alarm );
490 } 490 }
491 } 491 }
492 } 492 }
493} 493}
494 494
495void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms, 495void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms,
496 Incidence *incidence, 496 Incidence *incidence,
497 const QDateTime &from, 497 const QDateTime &from,
498 const QDateTime &to ) 498 const QDateTime &to )
499{ 499{
500 500
501 QPtrList<Alarm> alarmList = incidence->alarms(); 501 QPtrList<Alarm> alarmList = incidence->alarms();
502 Alarm *alarm; 502 Alarm *alarm;
503 QDateTime qdt; 503 QDateTime qdt;
504 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 504 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
505 if (incidence->recursOn(from.date())) { 505 if (incidence->recursOn(from.date())) {
506 qdt.setTime(alarm->time().time()); 506 qdt.setTime(alarm->time().time());
507 qdt.setDate(from.date()); 507 qdt.setDate(from.date());
508 } 508 }
509 else qdt = alarm->time(); 509 else qdt = alarm->time();
510 // 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());
511 if ( alarm->enabled() ) { 511 if ( alarm->enabled() ) {
512 if ( qdt >= from && qdt <= to ) { 512 if ( qdt >= from && qdt <= to ) {
513 alarms.append( alarm ); 513 alarms.append( alarm );
514 } 514 }
515 } 515 }
516 } 516 }
517} 517}
518 518
519 519
520/****************************** PROTECTED METHODS ****************************/ 520/****************************** PROTECTED METHODS ****************************/
521 521
522// after changes are made to an event, this should be called. 522// after changes are made to an event, this should be called.
523void CalendarLocal::update( IncidenceBase *incidence ) 523void CalendarLocal::update( IncidenceBase *incidence )
524{ 524{
525 incidence->setSyncStatus( Event::SYNCMOD ); 525 incidence->setSyncStatus( Event::SYNCMOD );
526 incidence->setLastModified( QDateTime::currentDateTime() ); 526 incidence->setLastModified( QDateTime::currentDateTime() );
527 // we should probably update the revision number here, 527 // we should probably update the revision number here,
528 // or internally in the Event itself when certain things change. 528 // or internally in the Event itself when certain things change.
529 // need to verify with ical documentation. 529 // need to verify with ical documentation.
530 530
531 setModified( true ); 531 setModified( true );
532} 532}
533 533
534void CalendarLocal::insertEvent( Event *event ) 534void CalendarLocal::insertEvent( Event *event )
535{ 535{
536 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event ); 536 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event );
537} 537}
538 538
539 539
540QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted ) 540QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted )
541{ 541{
542 QPtrList<Event> eventList; 542 QPtrList<Event> eventList;
543 543
544 Event *event; 544 Event *event;
545 for( event = mEventList.first(); event; event = mEventList.next() ) { 545 for( event = mEventList.first(); event; event = mEventList.next() ) {
546 if ( event->doesRecur() ) { 546 if ( event->doesRecur() ) {
547 if ( event->isMultiDay() ) { 547 if ( event->isMultiDay() ) {
548 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() ); 548 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() );
549 int i; 549 int i;
550 for ( i = 0; i <= extraDays; i++ ) { 550 for ( i = 0; i <= extraDays; i++ ) {
551 if ( event->recursOn( qd.addDays( -i ) ) ) { 551 if ( event->recursOn( qd.addDays( -i ) ) ) {
552 eventList.append( event ); 552 eventList.append( event );
553 break; 553 break;
554 } 554 }
555 } 555 }
556 } else { 556 } else {
557 if ( event->recursOn( qd ) ) 557 if ( event->recursOn( qd ) )
558 eventList.append( event ); 558 eventList.append( event );
559 } 559 }
560 } else { 560 } else {
561 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) { 561 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) {
562 eventList.append( event ); 562 eventList.append( event );
563 } 563 }
564 } 564 }
565 } 565 }
566 566
567 if ( !sorted ) { 567 if ( !sorted ) {
568 return eventList; 568 return eventList;
569 } 569 }
570 570
571 // kdDebug(5800) << "Sorting events for date\n" << endl; 571 // kdDebug(5800) << "Sorting events for date\n" << endl;
572 // now, we have to sort it based on dtStart.time() 572 // now, we have to sort it based on dtStart.time()
573 QPtrList<Event> eventListSorted; 573 QPtrList<Event> eventListSorted;
574 Event *sortEvent; 574 Event *sortEvent;
575 for ( event = eventList.first(); event; event = eventList.next() ) { 575 for ( event = eventList.first(); event; event = eventList.next() ) {
576 sortEvent = eventListSorted.first(); 576 sortEvent = eventListSorted.first();
577 int i = 0; 577 int i = 0;
578 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() ) 578 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() )
579 { 579 {
580 i++; 580 i++;
581 sortEvent = eventListSorted.next(); 581 sortEvent = eventListSorted.next();
582 } 582 }
583 eventListSorted.insert( i, event ); 583 eventListSorted.insert( i, event );
584 } 584 }
585 return eventListSorted; 585 return eventListSorted;
586} 586}
587 587
588 588
589QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end, 589QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end,
590 bool inclusive ) 590 bool inclusive )
591{ 591{
592 Event *event = 0; 592 Event *event = 0;
593 593
594 QPtrList<Event> eventList; 594 QPtrList<Event> eventList;
595 595
596 // Get non-recurring events 596 // Get non-recurring events
597 for( event = mEventList.first(); event; event = mEventList.next() ) { 597 for( event = mEventList.first(); event; event = mEventList.next() ) {
598 if ( event->doesRecur() ) { 598 if ( event->doesRecur() ) {
599 QDate rStart = event->dtStart().date(); 599 QDate rStart = event->dtStart().date();
600 bool found = false; 600 bool found = false;
601 if ( inclusive ) { 601 if ( inclusive ) {
602 if ( rStart >= start && rStart <= end ) { 602 if ( rStart >= start && rStart <= end ) {
603 // 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.
604 // 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.
605 if ( event->recurrence()->duration() == 0 ) { // End date set 605 if ( event->recurrence()->duration() == 0 ) { // End date set
606 QDate rEnd = event->recurrence()->endDate(); 606 QDate rEnd = event->recurrence()->endDate();
607 if ( rEnd >= start && rEnd <= end ) { // End date within range 607 if ( rEnd >= start && rEnd <= end ) { // End date within range
608 found = true; 608 found = true;
609 } 609 }
610 } else if ( event->recurrence()->duration() > 0 ) { // Duration set 610 } else if ( event->recurrence()->duration() > 0 ) { // Duration set
611 // TODO: Calculate end date from duration. Should be done in Event 611 // TODO: Calculate end date from duration. Should be done in Event
612 // For now exclude all events with a duration. 612 // For now exclude all events with a duration.
613 } 613 }
614 } 614 }
615 } else { 615 } else {
616 bool founOne; 616 bool founOne;
617 QDate next = event->getNextOccurence( start, &founOne ).date(); 617 QDate next = event->getNextOccurence( start, &founOne ).date();
618 if ( founOne ) { 618 if ( founOne ) {
619 if ( next <= end ) { 619 if ( next <= end ) {
620 found = true; 620 found = true;
621 } 621 }
622 } 622 }
623 623
624 /* 624 /*
625 // crap !!! 625 // crap !!!
626 if ( rStart <= end ) { // Start date not after range 626 if ( rStart <= end ) { // Start date not after range
627 if ( rStart >= start ) { // Start date within range 627 if ( rStart >= start ) { // Start date within range
628 found = true; 628 found = true;
629 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever 629 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever
630 found = true; 630 found = true;
631 } else if ( event->recurrence()->duration() == 0 ) { // End date set 631 } else if ( event->recurrence()->duration() == 0 ) { // End date set
632 QDate rEnd = event->recurrence()->endDate(); 632 QDate rEnd = event->recurrence()->endDate();
633 if ( rEnd >= start && rEnd <= end ) { // End date within range 633 if ( rEnd >= start && rEnd <= end ) { // End date within range
634 found = true; 634 found = true;
635 } 635 }
636 } else { // Duration set 636 } else { // Duration set
637 // TODO: Calculate end date from duration. Should be done in Event 637 // TODO: Calculate end date from duration. Should be done in Event
638 // For now include all events with a duration. 638 // For now include all events with a duration.
639 found = true; 639 found = true;
640 } 640 }
641 } 641 }
642 */ 642 */
643 643
644 } 644 }
645 645
646 if ( found ) eventList.append( event ); 646 if ( found ) eventList.append( event );
647 } else { 647 } else {
648 QDate s = event->dtStart().date(); 648 QDate s = event->dtStart().date();
649 QDate e = event->dtEnd().date(); 649 QDate e = event->dtEnd().date();
650 650
651 if ( inclusive ) { 651 if ( inclusive ) {
652 if ( s >= start && e <= end ) { 652 if ( s >= start && e <= end ) {
653 eventList.append( event ); 653 eventList.append( event );
654 } 654 }
655 } else { 655 } else {
656 if ( ( s >= start && s <= end ) || ( e >= start && e <= end ) ) { 656 if ( ( s >= start && s <= end ) || ( e >= start && e <= end ) ) {
657 eventList.append( event ); 657 eventList.append( event );
658 } 658 }
659 } 659 }
660 } 660 }
661 } 661 }
662 662
663 return eventList; 663 return eventList;
664} 664}
665 665
666QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt ) 666QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt )
667{ 667{
668 return rawEventsForDate( qdt.date() ); 668 return rawEventsForDate( qdt.date() );
669} 669}
670 670
671QPtrList<Event> CalendarLocal::rawEvents() 671QPtrList<Event> CalendarLocal::rawEvents()
672{ 672{
673 return mEventList; 673 return mEventList;
674} 674}
675 675
676bool CalendarLocal::addJournal(Journal *journal) 676bool CalendarLocal::addJournal(Journal *journal)
677{ 677{
678 if ( journal->dtStart().isValid()) 678 if ( journal->dtStart().isValid())
679 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl; 679 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
680 else 680 else
681 kdDebug(5800) << "Adding Journal without a DTSTART" << endl; 681 kdDebug(5800) << "Adding Journal without a DTSTART" << endl;
682 682
683 mJournalList.append(journal); 683 mJournalList.append(journal);
684 684
685 journal->registerObserver( this ); 685 journal->registerObserver( this );
686 686
687 setModified( true ); 687 setModified( true );
688 688
689 return true; 689 return true;
690} 690}
691 691
692void CalendarLocal::deleteJournal( Journal *journal ) 692void CalendarLocal::deleteJournal( Journal *journal )
693{ 693{
694 if ( mUndoIncidence ) delete mUndoIncidence; 694 if ( mUndoIncidence ) delete mUndoIncidence;
695 mUndoIncidence = journal->clone(); 695 mUndoIncidence = journal->clone();
696 mUndoIncidence->setSummary( mUndoIncidence->description().left(25)); 696 mUndoIncidence->setSummary( mUndoIncidence->description().left(25));
697 if ( mJournalList.removeRef(journal) ) { 697 if ( mJournalList.removeRef(journal) ) {
698 setModified( true ); 698 setModified( true );
699 } 699 }
700} 700}
701 701
702Journal *CalendarLocal::journal( const QDate &date ) 702Journal *CalendarLocal::journal( const QDate &date )
703{ 703{
704// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl; 704// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl;
705 705
706 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 706 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
707 if ( it->dtStart().date() == date ) 707 if ( it->dtStart().date() == date )
708 return it; 708 return it;
709 709
710 return 0; 710 return 0;
711} 711}
712 712
713Journal *CalendarLocal::journal( const QString &uid ) 713Journal *CalendarLocal::journal( const QString &uid )
714{ 714{
715 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 715 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
716 if ( it->uid() == uid ) 716 if ( it->uid() == uid )
717 return it; 717 return it;
718 718
719 return 0; 719 return 0;
720} 720}
721 721
722QPtrList<Journal> CalendarLocal::journals() 722QPtrList<Journal> CalendarLocal::journals()
723{ 723{
724 return mJournalList; 724 return mJournalList;
725} 725}
726 726