summaryrefslogtreecommitdiffabout
path: root/libkcal/calendar.cpp
Unidiff
Diffstat (limited to 'libkcal/calendar.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/calendar.cpp14
1 files changed, 11 insertions, 3 deletions
diff --git a/libkcal/calendar.cpp b/libkcal/calendar.cpp
index 7e8e2c5..f4350d9 100644
--- a/libkcal/calendar.cpp
+++ b/libkcal/calendar.cpp
@@ -1,179 +1,186 @@
1/* 1/*
2 This file is part of libkcal. 2 This file is part of libkcal.
3 Copyright (c) 1998 Preston Brown 3 Copyright (c) 1998 Preston Brown
4 Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org> 4 Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org>
5 5
6 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public 7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either 8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version. 9 version 2 of the License, or (at your option) any later version.
10 10
11 This library is distributed in the hope that it will be useful, 11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details. 14 Library General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public License 16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to 17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. 19 Boston, MA 02111-1307, USA.
20*/ 20*/
21 21
22#include <stdlib.h> 22#include <stdlib.h>
23#include <time.h> 23#include <time.h>
24 24
25#include <kdebug.h> 25#include <kdebug.h>
26#include <kglobal.h> 26#include <kglobal.h>
27#include <klocale.h> 27#include <klocale.h>
28 28
29#include "exceptions.h" 29#include "exceptions.h"
30#include "calfilter.h" 30#include "calfilter.h"
31 31
32#include "calendar.h" 32#include "calendar.h"
33#include "syncdefines.h" 33#include "syncdefines.h"
34 34
35using namespace KCal; 35using namespace KCal;
36 36
37Calendar::Calendar() 37Calendar::Calendar()
38{ 38{
39 39
40 init(); 40 init();
41 setTimeZoneId( " 00:00 Europe/London(UTC)" ); 41 setTimeZoneId( " 00:00 Europe/London(UTC)" );
42} 42}
43 43
44Calendar::Calendar( const QString &timeZoneId ) 44Calendar::Calendar( const QString &timeZoneId )
45{ 45{
46 46
47 init(); 47 init();
48 setTimeZoneId(timeZoneId); 48 setTimeZoneId(timeZoneId);
49} 49}
50 50
51void Calendar::init() 51void Calendar::init()
52{ 52{
53 mObserver = 0; 53 mObserver = 0;
54 mNewObserver = false; 54 mNewObserver = false;
55 mUndoIncidence = 0; 55 mUndoIncidence = 0;
56 mModified = false; 56 mModified = false;
57 57 mDefaultCalendar = 1;
58 // Setup default filter, which does nothing 58 // Setup default filter, which does nothing
59 mDefaultFilter = new CalFilter; 59 mDefaultFilter = new CalFilter;
60 mFilter = mDefaultFilter; 60 mFilter = mDefaultFilter;
61 mFilter->setEnabled(false); 61 mFilter->setEnabled(false);
62 62
63 // initialize random numbers. This is a hack, and not 63 // initialize random numbers. This is a hack, and not
64 // even that good of one at that. 64 // even that good of one at that.
65// srandom(time(0)); 65// srandom(time(0));
66 66
67 // user information... 67 // user information...
68 setOwner(i18n("Unknown Name")); 68 setOwner(i18n("Unknown Name"));
69 setEmail(i18n("unknown@nowhere")); 69 setEmail(i18n("unknown@nowhere"));
70 70
71#if 0 71#if 0
72 tmpStr = KOPrefs::instance()->mTimeZone; 72 tmpStr = KOPrefs::instance()->mTimeZone;
73// kdDebug(5800) << "Calendar::Calendar(): TimeZone: " << tmpStr << endl; 73// kdDebug(5800) << "Calendar::Calendar(): TimeZone: " << tmpStr << endl;
74 int dstSetting = KOPrefs::instance()->mDaylightSavings; 74 int dstSetting = KOPrefs::instance()->mDaylightSavings;
75 extern long int timezone; 75 extern long int timezone;
76 struct tm *now; 76 struct tm *now;
77 time_t curtime; 77 time_t curtime;
78 curtime = time(0); 78 curtime = time(0);
79 now = localtime(&curtime); 79 now = localtime(&curtime);
80 int hourOff = - ((timezone / 60) / 60); 80 int hourOff = - ((timezone / 60) / 60);
81 if (now->tm_isdst) 81 if (now->tm_isdst)
82 hourOff += 1; 82 hourOff += 1;
83 QString tzStr; 83 QString tzStr;
84 tzStr.sprintf("%.2d%.2d", 84 tzStr.sprintf("%.2d%.2d",
85 hourOff, 85 hourOff,
86 abs((timezone / 60) % 60)); 86 abs((timezone / 60) % 60));
87 87
88 // if no time zone was in the config file, write what we just discovered. 88 // if no time zone was in the config file, write what we just discovered.
89 if (tmpStr.isEmpty()) { 89 if (tmpStr.isEmpty()) {
90// KOPrefs::instance()->mTimeZone = tzStr; 90// KOPrefs::instance()->mTimeZone = tzStr;
91 } else { 91 } else {
92 tzStr = tmpStr; 92 tzStr = tmpStr;
93 } 93 }
94 94
95 // if daylight savings has changed since last load time, we need 95 // if daylight savings has changed since last load time, we need
96 // to rewrite these settings to the config file. 96 // to rewrite these settings to the config file.
97 if ((now->tm_isdst && !dstSetting) || 97 if ((now->tm_isdst && !dstSetting) ||
98 (!now->tm_isdst && dstSetting)) { 98 (!now->tm_isdst && dstSetting)) {
99 KOPrefs::instance()->mTimeZone = tzStr; 99 KOPrefs::instance()->mTimeZone = tzStr;
100 KOPrefs::instance()->mDaylightSavings = now->tm_isdst; 100 KOPrefs::instance()->mDaylightSavings = now->tm_isdst;
101 } 101 }
102 102
103 setTimeZone(tzStr); 103 setTimeZone(tzStr);
104#endif 104#endif
105 105
106// KOPrefs::instance()->writeConfig(); 106// KOPrefs::instance()->writeConfig();
107} 107}
108 108
109Calendar::~Calendar() 109Calendar::~Calendar()
110{ 110{
111 delete mDefaultFilter; 111 delete mDefaultFilter;
112 if ( mUndoIncidence ) 112 if ( mUndoIncidence )
113 delete mUndoIncidence; 113 delete mUndoIncidence;
114} 114}
115 115void Calendar::setDefaultCalendar( int d )
116{
117 mDefaultCalendar = d;
118}
119int Calendar::defaultCalendar()
120{
121 return mDefaultCalendar;
122}
116const QString &Calendar::getOwner() const 123const QString &Calendar::getOwner() const
117{ 124{
118 return mOwner; 125 return mOwner;
119} 126}
120 127
121bool Calendar::undoDeleteIncidence() 128bool Calendar::undoDeleteIncidence()
122{ 129{
123 if (!mUndoIncidence) 130 if (!mUndoIncidence)
124 return false; 131 return false;
125 addIncidence(mUndoIncidence); 132 addIncidence(mUndoIncidence);
126 mUndoIncidence = 0; 133 mUndoIncidence = 0;
127 return true; 134 return true;
128} 135}
129void Calendar::setOwner(const QString &os) 136void Calendar::setOwner(const QString &os)
130{ 137{
131 int i; 138 int i;
132 mOwner = os; 139 mOwner = os;
133 i = mOwner.find(','); 140 i = mOwner.find(',');
134 if (i != -1) 141 if (i != -1)
135 mOwner = mOwner.left(i); 142 mOwner = mOwner.left(i);
136 143
137 setModified( true ); 144 setModified( true );
138} 145}
139 146
140void Calendar::setTimeZone(const QString & tz) 147void Calendar::setTimeZone(const QString & tz)
141{ 148{
142 bool neg = FALSE; 149 bool neg = FALSE;
143 int hours, minutes; 150 int hours, minutes;
144 QString tmpStr(tz); 151 QString tmpStr(tz);
145 152
146 if (tmpStr.left(1) == "-") 153 if (tmpStr.left(1) == "-")
147 neg = TRUE; 154 neg = TRUE;
148 if (tmpStr.left(1) == "-" || tmpStr.left(1) == "+") 155 if (tmpStr.left(1) == "-" || tmpStr.left(1) == "+")
149 tmpStr.remove(0, 1); 156 tmpStr.remove(0, 1);
150 hours = tmpStr.left(2).toInt(); 157 hours = tmpStr.left(2).toInt();
151 if (tmpStr.length() > 2) 158 if (tmpStr.length() > 2)
152 minutes = tmpStr.right(2).toInt(); 159 minutes = tmpStr.right(2).toInt();
153 else 160 else
154 minutes = 0; 161 minutes = 0;
155 mTimeZone = (60*hours+minutes); 162 mTimeZone = (60*hours+minutes);
156 if (neg) 163 if (neg)
157 mTimeZone = -mTimeZone; 164 mTimeZone = -mTimeZone;
158 mLocalTime = false; 165 mLocalTime = false;
159 166
160 setModified( true ); 167 setModified( true );
161} 168}
162 169
163QString Calendar::getTimeZoneStr() const 170QString Calendar::getTimeZoneStr() const
164{ 171{
165 if (mLocalTime) 172 if (mLocalTime)
166 return ""; 173 return "";
167 QString tmpStr; 174 QString tmpStr;
168 int hours = abs(mTimeZone / 60); 175 int hours = abs(mTimeZone / 60);
169 int minutes = abs(mTimeZone % 60); 176 int minutes = abs(mTimeZone % 60);
170 bool neg = mTimeZone < 0; 177 bool neg = mTimeZone < 0;
171 178
172 tmpStr.sprintf("%c%.2d%.2d", 179 tmpStr.sprintf("%c%.2d%.2d",
173 (neg ? '-' : '+'), 180 (neg ? '-' : '+'),
174 hours, minutes); 181 hours, minutes);
175 return tmpStr; 182 return tmpStr;
176} 183}
177 184
178void Calendar::setTimeZone(int tz) 185void Calendar::setTimeZone(int tz)
179{ 186{
@@ -287,129 +294,130 @@ void Calendar::resetTempSyncStat()
287 for( i = t.first(); i; i = t.next() ) i->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); 294 for( i = t.first(); i; i = t.next() ) i->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
288 295
289 QPtrList<Journal> j = journals(); 296 QPtrList<Journal> j = journals();
290 for( i = j.first(); i; i = j.next() ) i->setTempSyncStat( SYNC_TEMPSTATE_INITIAL ); 297 for( i = j.first(); i; i = j.next() ) i->setTempSyncStat( SYNC_TEMPSTATE_INITIAL );
291} 298}
292QPtrList<Incidence> Calendar::rawIncidences() 299QPtrList<Incidence> Calendar::rawIncidences()
293{ 300{
294 QPtrList<Incidence> incidences; 301 QPtrList<Incidence> incidences;
295 302
296 Incidence *i; 303 Incidence *i;
297 304
298 QPtrList<Event> e = rawEvents(); 305 QPtrList<Event> e = rawEvents();
299 for( i = e.first(); i; i = e.next() ) incidences.append( i ); 306 for( i = e.first(); i; i = e.next() ) incidences.append( i );
300 307
301 QPtrList<Todo> t = rawTodos(); 308 QPtrList<Todo> t = rawTodos();
302 for( i = t.first(); i; i = t.next() ) incidences.append( i ); 309 for( i = t.first(); i; i = t.next() ) incidences.append( i );
303 310
304 QPtrList<Journal> j = journals(); 311 QPtrList<Journal> j = journals();
305 for( i = j.first(); i; i = j.next() ) incidences.append( i ); 312 for( i = j.first(); i; i = j.next() ) incidences.append( i );
306 313
307 return incidences; 314 return incidences;
308} 315}
309 316
310QPtrList<Event> Calendar::events( const QDate &date, bool sorted ) 317QPtrList<Event> Calendar::events( const QDate &date, bool sorted )
311{ 318{
312 QPtrList<Event> el = rawEventsForDate(date,sorted); 319 QPtrList<Event> el = rawEventsForDate(date,sorted);
313 mFilter->apply(&el); 320 mFilter->apply(&el);
314 return el; 321 return el;
315} 322}
316 323
317QPtrList<Event> Calendar::events( const QDateTime &qdt ) 324QPtrList<Event> Calendar::events( const QDateTime &qdt )
318{ 325{
319 QPtrList<Event> el = rawEventsForDate(qdt); 326 QPtrList<Event> el = rawEventsForDate(qdt);
320 mFilter->apply(&el); 327 mFilter->apply(&el);
321 return el; 328 return el;
322} 329}
323 330
324QPtrList<Event> Calendar::events( const QDate &start, const QDate &end, 331QPtrList<Event> Calendar::events( const QDate &start, const QDate &end,
325 bool inclusive) 332 bool inclusive)
326{ 333{
327 QPtrList<Event> el = rawEvents(start,end,inclusive); 334 QPtrList<Event> el = rawEvents(start,end,inclusive);
328 mFilter->apply(&el); 335 mFilter->apply(&el);
329 return el; 336 return el;
330} 337}
331 338
332QPtrList<Event> Calendar::events() 339QPtrList<Event> Calendar::events()
333{ 340{
334 QPtrList<Event> el = rawEvents(); 341 QPtrList<Event> el = rawEvents();
335 mFilter->apply(&el); 342 mFilter->apply(&el);
336 return el; 343 return el;
337} 344}
338void Calendar::addIncidenceBranch(Incidence *i) 345void Calendar::addIncidenceBranch(Incidence *i)
339{ 346{
340 addIncidence( i ); 347 addIncidence( i );
341 Incidence * inc; 348 Incidence * inc;
342 QPtrList<Incidence> Relations = i->relations(); 349 QPtrList<Incidence> Relations = i->relations();
343 for (inc=Relations.first();inc;inc=Relations.next()) { 350 for (inc=Relations.first();inc;inc=Relations.next()) {
344 addIncidenceBranch( inc ); 351 addIncidenceBranch( inc );
345 } 352 }
346} 353}
347 354
348bool Calendar::addIncidence(Incidence *i) 355bool Calendar::addIncidence(Incidence *i)
349{ 356{
350 Incidence::AddVisitor<Calendar> v(this); 357 Incidence::AddVisitor<Calendar> v(this);
351 358 i->setCalID( mDefaultCalendar );
359 i->setCalEnabled( true );
352 return i->accept(v); 360 return i->accept(v);
353} 361}
354void Calendar::deleteIncidence(Incidence *in) 362void Calendar::deleteIncidence(Incidence *in)
355{ 363{
356 if ( in->typeID() == eventID ) 364 if ( in->typeID() == eventID )
357 deleteEvent( (Event*) in ); 365 deleteEvent( (Event*) in );
358 else if ( in->typeID() == todoID ) 366 else if ( in->typeID() == todoID )
359 deleteTodo( (Todo*) in); 367 deleteTodo( (Todo*) in);
360 else if ( in->typeID() == journalID ) 368 else if ( in->typeID() == journalID )
361 deleteJournal( (Journal*) in ); 369 deleteJournal( (Journal*) in );
362} 370}
363 371
364Incidence* Calendar::incidence( const QString& uid ) 372Incidence* Calendar::incidence( const QString& uid )
365{ 373{
366 Incidence* i; 374 Incidence* i;
367 375
368 if( (i = todo( uid )) != 0 ) 376 if( (i = todo( uid )) != 0 )
369 return i; 377 return i;
370 if( (i = event( uid )) != 0 ) 378 if( (i = event( uid )) != 0 )
371 return i; 379 return i;
372 if( (i = journal( uid )) != 0 ) 380 if( (i = journal( uid )) != 0 )
373 return i; 381 return i;
374 382
375 return 0; 383 return 0;
376} 384}
377 385
378QPtrList<Todo> Calendar::todos() 386QPtrList<Todo> Calendar::todos()
379{ 387{
380 QPtrList<Todo> tl = rawTodos(); 388 QPtrList<Todo> tl = rawTodos();
381 mFilter->apply( &tl ); 389 mFilter->apply( &tl );
382 return tl; 390 return tl;
383} 391}
384 392
385// When this is called, the todo have already been added to the calendar. 393// When this is called, the todo have already been added to the calendar.
386// This method is only about linking related todos 394// This method is only about linking related todos
387void Calendar::setupRelations( Incidence *incidence ) 395void Calendar::setupRelations( Incidence *incidence )
388{ 396{
389 QString uid = incidence->uid(); 397 QString uid = incidence->uid();
390 //qDebug("Calendar::setupRelations "); 398 //qDebug("Calendar::setupRelations ");
391 // First, go over the list of orphans and see if this is their parent 399 // First, go over the list of orphans and see if this is their parent
392 while( Incidence* i = mOrphans[ uid ] ) { 400 while( Incidence* i = mOrphans[ uid ] ) {
393 mOrphans.remove( uid ); 401 mOrphans.remove( uid );
394 i->setRelatedTo( incidence ); 402 i->setRelatedTo( incidence );
395 incidence->addRelation( i ); 403 incidence->addRelation( i );
396 mOrphanUids.remove( i->uid() ); 404 mOrphanUids.remove( i->uid() );
397 } 405 }
398 406
399 // Now see about this incidences parent 407 // Now see about this incidences parent
400 if( !incidence->relatedTo() && !incidence->relatedToUid().isEmpty() ) { 408 if( !incidence->relatedTo() && !incidence->relatedToUid().isEmpty() ) {
401 // This incidence has a uid it is related to, but is not registered to it yet 409 // This incidence has a uid it is related to, but is not registered to it yet
402 // Try to find it 410 // Try to find it
403 Incidence* parent = this->incidence( incidence->relatedToUid() ); 411 Incidence* parent = this->incidence( incidence->relatedToUid() );
404 if( parent ) { 412 if( parent ) {
405 // Found it 413 // Found it
406 incidence->setRelatedTo( parent ); 414 incidence->setRelatedTo( parent );
407 parent->addRelation( incidence ); 415 parent->addRelation( incidence );
408 } else { 416 } else {
409 // Not found, put this in the mOrphans list 417 // Not found, put this in the mOrphans list
410 mOrphans.insert( incidence->relatedToUid(), incidence ); 418 mOrphans.insert( incidence->relatedToUid(), incidence );
411 mOrphanUids.insert( incidence->uid(), incidence ); 419 mOrphanUids.insert( incidence->uid(), incidence );
412 } 420 }
413 } 421 }
414} 422}
415 423