author | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
---|---|---|
committer | zautrix <zautrix> | 2004-06-26 19:01:18 (UTC) |
commit | b9aad1f15dc600e4dbe4c62d3fcced6363188ba3 (patch) (unidiff) | |
tree | 2c3d4004fb21c72cba65793859f9bcd8ffd3a49c /libkcal/calendar.cpp | |
download | kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.zip kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.gz kdepimpi-b9aad1f15dc600e4dbe4c62d3fcced6363188ba3.tar.bz2 |
Initial revision
-rw-r--r-- | libkcal/calendar.cpp | 426 |
1 files changed, 426 insertions, 0 deletions
diff --git a/libkcal/calendar.cpp b/libkcal/calendar.cpp new file mode 100644 index 0000000..dc198bd --- a/dev/null +++ b/libkcal/calendar.cpp | |||
@@ -0,0 +1,426 @@ | |||
1 | /* | ||
2 | This file is part of libkcal. | ||
3 | Copyright (c) 1998 Preston Brown | ||
4 | Copyright (c) 2000,2001 Cornelius Schumacher <schumacher@kde.org> | ||
5 | |||
6 | This library is free software; you can redistribute it and/or | ||
7 | modify it under the terms of the GNU Library General Public | ||
8 | License as published by the Free Software Foundation; either | ||
9 | version 2 of the License, or (at your option) any later version. | ||
10 | |||
11 | This library is distributed in the hope that it will be useful, | ||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
14 | Library General Public License for more details. | ||
15 | |||
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 | ||
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
19 | Boston, MA 02111-1307, USA. | ||
20 | */ | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <time.h> | ||
24 | |||
25 | #include <kdebug.h> | ||
26 | #include <kglobal.h> | ||
27 | #include <klocale.h> | ||
28 | |||
29 | #include "exceptions.h" | ||
30 | #include "calfilter.h" | ||
31 | |||
32 | #include "calendar.h" | ||
33 | |||
34 | using namespace KCal; | ||
35 | |||
36 | Calendar::Calendar() | ||
37 | { | ||
38 | |||
39 | init(); | ||
40 | setTimeZoneId( i18n (" 00:00 Europe/London(UTC)") ); | ||
41 | } | ||
42 | |||
43 | Calendar::Calendar( const QString &timeZoneId ) | ||
44 | { | ||
45 | |||
46 | init(); | ||
47 | setTimeZoneId(timeZoneId); | ||
48 | } | ||
49 | |||
50 | void Calendar::init() | ||
51 | { | ||
52 | mObserver = 0; | ||
53 | mNewObserver = false; | ||
54 | |||
55 | mModified = false; | ||
56 | |||
57 | // Setup default filter, which does nothing | ||
58 | mDefaultFilter = new CalFilter; | ||
59 | mFilter = mDefaultFilter; | ||
60 | mFilter->setEnabled(false); | ||
61 | |||
62 | // initialize random numbers. This is a hack, and not | ||
63 | // even that good of one at that. | ||
64 | // srandom(time(0)); | ||
65 | |||
66 | // user information... | ||
67 | setOwner(i18n("Unknown Name")); | ||
68 | setEmail(i18n("unknown@nowhere")); | ||
69 | |||
70 | #if 0 | ||
71 | tmpStr = KOPrefs::instance()->mTimeZone; | ||
72 | // kdDebug(5800) << "Calendar::Calendar(): TimeZone: " << tmpStr << endl; | ||
73 | int dstSetting = KOPrefs::instance()->mDaylightSavings; | ||
74 | extern long int timezone; | ||
75 | struct tm *now; | ||
76 | time_t curtime; | ||
77 | curtime = time(0); | ||
78 | now = localtime(&curtime); | ||
79 | int hourOff = - ((timezone / 60) / 60); | ||
80 | if (now->tm_isdst) | ||
81 | hourOff += 1; | ||
82 | QString tzStr; | ||
83 | tzStr.sprintf("%.2d%.2d", | ||
84 | hourOff, | ||
85 | abs((timezone / 60) % 60)); | ||
86 | |||
87 | // if no time zone was in the config file, write what we just discovered. | ||
88 | if (tmpStr.isEmpty()) { | ||
89 | // KOPrefs::instance()->mTimeZone = tzStr; | ||
90 | } else { | ||
91 | tzStr = tmpStr; | ||
92 | } | ||
93 | |||
94 | // if daylight savings has changed since last load time, we need | ||
95 | // to rewrite these settings to the config file. | ||
96 | if ((now->tm_isdst && !dstSetting) || | ||
97 | (!now->tm_isdst && dstSetting)) { | ||
98 | KOPrefs::instance()->mTimeZone = tzStr; | ||
99 | KOPrefs::instance()->mDaylightSavings = now->tm_isdst; | ||
100 | } | ||
101 | |||
102 | setTimeZone(tzStr); | ||
103 | #endif | ||
104 | |||
105 | // KOPrefs::instance()->writeConfig(); | ||
106 | } | ||
107 | |||
108 | Calendar::~Calendar() | ||
109 | { | ||
110 | delete mDefaultFilter; | ||
111 | } | ||
112 | |||
113 | const QString &Calendar::getOwner() const | ||
114 | { | ||
115 | return mOwner; | ||
116 | } | ||
117 | |||
118 | void Calendar::setOwner(const QString &os) | ||
119 | { | ||
120 | int i; | ||
121 | mOwner = os; | ||
122 | i = mOwner.find(','); | ||
123 | if (i != -1) | ||
124 | mOwner = mOwner.left(i); | ||
125 | |||
126 | setModified( true ); | ||
127 | } | ||
128 | |||
129 | void Calendar::setTimeZone(const QString & tz) | ||
130 | { | ||
131 | bool neg = FALSE; | ||
132 | int hours, minutes; | ||
133 | QString tmpStr(tz); | ||
134 | |||
135 | if (tmpStr.left(1) == "-") | ||
136 | neg = TRUE; | ||
137 | if (tmpStr.left(1) == "-" || tmpStr.left(1) == "+") | ||
138 | tmpStr.remove(0, 1); | ||
139 | hours = tmpStr.left(2).toInt(); | ||
140 | if (tmpStr.length() > 2) | ||
141 | minutes = tmpStr.right(2).toInt(); | ||
142 | else | ||
143 | minutes = 0; | ||
144 | mTimeZone = (60*hours+minutes); | ||
145 | if (neg) | ||
146 | mTimeZone = -mTimeZone; | ||
147 | mLocalTime = false; | ||
148 | |||
149 | setModified( true ); | ||
150 | } | ||
151 | |||
152 | QString Calendar::getTimeZoneStr() const | ||
153 | { | ||
154 | if (mLocalTime) | ||
155 | return ""; | ||
156 | QString tmpStr; | ||
157 | int hours = abs(mTimeZone / 60); | ||
158 | int minutes = abs(mTimeZone % 60); | ||
159 | bool neg = mTimeZone < 0; | ||
160 | |||
161 | tmpStr.sprintf("%c%.2d%.2d", | ||
162 | (neg ? '-' : '+'), | ||
163 | hours, minutes); | ||
164 | return tmpStr; | ||
165 | } | ||
166 | |||
167 | void Calendar::setTimeZone(int tz) | ||
168 | { | ||
169 | mTimeZone = tz; | ||
170 | mLocalTime = false; | ||
171 | |||
172 | setModified( true ); | ||
173 | } | ||
174 | |||
175 | int Calendar::getTimeZone() const | ||
176 | { | ||
177 | return mTimeZone; | ||
178 | } | ||
179 | |||
180 | void Calendar::setTimeZoneId(const QString &id) | ||
181 | { | ||
182 | mTimeZoneId = id; | ||
183 | mLocalTime = false; | ||
184 | mTimeZone = KGlobal::locale()->timezoneOffset(mTimeZoneId); | ||
185 | if ( mTimeZone > 1000) | ||
186 | setLocalTime(); | ||
187 | //qDebug("Calendar::setTimeZoneOffset %s %d ",mTimeZoneId.latin1(), mTimeZone); | ||
188 | setModified( true ); | ||
189 | } | ||
190 | |||
191 | QString Calendar::timeZoneId() const | ||
192 | { | ||
193 | return mTimeZoneId; | ||
194 | } | ||
195 | |||
196 | void Calendar::setLocalTime() | ||
197 | { | ||
198 | //qDebug("Calendar::setLocalTime() "); | ||
199 | mLocalTime = true; | ||
200 | mTimeZone = 0; | ||
201 | mTimeZoneId = ""; | ||
202 | |||
203 | setModified( true ); | ||
204 | } | ||
205 | |||
206 | bool Calendar::isLocalTime() const | ||
207 | { | ||
208 | return mLocalTime; | ||
209 | } | ||
210 | |||
211 | const QString &Calendar::getEmail() | ||
212 | { | ||
213 | return mOwnerEmail; | ||
214 | } | ||
215 | |||
216 | void Calendar::setEmail(const QString &e) | ||
217 | { | ||
218 | mOwnerEmail = e; | ||
219 | |||
220 | setModified( true ); | ||
221 | } | ||
222 | |||
223 | void Calendar::setFilter(CalFilter *filter) | ||
224 | { | ||
225 | mFilter = filter; | ||
226 | } | ||
227 | |||
228 | CalFilter *Calendar::filter() | ||
229 | { | ||
230 | return mFilter; | ||
231 | } | ||
232 | |||
233 | QPtrList<Incidence> Calendar::incidences() | ||
234 | { | ||
235 | QPtrList<Incidence> incidences; | ||
236 | |||
237 | Incidence *i; | ||
238 | |||
239 | QPtrList<Event> e = events(); | ||
240 | for( i = e.first(); i; i = e.next() ) incidences.append( i ); | ||
241 | |||
242 | QPtrList<Todo> t = todos(); | ||
243 | for( i = t.first(); i; i = t.next() ) incidences.append( i ); | ||
244 | |||
245 | QPtrList<Journal> j = journals(); | ||
246 | for( i = j.first(); i; i = j.next() ) incidences.append( i ); | ||
247 | |||
248 | return incidences; | ||
249 | } | ||
250 | |||
251 | QPtrList<Incidence> Calendar::rawIncidences() | ||
252 | { | ||
253 | QPtrList<Incidence> incidences; | ||
254 | |||
255 | Incidence *i; | ||
256 | |||
257 | QPtrList<Event> e = rawEvents(); | ||
258 | for( i = e.first(); i; i = e.next() ) incidences.append( i ); | ||
259 | |||
260 | QPtrList<Todo> t = rawTodos(); | ||
261 | for( i = t.first(); i; i = t.next() ) incidences.append( i ); | ||
262 | |||
263 | QPtrList<Journal> j = journals(); | ||
264 | for( i = j.first(); i; i = j.next() ) incidences.append( i ); | ||
265 | |||
266 | return incidences; | ||
267 | } | ||
268 | |||
269 | QPtrList<Event> Calendar::events( const QDate &date, bool sorted ) | ||
270 | { | ||
271 | QPtrList<Event> el = rawEventsForDate(date,sorted); | ||
272 | mFilter->apply(&el); | ||
273 | return el; | ||
274 | } | ||
275 | |||
276 | QPtrList<Event> Calendar::events( const QDateTime &qdt ) | ||
277 | { | ||
278 | QPtrList<Event> el = rawEventsForDate(qdt); | ||
279 | mFilter->apply(&el); | ||
280 | return el; | ||
281 | } | ||
282 | |||
283 | QPtrList<Event> Calendar::events( const QDate &start, const QDate &end, | ||
284 | bool inclusive) | ||
285 | { | ||
286 | QPtrList<Event> el = rawEvents(start,end,inclusive); | ||
287 | mFilter->apply(&el); | ||
288 | return el; | ||
289 | } | ||
290 | |||
291 | QPtrList<Event> Calendar::events() | ||
292 | { | ||
293 | QPtrList<Event> el = rawEvents(); | ||
294 | mFilter->apply(&el); | ||
295 | return el; | ||
296 | } | ||
297 | |||
298 | |||
299 | bool Calendar::addIncidence(Incidence *i) | ||
300 | { | ||
301 | Incidence::AddVisitor<Calendar> v(this); | ||
302 | |||
303 | return i->accept(v); | ||
304 | } | ||
305 | void Calendar::deleteIncidence(Incidence *in) | ||
306 | { | ||
307 | if ( in->type() == "Event" ) | ||
308 | deleteEvent( (Event*) in ); | ||
309 | else if ( in->type() =="Todo" ) | ||
310 | deleteTodo( (Todo*) in); | ||
311 | else if ( in->type() =="Journal" ) | ||
312 | deleteJournal( (Journal*) in ); | ||
313 | } | ||
314 | |||
315 | Incidence* Calendar::incidence( const QString& uid ) | ||
316 | { | ||
317 | Incidence* i; | ||
318 | |||
319 | if( (i = todo( uid )) != 0 ) | ||
320 | return i; | ||
321 | if( (i = event( uid )) != 0 ) | ||
322 | return i; | ||
323 | if( (i = journal( uid )) != 0 ) | ||
324 | return i; | ||
325 | |||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | QPtrList<Todo> Calendar::todos() | ||
330 | { | ||
331 | QPtrList<Todo> tl = rawTodos(); | ||
332 | mFilter->apply( &tl ); | ||
333 | return tl; | ||
334 | } | ||
335 | |||
336 | // When this is called, the todo have already been added to the calendar. | ||
337 | // This method is only about linking related todos | ||
338 | void Calendar::setupRelations( Incidence *incidence ) | ||
339 | { | ||
340 | QString uid = incidence->uid(); | ||
341 | //qDebug("Calendar::setupRelations "); | ||
342 | // First, go over the list of orphans and see if this is their parent | ||
343 | while( Incidence* i = mOrphans[ uid ] ) { | ||
344 | mOrphans.remove( uid ); | ||
345 | i->setRelatedTo( incidence ); | ||
346 | incidence->addRelation( i ); | ||
347 | mOrphanUids.remove( i->uid() ); | ||
348 | } | ||
349 | |||
350 | // Now see about this incidences parent | ||
351 | if( !incidence->relatedTo() && !incidence->relatedToUid().isEmpty() ) { | ||
352 | // This incidence has a uid it is related to, but is not registered to it yet | ||
353 | // Try to find it | ||
354 | Incidence* parent = this->incidence( incidence->relatedToUid() ); | ||
355 | if( parent ) { | ||
356 | // Found it | ||
357 | incidence->setRelatedTo( parent ); | ||
358 | parent->addRelation( incidence ); | ||
359 | } else { | ||
360 | // Not found, put this in the mOrphans list | ||
361 | mOrphans.insert( incidence->relatedToUid(), incidence ); | ||
362 | mOrphanUids.insert( incidence->uid(), incidence ); | ||
363 | } | ||
364 | } | ||
365 | } | ||
366 | |||
367 | // If a task with subtasks is deleted, move it's subtasks to the orphans list | ||
368 | void Calendar::removeRelations( Incidence *incidence ) | ||
369 | { | ||
370 | // qDebug("Calendar::removeRelations "); | ||
371 | QString uid = incidence->uid(); | ||
372 | |||
373 | QPtrList<Incidence> relations = incidence->relations(); | ||
374 | for( Incidence* i = relations.first(); i; i = relations.next() ) | ||
375 | if( !mOrphanUids.find( i->uid() ) ) { | ||
376 | mOrphans.insert( uid, i ); | ||
377 | mOrphanUids.insert( i->uid(), i ); | ||
378 | i->setRelatedTo( 0 ); | ||
379 | i->setRelatedToUid( uid ); | ||
380 | } | ||
381 | |||
382 | // If this incidence is related to something else, tell that about it | ||
383 | if( incidence->relatedTo() ) | ||
384 | incidence->relatedTo()->removeRelation( incidence ); | ||
385 | |||
386 | // Remove this one from the orphans list | ||
387 | if( mOrphanUids.remove( uid ) ) | ||
388 | // This incidence is located in the orphans list - it should be removed | ||
389 | if( !( incidence->relatedTo() != 0 && mOrphans.remove( incidence->relatedTo()->uid() ) ) ) { | ||
390 | // Removing wasn't that easy | ||
391 | for( QDictIterator<Incidence> it( mOrphans ); it.current(); ++it ) { | ||
392 | if( it.current()->uid() == uid ) { | ||
393 | mOrphans.remove( it.currentKey() ); | ||
394 | break; | ||
395 | } | ||
396 | } | ||
397 | } | ||
398 | } | ||
399 | |||
400 | void Calendar::registerObserver( Observer *observer ) | ||
401 | { | ||
402 | mObserver = observer; | ||
403 | mNewObserver = true; | ||
404 | } | ||
405 | |||
406 | void Calendar::setModified( bool modified ) | ||
407 | { | ||
408 | if ( mObserver ) mObserver->calendarModified( modified, this ); | ||
409 | if ( modified != mModified || mNewObserver ) { | ||
410 | mNewObserver = false; | ||
411 | // if ( mObserver ) mObserver->calendarModified( modified, this ); | ||
412 | mModified = modified; | ||
413 | } | ||
414 | } | ||
415 | |||
416 | void Calendar::setLoadedProductId( const QString &id ) | ||
417 | { | ||
418 | mLoadedProductId = id; | ||
419 | } | ||
420 | |||
421 | QString Calendar::loadedProductId() | ||
422 | { | ||
423 | return mLoadedProductId; | ||
424 | } | ||
425 | |||
426 | #include "calendar.moc" | ||