summaryrefslogtreecommitdiffabout
path: root/libkcal/calendarlocal.cpp
Unidiff
Diffstat (limited to 'libkcal/calendarlocal.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendarlocal.cpp26
1 files changed, 25 insertions, 1 deletions
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index 1a1c6be..0ddfeca 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -1,981 +1,1005 @@
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 registerObserver( 0 ); 68 registerObserver( 0 );
69 if ( mDeleteIncidencesOnClose ) 69 if ( mDeleteIncidencesOnClose )
70 close(); 70 close();
71} 71}
72bool CalendarLocal::mergeCalendarFile( QString name ) 72bool CalendarLocal::mergeCalendarFile( QString name )
73{ 73{
74 CalendarLocal calendar( timeZoneId() ); 74 CalendarLocal calendar( timeZoneId() );
75 calendar.setDefaultCalendar( 1 ); 75 calendar.setDefaultCalendar( 1 );
76 if ( calendar.load( name ) ) { 76 if ( calendar.load( name ) ) {
77 mergeCalendar( &calendar ); 77 mergeCalendar( &calendar );
78 return true; 78 return true;
79 } 79 }
80 return false; 80 return false;
81} 81}
82 82
83Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates) 83Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates)
84{ 84{
85 Todo *todo;; 85 Todo *todo;;
86 Incidence *retVal = 0; 86 Incidence *retVal = 0;
87 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 87 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
88 if ( todo->uid() == uid ) { 88 if ( todo->uid() == uid ) {
89 if ( doNotCheckDuplicates ) return todo; 89 if ( doNotCheckDuplicates ) return todo;
90 if ( retVal ) { 90 if ( retVal ) {
91 if ( retVal->calID() > todo->calID() ) { 91 if ( retVal->calID() > todo->calID() ) {
92 retVal = todo; 92 retVal = todo;
93 } 93 }
94 } else { 94 } else {
95 retVal = todo; 95 retVal = todo;
96 } 96 }
97 } 97 }
98 } 98 }
99 if ( retVal ) return retVal; 99 if ( retVal ) return retVal;
100 Event *event; 100 Event *event;
101 for ( event = mEventList.first(); event; event = mEventList.next() ) { 101 for ( event = mEventList.first(); event; event = mEventList.next() ) {
102 if ( event->uid() == uid ) { 102 if ( event->uid() == uid ) {
103 if ( doNotCheckDuplicates ) return event; 103 if ( doNotCheckDuplicates ) return event;
104 if ( retVal ) { 104 if ( retVal ) {
105 if ( retVal->calID() > event->calID() ) { 105 if ( retVal->calID() > event->calID() ) {
106 retVal = event; 106 retVal = event;
107 } 107 }
108 } else { 108 } else {
109 retVal = event; 109 retVal = event;
110 } 110 }
111 } 111 }
112 } 112 }
113 if ( retVal ) return retVal; 113 if ( retVal ) return retVal;
114 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 114 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
115 if ( it->uid() == uid ) { 115 if ( it->uid() == uid ) {
116 if ( doNotCheckDuplicates ) return it; 116 if ( doNotCheckDuplicates ) return it;
117 if ( retVal ) { 117 if ( retVal ) {
118 if ( retVal->calID() > it->calID() ) { 118 if ( retVal->calID() > it->calID() ) {
119 retVal = it; 119 retVal = it;
120 } 120 }
121 } else { 121 } else {
122 retVal = it; 122 retVal = it;
123 } 123 }
124 } 124 }
125 return retVal; 125 return retVal;
126} 126}
127 127
128bool CalendarLocal::mergeCalendar( Calendar* remote ) 128bool CalendarLocal::mergeCalendar( Calendar* remote )
129{ 129{
130 // 1 look for raw inc in local 130 // 1 look for raw inc in local
131 // if inc not in remote, delete in local 131 // if inc not in remote, delete in local
132 // 2 look for raw inc in remote 132 // 2 look for raw inc in remote
133 // if inc in local, replace it 133 // if inc in local, replace it
134 // if not in local, add it to default calendar 134 // if not in local, add it to default calendar
135 QPtrList<Incidence> localInc = rawIncidences(); 135 QPtrList<Incidence> localInc = rawIncidences();
136 Incidence* inL = localInc.first(); 136 Incidence* inL = localInc.first();
137 while ( inL ) { 137 while ( inL ) {
138 if ( ! inL->isReadOnly () ) 138 if ( ! inL->isReadOnly () )
139 if ( !remote->incidenceForUid( inL->uid(), true )) 139 if ( !remote->incidenceForUid( inL->uid(), true ))
140 deleteIncidence( inL ); 140 deleteIncidence( inL );
141 inL = localInc.next(); 141 inL = localInc.next();
142 } 142 }
143 QPtrList<Incidence> er = remote->rawIncidences(); 143 QPtrList<Incidence> er = remote->rawIncidences();
144 Incidence* inR = er.first(); 144 Incidence* inR = er.first();
145 while ( inR ) { 145 while ( inR ) {
146 inL = incidenceForUid( inR->uid(),false ); 146 inL = incidenceForUid( inR->uid(),false );
147 if ( inL ) { 147 if ( inL ) {
148 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) { 148 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) {
149 int calID = inL->calID(); 149 int calID = inL->calID();
150 deleteIncidence( inL ); 150 deleteIncidence( inL );
151 inL = inR->clone(); 151 inL = inR->clone();
152 inL->setCalID( calID ); 152 inL->setCalID( calID );
153 addIncidence( inL ); 153 addIncidence( inL );
154 } 154 }
155 } else { 155 } else {
156 inL = inR->clone(); 156 inL = inR->clone();
157 inL->setCalID( 0 );// add to default cal 157 inL->setCalID( 0 );// add to default cal
158 addIncidence( inL ); 158 addIncidence( inL );
159 } 159 }
160 inR = er.next(); 160 inR = er.next();
161 } 161 }
162 return true; 162 return true;
163} 163}
164 164
165 165
166bool CalendarLocal::addCalendarFile( QString name, int id ) 166bool CalendarLocal::addCalendarFile( QString name, int id )
167{ 167{
168 CalendarLocal calendar( timeZoneId() ); 168 CalendarLocal calendar( timeZoneId() );
169 calendar.setDefaultCalendar( id ); 169 calendar.setDefaultCalendar( id );
170 if ( calendar.load( name ) ) { 170 if ( calendar.load( name ) ) {
171 addCalendar( &calendar ); 171 addCalendar( &calendar );
172 return true; 172 return true;
173 } 173 }
174 return false; 174 return false;
175} 175}
176void CalendarLocal::setSyncEventsReadOnly() 176void CalendarLocal::setSyncEventsReadOnly()
177{ 177{
178 Event * ev; 178 Event * ev;
179 ev = mEventList.first(); 179 ev = mEventList.first();
180 while ( ev ) { 180 while ( ev ) {
181 if ( ev->uid().left(15) == QString("last-syncEvent-") ) 181 if ( ev->uid().left(15) == QString("last-syncEvent-") )
182 ev->setReadOnly( true ); 182 ev->setReadOnly( true );
183 ev = mEventList.next(); 183 ev = mEventList.next();
184 } 184 }
185} 185}
186void CalendarLocal::addCalendar( Calendar* cal ) 186void CalendarLocal::addCalendar( Calendar* cal )
187{ 187{
188 cal->setDontDeleteIncidencesOnClose(); 188 cal->setDontDeleteIncidencesOnClose();
189 { 189 {
190 QPtrList<Event> EventList = cal->rawEvents(); 190 QPtrList<Event> EventList = cal->rawEvents();
191 Event * ev = EventList.first(); 191 Event * ev = EventList.first();
192 while ( ev ) { 192 while ( ev ) {
193 ev->unRegisterObserver( cal ); 193 ev->unRegisterObserver( cal );
194 ev->registerObserver( this ); 194 ev->registerObserver( this );
195 mEventList.append( ev ); 195 mEventList.append( ev );
196 ev = EventList.next(); 196 ev = EventList.next();
197 } 197 }
198 } 198 }
199 { 199 {
200 200
201 QPtrList<Todo> TodoList = cal->rawTodos(); 201 QPtrList<Todo> TodoList = cal->rawTodos();
202 Todo * ev = TodoList.first(); 202 Todo * ev = TodoList.first();
203 while ( ev ) { 203 while ( ev ) {
204 QString rel = ev->relatedToUid(); 204 QString rel = ev->relatedToUid();
205 if ( !rel.isEmpty() ){ 205 if ( !rel.isEmpty() ){
206 ev->setRelatedTo ( 0 ); 206 ev->setRelatedTo ( 0 );
207 ev->setRelatedToUid( rel ); 207 ev->setRelatedToUid( rel );
208 } 208 }
209 ev = TodoList.next(); 209 ev = TodoList.next();
210 } 210 }
211 //TodoList = cal->rawTodos(); 211 //TodoList = cal->rawTodos();
212 ev = TodoList.first(); 212 ev = TodoList.first();
213 while ( ev ) { 213 while ( ev ) {
214 ev->unRegisterObserver( cal ); 214 ev->unRegisterObserver( cal );
215 ev->registerObserver( this ); 215 ev->registerObserver( this );
216 mTodoList.append( ev ); 216 mTodoList.append( ev );
217 setupRelations( ev ); 217 setupRelations( ev );
218 ev = TodoList.next(); 218 ev = TodoList.next();
219 } 219 }
220 } 220 }
221 { 221 {
222 QPtrList<Journal> JournalList = cal->journals(); 222 QPtrList<Journal> JournalList = cal->journals();
223 Journal * ev = JournalList.first(); 223 Journal * ev = JournalList.first();
224 while ( ev ) { 224 while ( ev ) {
225 ev->unRegisterObserver( cal ); 225 ev->unRegisterObserver( cal );
226 ev->registerObserver( this ); 226 ev->registerObserver( this );
227 mJournalList.append( ev ); 227 mJournalList.append( ev );
228 ev = JournalList.next(); 228 ev = JournalList.next();
229 } 229 }
230 } 230 }
231 setModified( true ); 231 setModified( true );
232} 232}
233bool CalendarLocal::load( const QString &fileName ) 233bool CalendarLocal::load( const QString &fileName )
234{ 234{
235 FileStorage storage( this, fileName ); 235 FileStorage storage( this, fileName );
236 return storage.load(); 236 return storage.load();
237} 237}
238 238
239bool CalendarLocal::save( const QString &fileName, CalFormat *format ) 239bool CalendarLocal::save( const QString &fileName, CalFormat *format )
240{ 240{
241 FileStorage storage( this, fileName, format ); 241 FileStorage storage( this, fileName, format );
242 return storage.save(); 242 return storage.save();
243} 243}
244 244
245void CalendarLocal::stopAllTodos() 245void CalendarLocal::stopAllTodos()
246{ 246{
247 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 247 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
248 it->setRunning( false ); 248 it->setRunning( false );
249 249
250} 250}
251void CalendarLocal::close() 251void CalendarLocal::close()
252{ 252{
253 253
254 Todo * i; 254 Todo * i;
255 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false); 255 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false);
256 256
257 mEventList.setAutoDelete( true ); 257 mEventList.setAutoDelete( true );
258 mTodoList.setAutoDelete( true ); 258 mTodoList.setAutoDelete( true );
259 mJournalList.setAutoDelete( true ); 259 mJournalList.setAutoDelete( true );
260 260
261 mEventList.clear(); 261 mEventList.clear();
262 mTodoList.clear(); 262 mTodoList.clear();
263 mJournalList.clear(); 263 mJournalList.clear();
264 264
265 mEventList.setAutoDelete( false ); 265 mEventList.setAutoDelete( false );
266 mTodoList.setAutoDelete( false ); 266 mTodoList.setAutoDelete( false );
267 mJournalList.setAutoDelete( false ); 267 mJournalList.setAutoDelete( false );
268 268
269 setModified( false ); 269 setModified( false );
270} 270}
271 271
272bool CalendarLocal::addAnniversaryNoDup( Event *event ) 272bool CalendarLocal::addAnniversaryNoDup( Event *event )
273{ 273{
274 QString cat; 274 QString cat;
275 bool isBirthday = true; 275 bool isBirthday = true;
276 if( event->categoriesStr() == i18n( "Anniversary" ) ) { 276 if( event->categoriesStr() == i18n( "Anniversary" ) ) {
277 isBirthday = false; 277 isBirthday = false;
278 cat = i18n( "Anniversary" ); 278 cat = i18n( "Anniversary" );
279 } else if( event->categoriesStr() == i18n( "Birthday" ) ) { 279 } else if( event->categoriesStr() == i18n( "Birthday" ) ) {
280 isBirthday = true; 280 isBirthday = true;
281 cat = i18n( "Birthday" ); 281 cat = i18n( "Birthday" );
282 } else { 282 } else {
283 qDebug("addAnniversaryNoDup called without fitting category! "); 283 qDebug("addAnniversaryNoDup called without fitting category! ");
284 return false; 284 return false;
285 } 285 }
286 Event * eve; 286 Event * eve;
287 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 287 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
288 if ( !(eve->categories().contains( cat ) )) 288 if ( !(eve->categories().contains( cat ) ))
289 continue; 289 continue;
290 // now we have an event with fitting category 290 // now we have an event with fitting category
291 if ( eve->dtStart().date() != event->dtStart().date() ) 291 if ( eve->dtStart().date() != event->dtStart().date() )
292 continue; 292 continue;
293 // now we have an event with fitting category+date 293 // now we have an event with fitting category+date
294 if ( eve->summary() != event->summary() ) 294 if ( eve->summary() != event->summary() )
295 continue; 295 continue;
296 // now we have an event with fitting category+date+summary 296 // now we have an event with fitting category+date+summary
297 return false; 297 return false;
298 } 298 }
299 return addEvent( event ); 299 return addEvent( event );
300 300
301} 301}
302bool CalendarLocal::addEventNoDup( Event *event ) 302bool CalendarLocal::addEventNoDup( Event *event )
303{ 303{
304 Event * eve; 304 Event * eve;
305 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 305 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
306 if ( *eve == *event ) { 306 if ( *eve == *event ) {
307 //qDebug("CalendarLocal::Duplicate event found! Not inserted! "); 307 //qDebug("CalendarLocal::Duplicate event found! Not inserted! ");
308 return false; 308 return false;
309 } 309 }
310 } 310 }
311 return addEvent( event ); 311 return addEvent( event );
312} 312}
313 313
314bool CalendarLocal::addEvent( Event *event ) 314bool CalendarLocal::addEvent( Event *event )
315{ 315{
316 insertEvent( event ); 316 insertEvent( event );
317 317
318 event->registerObserver( this ); 318 event->registerObserver( this );
319 319
320 setModified( true ); 320 setModified( true );
321 if ( event->calID() == 0 ) 321 if ( event->calID() == 0 )
322 event->setCalID( mDefaultCalendar ); 322 event->setCalID( mDefaultCalendar );
323 event->setCalEnabled( true ); 323 event->setCalEnabled( true );
324 324
325 return true; 325 return true;
326} 326}
327 327
328void CalendarLocal::deleteEvent( Event *event ) 328void CalendarLocal::deleteEvent( Event *event )
329{ 329{
330 clearUndo(event); 330 clearUndo(event);
331 if ( mEventList.removeRef( event ) ) { 331 if ( mEventList.removeRef( event ) ) {
332 setModified( true ); 332 setModified( true );
333 } 333 }
334} 334}
335 335
336 336
337Event *CalendarLocal::event( const QString &uid ) 337Event *CalendarLocal::event( const QString &uid )
338{ 338{
339 Event *event; 339 Event *event;
340 Event *retVal = 0; 340 Event *retVal = 0;
341 for ( event = mEventList.first(); event; event = mEventList.next() ) { 341 for ( event = mEventList.first(); event; event = mEventList.next() ) {
342 if ( event->calEnabled() && event->uid() == uid ) { 342 if ( event->calEnabled() && event->uid() == uid ) {
343 if ( retVal ) { 343 if ( retVal ) {
344 if ( retVal->calID() > event->calID() ) { 344 if ( retVal->calID() > event->calID() ) {
345 retVal = event; 345 retVal = event;
346 } 346 }
347 } else { 347 } else {
348 retVal = event; 348 retVal = event;
349 } 349 }
350 } 350 }
351 } 351 }
352 return retVal; 352 return retVal;
353} 353}
354bool CalendarLocal::addTodoNoDup( Todo *todo ) 354bool CalendarLocal::addTodoNoDup( Todo *todo )
355{ 355{
356 Todo * eve; 356 Todo * eve;
357 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) { 357 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) {
358 if ( *eve == *todo ) { 358 if ( *eve == *todo ) {
359 //qDebug("duplicate todo found! not inserted! "); 359 //qDebug("duplicate todo found! not inserted! ");
360 return false; 360 return false;
361 } 361 }
362 } 362 }
363 return addTodo( todo ); 363 return addTodo( todo );
364} 364}
365bool CalendarLocal::addTodo( Todo *todo ) 365bool CalendarLocal::addTodo( Todo *todo )
366{ 366{
367 mTodoList.append( todo ); 367 mTodoList.append( todo );
368 368
369 todo->registerObserver( this ); 369 todo->registerObserver( this );
370 370
371 // Set up subtask relations 371 // Set up subtask relations
372 setupRelations( todo ); 372 setupRelations( todo );
373 373
374 setModified( true ); 374 setModified( true );
375 if ( todo->calID() == 0 ) 375 if ( todo->calID() == 0 )
376 todo->setCalID( mDefaultCalendar ); 376 todo->setCalID( mDefaultCalendar );
377 todo->setCalEnabled( true ); 377 todo->setCalEnabled( true );
378 return true; 378 return true;
379} 379}
380 380
381void CalendarLocal::deleteTodo( Todo *todo ) 381void CalendarLocal::deleteTodo( Todo *todo )
382{ 382{
383 // Handle orphaned children 383 // Handle orphaned children
384 removeRelations( todo ); 384 removeRelations( todo );
385 clearUndo(todo); 385 clearUndo(todo);
386 386
387 if ( mTodoList.removeRef( todo ) ) { 387 if ( mTodoList.removeRef( todo ) ) {
388 setModified( true ); 388 setModified( true );
389 } 389 }
390} 390}
391 391
392QPtrList<Todo> CalendarLocal::rawTodos() 392QPtrList<Todo> CalendarLocal::rawTodos()
393{ 393{
394 QPtrList<Todo> el; 394 QPtrList<Todo> el;
395 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 395 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
396 if ( it->calEnabled() ) el.append( it ); 396 if ( it->calEnabled() ) el.append( it );
397 return el; 397 return el;
398} 398}
399Todo *CalendarLocal::todo( QString syncProf, QString id ) 399Todo *CalendarLocal::todo( QString syncProf, QString id )
400{ 400{
401 Todo *todo; 401 Todo *todo;
402 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 402 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
403 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 403 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
404 } 404 }
405 405
406 return 0; 406 return 0;
407} 407}
408void CalendarLocal::removeSyncInfo( QString syncProfile) 408void CalendarLocal::removeSyncInfo( QString syncProfile)
409{ 409{
410 QPtrList<Incidence> all = rawIncidences() ; 410 QPtrList<Incidence> all = rawIncidences() ;
411 Incidence *inc; 411 Incidence *inc;
412 for ( inc = all.first(); inc; inc = all.next() ) { 412 for ( inc = all.first(); inc; inc = all.next() ) {
413 inc->removeID( syncProfile ); 413 inc->removeID( syncProfile );
414 } 414 }
415 if ( syncProfile.isEmpty() ) { 415 if ( syncProfile.isEmpty() ) {
416 QPtrList<Event> el; 416 QPtrList<Event> el;
417 Event *todo; 417 Event *todo;
418 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 418 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
419 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 419 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
420 el.append( todo ); 420 el.append( todo );
421 } 421 }
422 for ( todo = el.first(); todo; todo = el.next() ) { 422 for ( todo = el.first(); todo; todo = el.next() ) {
423 deleteIncidence ( todo ); 423 deleteIncidence ( todo );
424 } 424 }
425 } else { 425 } else {
426 Event *lse = event( "last-syncEvent-"+ syncProfile); 426 Event *lse = event( "last-syncEvent-"+ syncProfile);
427 if ( lse ) 427 if ( lse )
428 deleteIncidence ( lse ); 428 deleteIncidence ( lse );
429 } 429 }
430} 430}
431QPtrList<Event> CalendarLocal::getExternLastSyncEvents() 431QPtrList<Event> CalendarLocal::getExternLastSyncEvents()
432{ 432{
433 QPtrList<Event> el; 433 QPtrList<Event> el;
434 Event *todo; 434 Event *todo;
435 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 435 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
436 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 436 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
437 if ( todo->summary().left(3) == "E: " ) 437 if ( todo->summary().left(3) == "E: " )
438 el.append( todo ); 438 el.append( todo );
439 } 439 }
440 440
441 return el; 441 return el;
442 442
443} 443}
444Event *CalendarLocal::event( QString syncProf, QString id ) 444Event *CalendarLocal::event( QString syncProf, QString id )
445{ 445{
446 Event *todo; 446 Event *todo;
447 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 447 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
448 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 448 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
449 } 449 }
450 450
451 return 0; 451 return 0;
452} 452}
453Todo *CalendarLocal::todo( const QString &uid ) 453Todo *CalendarLocal::todo( const QString &uid )
454{ 454{
455 Todo *todo;; 455 Todo *todo;
456 Todo *retVal = 0; 456 Todo *retVal = 0;
457 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 457 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
458 if ( todo->calEnabled() && todo->uid() == uid ) { 458 if ( todo->calEnabled() && todo->uid() == uid ) {
459 if ( retVal ) { 459 if ( retVal ) {
460 if ( retVal->calID() > todo->calID() ) { 460 if ( retVal->calID() > todo->calID() ) {
461 retVal = todo; 461 retVal = todo;
462 } 462 }
463 } else { 463 } else {
464 retVal = todo; 464 retVal = todo;
465 } 465 }
466 } 466 }
467 } 467 }
468 return retVal; 468 return retVal;
469} 469}
470void CalendarLocal::getIncidenceCount( int calId, int& events, int & todos, int & journals)
471{
472 events = 0;
473 todos = 0;
474 journals = 0;
475 {
476 Todo *todo;
477 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
478 if ( todo->calID() == calId )
479 ++todos;
480 }
481 }
482 {
483 Event *todo;
484 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
485 if ( todo->calID() == calId )
486 ++events;
487
488 }
489 }
490 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
491 if ( it->calID() == calId ) ++journals;
492
493}
470QString CalendarLocal::nextSummary() const 494QString CalendarLocal::nextSummary() const
471{ 495{
472 return mNextSummary; 496 return mNextSummary;
473} 497}
474QDateTime CalendarLocal::nextAlarmEventDateTime() const 498QDateTime CalendarLocal::nextAlarmEventDateTime() const
475{ 499{
476 return mNextAlarmEventDateTime; 500 return mNextAlarmEventDateTime;
477} 501}
478void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted) 502void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted)
479{ 503{
480 //mNextAlarmIncidence 504 //mNextAlarmIncidence
481 //mNextAlarmDateTime 505 //mNextAlarmDateTime
482 //return mNextSummary; 506 //return mNextSummary;
483 //return mNextAlarmEventDateTime; 507 //return mNextAlarmEventDateTime;
484 bool newNextAlarm = false; 508 bool newNextAlarm = false;
485 bool computeNextAlarm = false; 509 bool computeNextAlarm = false;
486 bool ok; 510 bool ok;
487 int offset; 511 int offset;
488 QDateTime nextA; 512 QDateTime nextA;
489 // QString nextSum; 513 // QString nextSum;
490 //QDateTime nextEvent; 514 //QDateTime nextEvent;
491 if ( mNextAlarmIncidence == 0 || incidence == 0 ) { 515 if ( mNextAlarmIncidence == 0 || incidence == 0 ) {
492 computeNextAlarm = true; 516 computeNextAlarm = true;
493 } else { 517 } else {
494 if ( ! deleted ) { 518 if ( ! deleted ) {
495 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 519 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
496 if ( ok ) { 520 if ( ok ) {
497 if ( nextA < mNextAlarmDateTime ) { 521 if ( nextA < mNextAlarmDateTime ) {
498 deRegisterAlarm(); 522 deRegisterAlarm();
499 mNextAlarmDateTime = nextA; 523 mNextAlarmDateTime = nextA;
500 mNextSummary = incidence->summary(); 524 mNextSummary = incidence->summary();
501 mNextAlarmEventDateTime = nextA.addSecs(offset ) ; 525 mNextAlarmEventDateTime = nextA.addSecs(offset ) ;
502 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 526 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
503 newNextAlarm = true; 527 newNextAlarm = true;
504 mNextAlarmIncidence = incidence; 528 mNextAlarmIncidence = incidence;
505 } else { 529 } else {
506 if ( incidence == mNextAlarmIncidence ) { 530 if ( incidence == mNextAlarmIncidence ) {
507 computeNextAlarm = true; 531 computeNextAlarm = true;
508 } 532 }
509 } 533 }
510 } else { 534 } else {
511 if ( mNextAlarmIncidence == incidence ) { 535 if ( mNextAlarmIncidence == incidence ) {
512 computeNextAlarm = true; 536 computeNextAlarm = true;
513 } 537 }
514 } 538 }
515 } else { // deleted 539 } else { // deleted
516 if ( incidence == mNextAlarmIncidence ) { 540 if ( incidence == mNextAlarmIncidence ) {
517 computeNextAlarm = true; 541 computeNextAlarm = true;
518 } 542 }
519 } 543 }
520 } 544 }
521 if ( computeNextAlarm ) { 545 if ( computeNextAlarm ) {
522 deRegisterAlarm(); 546 deRegisterAlarm();
523 nextA = nextAlarm( 1000 ); 547 nextA = nextAlarm( 1000 );
524 if (! mNextAlarmIncidence ) { 548 if (! mNextAlarmIncidence ) {
525 return; 549 return;
526 } 550 }
527 newNextAlarm = true; 551 newNextAlarm = true;
528 } 552 }
529 if ( newNextAlarm ) 553 if ( newNextAlarm )
530 registerAlarm(); 554 registerAlarm();
531} 555}
532QString CalendarLocal:: getAlarmNotification() 556QString CalendarLocal:: getAlarmNotification()
533{ 557{
534 QString ret; 558 QString ret;
535 // this should not happen 559 // this should not happen
536 if (! mNextAlarmIncidence ) 560 if (! mNextAlarmIncidence )
537 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString; 561 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString;
538 Alarm* alarm = mNextAlarmIncidence->alarms().first(); 562 Alarm* alarm = mNextAlarmIncidence->alarms().first();
539 if ( alarm->type() == Alarm::Procedure ) { 563 if ( alarm->type() == Alarm::Procedure ) {
540 ret = "proc_alarm" + alarm->programFile()+"+++"; 564 ret = "proc_alarm" + alarm->programFile()+"+++";
541 } else { 565 } else {
542 ret = "audio_alarm" +alarm->audioFile() +"+++"; 566 ret = "audio_alarm" +alarm->audioFile() +"+++";
543 } 567 }
544 ret += "cal_alarm"+ mNextSummary.left( 25 ); 568 ret += "cal_alarm"+ mNextSummary.left( 25 );
545 if ( mNextSummary.length() > 25 ) 569 if ( mNextSummary.length() > 25 )
546 ret += "\n" + mNextSummary.mid(25, 25 ); 570 ret += "\n" + mNextSummary.mid(25, 25 );
547 ret+= "\n"+mNextAlarmEventDateTimeString; 571 ret+= "\n"+mNextAlarmEventDateTimeString;
548 return ret; 572 return ret;
549} 573}
550void CalendarLocal::registerAlarm() 574void CalendarLocal::registerAlarm()
551{ 575{
552 mLastAlarmNotificationString = getAlarmNotification(); 576 mLastAlarmNotificationString = getAlarmNotification();
553 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() ); 577 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() );
554 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 578 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
555// #ifndef DESKTOP_VERSION 579// #ifndef DESKTOP_VERSION
556// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() ); 580// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() );
557// #endif 581// #endif
558} 582}
559void CalendarLocal::deRegisterAlarm() 583void CalendarLocal::deRegisterAlarm()
560{ 584{
561 if ( mLastAlarmNotificationString.isNull() ) 585 if ( mLastAlarmNotificationString.isNull() )
562 return; 586 return;
563 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() ); 587 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() );
564 588
565 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 589 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
566 mNextAlarmEventDateTime = QDateTime(); 590 mNextAlarmEventDateTime = QDateTime();
567// #ifndef DESKTOP_VERSION 591// #ifndef DESKTOP_VERSION
568// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() ); 592// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() );
569// #endif 593// #endif
570} 594}
571 595
572QPtrList<Todo> CalendarLocal::todos( const QDate &date ) 596QPtrList<Todo> CalendarLocal::todos( const QDate &date )
573{ 597{
574 QPtrList<Todo> todos; 598 QPtrList<Todo> todos;
575 599
576 Todo *todo; 600 Todo *todo;
577 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 601 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
578 if ( !todo->calEnabled() ) continue; 602 if ( !todo->calEnabled() ) continue;
579 if ( todo->hasDueDate() && todo->dtDue().date() == date ) { 603 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
580 todos.append( todo ); 604 todos.append( todo );
581 } 605 }
582 } 606 }
583 607
584 filter()->apply( &todos ); 608 filter()->apply( &todos );
585 return todos; 609 return todos;
586} 610}
587void CalendarLocal::reInitAlarmSettings() 611void CalendarLocal::reInitAlarmSettings()
588{ 612{
589 if ( !mNextAlarmIncidence ) { 613 if ( !mNextAlarmIncidence ) {
590 nextAlarm( 1000 ); 614 nextAlarm( 1000 );
591 } 615 }
592 deRegisterAlarm(); 616 deRegisterAlarm();
593 mNextAlarmIncidence = 0; 617 mNextAlarmIncidence = 0;
594 checkAlarmForIncidence( 0, false ); 618 checkAlarmForIncidence( 0, false );
595 619
596} 620}
597 621
598 622
599 623
600QDateTime CalendarLocal::nextAlarm( int daysTo ) 624QDateTime CalendarLocal::nextAlarm( int daysTo )
601{ 625{
602 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo ); 626 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo );
603 QDateTime start = QDateTime::currentDateTime().addSecs( 30 ); 627 QDateTime start = QDateTime::currentDateTime().addSecs( 30 );
604 QDateTime next; 628 QDateTime next;
605 Event *e; 629 Event *e;
606 bool ok; 630 bool ok;
607 bool found = false; 631 bool found = false;
608 int offset; 632 int offset;
609 mNextAlarmIncidence = 0; 633 mNextAlarmIncidence = 0;
610 for( e = mEventList.first(); e; e = mEventList.next() ) { 634 for( e = mEventList.first(); e; e = mEventList.next() ) {
611 if ( !e->calEnabled() ) continue; 635 if ( !e->calEnabled() ) continue;
612 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 636 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
613 if ( ok ) { 637 if ( ok ) {
614 if ( next < nextA ) { 638 if ( next < nextA ) {
615 nextA = next; 639 nextA = next;
616 found = true; 640 found = true;
617 mNextSummary = e->summary(); 641 mNextSummary = e->summary();
618 mNextAlarmEventDateTime = next.addSecs(offset ) ; 642 mNextAlarmEventDateTime = next.addSecs(offset ) ;
619 mNextAlarmIncidence = (Incidence *) e; 643 mNextAlarmIncidence = (Incidence *) e;
620 } 644 }
621 } 645 }
622 } 646 }
623 Todo *t; 647 Todo *t;
624 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 648 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
625 if ( !t->calEnabled() ) continue; 649 if ( !t->calEnabled() ) continue;
626 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 650 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
627 if ( ok ) { 651 if ( ok ) {
628 if ( next < nextA ) { 652 if ( next < nextA ) {
629 nextA = next; 653 nextA = next;
630 found = true; 654 found = true;
631 mNextSummary = t->summary(); 655 mNextSummary = t->summary();
632 mNextAlarmEventDateTime = next.addSecs(offset ); 656 mNextAlarmEventDateTime = next.addSecs(offset );
633 mNextAlarmIncidence = (Incidence *) t; 657 mNextAlarmIncidence = (Incidence *) t;
634 } 658 }
635 } 659 }
636 } 660 }
637 if ( mNextAlarmIncidence ) { 661 if ( mNextAlarmIncidence ) {
638 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 662 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
639 mNextAlarmDateTime = nextA; 663 mNextAlarmDateTime = nextA;
640 } 664 }
641 return nextA; 665 return nextA;
642} 666}
643Alarm::List CalendarLocal::alarmsTo( const QDateTime &to ) 667Alarm::List CalendarLocal::alarmsTo( const QDateTime &to )
644{ 668{
645 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to ); 669 return alarms( QDateTime( QDate( 1900, 1, 1 ) ), to );
646} 670}
647 671
648Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to ) 672Alarm::List CalendarLocal::alarms( const QDateTime &from, const QDateTime &to )
649{ 673{
650 674
651 Alarm::List alarms; 675 Alarm::List alarms;
652 676
653 Event *e; 677 Event *e;
654 678
655 for( e = mEventList.first(); e; e = mEventList.next() ) { 679 for( e = mEventList.first(); e; e = mEventList.next() ) {
656 if ( !e->calEnabled() ) continue; 680 if ( !e->calEnabled() ) continue;
657 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to ); 681 if ( e->doesRecur() ) appendRecurringAlarms( alarms, e, from, to );
658 else appendAlarms( alarms, e, from, to ); 682 else appendAlarms( alarms, e, from, to );
659 } 683 }
660 684
661 Todo *t; 685 Todo *t;
662 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 686 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
663 if ( !t->calEnabled() ) continue; 687 if ( !t->calEnabled() ) continue;
664 appendAlarms( alarms, t, from, to ); 688 appendAlarms( alarms, t, from, to );
665 } 689 }
666 690
667 return alarms; 691 return alarms;
668} 692}
669 693
670void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence, 694void CalendarLocal::appendAlarms( Alarm::List &alarms, Incidence *incidence,
671 const QDateTime &from, const QDateTime &to ) 695 const QDateTime &from, const QDateTime &to )
672{ 696{
673 QPtrList<Alarm> alarmList = incidence->alarms(); 697 QPtrList<Alarm> alarmList = incidence->alarms();
674 Alarm *alarm; 698 Alarm *alarm;
675 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 699 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
676// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text() 700// kdDebug(5800) << "CalendarLocal::appendAlarms() '" << alarm->text()
677// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl; 701// << "': " << alarm->time().toString() << " - " << alarm->enabled() << endl;
678 if ( alarm->enabled() ) { 702 if ( alarm->enabled() ) {
679 if ( alarm->time() >= from && alarm->time() <= to ) { 703 if ( alarm->time() >= from && alarm->time() <= to ) {
680 alarms.append( alarm ); 704 alarms.append( alarm );
681 } 705 }
682 } 706 }
683 } 707 }
684} 708}
685 709
686void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms, 710void CalendarLocal::appendRecurringAlarms( Alarm::List &alarms,
687 Incidence *incidence, 711 Incidence *incidence,
688 const QDateTime &from, 712 const QDateTime &from,
689 const QDateTime &to ) 713 const QDateTime &to )
690{ 714{
691 715
692 QPtrList<Alarm> alarmList = incidence->alarms(); 716 QPtrList<Alarm> alarmList = incidence->alarms();
693 Alarm *alarm; 717 Alarm *alarm;
694 QDateTime qdt; 718 QDateTime qdt;
695 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) { 719 for( alarm = alarmList.first(); alarm; alarm = alarmList.next() ) {
696 if (incidence->recursOn(from.date())) { 720 if (incidence->recursOn(from.date())) {
697 qdt.setTime(alarm->time().time()); 721 qdt.setTime(alarm->time().time());
698 qdt.setDate(from.date()); 722 qdt.setDate(from.date());
699 } 723 }
700 else qdt = alarm->time(); 724 else qdt = alarm->time();
701 // qDebug("1 %s %s %s", qdt.toString().latin1(), from.toString().latin1(), to.toString().latin1()); 725 // qDebug("1 %s %s %s", qdt.toString().latin1(), from.toString().latin1(), to.toString().latin1());
702 if ( alarm->enabled() ) { 726 if ( alarm->enabled() ) {
703 if ( qdt >= from && qdt <= to ) { 727 if ( qdt >= from && qdt <= to ) {
704 alarms.append( alarm ); 728 alarms.append( alarm );
705 } 729 }
706 } 730 }
707 } 731 }
708} 732}
709 733
710 734
711/****************************** PROTECTED METHODS ****************************/ 735/****************************** PROTECTED METHODS ****************************/
712 736
713// after changes are made to an event, this should be called. 737// after changes are made to an event, this should be called.
714void CalendarLocal::update( IncidenceBase *incidence ) 738void CalendarLocal::update( IncidenceBase *incidence )
715{ 739{
716 incidence->setSyncStatus( Event::SYNCMOD ); 740 incidence->setSyncStatus( Event::SYNCMOD );
717 incidence->setLastModified( QDateTime::currentDateTime() ); 741 incidence->setLastModified( QDateTime::currentDateTime() );
718 // we should probably update the revision number here, 742 // we should probably update the revision number here,
719 // or internally in the Event itself when certain things change. 743 // or internally in the Event itself when certain things change.
720 // need to verify with ical documentation. 744 // need to verify with ical documentation.
721 745
722 setModified( true ); 746 setModified( true );
723} 747}
724 748
725void CalendarLocal::insertEvent( Event *event ) 749void CalendarLocal::insertEvent( Event *event )
726{ 750{
727 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event ); 751 if ( mEventList.findRef( event ) < 0 ) mEventList.append( event );
728} 752}
729 753
730 754
731QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted ) 755QPtrList<Event> CalendarLocal::rawEventsForDate( const QDate &qd, bool sorted )
732{ 756{
733 QPtrList<Event> eventList; 757 QPtrList<Event> eventList;
734 758
735 Event *event; 759 Event *event;
736 for( event = mEventList.first(); event; event = mEventList.next() ) { 760 for( event = mEventList.first(); event; event = mEventList.next() ) {
737 if ( !event->calEnabled() ) continue; 761 if ( !event->calEnabled() ) continue;
738 if ( event->doesRecur() ) { 762 if ( event->doesRecur() ) {
739 if ( event->isMultiDay() ) { 763 if ( event->isMultiDay() ) {
740 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() ); 764 int extraDays = event->dtStart().date().daysTo( event->dtEnd().date() );
741 int i; 765 int i;
742 for ( i = 0; i <= extraDays; i++ ) { 766 for ( i = 0; i <= extraDays; i++ ) {
743 if ( event->recursOn( qd.addDays( -i ) ) ) { 767 if ( event->recursOn( qd.addDays( -i ) ) ) {
744 eventList.append( event ); 768 eventList.append( event );
745 break; 769 break;
746 } 770 }
747 } 771 }
748 } else { 772 } else {
749 if ( event->recursOn( qd ) ) 773 if ( event->recursOn( qd ) )
750 eventList.append( event ); 774 eventList.append( event );
751 } 775 }
752 } else { 776 } else {
753 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) { 777 if ( event->dtStart().date() <= qd && event->dtEnd().date() >= qd ) {
754 eventList.append( event ); 778 eventList.append( event );
755 } 779 }
756 } 780 }
757 } 781 }
758 782
759 if ( !sorted ) { 783 if ( !sorted ) {
760 return eventList; 784 return eventList;
761 } 785 }
762 786
763 // kdDebug(5800) << "Sorting events for date\n" << endl; 787 // kdDebug(5800) << "Sorting events for date\n" << endl;
764 // now, we have to sort it based on dtStart.time() 788 // now, we have to sort it based on dtStart.time()
765 QPtrList<Event> eventListSorted; 789 QPtrList<Event> eventListSorted;
766 Event *sortEvent; 790 Event *sortEvent;
767 for ( event = eventList.first(); event; event = eventList.next() ) { 791 for ( event = eventList.first(); event; event = eventList.next() ) {
768 sortEvent = eventListSorted.first(); 792 sortEvent = eventListSorted.first();
769 int i = 0; 793 int i = 0;
770 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() ) 794 while ( sortEvent && event->dtStart().time()>=sortEvent->dtStart().time() )
771 { 795 {
772 i++; 796 i++;
773 sortEvent = eventListSorted.next(); 797 sortEvent = eventListSorted.next();
774 } 798 }
775 eventListSorted.insert( i, event ); 799 eventListSorted.insert( i, event );
776 } 800 }
777 return eventListSorted; 801 return eventListSorted;
778} 802}
779 803
780 804
781QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end, 805QPtrList<Event> CalendarLocal::rawEvents( const QDate &start, const QDate &end,
782 bool inclusive ) 806 bool inclusive )
783{ 807{
784 Event *event = 0; 808 Event *event = 0;
785 809
786 QPtrList<Event> eventList; 810 QPtrList<Event> eventList;
787 811
788 // Get non-recurring events 812 // Get non-recurring events
789 for( event = mEventList.first(); event; event = mEventList.next() ) { 813 for( event = mEventList.first(); event; event = mEventList.next() ) {
790 if ( !event->calEnabled() ) continue; 814 if ( !event->calEnabled() ) continue;
791 if ( event->doesRecur() ) { 815 if ( event->doesRecur() ) {
792 QDate rStart = event->dtStart().date(); 816 QDate rStart = event->dtStart().date();
793 bool found = false; 817 bool found = false;
794 if ( inclusive ) { 818 if ( inclusive ) {
795 if ( rStart >= start && rStart <= end ) { 819 if ( rStart >= start && rStart <= end ) {
796 // Start date of event is in range. Now check for end date. 820 // Start date of event is in range. Now check for end date.
797 // if duration is negative, event recurs forever, so do not include it. 821 // if duration is negative, event recurs forever, so do not include it.
798 if ( event->recurrence()->duration() == 0 ) { // End date set 822 if ( event->recurrence()->duration() == 0 ) { // End date set
799 QDate rEnd = event->recurrence()->endDate(); 823 QDate rEnd = event->recurrence()->endDate();
800 if ( rEnd >= start && rEnd <= end ) { // End date within range 824 if ( rEnd >= start && rEnd <= end ) { // End date within range
801 found = true; 825 found = true;
802 } 826 }
803 } else if ( event->recurrence()->duration() > 0 ) { // Duration set 827 } else if ( event->recurrence()->duration() > 0 ) { // Duration set
804 // TODO: Calculate end date from duration. Should be done in Event 828 // TODO: Calculate end date from duration. Should be done in Event
805 // For now exclude all events with a duration. 829 // For now exclude all events with a duration.
806 } 830 }
807 } 831 }
808 } else { 832 } else {
809 bool founOne; 833 bool founOne;
810 QDate next = event->getNextOccurence( start, &founOne ).date(); 834 QDate next = event->getNextOccurence( start, &founOne ).date();
811 if ( founOne ) { 835 if ( founOne ) {
812 if ( next <= end ) { 836 if ( next <= end ) {
813 found = true; 837 found = true;
814 } 838 }
815 } 839 }
816 840
817 /* 841 /*
818 // crap !!! 842 // crap !!!
819 if ( rStart <= end ) { // Start date not after range 843 if ( rStart <= end ) { // Start date not after range
820 if ( rStart >= start ) { // Start date within range 844 if ( rStart >= start ) { // Start date within range
821 found = true; 845 found = true;
822 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever 846 } else if ( event->recurrence()->duration() == -1 ) { // Recurs forever
823 found = true; 847 found = true;
824 } else if ( event->recurrence()->duration() == 0 ) { // End date set 848 } else if ( event->recurrence()->duration() == 0 ) { // End date set
825 QDate rEnd = event->recurrence()->endDate(); 849 QDate rEnd = event->recurrence()->endDate();
826 if ( rEnd >= start && rEnd <= end ) { // End date within range 850 if ( rEnd >= start && rEnd <= end ) { // End date within range
827 found = true; 851 found = true;
828 } 852 }
829 } else { // Duration set 853 } else { // Duration set
830 // TODO: Calculate end date from duration. Should be done in Event 854 // TODO: Calculate end date from duration. Should be done in Event
831 // For now include all events with a duration. 855 // For now include all events with a duration.
832 found = true; 856 found = true;
833 } 857 }
834 } 858 }
835 */ 859 */
836 860
837 } 861 }
838 862
839 if ( found ) eventList.append( event ); 863 if ( found ) eventList.append( event );
840 } else { 864 } else {
841 QDate s = event->dtStart().date(); 865 QDate s = event->dtStart().date();
842 QDate e = event->dtEnd().date(); 866 QDate e = event->dtEnd().date();
843 867
844 if ( inclusive ) { 868 if ( inclusive ) {
845 if ( s >= start && e <= end ) { 869 if ( s >= start && e <= end ) {
846 eventList.append( event ); 870 eventList.append( event );
847 } 871 }
848 } else { 872 } else {
849 if ( ( e >= start && s <= end ) ) { 873 if ( ( e >= start && s <= end ) ) {
850 eventList.append( event ); 874 eventList.append( event );
851 } 875 }
852 } 876 }
853 } 877 }
854 } 878 }
855 879
856 return eventList; 880 return eventList;
857} 881}
858 882
859QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt ) 883QPtrList<Event> CalendarLocal::rawEventsForDate( const QDateTime &qdt )
860{ 884{
861 return rawEventsForDate( qdt.date() ); 885 return rawEventsForDate( qdt.date() );
862} 886}
863 887
864QPtrList<Event> CalendarLocal::rawEvents() 888QPtrList<Event> CalendarLocal::rawEvents()
865{ 889{
866 QPtrList<Event> el; 890 QPtrList<Event> el;
867 for ( Event *it = mEventList.first(); it; it = mEventList.next() ) 891 for ( Event *it = mEventList.first(); it; it = mEventList.next() )
868 if ( it->calEnabled() ) el.append( it ); 892 if ( it->calEnabled() ) el.append( it );
869 return el; 893 return el;
870} 894}
871 895
872bool CalendarLocal::addJournal(Journal *journal) 896bool CalendarLocal::addJournal(Journal *journal)
873{ 897{
874 mJournalList.append(journal); 898 mJournalList.append(journal);
875 899
876 journal->registerObserver( this ); 900 journal->registerObserver( this );
877 901
878 setModified( true ); 902 setModified( true );
879 if ( journal->calID() == 0 ) 903 if ( journal->calID() == 0 )
880 journal->setCalID( mDefaultCalendar ); 904 journal->setCalID( mDefaultCalendar );
881 journal->setCalEnabled( true ); 905 journal->setCalEnabled( true );
882 return true; 906 return true;
883} 907}
884 908
885void CalendarLocal::deleteJournal( Journal *journal ) 909void CalendarLocal::deleteJournal( Journal *journal )
886{ 910{
887 clearUndo(journal); 911 clearUndo(journal);
888 if ( mJournalList.removeRef(journal) ) { 912 if ( mJournalList.removeRef(journal) ) {
889 setModified( true ); 913 setModified( true );
890 } 914 }
891} 915}
892 916
893QPtrList<Journal> CalendarLocal::journals4Date( const QDate & date ) 917QPtrList<Journal> CalendarLocal::journals4Date( const QDate & date )
894{ 918{
895 QPtrList<Journal> el; 919 QPtrList<Journal> el;
896 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 920 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
897 if ( it->calEnabled() && it->dtStart().date() == date) el.append( it ); 921 if ( it->calEnabled() && it->dtStart().date() == date) el.append( it );
898 return el; 922 return el;
899} 923}
900Journal *CalendarLocal::journal( const QDate &date ) 924Journal *CalendarLocal::journal( const QDate &date )
901{ 925{
902// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl; 926// kdDebug(5800) << "CalendarLocal::journal() " << date.toString() << endl;
903 927
904 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 928 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
905 if ( it->calEnabled() && it->dtStart().date() == date ) 929 if ( it->calEnabled() && it->dtStart().date() == date )
906 return it; 930 return it;
907 931
908 return 0; 932 return 0;
909} 933}
910 934
911Journal *CalendarLocal::journal( const QString &uid ) 935Journal *CalendarLocal::journal( const QString &uid )
912{ 936{
913 Journal * retVal = 0; 937 Journal * retVal = 0;
914 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 938 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
915 if ( it->calEnabled() && it->uid() == uid ) { 939 if ( it->calEnabled() && it->uid() == uid ) {
916 if ( retVal ) { 940 if ( retVal ) {
917 if ( retVal->calID() > it->calID() ) { 941 if ( retVal->calID() > it->calID() ) {
918 retVal = it; 942 retVal = it;
919 } 943 }
920 } else { 944 } else {
921 retVal = it; 945 retVal = it;
922 } 946 }
923 } 947 }
924 return retVal; 948 return retVal;
925} 949}
926 950
927QPtrList<Journal> CalendarLocal::journals() 951QPtrList<Journal> CalendarLocal::journals()
928{ 952{
929 QPtrList<Journal> el; 953 QPtrList<Journal> el;
930 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 954 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
931 if ( it->calEnabled() ) el.append( it ); 955 if ( it->calEnabled() ) el.append( it );
932 return el; 956 return el;
933} 957}
934void CalendarLocal::setCalendarRemove( int id ) 958void CalendarLocal::setCalendarRemove( int id )
935{ 959{
936 960
937 { 961 {
938 QPtrList<Event> EventList = mEventList; 962 QPtrList<Event> EventList = mEventList;
939 Event * ev = EventList.first(); 963 Event * ev = EventList.first();
940 while ( ev ) { 964 while ( ev ) {
941 if ( ev->calID() == id ) 965 if ( ev->calID() == id )
942 deleteEvent( ev ); 966 deleteEvent( ev );
943 ev = EventList.next(); 967 ev = EventList.next();
944 } 968 }
945 } 969 }
946 { 970 {
947 971
948 QPtrList<Todo> TodoList = mTodoList; 972 QPtrList<Todo> TodoList = mTodoList;
949 Todo * ev = TodoList.first(); 973 Todo * ev = TodoList.first();
950 while ( ev ) { 974 while ( ev ) {
951 if ( ev->calID() == id ) 975 if ( ev->calID() == id )
952 deleteTodo( ev ); 976 deleteTodo( ev );
953 ev = TodoList.next(); 977 ev = TodoList.next();
954 } 978 }
955 } 979 }
956 { 980 {
957 QPtrList<Journal> JournalList = mJournalList; 981 QPtrList<Journal> JournalList = mJournalList;
958 Journal * ev = JournalList.first(); 982 Journal * ev = JournalList.first();
959 while ( ev ) { 983 while ( ev ) {
960 if ( ev->calID() == id ) 984 if ( ev->calID() == id )
961 deleteJournal( ev ); 985 deleteJournal( ev );
962 ev = JournalList.next(); 986 ev = JournalList.next();
963 } 987 }
964 } 988 }
965 989
966 clearUndo(0); 990 clearUndo(0);
967 991
968} 992}
969 993
970void CalendarLocal::setCalendarEnabled( int id, bool enable ) 994void CalendarLocal::setCalendarEnabled( int id, bool enable )
971{ 995{
972 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 996 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
973 if ( it->calID() == id ) it->setCalEnabled( enable ); 997 if ( it->calID() == id ) it->setCalEnabled( enable );
974 998
975 for ( Event *it = mEventList.first(); it; it = mEventList.next() ) 999 for ( Event *it = mEventList.first(); it; it = mEventList.next() )
976 if ( it->calID() == id ) it->setCalEnabled( enable ); 1000 if ( it->calID() == id ) it->setCalEnabled( enable );
977 1001
978 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 1002 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
979 if ( it->calID() == id ) it->setCalEnabled( enable ); 1003 if ( it->calID() == id ) it->setCalEnabled( enable );
980 1004
981} 1005}