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