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