summaryrefslogtreecommitdiffabout
path: root/libkcal/calendarlocal.cpp
Unidiff
Diffstat (limited to 'libkcal/calendarlocal.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendarlocal.cpp2
1 files changed, 1 insertions, 1 deletions
diff --git a/libkcal/calendarlocal.cpp b/libkcal/calendarlocal.cpp
index eef8327..5baa7dc 100644
--- a/libkcal/calendarlocal.cpp
+++ b/libkcal/calendarlocal.cpp
@@ -1,643 +1,643 @@
1/* 1/*
2 This file is part of libkcal. 2 This file is part of libkcal.
3 3
4 Copyright (c) 1998 Preston Brown 4 Copyright (c) 1998 Preston Brown
5 Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org> 5 Copyright (c) 2001,2003 Cornelius Schumacher <schumacher@kde.org>
6 6
7 This library is free software; you can redistribute it and/or 7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public 8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either 9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version. 10 version 2 of the License, or (at your option) any later version.
11 11
12 This library is distributed in the hope that it will be useful, 12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of 13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details. 15 Library General Public License for more details.
16 16
17 You should have received a copy of the GNU Library General Public License 17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to 18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. 20 Boston, MA 02111-1307, USA.
21*/ 21*/
22 22
23#include <qdatetime.h> 23#include <qdatetime.h>
24#include <qstring.h> 24#include <qstring.h>
25#include <qptrlist.h> 25#include <qptrlist.h>
26 26
27#include <kdebug.h> 27#include <kdebug.h>
28#include <kconfig.h> 28#include <kconfig.h>
29#include <kglobal.h> 29#include <kglobal.h>
30#include <klocale.h> 30#include <klocale.h>
31 31
32#include "vcaldrag.h" 32#include "vcaldrag.h"
33#include "vcalformat.h" 33#include "vcalformat.h"
34#include "icalformat.h" 34#include "icalformat.h"
35#include "exceptions.h" 35#include "exceptions.h"
36#include "incidence.h" 36#include "incidence.h"
37#include "journal.h" 37#include "journal.h"
38#include "filestorage.h" 38#include "filestorage.h"
39#include "calfilter.h" 39#include "calfilter.h"
40 40
41#include "calendarlocal.h" 41#include "calendarlocal.h"
42 42
43// #ifndef DESKTOP_VERSION 43// #ifndef DESKTOP_VERSION
44// #include <qtopia/alarmserver.h> 44// #include <qtopia/alarmserver.h>
45// #endif 45// #endif
46using namespace KCal; 46using namespace KCal;
47 47
48CalendarLocal::CalendarLocal() 48CalendarLocal::CalendarLocal()
49 : Calendar() 49 : Calendar()
50{ 50{
51 init(); 51 init();
52} 52}
53 53
54CalendarLocal::CalendarLocal(const QString &timeZoneId) 54CalendarLocal::CalendarLocal(const QString &timeZoneId)
55 : Calendar(timeZoneId) 55 : Calendar(timeZoneId)
56{ 56{
57 init(); 57 init();
58} 58}
59 59
60void CalendarLocal::init() 60void CalendarLocal::init()
61{ 61{
62 mNextAlarmIncidence = 0; 62 mNextAlarmIncidence = 0;
63} 63}
64 64
65 65
66CalendarLocal::~CalendarLocal() 66CalendarLocal::~CalendarLocal()
67{ 67{
68 registerObserver( 0 ); 68 registerObserver( 0 );
69 if ( mDeleteIncidencesOnClose ) 69 if ( mDeleteIncidencesOnClose )
70 close(); 70 close();
71} 71}
72bool CalendarLocal::mergeCalendarFile( QString name ) 72bool CalendarLocal::mergeCalendarFile( QString name )
73{ 73{
74 CalendarLocal calendar( timeZoneId() ); 74 CalendarLocal calendar( timeZoneId() );
75 calendar.setDefaultCalendar( 1 ); 75 calendar.setDefaultCalendar( 1 );
76 if ( calendar.load( name ) ) { 76 if ( calendar.load( name ) ) {
77 mergeCalendar( &calendar ); 77 mergeCalendar( &calendar );
78 return true; 78 return true;
79 } 79 }
80 return false; 80 return false;
81} 81}
82 82
83Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates) 83Incidence* CalendarLocal::incidenceForUid( const QString& uid , bool doNotCheckDuplicates)
84{ 84{
85 Todo *todo;; 85 Todo *todo;;
86 Incidence *retVal = 0; 86 Incidence *retVal = 0;
87 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 87 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
88 if ( todo->uid() == uid ) { 88 if ( todo->uid() == uid ) {
89 if ( doNotCheckDuplicates ) return todo; 89 if ( doNotCheckDuplicates ) return todo;
90 if ( retVal ) { 90 if ( retVal ) {
91 if ( retVal->calID() > todo->calID() ) { 91 if ( retVal->calID() > todo->calID() ) {
92 retVal = todo; 92 retVal = todo;
93 } 93 }
94 } else { 94 } else {
95 retVal = todo; 95 retVal = todo;
96 } 96 }
97 } 97 }
98 } 98 }
99 if ( retVal ) return retVal; 99 if ( retVal ) return retVal;
100 Event *event; 100 Event *event;
101 for ( event = mEventList.first(); event; event = mEventList.next() ) { 101 for ( event = mEventList.first(); event; event = mEventList.next() ) {
102 if ( event->uid() == uid ) { 102 if ( event->uid() == uid ) {
103 if ( doNotCheckDuplicates ) return event; 103 if ( doNotCheckDuplicates ) return event;
104 if ( retVal ) { 104 if ( retVal ) {
105 if ( retVal->calID() > event->calID() ) { 105 if ( retVal->calID() > event->calID() ) {
106 retVal = event; 106 retVal = event;
107 } 107 }
108 } else { 108 } else {
109 retVal = event; 109 retVal = event;
110 } 110 }
111 } 111 }
112 } 112 }
113 if ( retVal ) return retVal; 113 if ( retVal ) return retVal;
114 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() ) 114 for ( Journal *it = mJournalList.first(); it; it = mJournalList.next() )
115 if ( it->uid() == uid ) { 115 if ( it->uid() == uid ) {
116 if ( doNotCheckDuplicates ) return it; 116 if ( doNotCheckDuplicates ) return it;
117 if ( retVal ) { 117 if ( retVal ) {
118 if ( retVal->calID() > it->calID() ) { 118 if ( retVal->calID() > it->calID() ) {
119 retVal = it; 119 retVal = it;
120 } 120 }
121 } else { 121 } else {
122 retVal = it; 122 retVal = it;
123 } 123 }
124 } 124 }
125 return retVal; 125 return retVal;
126} 126}
127 127
128bool CalendarLocal::mergeCalendar( Calendar* remote ) 128bool CalendarLocal::mergeCalendar( Calendar* remote )
129{ 129{
130 // 1 look for raw inc in local 130 // 1 look for raw inc in local
131 // if inc not in remote, delete in local 131 // if inc not in remote, delete in local
132 // 2 look for raw inc in remote 132 // 2 look for raw inc in remote
133 // if inc in local, replace it 133 // if inc in local, replace it
134 // if not in local, add it to default calendar 134 // if not in local, add it to default calendar
135 QPtrList<Incidence> localInc = rawIncidences(); 135 QPtrList<Incidence> localInc = rawIncidences();
136 Incidence* inL = localInc.first(); 136 Incidence* inL = localInc.first();
137 while ( inL ) { 137 while ( inL ) {
138 if ( ! inL->isReadOnly () ) 138 if ( ! inL->isReadOnly () )
139 if ( !remote->incidenceForUid( inL->uid(), true )) 139 if ( !remote->incidenceForUid( inL->uid(), true ))
140 deleteIncidence( inL ); 140 deleteIncidence( inL );
141 inL = localInc.next(); 141 inL = localInc.next();
142 } 142 }
143 QPtrList<Incidence> er = remote->rawIncidences(); 143 QPtrList<Incidence> er = remote->rawIncidences();
144 Incidence* inR = er.first(); 144 Incidence* inR = er.first();
145 while ( inR ) { 145 while ( inR ) {
146 inL = incidenceForUid( inR->uid(),false ); 146 inL = incidenceForUid( inR->uid(),false );
147 if ( inL ) { 147 if ( inL ) {
148 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) { 148 if ( ! inL->isReadOnly () || inL->uid().left(15) == QString("last-syncEvent-") ) {
149 int calID = inL->calID(); 149 int calID = inL->calID();
150 deleteIncidence( inL ); 150 deleteIncidence( inL );
151 inL = inR->clone(); 151 inL = inR->clone();
152 inL->setCalID( calID ); 152 inL->setCalID( calID );
153 addIncidence( inL ); 153 addIncidence( inL );
154 } 154 }
155 } else { 155 } else {
156 inL = inR->clone(); 156 inL = inR->clone();
157 inL->setCalID( 0 );// add to default cal 157 inL->setCalID( 0 );// add to default cal
158 addIncidence( inL ); 158 addIncidence( inL );
159 } 159 }
160 inR = er.next(); 160 inR = er.next();
161 } 161 }
162 return true; 162 return true;
163} 163}
164 164
165 165
166bool CalendarLocal::addCalendarFile( QString name, int id ) 166bool CalendarLocal::addCalendarFile( QString name, int id )
167{ 167{
168 CalendarLocal calendar( timeZoneId() ); 168 CalendarLocal calendar( timeZoneId() );
169 calendar.setDefaultCalendar( id ); 169 calendar.setDefaultCalendar( id );
170 if ( calendar.load( name ) ) { 170 if ( calendar.load( name ) ) {
171 addCalendar( &calendar ); 171 addCalendar( &calendar );
172 return true; 172 return true;
173 } 173 }
174 return false; 174 return false;
175} 175}
176void CalendarLocal::setSyncEventsReadOnly() 176void CalendarLocal::setSyncEventsReadOnly()
177{ 177{
178 Event * ev; 178 Event * ev;
179 ev = mEventList.first(); 179 ev = mEventList.first();
180 while ( ev ) { 180 while ( ev ) {
181 if ( ev->uid().left(15) == QString("last-syncEvent-") ) 181 if ( ev->uid().left(15) == QString("last-syncEvent-") )
182 ev->setReadOnly( true ); 182 ev->setReadOnly( true );
183 ev = mEventList.next(); 183 ev = mEventList.next();
184 } 184 }
185} 185}
186void CalendarLocal::addCalendar( Calendar* cal ) 186void CalendarLocal::addCalendar( Calendar* cal )
187{ 187{
188 cal->setDontDeleteIncidencesOnClose(); 188 cal->setDontDeleteIncidencesOnClose();
189 { 189 {
190 QPtrList<Event> EventList = cal->rawEvents(); 190 QPtrList<Event> EventList = cal->rawEvents();
191 Event * ev = EventList.first(); 191 Event * ev = EventList.first();
192 while ( ev ) { 192 while ( ev ) {
193 ev->unRegisterObserver( cal ); 193 ev->unRegisterObserver( cal );
194 ev->registerObserver( this ); 194 ev->registerObserver( this );
195 mEventList.append( ev ); 195 mEventList.append( ev );
196 ev = EventList.next(); 196 ev = EventList.next();
197 } 197 }
198 } 198 }
199 { 199 {
200 200
201 QPtrList<Todo> TodoList = cal->rawTodos(); 201 QPtrList<Todo> TodoList = cal->rawTodos();
202 Todo * ev = TodoList.first(); 202 Todo * ev = TodoList.first();
203 while ( ev ) { 203 while ( ev ) {
204 QString rel = ev->relatedToUid(); 204 QString rel = ev->relatedToUid();
205 if ( !rel.isEmpty() ){ 205 if ( !rel.isEmpty() ){
206 ev->setRelatedTo ( 0 ); 206 ev->setRelatedTo ( 0 );
207 ev->setRelatedToUid( rel ); 207 ev->setRelatedToUid( rel );
208 } 208 }
209 ev = TodoList.next(); 209 ev = TodoList.next();
210 } 210 }
211 //TodoList = cal->rawTodos(); 211 //TodoList = cal->rawTodos();
212 ev = TodoList.first(); 212 ev = TodoList.first();
213 while ( ev ) { 213 while ( ev ) {
214 ev->unRegisterObserver( cal ); 214 ev->unRegisterObserver( cal );
215 ev->registerObserver( this ); 215 ev->registerObserver( this );
216 mTodoList.append( ev ); 216 mTodoList.append( ev );
217 setupRelations( ev ); 217 setupRelations( ev );
218 ev = TodoList.next(); 218 ev = TodoList.next();
219 } 219 }
220 } 220 }
221 { 221 {
222 QPtrList<Journal> JournalList = cal->journals(); 222 QPtrList<Journal> JournalList = cal->journals();
223 Journal * ev = JournalList.first(); 223 Journal * ev = JournalList.first();
224 while ( ev ) { 224 while ( ev ) {
225 ev->unRegisterObserver( cal ); 225 ev->unRegisterObserver( cal );
226 ev->registerObserver( this ); 226 ev->registerObserver( this );
227 mJournalList.append( ev ); 227 mJournalList.append( ev );
228 ev = JournalList.next(); 228 ev = JournalList.next();
229 } 229 }
230 } 230 }
231 setModified( true ); 231 setModified( true );
232} 232}
233bool CalendarLocal::load( const QString &fileName ) 233bool CalendarLocal::load( const QString &fileName )
234{ 234{
235 FileStorage storage( this, fileName ); 235 FileStorage storage( this, fileName );
236 return storage.load(); 236 return storage.load();
237} 237}
238 238
239bool CalendarLocal::save( const QString &fileName, CalFormat *format ) 239bool CalendarLocal::save( const QString &fileName, CalFormat *format )
240{ 240{
241 FileStorage storage( this, fileName, format ); 241 FileStorage storage( this, fileName, format );
242 return storage.save(); 242 return storage.save();
243} 243}
244 244
245void CalendarLocal::stopAllTodos() 245void CalendarLocal::stopAllTodos()
246{ 246{
247 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 247 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
248 it->setRunning( false ); 248 it->setRunning( false );
249 249
250} 250}
251void CalendarLocal::close() 251void CalendarLocal::close()
252{ 252{
253 253
254 Todo * i; 254 Todo * i;
255 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false); 255 for( i = mTodoList.first(); i; i = mTodoList.next() ) i->setRunning(false);
256 256
257 mEventList.setAutoDelete( true ); 257 mEventList.setAutoDelete( true );
258 mTodoList.setAutoDelete( true ); 258 mTodoList.setAutoDelete( true );
259 mJournalList.setAutoDelete( false ); 259 mJournalList.setAutoDelete( true );
260 260
261 mEventList.clear(); 261 mEventList.clear();
262 mTodoList.clear(); 262 mTodoList.clear();
263 mJournalList.clear(); 263 mJournalList.clear();
264 264
265 mEventList.setAutoDelete( false ); 265 mEventList.setAutoDelete( false );
266 mTodoList.setAutoDelete( false ); 266 mTodoList.setAutoDelete( false );
267 mJournalList.setAutoDelete( false ); 267 mJournalList.setAutoDelete( false );
268 268
269 setModified( false ); 269 setModified( false );
270} 270}
271 271
272bool CalendarLocal::addAnniversaryNoDup( Event *event ) 272bool CalendarLocal::addAnniversaryNoDup( Event *event )
273{ 273{
274 QString cat; 274 QString cat;
275 bool isBirthday = true; 275 bool isBirthday = true;
276 if( event->categoriesStr() == i18n( "Anniversary" ) ) { 276 if( event->categoriesStr() == i18n( "Anniversary" ) ) {
277 isBirthday = false; 277 isBirthday = false;
278 cat = i18n( "Anniversary" ); 278 cat = i18n( "Anniversary" );
279 } else if( event->categoriesStr() == i18n( "Birthday" ) ) { 279 } else if( event->categoriesStr() == i18n( "Birthday" ) ) {
280 isBirthday = true; 280 isBirthday = true;
281 cat = i18n( "Birthday" ); 281 cat = i18n( "Birthday" );
282 } else { 282 } else {
283 qDebug("addAnniversaryNoDup called without fitting category! "); 283 qDebug("addAnniversaryNoDup called without fitting category! ");
284 return false; 284 return false;
285 } 285 }
286 Event * eve; 286 Event * eve;
287 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 287 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
288 if ( !(eve->categories().contains( cat ) )) 288 if ( !(eve->categories().contains( cat ) ))
289 continue; 289 continue;
290 // now we have an event with fitting category 290 // now we have an event with fitting category
291 if ( eve->dtStart().date() != event->dtStart().date() ) 291 if ( eve->dtStart().date() != event->dtStart().date() )
292 continue; 292 continue;
293 // now we have an event with fitting category+date 293 // now we have an event with fitting category+date
294 if ( eve->summary() != event->summary() ) 294 if ( eve->summary() != event->summary() )
295 continue; 295 continue;
296 // now we have an event with fitting category+date+summary 296 // now we have an event with fitting category+date+summary
297 return false; 297 return false;
298 } 298 }
299 return addEvent( event ); 299 return addEvent( event );
300 300
301} 301}
302bool CalendarLocal::addEventNoDup( Event *event ) 302bool CalendarLocal::addEventNoDup( Event *event )
303{ 303{
304 Event * eve; 304 Event * eve;
305 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) { 305 for ( eve = mEventList.first(); eve ; eve = mEventList.next() ) {
306 if ( *eve == *event ) { 306 if ( *eve == *event ) {
307 //qDebug("CalendarLocal::Duplicate event found! Not inserted! "); 307 //qDebug("CalendarLocal::Duplicate event found! Not inserted! ");
308 return false; 308 return false;
309 } 309 }
310 } 310 }
311 return addEvent( event ); 311 return addEvent( event );
312} 312}
313 313
314bool CalendarLocal::addEvent( Event *event ) 314bool CalendarLocal::addEvent( Event *event )
315{ 315{
316 insertEvent( event ); 316 insertEvent( event );
317 317
318 event->registerObserver( this ); 318 event->registerObserver( this );
319 319
320 setModified( true ); 320 setModified( true );
321 if ( event->calID() == 0 ) 321 if ( event->calID() == 0 )
322 event->setCalID( mDefaultCalendar ); 322 event->setCalID( mDefaultCalendar );
323 event->setCalEnabled( true ); 323 event->setCalEnabled( true );
324 324
325 return true; 325 return true;
326} 326}
327 327
328void CalendarLocal::deleteEvent( Event *event ) 328void CalendarLocal::deleteEvent( Event *event )
329{ 329{
330 if ( mUndoIncidence ) delete mUndoIncidence; 330 if ( mUndoIncidence ) delete mUndoIncidence;
331 mUndoIncidence = event->clone(); 331 mUndoIncidence = event->clone();
332 if ( mEventList.removeRef( event ) ) { 332 if ( mEventList.removeRef( event ) ) {
333 setModified( true ); 333 setModified( true );
334 } 334 }
335} 335}
336 336
337 337
338Event *CalendarLocal::event( const QString &uid ) 338Event *CalendarLocal::event( const QString &uid )
339{ 339{
340 Event *event; 340 Event *event;
341 Event *retVal = 0; 341 Event *retVal = 0;
342 for ( event = mEventList.first(); event; event = mEventList.next() ) { 342 for ( event = mEventList.first(); event; event = mEventList.next() ) {
343 if ( event->calEnabled() && event->uid() == uid ) { 343 if ( event->calEnabled() && event->uid() == uid ) {
344 if ( retVal ) { 344 if ( retVal ) {
345 if ( retVal->calID() > event->calID() ) { 345 if ( retVal->calID() > event->calID() ) {
346 retVal = event; 346 retVal = event;
347 } 347 }
348 } else { 348 } else {
349 retVal = event; 349 retVal = event;
350 } 350 }
351 } 351 }
352 } 352 }
353 return retVal; 353 return retVal;
354} 354}
355bool CalendarLocal::addTodoNoDup( Todo *todo ) 355bool CalendarLocal::addTodoNoDup( Todo *todo )
356{ 356{
357 Todo * eve; 357 Todo * eve;
358 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) { 358 for ( eve = mTodoList.first(); eve ; eve = mTodoList.next() ) {
359 if ( *eve == *todo ) { 359 if ( *eve == *todo ) {
360 //qDebug("duplicate todo found! not inserted! "); 360 //qDebug("duplicate todo found! not inserted! ");
361 return false; 361 return false;
362 } 362 }
363 } 363 }
364 return addTodo( todo ); 364 return addTodo( todo );
365} 365}
366bool CalendarLocal::addTodo( Todo *todo ) 366bool CalendarLocal::addTodo( Todo *todo )
367{ 367{
368 mTodoList.append( todo ); 368 mTodoList.append( todo );
369 369
370 todo->registerObserver( this ); 370 todo->registerObserver( this );
371 371
372 // Set up subtask relations 372 // Set up subtask relations
373 setupRelations( todo ); 373 setupRelations( todo );
374 374
375 setModified( true ); 375 setModified( true );
376 if ( todo->calID() == 0 ) 376 if ( todo->calID() == 0 )
377 todo->setCalID( mDefaultCalendar ); 377 todo->setCalID( mDefaultCalendar );
378 todo->setCalEnabled( true ); 378 todo->setCalEnabled( true );
379 return true; 379 return true;
380} 380}
381 381
382void CalendarLocal::deleteTodo( Todo *todo ) 382void CalendarLocal::deleteTodo( Todo *todo )
383{ 383{
384 // Handle orphaned children 384 // Handle orphaned children
385 if ( mUndoIncidence ) delete mUndoIncidence; 385 if ( mUndoIncidence ) delete mUndoIncidence;
386 removeRelations( todo ); 386 removeRelations( todo );
387 mUndoIncidence = todo->clone(); 387 mUndoIncidence = todo->clone();
388 388
389 if ( mTodoList.removeRef( todo ) ) { 389 if ( mTodoList.removeRef( todo ) ) {
390 setModified( true ); 390 setModified( true );
391 } 391 }
392} 392}
393 393
394QPtrList<Todo> CalendarLocal::rawTodos() 394QPtrList<Todo> CalendarLocal::rawTodos()
395{ 395{
396 QPtrList<Todo> el; 396 QPtrList<Todo> el;
397 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() ) 397 for ( Todo *it = mTodoList.first(); it; it = mTodoList.next() )
398 if ( it->calEnabled() ) el.append( it ); 398 if ( it->calEnabled() ) el.append( it );
399 return el; 399 return el;
400} 400}
401Todo *CalendarLocal::todo( QString syncProf, QString id ) 401Todo *CalendarLocal::todo( QString syncProf, QString id )
402{ 402{
403 Todo *todo; 403 Todo *todo;
404 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 404 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
405 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 405 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
406 } 406 }
407 407
408 return 0; 408 return 0;
409} 409}
410void CalendarLocal::removeSyncInfo( QString syncProfile) 410void CalendarLocal::removeSyncInfo( QString syncProfile)
411{ 411{
412 QPtrList<Incidence> all = rawIncidences() ; 412 QPtrList<Incidence> all = rawIncidences() ;
413 Incidence *inc; 413 Incidence *inc;
414 for ( inc = all.first(); inc; inc = all.next() ) { 414 for ( inc = all.first(); inc; inc = all.next() ) {
415 inc->removeID( syncProfile ); 415 inc->removeID( syncProfile );
416 } 416 }
417 if ( syncProfile.isEmpty() ) { 417 if ( syncProfile.isEmpty() ) {
418 QPtrList<Event> el; 418 QPtrList<Event> el;
419 Event *todo; 419 Event *todo;
420 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 420 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
421 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 421 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
422 el.append( todo ); 422 el.append( todo );
423 } 423 }
424 for ( todo = el.first(); todo; todo = el.next() ) { 424 for ( todo = el.first(); todo; todo = el.next() ) {
425 deleteIncidence ( todo ); 425 deleteIncidence ( todo );
426 } 426 }
427 } else { 427 } else {
428 Event *lse = event( "last-syncEvent-"+ syncProfile); 428 Event *lse = event( "last-syncEvent-"+ syncProfile);
429 if ( lse ) 429 if ( lse )
430 deleteIncidence ( lse ); 430 deleteIncidence ( lse );
431 } 431 }
432} 432}
433QPtrList<Event> CalendarLocal::getExternLastSyncEvents() 433QPtrList<Event> CalendarLocal::getExternLastSyncEvents()
434{ 434{
435 QPtrList<Event> el; 435 QPtrList<Event> el;
436 Event *todo; 436 Event *todo;
437 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 437 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
438 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") ) 438 if ( todo->uid().left( 15 ) == QString("last-syncEvent-") )
439 if ( todo->summary().left(3) == "E: " ) 439 if ( todo->summary().left(3) == "E: " )
440 el.append( todo ); 440 el.append( todo );
441 } 441 }
442 442
443 return el; 443 return el;
444 444
445} 445}
446Event *CalendarLocal::event( QString syncProf, QString id ) 446Event *CalendarLocal::event( QString syncProf, QString id )
447{ 447{
448 Event *todo; 448 Event *todo;
449 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) { 449 for ( todo = mEventList.first(); todo; todo = mEventList.next() ) {
450 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo; 450 if ( todo->calEnabled() && todo->getID( syncProf ) == id ) return todo;
451 } 451 }
452 452
453 return 0; 453 return 0;
454} 454}
455Todo *CalendarLocal::todo( const QString &uid ) 455Todo *CalendarLocal::todo( const QString &uid )
456{ 456{
457 Todo *todo;; 457 Todo *todo;;
458 Todo *retVal = 0; 458 Todo *retVal = 0;
459 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 459 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
460 if ( todo->calEnabled() && todo->uid() == uid ) { 460 if ( todo->calEnabled() && todo->uid() == uid ) {
461 if ( retVal ) { 461 if ( retVal ) {
462 if ( retVal->calID() > todo->calID() ) { 462 if ( retVal->calID() > todo->calID() ) {
463 retVal = todo; 463 retVal = todo;
464 } 464 }
465 } else { 465 } else {
466 retVal = todo; 466 retVal = todo;
467 } 467 }
468 } 468 }
469 } 469 }
470 return retVal; 470 return retVal;
471} 471}
472QString CalendarLocal::nextSummary() const 472QString CalendarLocal::nextSummary() const
473{ 473{
474 return mNextSummary; 474 return mNextSummary;
475} 475}
476QDateTime CalendarLocal::nextAlarmEventDateTime() const 476QDateTime CalendarLocal::nextAlarmEventDateTime() const
477{ 477{
478 return mNextAlarmEventDateTime; 478 return mNextAlarmEventDateTime;
479} 479}
480void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted) 480void CalendarLocal::checkAlarmForIncidence( Incidence * incidence, bool deleted)
481{ 481{
482 //mNextAlarmIncidence 482 //mNextAlarmIncidence
483 //mNextAlarmDateTime 483 //mNextAlarmDateTime
484 //return mNextSummary; 484 //return mNextSummary;
485 //return mNextAlarmEventDateTime; 485 //return mNextAlarmEventDateTime;
486 bool newNextAlarm = false; 486 bool newNextAlarm = false;
487 bool computeNextAlarm = false; 487 bool computeNextAlarm = false;
488 bool ok; 488 bool ok;
489 int offset; 489 int offset;
490 QDateTime nextA; 490 QDateTime nextA;
491 // QString nextSum; 491 // QString nextSum;
492 //QDateTime nextEvent; 492 //QDateTime nextEvent;
493 if ( mNextAlarmIncidence == 0 || incidence == 0 ) { 493 if ( mNextAlarmIncidence == 0 || incidence == 0 ) {
494 computeNextAlarm = true; 494 computeNextAlarm = true;
495 } else { 495 } else {
496 if ( ! deleted ) { 496 if ( ! deleted ) {
497 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 497 nextA = incidence->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
498 if ( ok ) { 498 if ( ok ) {
499 if ( nextA < mNextAlarmDateTime ) { 499 if ( nextA < mNextAlarmDateTime ) {
500 deRegisterAlarm(); 500 deRegisterAlarm();
501 mNextAlarmDateTime = nextA; 501 mNextAlarmDateTime = nextA;
502 mNextSummary = incidence->summary(); 502 mNextSummary = incidence->summary();
503 mNextAlarmEventDateTime = nextA.addSecs(offset ) ; 503 mNextAlarmEventDateTime = nextA.addSecs(offset ) ;
504 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 504 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
505 newNextAlarm = true; 505 newNextAlarm = true;
506 mNextAlarmIncidence = incidence; 506 mNextAlarmIncidence = incidence;
507 } else { 507 } else {
508 if ( incidence == mNextAlarmIncidence ) { 508 if ( incidence == mNextAlarmIncidence ) {
509 computeNextAlarm = true; 509 computeNextAlarm = true;
510 } 510 }
511 } 511 }
512 } else { 512 } else {
513 if ( mNextAlarmIncidence == incidence ) { 513 if ( mNextAlarmIncidence == incidence ) {
514 computeNextAlarm = true; 514 computeNextAlarm = true;
515 } 515 }
516 } 516 }
517 } else { // deleted 517 } else { // deleted
518 if ( incidence == mNextAlarmIncidence ) { 518 if ( incidence == mNextAlarmIncidence ) {
519 computeNextAlarm = true; 519 computeNextAlarm = true;
520 } 520 }
521 } 521 }
522 } 522 }
523 if ( computeNextAlarm ) { 523 if ( computeNextAlarm ) {
524 deRegisterAlarm(); 524 deRegisterAlarm();
525 nextA = nextAlarm( 1000 ); 525 nextA = nextAlarm( 1000 );
526 if (! mNextAlarmIncidence ) { 526 if (! mNextAlarmIncidence ) {
527 return; 527 return;
528 } 528 }
529 newNextAlarm = true; 529 newNextAlarm = true;
530 } 530 }
531 if ( newNextAlarm ) 531 if ( newNextAlarm )
532 registerAlarm(); 532 registerAlarm();
533} 533}
534QString CalendarLocal:: getAlarmNotification() 534QString CalendarLocal:: getAlarmNotification()
535{ 535{
536 QString ret; 536 QString ret;
537 // this should not happen 537 // this should not happen
538 if (! mNextAlarmIncidence ) 538 if (! mNextAlarmIncidence )
539 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString; 539 return "cal_alarm"+ mNextSummary.left( 25 )+"\n"+mNextAlarmEventDateTimeString;
540 Alarm* alarm = mNextAlarmIncidence->alarms().first(); 540 Alarm* alarm = mNextAlarmIncidence->alarms().first();
541 if ( alarm->type() == Alarm::Procedure ) { 541 if ( alarm->type() == Alarm::Procedure ) {
542 ret = "proc_alarm" + alarm->programFile()+"+++"; 542 ret = "proc_alarm" + alarm->programFile()+"+++";
543 } else { 543 } else {
544 ret = "audio_alarm" +alarm->audioFile() +"+++"; 544 ret = "audio_alarm" +alarm->audioFile() +"+++";
545 } 545 }
546 ret += "cal_alarm"+ mNextSummary.left( 25 ); 546 ret += "cal_alarm"+ mNextSummary.left( 25 );
547 if ( mNextSummary.length() > 25 ) 547 if ( mNextSummary.length() > 25 )
548 ret += "\n" + mNextSummary.mid(25, 25 ); 548 ret += "\n" + mNextSummary.mid(25, 25 );
549 ret+= "\n"+mNextAlarmEventDateTimeString; 549 ret+= "\n"+mNextAlarmEventDateTimeString;
550 return ret; 550 return ret;
551} 551}
552void CalendarLocal::registerAlarm() 552void CalendarLocal::registerAlarm()
553{ 553{
554 mLastAlarmNotificationString = getAlarmNotification(); 554 mLastAlarmNotificationString = getAlarmNotification();
555 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() ); 555 // qDebug("++ register Alarm %s %s",mNextAlarmDateTime.toString().latin1(), mLastAlarmNotificationString.latin1() );
556 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 556 emit addAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
557// #ifndef DESKTOP_VERSION 557// #ifndef DESKTOP_VERSION
558// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() ); 558// AlarmServer::addAlarm ( mNextAlarmDateTime,"koalarm", mLastAlarmNotificationString.latin1() );
559// #endif 559// #endif
560} 560}
561void CalendarLocal::deRegisterAlarm() 561void CalendarLocal::deRegisterAlarm()
562{ 562{
563 if ( mLastAlarmNotificationString.isNull() ) 563 if ( mLastAlarmNotificationString.isNull() )
564 return; 564 return;
565 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() ); 565 //qDebug("-- deregister Alarm %s ", mLastAlarmNotificationString.latin1() );
566 566
567 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString ); 567 emit removeAlarm ( mNextAlarmDateTime, mLastAlarmNotificationString );
568 mNextAlarmEventDateTime = QDateTime(); 568 mNextAlarmEventDateTime = QDateTime();
569// #ifndef DESKTOP_VERSION 569// #ifndef DESKTOP_VERSION
570// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() ); 570// AlarmServer::deleteAlarm (mNextAlarmDateTime ,"koalarm" ,mLastAlarmNotificationString.latin1() );
571// #endif 571// #endif
572} 572}
573 573
574QPtrList<Todo> CalendarLocal::todos( const QDate &date ) 574QPtrList<Todo> CalendarLocal::todos( const QDate &date )
575{ 575{
576 QPtrList<Todo> todos; 576 QPtrList<Todo> todos;
577 577
578 Todo *todo; 578 Todo *todo;
579 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) { 579 for ( todo = mTodoList.first(); todo; todo = mTodoList.next() ) {
580 if ( !todo->calEnabled() ) continue; 580 if ( !todo->calEnabled() ) continue;
581 if ( todo->hasDueDate() && todo->dtDue().date() == date ) { 581 if ( todo->hasDueDate() && todo->dtDue().date() == date ) {
582 todos.append( todo ); 582 todos.append( todo );
583 } 583 }
584 } 584 }
585 585
586 filter()->apply( &todos ); 586 filter()->apply( &todos );
587 return todos; 587 return todos;
588} 588}
589void CalendarLocal::reInitAlarmSettings() 589void CalendarLocal::reInitAlarmSettings()
590{ 590{
591 if ( !mNextAlarmIncidence ) { 591 if ( !mNextAlarmIncidence ) {
592 nextAlarm( 1000 ); 592 nextAlarm( 1000 );
593 } 593 }
594 deRegisterAlarm(); 594 deRegisterAlarm();
595 mNextAlarmIncidence = 0; 595 mNextAlarmIncidence = 0;
596 checkAlarmForIncidence( 0, false ); 596 checkAlarmForIncidence( 0, false );
597 597
598} 598}
599 599
600 600
601 601
602QDateTime CalendarLocal::nextAlarm( int daysTo ) 602QDateTime CalendarLocal::nextAlarm( int daysTo )
603{ 603{
604 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo ); 604 QDateTime nextA = QDateTime::currentDateTime().addDays( daysTo );
605 QDateTime start = QDateTime::currentDateTime().addSecs( 30 ); 605 QDateTime start = QDateTime::currentDateTime().addSecs( 30 );
606 QDateTime next; 606 QDateTime next;
607 Event *e; 607 Event *e;
608 bool ok; 608 bool ok;
609 bool found = false; 609 bool found = false;
610 int offset; 610 int offset;
611 mNextAlarmIncidence = 0; 611 mNextAlarmIncidence = 0;
612 for( e = mEventList.first(); e; e = mEventList.next() ) { 612 for( e = mEventList.first(); e; e = mEventList.next() ) {
613 if ( !e->calEnabled() ) continue; 613 if ( !e->calEnabled() ) continue;
614 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 614 next = e->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
615 if ( ok ) { 615 if ( ok ) {
616 if ( next < nextA ) { 616 if ( next < nextA ) {
617 nextA = next; 617 nextA = next;
618 found = true; 618 found = true;
619 mNextSummary = e->summary(); 619 mNextSummary = e->summary();
620 mNextAlarmEventDateTime = next.addSecs(offset ) ; 620 mNextAlarmEventDateTime = next.addSecs(offset ) ;
621 mNextAlarmIncidence = (Incidence *) e; 621 mNextAlarmIncidence = (Incidence *) e;
622 } 622 }
623 } 623 }
624 } 624 }
625 Todo *t; 625 Todo *t;
626 for( t = mTodoList.first(); t; t = mTodoList.next() ) { 626 for( t = mTodoList.first(); t; t = mTodoList.next() ) {
627 if ( !t->calEnabled() ) continue; 627 if ( !t->calEnabled() ) continue;
628 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ; 628 next = t->getNextAlarmDateTime(& ok, &offset, QDateTime::currentDateTime() ) ;
629 if ( ok ) { 629 if ( ok ) {
630 if ( next < nextA ) { 630 if ( next < nextA ) {
631 nextA = next; 631 nextA = next;
632 found = true; 632 found = true;
633 mNextSummary = t->summary(); 633 mNextSummary = t->summary();
634 mNextAlarmEventDateTime = next.addSecs(offset ); 634 mNextAlarmEventDateTime = next.addSecs(offset );
635 mNextAlarmIncidence = (Incidence *) t; 635 mNextAlarmIncidence = (Incidence *) t;
636 } 636 }
637 } 637 }
638 } 638 }
639 if ( mNextAlarmIncidence ) { 639 if ( mNextAlarmIncidence ) {
640 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime); 640 mNextAlarmEventDateTimeString = KGlobal::locale()->formatDateTime(mNextAlarmEventDateTime);
641 mNextAlarmDateTime = nextA; 641 mNextAlarmDateTime = nextA;
642 } 642 }
643 return nextA; 643 return nextA;