26#include <tqdatastream.h>
27#include <tqdatetime.h>
35#include <kstandarddirs.h>
38#include "exceptions.h"
45#include "resourcecached.h"
49static bool m_editoropen =
false;
51ResourceCached::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() ) );
62ResourceCached::~ResourceCached()
80 mReloadInterval = minutes;
85 return mReloadInterval;
102 mSaveInterval = minutes;
107 return mSaveInterval;
110void 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" );
125void 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 );
136void 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 );
147void 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 );
166 Q_UNUSED( subresource );
173 kdDebug(5800) <<
"ResourceCached::deleteEvent" << endl;
181 return mCalendar.
event( uid );
196 return mCalendar.
rawEvents( start, end, inclusive );
206 return mCalendar.
rawEvents( sortField, sortDirection );
216 Q_UNUSED( subresource );
233 return mCalendar.
rawTodos( sortField, sortDirection );
238 return mCalendar.
todo( uid );
253 Q_UNUSED( subresource );
259 return mCalendar.
journal( uid );
264 return mCalendar.
rawJournals( sortField, sortDirection );
281 return mCalendar.
alarms( from, to );
290TQString ResourceCached::timeZoneId()
const
295void ResourceCached::clearChanges()
297 mAddedIncidences.clear();
298 mChangedIncidences.clear();
299 mDeletedIncidences.clear();
304 setIdMapperIdentifier();
307 if ( TDEStandardDirs::exists(
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();
329void ResourceCached::setIdMapperIdentifier()
331 mIdMapper.setIdentifier( type() +
"_" + identifier() );
354void ResourceCached::cleanUpEventCache(
const Event::List &eventList )
358 if ( TDEStandardDirs::exists(
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() );
383void ResourceCached::cleanUpTodoCache(
const Todo::List &todoList )
387 if ( TDEStandardDirs::exists(
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() ) );
420 return locateLocal(
"cache",
"kcal/tderesources/" + identifier() );
425 return locateLocal(
"cache",
"kcal/changescache/" + identifier() +
"_" + type );
428void 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 ) {
436 calendar.addIncidence( it.key()->clone() );
449void ResourceCached::saveChangesCache()
451 saveChangesCache( mAddedIncidences,
"added" );
452 saveChangesCache( mDeletedIncidences,
"deleted" );
453 saveChangesCache( mChangedIncidences,
"changed" );
456void ResourceCached::loadChangesCache( TQMap<Incidence*, bool> &map,
const TQString &type )
466 Incidence::List::ConstIterator it;
467 for ( it = list.begin(); it != list.end(); ++it )
468 map.insert( (*it)->clone(), true );
473void 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;
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() );
587bool ResourceCached::hasChanges()
const
589 return !( mAddedIncidences.isEmpty() && mChangedIncidences.isEmpty() &&
590 mDeletedIncidences.isEmpty() );
593void ResourceCached::clearChange(
Incidence *incidence )
598void 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 );
621void ResourceCached::enableChangeNotification()
626void ResourceCached::disableChangeNotification()
631bool ResourceCached::editorWindowOpen()
636void ResourceCached::setEditorWindowOpen(
bool open)
641void ResourceCached::slotReload()
643 if ( !isActive() )
return;
646 if (editorWindowOpen() ==
true)
return;
648 kdDebug(5800) <<
"ResourceCached::slotReload()" << endl;
653void ResourceCached::slotSave()
655 if ( !isActive() )
return;
657 kdDebug(5800) <<
"ResourceCached::slotSave()" << endl;
662void 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"
This class provides a calendar stored as a local file.
bool addTodo(Todo *todo)
Add a todo to the todolist.
Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return unfiltered list of all events in calendar.
bool deleteJournal(Journal *)
Remove a Journal from the calendar.
Journal::List rawJournalsForDate(const TQDate &date)
Get unfiltered journals for a given date.
bool deleteEvent(Event *event)
Deletes an event from this calendar.
bool deleteTodo(Todo *)
Remove a todo from the todolist.
Journal * journal(const TQString &uid)
Return Journal with given UID.
void closeJournals()
Clears out the current calendar, freeing all used memory etc.
Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all todos.
Alarm::List alarmsTo(const TQDateTime &to)
Return all alarms, which ocur before given date.
bool addEvent(Event *event)
Add Event to calendar.
bool addJournal(Journal *)
Add a Journal entry to calendar.
void close()
Clears out the current calendar, freeing all used memory etc.
void closeTodos()
Clears out the current calendar, freeing all used memory etc.
Event * event(const TQString &uid)
Retrieves an event on the basis of the unique string ID.
bool load(const TQString &fileName, CalFormat *format=0)
Loads a calendar on disk in vCalendar or iCalendar format into the current calendar.
Todo * todo(const TQString &uid)
Searches todolist for an event with this unique string identifier, returns a pointer or null.
Journal::List rawJournals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all journals.
bool save(const TQString &fileName, CalFormat *format=0)
Writes out the calendar to disk in the specified format.
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.
void closeEvents()
Clears out the current calendar, freeing all used memory etc.
Alarm::List alarms(const TQDateTime &from, const TQDateTime &to)
Return all alarms, which ocur in the given time interval.
Todo::List rawTodosForDate(const TQDate &date)
Returns list of todos due on the specified date.
void setTimeZoneId(const TQString &timeZoneId)
Set the Time Zone Id for the Calendar.
void registerObserver(Observer *observer)
Register an Observer for this Calendar.
void unregisterObserver(Observer *observer)
Unregister an Observer for this Calendar.
TQString timeZoneId() const
Get the Time Zone ID for the Calendar.
This class provides an Event in the sense of RFC2445.
TQString uid() const
Return the unique id for the event.
This class provides the base class common to all calendar components.
IncidenceList childIncidences() const
Returns an EventList of all child incidences.
bool hasRecurrenceID() const
Returns true if the incidence has recurrenceID, otherwise return false.
This class provides a Journal in the sense of RFC2445.
This class represents a person.
void setSavePolicy(int policy)
Set save policy.
void clearTodosCache()
Clear todos cache.
void loadCache()
Loads the cache, this method should be called on load.
Alarm::List alarms(const TQDateTime &from, const TQDateTime &to)
Return all alarms, which ocur in the given time interval.
virtual bool doOpen()
Opens the resource.
virtual TQString cacheFile() const
This method is used by loadCache() and saveCache(), reimplement it to change the location of the cach...
bool deleteJournal(Journal *)
Remove a Journal from the calendar.
int reloadInterval() const
Return reload interval in minutes.
int reloadPolicy() const
Return reload policy.
void setSaveInterval(int minutes)
Set save interval in minutes which is used when save policy is SaveInterval.
void clearJournalsCache()
Clear journals cache.
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.
int savePolicy() const
Return save policy.
KPIM::IdMapper & idMapper()
Returns a reference to the id mapper.
virtual TQString changesCacheFile(const TQString &) const
Functions for keeping the changes persistent.
void calendarIncidenceChanged(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been modified.
Todo::List rawTodosForDate(const TQDate &date)
Returns list of todos due on the specified date.
void addInfoText(TQString &) const
Add info text for concrete resources.
void setOwner(const Person &owner)
Set the owner of the calendar.
void calendarIncidenceAdded(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been inserted.
const Person & getOwner() const
Return the owner of the calendar's full name.
void saveCache()
Saves the cache back.
void setTimeZoneId(const TQString &timeZoneId)
Set id of timezone, e.g.
Event::List rawEvents(EventSortField sortField=EventSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return unfiltered list of all events in calendar.
Journal * journal(const TQString &uid)
Return Journal with given unique id.
Journal::List rawJournals(JournalSortField sortField=JournalSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all journals.
void setReloadInterval(int minutes)
Set reload interval in minutes which is used when reload policy is ReloadInterval.
virtual void doClose()
Virtual method from KRES::Resource, called when the last instace of the resource is closed.
bool deleteEvent(Event *)
Deletes an event from this calendar.
Journal::List rawJournalsForDate(const TQDate &date)
Return list of journals for the given date.
bool deleteTodo(Todo *)
Remove a todo from the todolist.
void calendarIncidenceDeleted(KCal::Incidence *incidence)
Notify the Observer that an Incidence has been removed.
Todo * todo(const TQString &uid)
Searches todolist for an event with this unique string identifier, returns a pointer or null.
void setReloadPolicy(int policy)
Set reload policy.
TDE_DEPRECATED bool addJournal(Journal *journal)
Add a Journal entry to calendar.
Alarm::List alarmsTo(const TQDateTime &to)
Return all alarms, which ocur before given date.
TDE_DEPRECATED bool addEvent(Event *event)
Add event to calendar.
void clearEventsCache()
Clear events cache.
bool checkForSave()
Check if save required according to save policy.
bool checkForReload()
Check if reload required according to reload policy.
int saveInterval() const
Return save interval in minutes.
TDE_DEPRECATED bool addTodo(Todo *todo)
Add a todo to the todolist.
Todo::List rawTodos(TodoSortField sortField=TodoSortUnsorted, SortDirection sortDirection=SortDirectionAscending)
Return list of all todos.
Event * event(const TQString &UniqueStr)
Retrieves an event on the basis of the unique string ID.
void clearCache()
Clear cache.
This class provides the interfaces for a calendar resource.
bool save(Incidence *incidence=0)
Save resource data.
bool load()
Load resource data.
Incidence * incidence(const TQString &uid)
Return incidence with given unique id.
Incidence::List rawIncidences()
Returns a list of all incideces.
This class provides a Todo in the sense of RFC2445.
Namespace KCal is for global classes, objects and/or functions in libkcal.
TodoSortField
How Todos are to be sorted.
JournalSortField
How Journals are to be sorted.
EventSortField
How Events are to be sorted.
SortDirection
Sort direction.