summaryrefslogtreecommitdiffabout
path: root/libkcal
Unidiff
Diffstat (limited to 'libkcal') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendarlocal.cpp14
1 files changed, 7 insertions, 7 deletions
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index 8a5a76f..ce3cd09 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -1,700 +1,700 @@
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
83Todo* CalendarLocal::todoForUid( const QString& uid, bool doNotCheckDuplicates, bool enabledOnly,int * isDup ) 83Todo* CalendarLocal::todoForUid( const QString& uid, bool doNotCheckDuplicates, bool enabledOnly,int * isDup )
84{ 84{
85 85
86 int calID = 0; 86 int calID = 0;
87 if ( isDup && *isDup > 0 ) 87 if ( isDup && *isDup > 0 )
88 calID = *isDup; 88 calID = *isDup;
89 Todo *todo;; 89 Todo *todo;;
90 Todo *retVal = 0; 90 Todo *retVal = 0;
91 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 91 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
92 if ( todo->uid() == uid ) { 92 if ( todo->uid() == uid ) {
93 if( calID ) { 93 if( calID ) {
94 if ( todo->calID() != calID ) 94 if ( todo->calID() != calID )
95 continue; 95 continue;
96 } 96 }
97 else { 97 else {
98 if ( enabledOnly ) { 98 if ( enabledOnly ) {
99 if ( !todo->calEnabled() ) { 99 if ( !todo->calEnabled() ) {
100 if ( isDup ) 100 if ( isDup )
101 *isDup = todo->calID(); 101 *isDup = todo->calID();
102 continue; 102 continue;
103 } 103 }
104 } 104 }
105 } 105 }
106 if ( doNotCheckDuplicates ) return todo; 106 if ( doNotCheckDuplicates ) return todo;
107 if ( retVal ) { 107 if ( retVal ) {
108 if ( retVal->calID() > todo->calID() ) { 108 if ( retVal->calID() > todo->calID() ) {
109 if ( isDup ) 109 if ( isDup )
110 *isDup = retVal->calID(); 110 *isDup = retVal->calID();
111 retVal = todo; 111 retVal = todo;
112 } 112 }
113 } else { 113 } else {
114 retVal = todo; 114 retVal = todo;
115 } 115 }
116 } 116 }
117 } 117 }
118 return retVal; 118 return retVal;
119} 119}
120//if ( isDup) and * isDup == 0: store duplicate found cal id in isDup 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 121//if ( isDup) and * isDup > 0: search only in calendar with ID *isDup, ignore enabledOnly
122 122
123Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates, bool enabledOnly, int * isDup ) 123Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates, bool enabledOnly, int * isDup )
124{ 124{
125 int calID = 0; 125 int calID = 0;
126 if ( isDup && *isDup > 0 ) 126 if ( isDup && *isDup > 0 )
127 calID = *isDup; 127 calID = *isDup;
128 Incidence *retVal = todoForUid( uid , doNotCheckDuplicates,enabledOnly, isDup ); 128 Incidence *retVal = todoForUid( uid , doNotCheckDuplicates,enabledOnly, isDup );
129 if ( retVal ) return retVal; 129 if ( retVal ) return retVal;
130 Event *event; 130 Event *event;
131 for ( event = mEventList.first(); event; event = mEventList.next() ) { 131 for ( event = mEventList.first(); event; event = mEventList.next() ) {
132 if ( event->uid() == uid ) { 132 if ( event->uid() == uid ) {
133 if( calID ) { 133 if( calID ) {
134 if ( event->calID() != calID ) 134 if ( event->calID() != calID )
135 continue; 135 continue;
136 } 136 }
137 else { 137 else {
138 if ( enabledOnly ) { 138 if ( enabledOnly ) {
139 if ( !event->calEnabled() ) { 139 if ( !event->calEnabled() ) {
140 if ( isDup ) 140 if ( isDup )
141 *isDup =event->calID() ; 141 *isDup =event->calID() ;
142 continue; 142 continue;
143 } 143 }
144 } 144 }
145 } 145 }
146 if ( doNotCheckDuplicates ) return event; 146 if ( doNotCheckDuplicates ) return event;
147 if ( retVal ) { 147 if ( retVal ) {
148 if ( retVal->calID() > event->calID() ) { 148 if ( retVal->calID() > event->calID() ) {
149 if ( isDup ) 149 if ( isDup )
150 *isDup = retVal->calID(); 150 *isDup = retVal->calID();
151 retVal = event; 151 retVal = event;
152 } 152 }
153 } else { 153 } else {
154 retVal = event; 154 retVal = event;
155 } 155 }
156 } 156 }
157 } 157 }
158 if ( retVal ) return retVal; 158 if ( retVal ) return retVal;
159 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 159 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
160 if ( it->uid() == uid ) { 160 if ( it->uid() == uid ) {
161 if( calID ) { 161 if( calID ) {
162 if ( event->calID() != calID ) 162 if ( event->calID() != calID )
163 continue; 163 continue;
164 } 164 }
165 else { 165 else {
166 if ( enabledOnly ) { 166 if ( enabledOnly ) {
167 if ( !it->calEnabled() ) { 167 if ( !it->calEnabled() ) {
168 if ( isDup ) 168 if ( isDup )
169 *isDup = it->calID(); 169 *isDup = it->calID();
170 continue; 170 continue;
171 } 171 }
172 } 172 }
173 } 173 }
174 if ( doNotCheckDuplicates ) return it; 174 if ( doNotCheckDuplicates ) return it;
175 if ( retVal ) { 175 if ( retVal ) {
176 if ( retVal->calID() > it->calID() ) { 176 if ( retVal->calID() > it->calID() ) {
177 if ( isDup ) 177 if ( isDup )
178 *isDup = retVal->calID(); 178 *isDup = retVal->calID();
179 retVal = it; 179 retVal = it;
180 } 180 }
181 } else { 181 } else {
182 retVal = it; 182 retVal = it;
183 } 183 }
184 } 184 }
185 return retVal; 185 return retVal;
186} 186}
187 187
188bool CalendarLocal::mergeCalendar( Calendar* remote ) 188bool CalendarLocal::mergeCalendar( Calendar* remote )
189{ 189{
190 // 1 look for raw inc in local 190 // 1 look for raw inc in local
191 // if inc not in remote, delete in local 191 // if inc not in remote, delete in local
192 // 2 look for raw inc in remote 192 // 2 look for raw inc in remote
193 // if inc in local, replace it 193 // if inc in local, replace it
194 // if not in local, add it to default calendar 194 // if not in local, add it to default calendar
195 QPtrList<Incidence> localInc = rawIncidences(); 195 QPtrList<Incidence> localInc = rawIncidences();
196 Incidence* inL = localInc.first(); 196 Incidence* inL = localInc.first();
197 while ( inL ) { 197 while ( inL ) {
198 if ( ! inL->isReadOnly () ) 198 if ( ! inL->isReadOnly () )
199 if ( !remote->incidenceForUid( inL->uid(), true )) 199 if ( !remote->incidenceForUid( inL->uid(), true ))
200 deleteIncidence( inL ); 200 deleteIncidence( inL );
201 inL = localInc.next(); 201 inL = localInc.next();
202 } 202 }
203 QPtrList<Incidence> er = remote->rawIncidences(); 203 QPtrList<Incidence> er = remote->rawIncidences();
204 Incidence* inR = er.first(); 204 Incidence* inR = er.first();
205 while ( inR ) { 205 while ( inR ) {
206 inL = incidenceForUid( inR->uid(),false ); 206 inL = incidenceForUid( inR->uid(),false );
207 if ( inL ) { 207 if ( inL ) {
208 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) { 208 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) {
209 int calID = inL->calID(); 209 int calID = inL->calID();
210 deleteIncidence( inL ); 210 deleteIncidence( inL );
211 inL = inR->clone(); 211 inL = inR->clone();
212 inL->setCalID_block( calID ); 212 inL->setCalID_block( calID );
213 addIncidence( inL ); 213 addIncidence( inL );
214 } 214 }
215 } else { 215 } else {
216 inL = inR->clone(); 216 inL = inR->clone();
217 inL->setCalID_block( 0 );// add to default cal 217 inL->setCalID_block( 0 );// add to default cal
218 addIncidence( inL ); 218 addIncidence( inL );
219 } 219 }
220 inR = er.next(); 220 inR = er.next();
221 } 221 }
222 return true; 222 return true;
223} 223}
224 224
225 225
226bool CalendarLocal::addCalendarFile( QString name, int id ) 226bool CalendarLocal::addCalendarFile( QString name, int id )
227{ 227{
228 CalendarLocal calendar( timeZoneId() ); 228 CalendarLocal calendar( timeZoneId() );
229 calendar.setDefaultCalendar( id ); 229 calendar.setDefaultCalendar( id );
230 if ( calendar.load( name ) ) { 230 if ( calendar.load( name ) ) {
231 addCalendar( &calendar ); 231 addCalendar( &calendar );
232 return true; 232 return true;
233 } 233 }
234 return false; 234 return false;
235} 235}
236void CalendarLocal::setSyncEventsEnabled() 236void CalendarLocal::setSyncEventsEnabled()
237{ 237{
238 Event * ev; 238 Event * ev;
239 ev = mEventList.first(); 239 ev = mEventList.first();
240 while ( ev ) { 240 while ( ev ) {
241 if ( ev->uid().left(15) == QString("last-syncEvent-") ) 241 if ( ev->uid().left(15) == QString("last-syncEvent-") )
242 ev->setCalEnabled( true ); 242 ev->setCalEnabled( true );
243 ev = mEventList.next(); 243 ev = mEventList.next();
244 } 244 }
245} 245}
246void CalendarLocal::setSyncEventsReadOnly() 246void CalendarLocal::setSyncEventsReadOnly()
247{ 247{
248 Event * ev; 248 Event * ev;
249 ev = mEventList.first(); 249 ev = mEventList.first();
250 while ( ev ) { 250 while ( ev ) {
251 if ( ev->uid().left(15) == QString("last-syncEvent-") ) { 251 if ( ev->uid().left(15) == QString("last-syncEvent-") ) {
252 ev->setReadOnly( true ); 252 ev->setReadOnly( true );
253 } 253 }
254 ev = mEventList.next(); 254 ev = mEventList.next();
255 } 255 }
256} 256}
257 257
258void CalendarLocal::addCalendar( Calendar* cal ) 258void CalendarLocal::addCalendar( Calendar* cal )
259{ 259{
260 cal->setDontDeleteIncidencesOnClose(); 260 cal->setDontDeleteIncidencesOnClose();
261 setSyncEventsEnabled(); 261 setSyncEventsEnabled();
262 QPtrList<Incidence> incList; 262 QPtrList<Incidence> incList;
263 { 263 {
264 QPtrList<Event> EventList = cal->rawEvents(); 264 QPtrList<Event> EventList = cal->rawEvents();
265 Event * ev = EventList.first(); 265 Event * ev = EventList.first();
266 while ( ev ) { 266 while ( ev ) {
267 if ( ev->uid().left( 15 ) == QString("last-syncEvent-") ) { 267 if ( ev->uid().left( 15 ) == QString("last-syncEvent-") ) {
268 ev->setCalID_block( 1 ); 268 ev->setCalID_block( 1 );
269 } 269 }
270 Event * se = event( ev->uid() ); 270 Event * se = event( ev->uid() );
271 if ( se ) 271 if ( se )
272 incList.append( se ); 272 incList.append( se );
273 ev->unRegisterObserver( cal ); 273 ev->unRegisterObserver( cal );
274 ev->registerObserver( this ); 274 ev->registerObserver( this );
275 mEventList.append( ev ); 275 mEventList.append( ev );
276 ev = EventList.next(); 276 ev = EventList.next();
277 } 277 }
278 278
279 } 279 }
280 { 280 {
281 281
282 QPtrList<Todo> TodoList = cal->rawTodos(); 282 QPtrList<Todo> TodoList = cal->rawTodos();
283 Todo * ev = TodoList.first(); 283 Todo * ev = TodoList.first();
284 while ( ev ) { 284 while ( ev ) {
285 ev->resetRelatedTo(); 285 ev->resetRelatedTo();
286 ev = TodoList.next(); 286 Todo * se = todo( ev->uid() );
287 if ( se )
288 deleteTodo( se );
289 ev = TodoList.next();
287 } 290 }
288 //TodoList = cal->rawTodos(); 291 //TodoList = cal->rawTodos();
289 ev = TodoList.first(); 292 ev = TodoList.first();
290 while ( ev ) { 293 while ( ev ) {
291 Todo * se = todo( ev->uid() );
292 if ( se )
293 incList.append( se );
294 ev->unRegisterObserver( cal ); 294 ev->unRegisterObserver( cal );
295 ev->registerObserver( this ); 295 ev->registerObserver( this );
296 mTodoList.append( ev ); 296 mTodoList.append( ev );
297 setupRelations( ev ); 297 setupRelations( ev );
298 ev = TodoList.next(); 298 ev = TodoList.next();
299 } 299 }
300 } 300 }
301 { 301 {
302 QPtrList<Journal> JournalList = cal->journals(); 302 QPtrList<Journal> JournalList = cal->journals();
303 Journal * ev = JournalList.first(); 303 Journal * ev = JournalList.first();
304 while ( ev ) { 304 while ( ev ) {
305 Journal * se = journal( ev->uid() ); 305 Journal * se = journal( ev->uid() );
306 if ( se ) 306 if ( se )
307 incList.append( se ); 307 incList.append( se );
308 ev->unRegisterObserver( cal ); 308 ev->unRegisterObserver( cal );
309 ev->registerObserver( this ); 309 ev->registerObserver( this );
310 mJournalList.append( ev ); 310 mJournalList.append( ev );
311 ev = JournalList.next(); 311 ev = JournalList.next();
312 } 312 }
313 } 313 }
314 { 314 {
315 for (Incidence * ev = incList.first(); ev; ev = incList.next() ) { 315 for (Incidence * inc = incList.first(); inc; inc = incList.next() ) {
316 deleteIncidence ( ev ); 316 deleteIncidence ( inc );
317 } 317 }
318 } 318 }
319 setModified( true ); 319 setModified( true );
320} 320}
321bool CalendarLocal::load( const QString &fileName ) 321bool CalendarLocal::load( const QString &fileName )
322{ 322{
323 FileStorage storage( this, fileName ); 323 FileStorage storage( this, fileName );
324 return storage.load(); 324 return storage.load();
325} 325}
326 326
327bool CalendarLocal::save( const QString &fileName, CalFormat *format ) 327bool CalendarLocal::save( const QString &fileName, CalFormat *format )
328{ 328{
329 FileStorage storage( this, fileName, format ); 329 FileStorage storage( this, fileName, format );
330 return storage.save(); 330 return storage.save();
331} 331}
332 332
333void CalendarLocal::stopAllTodos() 333void CalendarLocal::stopAllTodos()
334{ 334{
335 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 335 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
336 it->setRunning( false ); 336 it->setRunning( false );
337 337
338} 338}
339void CalendarLocal::close() 339void CalendarLocal::close()
340{ 340{
341 341
342 Todo * i; 342 Todo * i;
343 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false); 343 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false);
344 344
345 mEventList.setAutoDelete( true ); 345 mEventList.setAutoDelete( true );
346 mTodoList.setAutoDelete( true ); 346 mTodoList.setAutoDelete( true );
347 mJournalList.setAutoDelete( true ); 347 mJournalList.setAutoDelete( true );
348 348
349 mEventList.clear(); 349 mEventList.clear();
350 mTodoList.clear(); 350 mTodoList.clear();
351 mJournalList.clear(); 351 mJournalList.clear();
352 352
353 mEventList.setAutoDelete( false ); 353 mEventList.setAutoDelete( false );
354 mTodoList.setAutoDelete( false ); 354 mTodoList.setAutoDelete( false );
355 mJournalList.setAutoDelete( false ); 355 mJournalList.setAutoDelete( false );
356 356
357 setModified( false ); 357 setModified( false );
358} 358}
359 359
360bool CalendarLocal::addAnniversaryNoDup( Event *event ) 360bool CalendarLocal::addAnniversaryNoDup( Event *event )
361{ 361{
362 QString cat; 362 QString cat;
363 bool isBirthday = true; 363 bool isBirthday = true;
364 if( event->categoriesStr() == i18n( "Anniversary" ) ) { 364 if( event->categoriesStr() == i18n( "Anniversary" ) ) {
365 isBirthday = false; 365 isBirthday = false;
366 cat = i18n( "Anniversary" ); 366 cat = i18n( "Anniversary" );
367 } else if( event->categoriesStr() == i18n( "Birthday" ) ) { 367 } else if( event->categoriesStr() == i18n( "Birthday" ) ) {
368 isBirthday = true; 368 isBirthday = true;
369 cat = i18n( "Birthday" ); 369 cat = i18n( "Birthday" );
370 } else { 370 } else {
371 qDebug("addAnniversaryNoDup called without fitting category! "); 371 qDebug("addAnniversaryNoDup called without fitting category! ");
372 return false; 372 return false;
373 } 373 }
374 Event * eve; 374 Event * eve;
375 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 375 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
376 if ( !(eve->categories().contains( cat ) )) 376 if ( !(eve->categories().contains( cat ) ))
377 continue; 377 continue;
378 // now we have an event with fitting category 378 // now we have an event with fitting category
379 if ( eve->dtStart().date() != event->dtStart().date() ) 379 if ( eve->dtStart().date() != event->dtStart().date() )
380 continue; 380 continue;
381 // now we have an event with fitting category+date 381 // now we have an event with fitting category+date
382 if ( eve->summary() != event->summary() ) 382 if ( eve->summary() != event->summary() )
383 continue; 383 continue;
384 // now we have an event with fitting category+date+summary 384 // now we have an event with fitting category+date+summary
385 return false; 385 return false;
386 } 386 }
387 return addEvent( event ); 387 return addEvent( event );
388 388
389} 389}
390bool CalendarLocal::addEventNoDup( Event *event ) 390bool CalendarLocal::addEventNoDup( Event *event )
391{ 391{
392 Event * eve; 392 Event * eve;
393 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 393 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
394 if ( *eve == *event ) { 394 if ( *eve == *event ) {
395 //qDebug("CalendarLocal::Duplicate event found! Not inserted! "); 395 //qDebug("CalendarLocal::Duplicate event found! Not inserted! ");
396 return false; 396 return false;
397 } 397 }
398 } 398 }
399 return addEvent( event ); 399 return addEvent( event );
400} 400}
401 401
402bool CalendarLocal::addEvent( Event *event ) 402bool CalendarLocal::addEvent( Event *event )
403{ 403{
404 insertEvent( event ); 404 insertEvent( event );
405 405
406 event->registerObserver( this ); 406 event->registerObserver( this );
407 407
408 setModified( true ); 408 setModified( true );
409 if ( event->calID() == 0 ) 409 if ( event->calID() == 0 )
410 event->setCalID_block( mDefaultCalendar ); 410 event->setCalID_block( mDefaultCalendar );
411 event->setCalEnabled( true ); 411 event->setCalEnabled( true );
412 412
413 return true; 413 return true;
414} 414}
415 415
416void CalendarLocal::deleteEvent( Event *event ) 416void CalendarLocal::deleteEvent( Event *event )
417{ 417{
418 clearUndo(event); 418 clearUndo(event);
419 if ( mEventList.removeRef( event ) ) { 419 if ( mEventList.removeRef( event ) ) {
420 setModified( true ); 420 setModified( true );
421 } 421 }
422} 422}
423 423
424 424
425Event *CalendarLocal::event( const QString &uid ) 425Event *CalendarLocal::event( const QString &uid )
426{ 426{
427 Event *event; 427 Event *event;
428 Event *retVal = 0; 428 Event *retVal = 0;
429 for ( event = mEventList.first(); event; event = mEventList.next() ) { 429 for ( event = mEventList.first(); event; event = mEventList.next() ) {
430 if ( event->calEnabled() && event->uid() == uid ) { 430 if ( event->calEnabled() && event->uid() == uid ) {
431 if ( retVal ) { 431 if ( retVal ) {
432 if ( retVal->calID() > event->calID() ) { 432 if ( retVal->calID() > event->calID() ) {
433 retVal = event; 433 retVal = event;
434 } 434 }
435 } else { 435 } else {
436 retVal = event; 436 retVal = event;
437 } 437 }
438 } 438 }
439 } 439 }
440 return retVal; 440 return retVal;
441} 441}
442bool CalendarLocal::addTodoNoDup( Todo *todo ) 442bool CalendarLocal::addTodoNoDup( Todo *todo )
443{ 443{
444 Todo * eve; 444 Todo * eve;
445 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) { 445 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) {
446 if ( *eve == *todo ) { 446 if ( *eve == *todo ) {
447 //qDebug("duplicate todo found! not inserted! "); 447 //qDebug("duplicate todo found! not inserted! ");
448 return false; 448 return false;
449 } 449 }
450 } 450 }
451 return addTodo( todo ); 451 return addTodo( todo );
452} 452}
453bool CalendarLocal::addTodo( Todo *todo ) 453bool CalendarLocal::addTodo( Todo *todo )
454{ 454{
455 mTodoList.append( todo ); 455 mTodoList.append( todo );
456 456
457 todo->registerObserver( this ); 457 todo->registerObserver( this );
458 458
459 // Set up subtask relations 459 // Set up subtask relations
460 setupRelations( todo ); 460 setupRelations( todo );
461 461
462 setModified( true ); 462 setModified( true );
463 if ( todo->calID() == 0 ) 463 if ( todo->calID() == 0 )
464 todo->setCalID_block( mDefaultCalendar ); 464 todo->setCalID_block( mDefaultCalendar );
465 todo->setCalEnabled( true ); 465 todo->setCalEnabled( true );
466 return true; 466 return true;
467} 467}
468 468
469void CalendarLocal::deleteTodo( Todo *todo ) 469void CalendarLocal::deleteTodo( Todo *todo )
470{ 470{
471 QString uid = todo->uid(); 471 QString uid = todo->uid();
472 // Handle orphaned children 472 // Handle orphaned children
473 removeRelations( todo ); 473 removeRelations( todo );
474 clearUndo(todo); 474 clearUndo(todo);
475 475
476 if ( mTodoList.removeRef( todo ) ) { 476 if ( mTodoList.removeRef( todo ) ) {
477 setModified( true ); 477 setModified( true );
478 } 478 }
479 Todo* dup = todoForUid( uid ); 479 Todo* dup = todoForUid( uid );
480 if ( dup ) 480 if ( dup )
481 setupRelations( dup ); 481 setupRelations( dup );
482} 482}
483 483
484QPtrList<Todo> CalendarLocal::rawTodos() 484QPtrList<Todo> CalendarLocal::rawTodos()
485{ 485{
486 QPtrList<Todo> el; 486 QPtrList<Todo> el;
487 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 487 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
488 if ( it->calEnabled() ) el.append( it ); 488 if ( it->calEnabled() ) el.append( it );
489 return el; 489 return el;
490} 490}
491Todo *CalendarLocal::todo( QString syncProf, QString id ) 491Todo *CalendarLocal::todo( QString syncProf, QString id )
492{ 492{
493 Todo *todo; 493 Todo *todo;
494 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 494 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
495 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 495 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
496 } 496 }
497 497
498 return 0; 498 return 0;
499} 499}
500void CalendarLocal::removeSyncInfo( QString syncProfile) 500void CalendarLocal::removeSyncInfo( QString syncProfile)
501{ 501{
502 QPtrList<Incidence> all = rawIncidences() ; 502 QPtrList<Incidence> all = rawIncidences() ;
503 Incidence *inc; 503 Incidence *inc;
504 for ( inc = all.first(); inc; inc = all.next() ) { 504 for ( inc = all.first(); inc; inc = all.next() ) {
505 inc->removeID( syncProfile ); 505 inc->removeID( syncProfile );
506 } 506 }
507 if ( syncProfile.isEmpty() ) { 507 if ( syncProfile.isEmpty() ) {
508 QPtrList<Event> el; 508 QPtrList<Event> el;
509 Event *todo; 509 Event *todo;
510 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 510 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
511 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 511 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
512 el.append( todo ); 512 el.append( todo );
513 } 513 }
514 for ( todo = el.first(); todo; todo = el.next() ) { 514 for ( todo = el.first(); todo; todo = el.next() ) {
515 deleteIncidence ( todo ); 515 deleteIncidence ( todo );
516 } 516 }
517 } else { 517 } else {
518 Event *lse = event( "last-syncEvent-"+ syncProfile); 518 Event *lse = event( "last-syncEvent-"+ syncProfile);
519 if ( lse ) 519 if ( lse )
520 deleteIncidence ( lse ); 520 deleteIncidence ( lse );
521 } 521 }
522} 522}
523QPtrList<Event> CalendarLocal::getExternLastSyncEvents() 523QPtrList<Event> CalendarLocal::getExternLastSyncEvents()
524{ 524{
525 QPtrList<Event> el; 525 QPtrList<Event> el;
526 Event *todo; 526 Event *todo;
527 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 527 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
528 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 528 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
529 if ( todo->summary().left(3) == "E: " ) 529 if ( todo->summary().left(3) == "E: " )
530 el.append( todo ); 530 el.append( todo );
531 } 531 }
532 532
533 return el; 533 return el;
534 534
535} 535}
536Event *CalendarLocal::event( QString syncProf, QString id ) 536Event *CalendarLocal::event( QString syncProf, QString id )
537{ 537{
538 Event *todo; 538 Event *todo;
539 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 539 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
540 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 540 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
541 } 541 }
542 542
543 return 0; 543 return 0;
544} 544}
545Todo *CalendarLocal::todo( const QString &uid ) 545Todo *CalendarLocal::todo( const QString &uid )
546{ 546{
547 Todo *todo; 547 Todo *todo;
548 Todo *retVal = 0; 548 Todo *retVal = 0;
549 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 549 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
550 if ( todo->calEnabled() && todo->uid() == uid ) { 550 if ( todo->calEnabled() && todo->uid() == uid ) {
551 if ( retVal ) { 551 if ( retVal ) {
552 if ( retVal->calID() > todo->calID() ) { 552 if ( retVal->calID() > todo->calID() ) {
553 retVal = todo; 553 retVal = todo;
554 } 554 }
555 } else { 555 } else {
556 retVal = todo; 556 retVal = todo;
557 } 557 }
558 } 558 }
559 } 559 }
560 return retVal; 560 return retVal;
561} 561}
562void CalendarLocal::getIncidenceCount( int calId, int& events, int & todos, int & journals) 562void CalendarLocal::getIncidenceCount( int calId, int& events, int & todos, int & journals)
563{ 563{
564 events = 0; 564 events = 0;
565 todos = 0; 565 todos = 0;
566 journals = 0; 566 journals = 0;
567 { 567 {
568 Todo *todo; 568 Todo *todo;
569 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 569 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
570 if ( todo->calID() == calId ) 570 if ( todo->calID() == calId )
571 ++todos; 571 ++todos;
572 } 572 }
573 } 573 }
574 { 574 {
575 Event *todo; 575 Event *todo;
576 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 576 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
577 if ( todo->calID() == calId ) 577 if ( todo->calID() == calId )
578 ++events; 578 ++events;
579 579
580 } 580 }
581 } 581 }
582 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 582 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
583 if ( it->calID() == calId ) ++journals; 583 if ( it->calID() == calId ) ++journals;
584 584
585} 585}
586QString CalendarLocal::nextSummary() const 586QString CalendarLocal::nextSummary() const
587{ 587{
588 return mNextSummary; 588 return mNextSummary;
589} 589}
590QDateTime CalendarLocal::nextAlarmEventDateTime() const 590QDateTime CalendarLocal::nextAlarmEventDateTime() const
591{ 591{
592 return mNextAlarmEventDateTime; 592 return mNextAlarmEventDateTime;
593} 593}
594void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted) 594void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted)
595{ 595{
596 //mNextAlarmIncidence 596 //mNextAlarmIncidence
597 //mNextAlarmDateTime 597 //mNextAlarmDateTime
598 //return mNextSummary; 598 //return mNextSummary;
599 //return mNextAlarmEventDateTime; 599 //return mNextAlarmEventDateTime;
600 bool newNextAlarm = false; 600 bool newNextAlarm = false;
601 bool computeNextAlarm = false; 601 bool computeNextAlarm = false;
602 bool ok; 602 bool ok;
603 int offset; 603 int offset;
604 QDateTime nextA; 604 QDateTime nextA;
605 // QString nextSum; 605 // QString nextSum;
606 //QDateTime nextEvent; 606 //QDateTime nextEvent;
607 if ( mNextAlarmIncidence == 0 || incidence == 0 ) { 607 if ( mNextAlarmIncidence == 0 || incidence == 0 ) {
608 computeNextAlarm = true; 608 computeNextAlarm = true;
609 } else { 609 } else {
610 if ( ! deleted ) { 610 if ( ! deleted ) {
611 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 611 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
612 if ( ok ) { 612 if ( ok ) {
613 if ( nextA < mNextAlarmDateTime ) { 613 if ( nextA < mNextAlarmDateTime ) {
614 deRegisterAlarm(); 614 deRegisterAlarm();
615 mNextAlarmDateTime = nextA; 615 mNextAlarmDateTime = nextA;
616 mNextSummary = incidence->summary(); 616 mNextSummary = incidence->summary();
617 mNextAlarmEventDateTime = nextA.addSecs(offset ) ; 617 mNextAlarmEventDateTime = nextA.addSecs(offset ) ;
618 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 618 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
619 newNextAlarm = true; 619 newNextAlarm = true;
620 mNextAlarmIncidence = incidence; 620 mNextAlarmIncidence = incidence;
621 } else { 621 } else {
622 if ( incidence == mNextAlarmIncidence ) { 622 if ( incidence == mNextAlarmIncidence ) {
623 computeNextAlarm = true; 623 computeNextAlarm = true;
624 } 624 }
625 } 625 }
626 } else { 626 } else {
627 if ( mNextAlarmIncidence == incidence ) { 627 if ( mNextAlarmIncidence == incidence ) {
628 computeNextAlarm = true; 628 computeNextAlarm = true;
629 } 629 }
630 } 630 }
631 } else { // deleted 631 } else { // deleted
632 if ( incidence == mNextAlarmIncidence ) { 632 if ( incidence == mNextAlarmIncidence ) {
633 computeNextAlarm = true; 633 computeNextAlarm = true;
634 } 634 }
635 } 635 }
636 } 636 }
637 if ( computeNextAlarm ) { 637 if ( computeNextAlarm ) {
638 deRegisterAlarm(); 638 deRegisterAlarm();
639 nextA = nextAlarm( 1000 ); 639 nextA = nextAlarm( 1000 );
640 if (! mNextAlarmIncidence ) { 640 if (! mNextAlarmIncidence ) {
641 return; 641 return;
642 } 642 }
643 newNextAlarm = true; 643 newNextAlarm = true;
644 } 644 }
645 if ( newNextAlarm ) 645 if ( newNextAlarm )
646 registerAlarm(); 646 registerAlarm();
647} 647}
648QString CalendarLocal:: getAlarmNotification() 648QString CalendarLocal:: getAlarmNotification()
649{ 649{
650 QString ret; 650 QString ret;
651 // this should not happen 651 // this should not happen
652 if (! mNextAlarmIncidence ) 652 if (! mNextAlarmIncidence )
653 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString; 653 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString;
654 Alarm* alarm = mNextAlarmIncidence->alarms().first(); 654 Alarm* alarm = mNextAlarmIncidence->alarms().first();
655 if ( alarm->type() == Alarm::Procedure ) { 655 if ( alarm->type() == Alarm::Procedure ) {
656 ret = "proc_alarm" + alarm->programFile()+"+++"; 656 ret = "proc_alarm" + alarm->programFile()+"+++";
657 } else { 657 } else {
658 ret = "audio_alarm" +alarm->audioFile() +"+++"; 658 ret = "audio_alarm" +alarm->audioFile() +"+++";
659 } 659 }
660 ret += "cal_alarm"+ mNextSummary.left( 25 ); 660 ret += "cal_alarm"+ mNextSummary.left( 25 );
661 if ( mNextSummary.length() > 25 ) 661 if ( mNextSummary.length() > 25 )
662 ret += "\n" + mNextSummary.mid(25, 25 ); 662 ret += "\n" + mNextSummary.mid(25, 25 );
663 ret+= "\n"+mNextAlarmEventDateTimeString; 663 ret+= "\n"+mNextAlarmEventDateTimeString;
664 return ret; 664 return ret;
665} 665}
666void CalendarLocal::registerAlarm() 666void CalendarLocal::registerAlarm()
667{ 667{
668 mLastAlarmNotificationString = getAlarmNotification(); 668 mLastAlarmNotificationString = getAlarmNotification();
669 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() ); 669 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() );
670 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 670 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
671// #ifndef DESKTOP_VERSION 671// #ifndef DESKTOP_VERSION
672// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() ); 672// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() );
673// #endif 673// #endif
674} 674}
675void CalendarLocal::deRegisterAlarm() 675void CalendarLocal::deRegisterAlarm()
676{ 676{
677 if ( mLastAlarmNotificationString.isNull() ) 677 if ( mLastAlarmNotificationString.isNull() )
678 return; 678 return;
679 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() ); 679 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() );
680 680
681 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 681 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
682 mNextAlarmEventDateTime = QDateTime(); 682 mNextAlarmEventDateTime = QDateTime();
683// #ifndef DESKTOP_VERSION 683// #ifndef DESKTOP_VERSION
684// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() ); 684// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() );
685// #endif 685// #endif
686} 686}
687 687
688QPtrList<Todo> CalendarLocal::todos( const QDate &date ) 688QPtrList<Todo> CalendarLocal::todos( const QDate &date )
689{ 689{
690 QPtrList<Todo> todos; 690 QPtrList<Todo> todos;
691 691
692 Todo *todo; 692 Todo *todo;
693 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 693 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
694 if ( !todo->calEnabled() ) continue; 694 if ( !todo->calEnabled() ) continue;
695 if ( todo->hasDueDate() && todo->dtDue().date() == date ) { 695 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
696 todos.append( todo ); 696 todos.append( todo );
697 } 697 }
698 } 698 }
699 699
700 filter()->apply( &todos ); 700 filter()->apply( &todos );