1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
|
/*
This file is part of libkcal.
Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
// TODO [FIXME] IMPORTANT
// If a cached resource initiates a reload while an event editor is active, or an event is locked for editing,
// a big fat crash will ensue. The reload subroutine must ABORT if ANY korganizer events are locked for editing!!!
#include <tqdatastream.h>
#include <tqdatetime.h>
#include <tqfile.h>
#include <tqstring.h>
#include <tqptrlist.h>
#include <kdebug.h>
#include <klocale.h>
#include <kurl.h>
#include <kstandarddirs.h>
#include "event.h"
#include "exceptions.h"
#include "incidence.h"
#include "journal.h"
#include "todo.h"
#include <unistd.h>
#include "resourcecached.h"
using namespace KCal;
static bool m_editoropen = false;
ResourceCached::ResourceCached( const KConfig* config )
: ResourceCalendar( config ), mCalendar( TQString::tqfromLatin1( "UTC" ) ),
mReloadPolicy( ReloadNever ), mReloadInterval( 10 ),
mReloadTimer( 0, "mReloadTimer" ), mReloaded( false ),
mSavePolicy( SaveNever ), mSaveInterval( 10 ),
mSaveTimer( 0, "mSaveTimer" ), mIdMapper( "kcal/uidmaps/" )
{
connect( &mReloadTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotReload() ) );
connect( &mSaveTimer, TQT_SIGNAL( timeout() ), TQT_SLOT( slotSave() ) );
}
ResourceCached::~ResourceCached()
{
}
void ResourceCached::setReloadPolicy( int i )
{
mReloadPolicy = i;
setupReloadTimer();
}
int ResourceCached::reloadPolicy() const
{
return mReloadPolicy;
}
void ResourceCached::setReloadInterval( int minutes )
{
mReloadInterval = minutes;
}
int ResourceCached::reloadInterval() const
{
return mReloadInterval;
}
void ResourceCached::setSavePolicy( int i )
{
mSavePolicy = i;
setupSaveTimer();
}
int ResourceCached::savePolicy() const
{
return mSavePolicy;
}
void ResourceCached::setSaveInterval( int minutes )
{
mSaveInterval = minutes;
}
int ResourceCached::saveInterval() const
{
return mSaveInterval;
}
void ResourceCached::readConfig( const KConfig *config )
{
mReloadPolicy = config->readNumEntry( "ReloadPolicy", ReloadNever );
mReloadInterval = config->readNumEntry( "ReloadInterval", 10 );
mSaveInterval = config->readNumEntry( "SaveInterval", 10 );
mSavePolicy = config->readNumEntry( "SavePolicy", SaveNever );
mLastLoad = config->readDateTimeEntry( "LastLoad" );
mLastSave = config->readDateTimeEntry( "LastSave" );
setupSaveTimer();
setupReloadTimer();
}
void ResourceCached::setupSaveTimer()
{
if ( mSavePolicy == SaveInterval ) {
kdDebug(5800) << "ResourceCached::setSavePolicy(): start save timer (interval "
<< mSaveInterval << " minutes)." << endl;
mSaveTimer.start( mSaveInterval * 60 * 1000 ); // n minutes
} else {
mSaveTimer.stop();
}
}
void ResourceCached::setupReloadTimer()
{
if ( mReloadPolicy == ReloadInterval ) {
kdDebug(5800) << "ResourceCached::setSavePolicy(): start reload timer "
"(interval " << mReloadInterval << " minutes)" << endl;
mReloadTimer.start( mReloadInterval * 60 * 1000 ); // n minutes
} else {
mReloadTimer.stop();
}
}
void ResourceCached::writeConfig( KConfig *config )
{
config->writeEntry( "ReloadPolicy", mReloadPolicy );
config->writeEntry( "ReloadInterval", mReloadInterval );
config->writeEntry( "SavePolicy", mSavePolicy );
config->writeEntry( "SaveInterval", mSaveInterval );
config->writeEntry( "LastLoad", mLastLoad );
config->writeEntry( "LastSave", mLastSave );
}
bool ResourceCached::addEvent(Event *event)
{
return mCalendar.addEvent( event );
}
bool ResourceCached::addEvent(Event *event, const TQString &subresource )
{
Q_UNUSED( subresource ); // CalendarLocal does not support subresources
return mCalendar.addEvent( event );
}
// probably not really efficient, but...it works for now.
bool ResourceCached::deleteEvent( Event *event )
{
kdDebug(5800) << "ResourceCached::deleteEvent" << endl;
return mCalendar.deleteEvent( event );
}
Event *ResourceCached::event( const TQString &uid )
{
return mCalendar.event( uid );
}
Event::List ResourceCached::rawEventsForDate( const TQDate &qd,
EventSortField sortField,
SortDirection sortDirection )
{
Event::List list = mCalendar.rawEventsForDate( qd, sortField, sortDirection );
return list;
}
Event::List ResourceCached::rawEvents( const TQDate &start, const TQDate &end,
bool inclusive )
{
return mCalendar.rawEvents( start, end, inclusive );
}
Event::List ResourceCached::rawEventsForDate( const TQDateTime &qdt )
{
return mCalendar.rawEventsForDate( qdt.date() );
}
Event::List ResourceCached::rawEvents( EventSortField sortField, SortDirection sortDirection )
{
return mCalendar.rawEvents( sortField, sortDirection );
}
bool ResourceCached::addTodo( Todo *todo )
{
return mCalendar.addTodo( todo );
}
bool ResourceCached::addTodo( Todo *todo, const TQString &subresource )
{
Q_UNUSED( subresource ); // CalendarLocal does not support subresources
return mCalendar.addTodo( todo );
}
bool ResourceCached::deleteTodo( Todo *todo )
{
return mCalendar.deleteTodo( todo );
}
bool ResourceCached::deleteJournal( Journal *journal )
{
return mCalendar.deleteJournal( journal );
}
Todo::List ResourceCached::rawTodos( TodoSortField sortField, SortDirection sortDirection )
{
return mCalendar.rawTodos( sortField, sortDirection );
}
Todo *ResourceCached::todo( const TQString &uid )
{
return mCalendar.todo( uid );
}
Todo::List ResourceCached::rawTodosForDate( const TQDate &date )
{
return mCalendar.rawTodosForDate( date );
}
bool ResourceCached::addJournal( Journal *journal )
{
return mCalendar.addJournal( journal );
}
bool ResourceCached::addJournal( Journal *journal, const TQString &subresource )
{
Q_UNUSED( subresource ); // CalendarLocal does not support subresources
return mCalendar.addJournal( journal );
}
Journal *ResourceCached::journal( const TQString &uid )
{
return mCalendar.journal( uid );
}
Journal::List ResourceCached::rawJournals( JournalSortField sortField, SortDirection sortDirection )
{
return mCalendar.rawJournals( sortField, sortDirection );
}
Journal::List ResourceCached::rawJournalsForDate( const TQDate &date )
{
return mCalendar.rawJournalsForDate( date );
}
Alarm::List ResourceCached::alarmsTo( const TQDateTime &to )
{
return mCalendar.alarmsTo( to );
}
Alarm::List ResourceCached::alarms( const TQDateTime &from, const TQDateTime &to )
{
// kdDebug(5800) << "ResourceCached::alarms(" << from.toString() << " - " << to.toString() << ")\n";
return mCalendar.alarms( from, to );
}
void ResourceCached::setTimeZoneId( const TQString& tzid )
{
mCalendar.setTimeZoneId( tzid );
}
TQString ResourceCached::timeZoneId() const
{
return mCalendar.timeZoneId();
}
void ResourceCached::clearChanges()
{
mAddedIncidences.clear();
mChangedIncidences.clear();
mDeletedIncidences.clear();
}
void ResourceCached::loadCache()
{
setIdMapperIdentifier();
mIdMapper.load();
if ( KStandardDirs::exists( cacheFile() ) ) {
mCalendar.load( cacheFile() );
if ( readOnly() ) {
Incidence::List incidences( rawIncidences() );
Incidence::List::Iterator it;
for ( it = incidences.begin(); it != incidences.end(); ++it ) {
(*it)->setReadOnly( true );
}
}
}
}
void ResourceCached::saveCache()
{
kdDebug(5800) << "ResourceCached::saveCache(): " << cacheFile() << endl;
setIdMapperIdentifier();
mIdMapper.save();
mCalendar.save( cacheFile() );
}
void ResourceCached::setIdMapperIdentifier()
{
mIdMapper.setIdentifier( type() + "_" + identifier() );
}
void ResourceCached::clearCache()
{
mCalendar.close();
}
void ResourceCached::clearEventsCache()
{
mCalendar.closeEvents();
}
void ResourceCached::clearTodosCache()
{
mCalendar.closeTodos();
}
void ResourceCached::clearJournalsCache()
{
mCalendar.closeJournals();
}
void ResourceCached::cleanUpEventCache( const Event::List &eventList )
{
CalendarLocal calendar ( TQString::tqfromLatin1( "UTC" ) );
if ( KStandardDirs::exists( cacheFile() ) )
calendar.load( cacheFile() );
else
return;
Event::List list = calendar.events();
Event::List::ConstIterator cacheIt, it;
for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
bool found = false;
for ( it = eventList.begin(); it != eventList.end(); ++it ) {
if ( (*it)->uid() == (*cacheIt)->uid() )
found = true;
}
if ( !found ) {
mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt)->uid() ) );
Event *event = mCalendar.event( (*cacheIt)->uid() );
if ( event )
mCalendar.deleteEvent( event );
}
}
calendar.close();
}
void ResourceCached::cleanUpTodoCache( const Todo::List &todoList )
{
CalendarLocal calendar ( TQString::tqfromLatin1( "UTC" ) );
if ( KStandardDirs::exists( cacheFile() ) )
calendar.load( cacheFile() );
else
return;
Todo::List list = calendar.todos();
Todo::List::ConstIterator cacheIt, it;
for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
bool found = false;
for ( it = todoList.begin(); it != todoList.end(); ++it ) {
if ( (*it)->uid() == (*cacheIt)->uid() )
found = true;
}
if ( !found ) {
mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt)->uid() ) );
Todo *todo = mCalendar.todo( (*cacheIt)->uid() );
if ( todo )
mCalendar.deleteTodo( todo );
}
}
calendar.close();
}
KPIM::IdMapper& ResourceCached::idMapper()
{
return mIdMapper;
}
TQString ResourceCached::cacheFile() const
{
return locateLocal( "cache", "kcal/kresources/" + identifier() );
}
TQString ResourceCached::changesCacheFile( const TQString &type ) const
{
return locateLocal( "cache", "kcal/changescache/" + identifier() + "_" + type );
}
void ResourceCached::saveChangesCache( const TQMap<Incidence*, bool> &map, const TQString &type )
{
CalendarLocal calendar ( TQString::tqfromLatin1( "UTC" ) );
bool isEmpty = true;
TQMap<Incidence *,bool>::ConstIterator it;
for ( it = map.begin(); it != map.end(); ++it ) {
isEmpty = false;
calendar.addIncidence( it.key()->clone() );
}
if ( !isEmpty ) {
calendar.save( changesCacheFile( type ) );
} else {
TQFile file( changesCacheFile( type ) );
file.remove();
}
calendar.close();
}
void ResourceCached::saveChangesCache()
{
saveChangesCache( mAddedIncidences, "added" );
saveChangesCache( mDeletedIncidences, "deleted" );
saveChangesCache( mChangedIncidences, "changed" );
}
void ResourceCached::loadChangesCache( TQMap<Incidence*, bool> &map, const TQString &type )
{
CalendarLocal calendar ( TQString::tqfromLatin1( "UTC" ) );
if ( KStandardDirs::exists( changesCacheFile( type ) ) )
calendar.load( changesCacheFile( type ) );
else
return;
const Incidence::List list = calendar.incidences();
Incidence::List::ConstIterator it;
for ( it = list.begin(); it != list.end(); ++it )
map.insert( (*it)->clone(), true );
calendar.close();
}
void ResourceCached::loadChangesCache()
{
loadChangesCache( mAddedIncidences, "added" );
loadChangesCache( mDeletedIncidences, "deleted" );
loadChangesCache( mChangedIncidences, "changed" );
}
void ResourceCached::calendarIncidenceAdded( Incidence *i )
{
#if 1
kdDebug(5800) << "ResourceCached::calendarIncidenceAdded(): "
<< i->uid() << endl;
#endif
TQMap<Incidence *,bool>::ConstIterator it;
it = mAddedIncidences.find( i );
if ( it == mAddedIncidences.end() ) {
mAddedIncidences.insert( i, true );
}
checkForAutomaticSave();
}
void ResourceCached::calendarIncidenceChanged( Incidence *i )
{
#if 1
kdDebug(5800) << "ResourceCached::calendarIncidenceChanged(): "
<< i->uid() << endl;
#endif
TQMap<Incidence *,bool>::ConstIterator it;
it = mChangedIncidences.find( i );
// FIXME: If you modify an added incidence, there's no need to add it to mChangedIncidences!
if ( it == mChangedIncidences.end() ) {
mChangedIncidences.insert( i, true );
}
checkForAutomaticSave();
}
void ResourceCached::calendarIncidenceDeleted( Incidence *i )
{
#if 1
kdDebug(5800) << "ResourceCached::calendarIncidenceDeleted(): "
<< i->uid() << endl;
#endif
if (i->hasRecurrenceID()) {
// This incidence has a tqparent; notify the tqparent of the child's death and do not destroy the tqparent!
// Get the tqparent
IncidenceList il = i->childIncidences();
IncidenceListIterator it;
it = il.begin();
Incidence *parentIncidence;
parentIncidence = this->incidence(*it);
// Remove the child
calendarIncidenceChanged(parentIncidence);
}
else {
TQMap<Incidence *,bool>::ConstIterator it;
it = mDeletedIncidences.find( i );
if ( it == mDeletedIncidences.end() ) {
mDeletedIncidences.insert( i, true );
}
}
checkForAutomaticSave();
}
Incidence::List ResourceCached::addedIncidences() const
{
Incidence::List added;
TQMap<Incidence *,bool>::ConstIterator it;
for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
added.append( it.key() );
}
return added;
}
Incidence::List ResourceCached::changedIncidences() const
{
Incidence::List changed;
TQMap<Incidence *,bool>::ConstIterator it;
for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
changed.append( it.key() );
}
return changed;
}
Incidence::List ResourceCached::deletedIncidences() const
{
Incidence::List deleted;
TQMap<Incidence *,bool>::ConstIterator it;
for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
deleted.append( it.key() );
}
return deleted;
}
Incidence::List ResourceCached::allChanges() const
{
Incidence::List changes;
TQMap<Incidence *,bool>::ConstIterator it;
for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
changes.append( it.key() );
}
for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
changes.append( it.key() );
}
for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
changes.append( it.key() );
}
return changes;
}
bool ResourceCached::hasChanges() const
{
return !( mAddedIncidences.isEmpty() && mChangedIncidences.isEmpty() &&
mDeletedIncidences.isEmpty() );
}
void ResourceCached::clearChange( Incidence *incidence )
{
clearChange( incidence->uid() );
}
void ResourceCached::clearChange( const TQString &uid )
{
TQMap<Incidence*, bool>::Iterator it;
for ( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it )
if ( it.key()->uid() == uid ) {
mAddedIncidences.remove( it );
break;
}
for ( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it )
if ( it.key()->uid() == uid ) {
mChangedIncidences.remove( it );
break;
}
for ( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it )
if ( it.key()->uid() == uid ) {
mDeletedIncidences.remove( it );
break;
}
}
void ResourceCached::enableChangeNotification()
{
mCalendar.registerObserver( this );
}
void ResourceCached::disableChangeNotification()
{
mCalendar.unregisterObserver( this );
}
bool ResourceCached::editorWindowOpen()
{
return m_editoropen;
}
void ResourceCached::setEditorWindowOpen(bool open)
{
m_editoropen = open;
}
void ResourceCached::slotReload()
{
if ( !isActive() ) return;
// Make sure no editor windows are open
if (editorWindowOpen() == true) return;
kdDebug(5800) << "ResourceCached::slotReload()" << endl;
load();
}
void ResourceCached::slotSave()
{
if ( !isActive() ) return;
kdDebug(5800) << "ResourceCached::slotSave()" << endl;
save();
}
void ResourceCached::checkForAutomaticSave()
{
if ( mSavePolicy == SaveAlways ) {
kdDebug(5800) << "ResourceCached::checkForAutomaticSave(): save now" << endl;
mSaveTimer.start( 1 * 1000, true ); // 1 second
} else if ( mSavePolicy == SaveDelayed ) {
kdDebug(5800) << "ResourceCached::checkForAutomaticSave(): save delayed"
<< endl;
mSaveTimer.start( 15 * 1000, true ); // 15 seconds
}
}
bool ResourceCached::checkForReload()
{
if ( mReloadPolicy == ReloadNever ) return false;
if ( mReloadPolicy == ReloadOnStartup ) return !mReloaded;
return true;
}
bool ResourceCached::checkForSave()
{
if ( mSavePolicy == SaveNever ) return false;
return true;
}
void ResourceCached::addInfoText( TQString &txt ) const
{
if ( mLastLoad.isValid() ) {
txt += "<br>";
txt += i18n("Last loaded: %1")
.tqarg( KGlobal::locale()->formatDateTime( mLastLoad ) );
}
if ( mLastSave.isValid() ) {
txt += "<br>";
txt += i18n("Last saved: %1")
.tqarg( KGlobal::locale()->formatDateTime( mLastSave ) );
}
}
void ResourceCached::doClose()
{
mCalendar.close();
}
bool ResourceCached::doOpen()
{
kdDebug(5800) << "Opening resource " << resourceName() << endl;
return true;
}
void KCal::ResourceCached::setOwner( const Person &owner )
{
mCalendar.setOwner( owner );
}
const Person & KCal::ResourceCached::getOwner() const
{
return mCalendar.getOwner();
}
#include "resourcecached.moc"
|