26 #include <tqdatastream.h> 27 #include <tqdatetime.h> 30 #include <tqptrlist.h> 33 #include <tdelocale.h> 35 #include <kstandarddirs.h> 38 #include "exceptions.h" 39 #include "incidence.h" 45 #include "resourcecached.h" 49 static bool m_editoropen =
false;
51 ResourceCached::ResourceCached(
const TDEConfig* config )
53 mReloadPolicy( ReloadNever ), mReloadInterval( 10 ),
54 mReloadTimer( 0,
"mReloadTimer" ), mReloaded( false ),
55 mSavePolicy( SaveNever ), mSaveInterval( 10 ),
56 mSaveTimer( 0,
"mSaveTimer" ), mIdMapper(
"kcal/uidmaps/" )
58 connect( &mReloadTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotReload() ) );
59 connect( &mSaveTimer, TQ_SIGNAL( timeout() ), TQ_SLOT( slotSave() ) );
62 ResourceCached::~ResourceCached()
80 mReloadInterval = minutes;
85 return mReloadInterval;
102 mSaveInterval = minutes;
107 return mSaveInterval;
110 void ResourceCached::readConfig(
const TDEConfig *config )
112 mReloadPolicy = config->readNumEntry(
"ReloadPolicy", ReloadNever );
113 mReloadInterval = config->readNumEntry(
"ReloadInterval", 10 );
115 mSaveInterval = config->readNumEntry(
"SaveInterval", 10 );
116 mSavePolicy = config->readNumEntry(
"SavePolicy", SaveNever );
118 mLastLoad = config->readDateTimeEntry(
"LastLoad" );
119 mLastSave = config->readDateTimeEntry(
"LastSave" );
125 void ResourceCached::setupSaveTimer()
127 if ( mSavePolicy == SaveInterval ) {
128 kdDebug(5800) <<
"ResourceCached::setSavePolicy(): start save timer (interval " 129 << mSaveInterval <<
" minutes)." << endl;
130 mSaveTimer.start( mSaveInterval * 60 * 1000 );
136 void ResourceCached::setupReloadTimer()
138 if ( mReloadPolicy == ReloadInterval ) {
139 kdDebug(5800) <<
"ResourceCached::setSavePolicy(): start reload timer " 140 "(interval " << mReloadInterval <<
" minutes)" << endl;
141 mReloadTimer.start( mReloadInterval * 60 * 1000 );
147 void ResourceCached::writeConfig( TDEConfig *config )
149 config->writeEntry(
"ReloadPolicy", mReloadPolicy );
150 config->writeEntry(
"ReloadInterval", mReloadInterval );
152 config->writeEntry(
"SavePolicy", mSavePolicy );
153 config->writeEntry(
"SaveInterval", mSaveInterval );
155 config->writeEntry(
"LastLoad", mLastLoad );
156 config->writeEntry(
"LastSave", mLastSave );
161 return mCalendar.addEvent( event );
166 Q_UNUSED( subresource );
167 return mCalendar.addEvent( event );
173 kdDebug(5800) <<
"ResourceCached::deleteEvent" << endl;
175 return mCalendar.deleteEvent( event );
181 return mCalendar.event( uid );
188 Event::List list = mCalendar.rawEventsForDate( qd, sortField, sortDirection );
196 return mCalendar.rawEvents( start, end, inclusive );
201 return mCalendar.rawEventsForDate( qdt.date() );
206 return mCalendar.rawEvents( sortField, sortDirection );
211 return mCalendar.addTodo( todo );
216 Q_UNUSED( subresource );
217 return mCalendar.addTodo( todo );
222 return mCalendar.deleteTodo( todo );
227 return mCalendar.deleteJournal( journal );
233 return mCalendar.rawTodos( sortField, sortDirection );
238 return mCalendar.todo( uid );
243 return mCalendar.rawTodosForDate( date );
248 return mCalendar.addJournal( journal );
253 Q_UNUSED( subresource );
254 return mCalendar.addJournal( journal );
259 return mCalendar.journal( uid );
264 return mCalendar.rawJournals( sortField, sortDirection );
269 return mCalendar.rawJournalsForDate( date );
275 return mCalendar.alarmsTo( to );
281 return mCalendar.alarms( from, to );
287 mCalendar.setTimeZoneId( tzid );
290 TQString ResourceCached::timeZoneId()
const 292 return mCalendar.timeZoneId();
295 void ResourceCached::clearChanges()
297 mAddedIncidences.clear();
298 mChangedIncidences.clear();
299 mDeletedIncidences.clear();
304 setIdMapperIdentifier();
307 if ( TDEStandardDirs::exists( cacheFile() ) ) {
308 mCalendar.load( cacheFile() );
311 Incidence::List::Iterator it;
312 for ( it = incidences.begin(); it != incidences.end(); ++it ) {
313 (*it)->setReadOnly(
true );
321 kdDebug(5800) <<
"ResourceCached::saveCache(): " << cacheFile() << endl;
323 setIdMapperIdentifier();
326 mCalendar.save( cacheFile() );
329 void ResourceCached::setIdMapperIdentifier()
331 mIdMapper.setIdentifier( type() +
"_" + identifier() );
341 mCalendar.closeEvents();
346 mCalendar.closeTodos();
351 mCalendar.closeJournals();
354 void ResourceCached::cleanUpEventCache(
const Event::List &eventList )
358 if ( TDEStandardDirs::exists( cacheFile() ) )
359 calendar.
load( cacheFile() );
364 Event::List::ConstIterator cacheIt, it;
365 for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
367 for ( it = eventList.begin(); it != eventList.end(); ++it ) {
368 if ( (*it)->uid() == (*cacheIt)->uid() )
373 mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt)->uid() ) );
374 Event *
event = mCalendar.event( (*cacheIt)->uid() );
376 mCalendar.deleteEvent( event );
383 void ResourceCached::cleanUpTodoCache(
const Todo::List &todoList )
387 if ( TDEStandardDirs::exists( cacheFile() ) )
388 calendar.
load( cacheFile() );
393 Todo::List::ConstIterator cacheIt, it;
394 for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
397 for ( it = todoList.begin(); it != todoList.end(); ++it ) {
398 if ( (*it)->uid() == (*cacheIt)->uid() )
403 mIdMapper.removeRemoteId( mIdMapper.remoteId( (*cacheIt)->uid() ) );
404 Todo *todo = mCalendar.todo( (*cacheIt)->uid() );
406 mCalendar.deleteTodo( todo );
420 return locateLocal(
"cache",
"kcal/tderesources/" + identifier() );
425 return locateLocal(
"cache",
"kcal/changescache/" + identifier() +
"_" + type );
428 void ResourceCached::saveChangesCache(
const TQMap<Incidence*, bool> &map,
const TQString &type )
433 TQMap<Incidence *,bool>::ConstIterator it;
434 for ( it = map.begin(); it != map.end(); ++it ) {
440 calendar.
save( changesCacheFile( type ) );
442 TQFile file( changesCacheFile( type ) );
449 void ResourceCached::saveChangesCache()
451 saveChangesCache( mAddedIncidences,
"added" );
452 saveChangesCache( mDeletedIncidences,
"deleted" );
453 saveChangesCache( mChangedIncidences,
"changed" );
456 void ResourceCached::loadChangesCache( TQMap<Incidence*, bool> &map,
const TQString &type )
460 if ( TDEStandardDirs::exists( changesCacheFile( type ) ) )
461 calendar.
load( changesCacheFile( type ) );
466 Incidence::List::ConstIterator it;
467 for ( it = list.begin(); it != list.end(); ++it )
468 map.insert( (*it)->clone(), true );
473 void ResourceCached::loadChangesCache()
475 loadChangesCache( mAddedIncidences,
"added" );
476 loadChangesCache( mDeletedIncidences,
"deleted" );
477 loadChangesCache( mChangedIncidences,
"changed" );
483 kdDebug(5800) <<
"ResourceCached::calendarIncidenceAdded(): " 487 TQMap<Incidence *,bool>::ConstIterator it;
488 it = mAddedIncidences.find( i );
489 if ( it == mAddedIncidences.end() ) {
490 mAddedIncidences.insert( i,
true );
493 checkForAutomaticSave();
499 kdDebug(5800) <<
"ResourceCached::calendarIncidenceChanged(): " 503 TQMap<Incidence *,bool>::ConstIterator it;
504 it = mChangedIncidences.find( i );
506 if ( it == mChangedIncidences.end() ) {
507 mChangedIncidences.insert( i,
true );
510 checkForAutomaticSave();
516 kdDebug(5800) <<
"ResourceCached::calendarIncidenceDeleted(): " 524 IncidenceListIterator it;
527 parentIncidence = this->incidence(*it);
529 calendarIncidenceChanged(parentIncidence);
532 TQMap<Incidence *,bool>::ConstIterator it;
533 it = mDeletedIncidences.find( i );
534 if ( it == mDeletedIncidences.end() ) {
535 mDeletedIncidences.insert( i,
true );
538 checkForAutomaticSave();
544 TQMap<Incidence *,bool>::ConstIterator it;
545 for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
546 added.append( it.key() );
554 TQMap<Incidence *,bool>::ConstIterator it;
555 for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
556 changed.append( it.key() );
564 TQMap<Incidence *,bool>::ConstIterator it;
565 for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
566 deleted.append( it.key() );
574 TQMap<Incidence *,bool>::ConstIterator it;
575 for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
576 changes.append( it.key() );
578 for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
579 changes.append( it.key() );
581 for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
582 changes.append( it.key() );
587 bool ResourceCached::hasChanges()
const 589 return !( mAddedIncidences.isEmpty() && mChangedIncidences.isEmpty() &&
590 mDeletedIncidences.isEmpty() );
593 void ResourceCached::clearChange(
Incidence *incidence )
595 clearChange( incidence->
uid() );
598 void ResourceCached::clearChange(
const TQString &uid )
600 TQMap<Incidence*, bool>::Iterator it;
602 for ( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it )
603 if ( it.key()->uid() == uid ) {
604 mAddedIncidences.remove( it );
608 for ( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it )
609 if ( it.key()->uid() == uid ) {
610 mChangedIncidences.remove( it );
614 for ( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it )
615 if ( it.key()->uid() == uid ) {
616 mDeletedIncidences.remove( it );
621 void ResourceCached::enableChangeNotification()
623 mCalendar.registerObserver(
this );
626 void ResourceCached::disableChangeNotification()
628 mCalendar.unregisterObserver(
this );
631 bool ResourceCached::editorWindowOpen()
636 void ResourceCached::setEditorWindowOpen(
bool open)
641 void ResourceCached::slotReload()
643 if ( !isActive() )
return;
646 if (editorWindowOpen() ==
true)
return;
648 kdDebug(5800) <<
"ResourceCached::slotReload()" << endl;
653 void ResourceCached::slotSave()
655 if ( !isActive() )
return;
657 kdDebug(5800) <<
"ResourceCached::slotSave()" << endl;
662 void ResourceCached::checkForAutomaticSave()
664 if ( mSavePolicy == SaveAlways ) {
665 kdDebug(5800) <<
"ResourceCached::checkForAutomaticSave(): save now" << endl;
666 mSaveTimer.start( 1 * 1000,
true );
667 }
else if ( mSavePolicy == SaveDelayed ) {
668 kdDebug(5800) <<
"ResourceCached::checkForAutomaticSave(): save delayed" 670 mSaveTimer.start( 15 * 1000,
true );
676 if ( mReloadPolicy == ReloadNever )
return false;
677 if ( mReloadPolicy == ReloadOnStartup )
return !mReloaded;
683 if ( mSavePolicy == SaveNever )
return false;
689 if ( mLastLoad.isValid() ) {
691 txt += i18n(
"Last loaded: %1")
692 .arg( TDEGlobal::locale()->formatDateTime( mLastLoad ) );
694 if ( mLastSave.isValid() ) {
696 txt += i18n(
"Last saved: %1")
697 .arg( TDEGlobal::locale()->formatDateTime( mLastSave ) );
708 kdDebug(5800) <<
"Opening resource " << resourceName() << endl;
714 mCalendar.setOwner( owner );
719 return mCalendar.getOwner();
722 #include "resourcecached.moc" JournalSortField
How Journals are to be sorted.
bool save(const TQString &fileName, CalFormat *format=0)
Writes out the calendar to disk in the specified format.
bool deleteTodo(Todo *)
Remove a todo from the todolist.
bool load(const TQString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
Journal::List rawJournals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all journals.
void setSavePolicy(int policy)
Set save policy.
This class provides a Todo in the sense of RFC2445.
void setOwner(const Person &owner)
Set the owner of the calendar.
bool hasRecurrenceID() const
Returns true if the incidence has recurrenceID, otherwise return false.
void calendarIncidenceChanged(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been modified.
Alarm::List alarms(const TQDateTime &from, const TQDateTime &to)
Return all alarms, which ocur in the given time interval.
void setSaveInterval(int minutes)
Set save interval in minutes which is used when save policy is SaveInterval.
This class provides an Event in the sense of RFC2445.
Journal::List rawJournalsForDate(const TQDate &date)
Return list of journals for the given date.
This class provides a calendar stored as a local file.
void addInfoText(TQString &) const
Add info text for concrete resources.
TDE_DEPRECATED bool addEvent(Event *event)
Add event to calendar.
void saveCache()
Saves the cache back.
void setReloadInterval(int minutes)
Set reload interval in minutes which is used when reload policy is ReloadInterval.
This class provides the interfaces for a calendar resource.
TQString uid() const
Return the unique id for the event.
virtual void doClose()
Virtual method from KRES::Resource, called when the last instace of the resource is closed...
virtual bool doOpen()
Opens the resource.
int reloadInterval() const
Return reload interval in minutes.
void clearTodosCache()
Clear todos cache.
Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return unfiltered list of all events in calendar.
int savePolicy() const
Return save policy.
This class represents a person.
void clearJournalsCache()
Clear journals cache.
This class provides the base class common to all calendar components.
void close()
Clears out the current calendar, freeing all used memory etc.
TDE_DEPRECATED bool addTodo(Todo *todo)
Add a todo to the todolist.
This class provides a Journal in the sense of RFC2445.
void calendarIncidenceAdded(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been inserted.
bool deleteEvent(Event *)
Deletes an event from this calendar.
const Person & getOwner() const
Return the owner of the calendar's full name.
void clearCache()
Clear cache.
bool checkForReload()
Check if reload required according to reload policy.
Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all todos.
Event::List rawEventsForDate(const TQDate &date, EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Builds and then returns a list of all events that match for the date specified.
Namespace KCal is for global classes, objects and/or functions in libkcal.
void setTimeZoneId(const TQString &timeZoneId)
Set id of timezone, e.g.
void calendarIncidenceDeleted(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been removed.
int saveInterval() const
Return save interval in minutes.
SortDirection
Sort direction.
virtual bool addIncidence(Incidence *incidence)
Insert an Incidence into the Calendar.
Todo * todo(const TQString &uid)
Searches todolist for an event with this unique string identifier, returns a pointer or null...
Journal * journal(const TQString &uid)
Return Journal with given unique id.
void loadCache()
Loads the cache, this method should be called on load.
bool checkForSave()
Check if save required according to save policy.
TodoSortField
How Todos are to be sorted.
virtual Todo::List todos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return a sorted, filtered list of all Todos for this Calendar.
KPIM::IdMapper & idMapper()
Returns a reference to the id mapper.
void setReloadPolicy(int policy)
Set reload policy.
virtual Event::List events(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return a sorted, filtered list of all Events for this Calendar.
EventSortField
How Events are to be sorted.
int reloadPolicy() const
Return reload policy.
virtual Incidence::List incidences()
Return a filtered list of all Incidences for this Calendar.
Todo::List rawTodosForDate(const TQDate &date)
Returns list of todos due on the specified date.
IncidenceList childIncidences() const
Returns an EventList of all child incidences.
Event * event(const TQString &UniqueStr)
Retrieves an event on the basis of the unique string ID.
Alarm::List alarmsTo(const TQDateTime &to)
Return all alarms, which ocur before given date.
virtual TQString cacheFile() const
This method is used by loadCache() and saveCache(), reimplement it to change the location of the cach...
virtual TQString changesCacheFile(const TQString &) const
Functions for keeping the changes persistent.
TDE_DEPRECATED bool addJournal(Journal *journal)
Add a Journal entry to calendar.
bool deleteJournal(Journal *)
Remove a Journal from the calendar.
void clearEventsCache()
Clear events cache.