• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDE-PIM Libraries
  • Sitemap
  • Contact Us
 

KCal Library

resourcecached.cpp

00001 /*
00002   This file is part of the kcal library.
00003 
00004   Copyright © 2006 by David Jarvie <software@astrojar.org.uk>
00005   Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
00006 
00007   This library is free software; you can redistribute it and/or
00008   modify it under the terms of the GNU Library General Public
00009   License as published by the Free Software Foundation; either
00010   version 2 of the License, or (at your option) any later version.
00011 
00012   This library is distributed in the hope that it will be useful,
00013   but WITHOUT ANY WARRANTY; without even the implied warranty of
00014   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015   Library General Public License for more details.
00016 
00017   You should have received a copy of the GNU Library General Public License
00018   along with this library; see the file COPYING.LIB.  If not, write to
00019   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020   Boston, MA 02110-1301, USA.
00021 */
00022 
00023 #include "resourcecached.h"
00024 #include "calendarlocal.h"
00025 #include "event.h"
00026 #include "exceptions.h"
00027 #include "incidence.h"
00028 #include "journal.h"
00029 #include "todo.h"
00030 
00031 #include "kresources/idmapper.h"
00032 
00033 #include <kconfiggroup.h>
00034 #include <kdebug.h>
00035 #include <klocale.h>
00036 #include <kstandarddirs.h>
00037 #include <kurl.h>
00038 
00039 #include <QtCore/QDateTime>
00040 #include <QtCore/QDataStream>
00041 #include <QtCore/QFile>
00042 #include <QtCore/QString>
00043 #include <QtCore/QTimer>
00044 
00045 #include "resourcecached.moc"
00046 
00047 using namespace KCal;
00048 
00049 //@cond PRIVATE
00050 class ResourceCached::Private
00051 {
00052   public:
00053     Private()
00054       : mCalendar( QLatin1String( "UTC" ) ),
00055         mReloadPolicy( ResourceCached::ReloadNever ),
00056         mReloadInterval( 10 ),
00057         mInhibitReload( false ),
00058         mReloaded( false ),
00059         mSavePending( false ),
00060         mSavePolicy( ResourceCached::SaveNever ),
00061         mSaveInterval( 10 ),
00062         mIdMapper( "kcal/uidmaps/" )
00063     {}
00064 
00065     CalendarLocal mCalendar;
00066 
00067     int mReloadPolicy;
00068     int mReloadInterval;
00069     QTimer mReloadTimer;
00070     bool mInhibitReload;   // true to prevent downloads by load(DefaultCache)
00071     bool mReloaded;        // true once it has been downloaded
00072     bool mSavePending;     // true if a save of changes has been scheduled on the timer
00073 
00074     int mSavePolicy;
00075     int mSaveInterval;
00076     QTimer mSaveTimer;
00077 
00078     KDateTime mLastLoad;
00079     KDateTime mLastSave;
00080 
00081     QMap<KCal::Incidence *,bool> mAddedIncidences;
00082     QMap<KCal::Incidence *,bool> mChangedIncidences;
00083     QMap<KCal::Incidence *,bool> mDeletedIncidences;
00084 
00085     KRES::IdMapper mIdMapper;
00086 };
00087 //@endcond
00088 
00089 ResourceCached::ResourceCached()
00090   : ResourceCalendar(),
00091     d( new Private )
00092 {
00093   connect( &d->mReloadTimer, SIGNAL( timeout() ), SLOT( slotReload() ) );
00094   connect( &d->mSaveTimer, SIGNAL( timeout() ), SLOT( slotSave() ) );
00095 }
00096 
00097 ResourceCached::ResourceCached( const KConfigGroup &group )
00098   : ResourceCalendar( group ),
00099     d( new Private )
00100 {
00101   connect( &d->mReloadTimer, SIGNAL( timeout() ), SLOT( slotReload() ) );
00102   connect( &d->mSaveTimer, SIGNAL( timeout() ), SLOT( slotSave() ) );
00103 }
00104 
00105 ResourceCached::~ResourceCached()
00106 {
00107   delete d;
00108 }
00109 
00110 CalendarLocal *ResourceCached::calendar() const
00111 {
00112   return &d->mCalendar;
00113 }
00114 
00115 bool ResourceCached::defaultReloadInhibited() const
00116 {
00117   return d->mInhibitReload;
00118 }
00119 
00120 bool ResourceCached::reloaded() const
00121 {
00122   return d->mReloaded;
00123 }
00124 
00125 void ResourceCached::setReloaded( bool done )
00126 {
00127   d->mReloaded = done;
00128 }
00129 
00130 void ResourceCached::setReloadPolicy( int i )
00131 {
00132   d->mReloadPolicy = i;
00133 
00134   setupReloadTimer();
00135 }
00136 
00137 int ResourceCached::reloadPolicy() const
00138 {
00139   return d->mReloadPolicy;
00140 }
00141 
00142 void ResourceCached::setReloadInterval( int minutes )
00143 {
00144   d->mReloadInterval = minutes;
00145 }
00146 
00147 int ResourceCached::reloadInterval() const
00148 {
00149   return d->mReloadInterval;
00150 }
00151 
00152 bool ResourceCached::inhibitDefaultReload( bool inhibit )
00153 {
00154   if ( inhibit == d->mInhibitReload ) {
00155     return false;
00156   }
00157   d->mInhibitReload = inhibit;
00158   return true;
00159 }
00160 
00161 void ResourceCached::setSavePolicy( int i )
00162 {
00163   d->mSavePolicy = i;
00164 
00165   setupSaveTimer();
00166 }
00167 
00168 int ResourceCached::savePolicy() const
00169 {
00170   return d->mSavePolicy;
00171 }
00172 
00173 void ResourceCached::setSaveInterval( int minutes )
00174 {
00175   d->mSaveInterval = minutes;
00176 }
00177 
00178 int ResourceCached::saveInterval() const
00179 {
00180   return d->mSaveInterval;
00181 }
00182 
00183 void ResourceCached::readConfig( const KConfigGroup &group )
00184 {
00185   d->mReloadPolicy = group.readEntry( "ReloadPolicy", int(ReloadNever) );
00186   d->mReloadInterval = group.readEntry( "ReloadInterval", 10 );
00187 
00188   d->mSaveInterval = group.readEntry( "SaveInterval", 10 );
00189   d->mSavePolicy = group.readEntry( "SavePolicy", int(SaveNever) );
00190 
00191   QDateTime curDt = QDateTime::currentDateTime();
00192   QDateTime dt = group.readEntry( "LastLoad", curDt );
00193   d->mLastLoad = KDateTime( dt, KDateTime::UTC );
00194   dt = group.readEntry( "LastSave", curDt );
00195   d->mLastSave = KDateTime( dt, KDateTime::UTC );
00196 
00197   setupSaveTimer();
00198   setupReloadTimer();
00199 }
00200 
00201 void ResourceCached::setupSaveTimer()
00202 {
00203   if ( d->mSavePolicy == SaveInterval ) {
00204     kDebug() << "start save timer (interval " << d->mSaveInterval << "mins)";
00205     d->mSaveTimer.start( d->mSaveInterval * 60 * 1000 ); // n minutes
00206   } else {
00207     d->mSaveTimer.stop();
00208   }
00209 }
00210 
00211 void ResourceCached::setupReloadTimer()
00212 {
00213   if ( d->mReloadPolicy == ReloadInterval ) {
00214     kDebug() << "start reload timer (interval " << d->mReloadInterval << "mins)";
00215     d->mReloadTimer.start( d->mReloadInterval * 60 * 1000 ); // n minutes
00216   } else {
00217     d->mReloadTimer.stop();
00218   }
00219 }
00220 
00221 void ResourceCached::writeConfig( KConfigGroup &group )
00222 {
00223   group.writeEntry( "ReloadPolicy", d->mReloadPolicy );
00224   group.writeEntry( "ReloadInterval", d->mReloadInterval );
00225 
00226   group.writeEntry( "SavePolicy", d->mSavePolicy );
00227   group.writeEntry( "SaveInterval", d->mSaveInterval );
00228 
00229   group.writeEntry( "LastLoad", d->mLastLoad.toUtc().dateTime() );
00230   group.writeEntry( "LastSave", d->mLastSave.toUtc().dateTime() );
00231 }
00232 
00233 bool ResourceCached::addEvent( Event *event )
00234 {
00235   return d->mCalendar.addEvent( event );
00236 }
00237 
00238 // probably not really efficient, but...it works for now.
00239 bool ResourceCached::deleteEvent( Event *event )
00240 {
00241   kDebug();
00242 
00243   return d->mCalendar.deleteEvent( event );
00244 }
00245 
00246 void ResourceCached::deleteAllEvents()
00247 {
00248   d->mCalendar.deleteAllEvents();
00249 }
00250 
00251 Event *ResourceCached::event( const QString &uid )
00252 {
00253   return d->mCalendar.event( uid );
00254 }
00255 
00256 Event::List ResourceCached::rawEventsForDate( const QDate &qd, const KDateTime::Spec &timespec,
00257                                               EventSortField sortField,
00258                                               SortDirection sortDirection )
00259 {
00260   Event::List list = d->mCalendar.rawEventsForDate( qd, timespec, sortField, sortDirection );
00261 
00262   return list;
00263 }
00264 
00265 Event::List ResourceCached::rawEvents( const QDate &start, const QDate &end,
00266                                        const KDateTime::Spec &timespec, bool inclusive )
00267 {
00268   return d->mCalendar.rawEvents( start, end, timespec, inclusive );
00269 }
00270 
00271 Event::List ResourceCached::rawEventsForDate( const KDateTime &kdt )
00272 {
00273   return d->mCalendar.rawEventsForDate( kdt );
00274 }
00275 
00276 Event::List ResourceCached::rawEvents( EventSortField sortField, SortDirection sortDirection )
00277 {
00278   return d->mCalendar.rawEvents( sortField, sortDirection );
00279 }
00280 
00281 bool ResourceCached::addTodo( Todo *todo )
00282 {
00283   return d->mCalendar.addTodo( todo );
00284 }
00285 
00286 bool ResourceCached::deleteTodo( Todo *todo )
00287 {
00288   return d->mCalendar.deleteTodo( todo );
00289 }
00290 
00291 void ResourceCached::deleteAllTodos()
00292 {
00293   d->mCalendar.deleteAllTodos();
00294 }
00295 
00296 bool ResourceCached::deleteJournal( Journal *journal )
00297 {
00298   return d->mCalendar.deleteJournal( journal );
00299 }
00300 
00301 void ResourceCached::deleteAllJournals()
00302 {
00303   d->mCalendar.deleteAllJournals();
00304 }
00305 
00306 Todo::List ResourceCached::rawTodos( TodoSortField sortField, SortDirection sortDirection )
00307 {
00308   return d->mCalendar.rawTodos( sortField, sortDirection );
00309 }
00310 
00311 Todo *ResourceCached::todo( const QString &uid )
00312 {
00313   return d->mCalendar.todo( uid );
00314 }
00315 
00316 Todo::List ResourceCached::rawTodosForDate( const QDate &date )
00317 {
00318   return d->mCalendar.rawTodosForDate( date );
00319 }
00320 
00321 bool ResourceCached::addJournal( Journal *journal )
00322 {
00323   kDebug() << "Adding Journal on" << journal->dtStart().toString();
00324 
00325   return d->mCalendar.addJournal( journal );
00326 }
00327 
00328 Journal *ResourceCached::journal( const QString &uid )
00329 {
00330   return d->mCalendar.journal( uid );
00331 }
00332 
00333 Journal::List ResourceCached::rawJournals( JournalSortField sortField, SortDirection sortDirection )
00334 {
00335   return d->mCalendar.rawJournals( sortField, sortDirection );
00336 }
00337 
00338 Journal::List ResourceCached::rawJournalsForDate( const QDate &date )
00339 {
00340   return d->mCalendar.rawJournalsForDate( date );
00341 }
00342 
00343 Alarm::List ResourceCached::alarmsTo( const KDateTime &to )
00344 {
00345   return d->mCalendar.alarmsTo( to );
00346 }
00347 
00348 Alarm::List ResourceCached::alarms( const KDateTime &from, const KDateTime &to )
00349 {
00350   return d->mCalendar.alarms( from, to );
00351 }
00352 
00353 void ResourceCached::setTimeSpec( const KDateTime::Spec &timeSpec )
00354 {
00355   d->mCalendar.setTimeSpec( timeSpec );
00356 }
00357 
00358 KDateTime::Spec ResourceCached::timeSpec() const
00359 {
00360   return d->mCalendar.timeSpec();
00361 }
00362 
00363 void ResourceCached::setTimeZoneId( const QString &tzid )
00364 {
00365   d->mCalendar.setTimeZoneId( tzid );
00366 }
00367 
00368 QString ResourceCached::timeZoneId() const
00369 {
00370   return d->mCalendar.timeZoneId();
00371 }
00372 
00373 void ResourceCached::shiftTimes( const KDateTime::Spec &oldSpec, const KDateTime::Spec &newSpec )
00374 {
00375   d->mCalendar.shiftTimes( oldSpec, newSpec );
00376 }
00377 
00378 void ResourceCached::clearChanges()
00379 {
00380   d->mAddedIncidences.clear();
00381   d->mChangedIncidences.clear();
00382   d->mDeletedIncidences.clear();
00383 }
00384 
00385 bool ResourceCached::load( CacheAction action )
00386 {
00387   kDebug() << resourceName();
00388 
00389   setReceivedLoadError( false );
00390 
00391   bool success = true;
00392   if ( !isOpen() ) {
00393     success = open(); //krazy:exclude=syscalls open is a class method
00394   }
00395   if ( success ) {
00396     bool update = false;
00397     switch ( action ) {
00398     case DefaultCache:
00399       if ( !d->mReloaded && !d->mInhibitReload ) {
00400         update = checkForReload();
00401       }
00402       break;
00403     case NoSyncCache:
00404       break;
00405     case SyncCache:
00406       update = true;
00407       break;
00408     }
00409     success = doLoad( update );
00410   }
00411   if ( !success && !receivedLoadError() ) {
00412     loadError();
00413   }
00414 
00415   // If the resource is read-only, we need to set its incidences to read-only,
00416   // too. This can't be done at a lower-level, since the read-only setting
00417   // happens at this level
00418   if ( !noReadOnlyOnLoad() && readOnly() ) {
00419     Incidence::List incidences( rawIncidences() );
00420     Incidence::List::Iterator it;
00421     for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00422       (*it)->setReadOnly( true );
00423     }
00424   }
00425 
00426   kDebug() << "Done loading resource" << resourceName();
00427 
00428   if ( success ) {
00429     emit resourceLoaded( this );
00430   }
00431 
00432   return success;
00433 }
00434 
00435 bool ResourceCached::load()
00436 {
00437   return load( SyncCache );
00438 }
00439 
00440 bool ResourceCached::loadFromCache()
00441 {
00442   setIdMapperIdentifier();
00443   d->mIdMapper.load();
00444 
00445   if ( !KStandardDirs::exists( cacheFile() ) ) {
00446     return false;
00447   }
00448   d->mCalendar.load( cacheFile() );
00449   if ( !noReadOnlyOnLoad() && readOnly() ) {
00450     Incidence::List incidences( rawIncidences() );
00451     Incidence::List::Iterator it;
00452     for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00453       (*it)->setReadOnly( true );
00454     }
00455   }
00456   return true;
00457 }
00458 
00459 bool ResourceCached::save( CacheAction action, Incidence *incidence )
00460 {
00461   if ( !incidence && ( d->mSavePolicy == SaveAlways || d->mSavePolicy == SaveDelayed ) ) {
00462     d->mSaveTimer.stop();   // in case it's called manually while save is pending
00463   }
00464   d->mSavePending = false;
00465   if ( saveInhibited() ) {
00466     return true;
00467   }
00468   if ( !readOnly() ) {
00469     kDebug() << "Save resource" << resourceName();
00470 
00471     setReceivedSaveError( false );
00472 
00473     if ( !isOpen() ) {
00474       return true;
00475     }
00476     bool upload = false;
00477     switch ( action ) {
00478     case DefaultCache:
00479       upload = checkForSave();
00480       break;
00481     case NoSyncCache:
00482       break;
00483     case SyncCache:
00484       upload = true;
00485       break;
00486     }
00487     bool success = incidence ? doSave( upload, incidence ) : doSave( upload );
00488     if ( !success && !receivedSaveError() ) {
00489       saveError();
00490     } else {
00491       emit resourceSaved( this );
00492     }
00493     return success;
00494   } else {
00495     // Read-only, just don't save...
00496     kDebug() << "Don't save read-only resource" << resourceName();
00497     return true;
00498   }
00499 }
00500 
00501 bool ResourceCached::save( Incidence *incidence )
00502 {
00503   return save( SyncCache, incidence );
00504 }
00505 
00506 bool ResourceCached::doSave( bool syncCache, Incidence *incidence )
00507 {
00508   Q_UNUSED( incidence );
00509   return doSave( syncCache );
00510 }
00511 
00512 void ResourceCached::saveToCache()
00513 {
00514   kDebug() << cacheFile();
00515 
00516   setIdMapperIdentifier();
00517   d->mIdMapper.save();
00518 
00519   d->mCalendar.save( cacheFile() );
00520 }
00521 
00522 void ResourceCached::setIdMapperIdentifier()
00523 {
00524   d->mIdMapper.setIdentifier( type() + '_' + identifier() );
00525 }
00526 
00527 void ResourceCached::clearCache()
00528 {
00529   d->mCalendar.close();
00530 }
00531 
00532 void ResourceCached::cleanUpEventCache( const Event::List &eventList )
00533 {
00534   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00535 
00536   if ( KStandardDirs::exists( cacheFile() ) ) {
00537     calendar.load( cacheFile() );
00538   } else {
00539     return;
00540   }
00541 
00542   Event::List list = calendar.events();
00543   Event::List::ConstIterator cacheIt, it;
00544   for ( cacheIt = list.constBegin(); cacheIt != list.constEnd(); ++cacheIt ) {
00545     bool found = false;
00546     for ( it = eventList.begin(); it != eventList.end(); ++it ) {
00547       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00548         found = true;
00549         break;
00550       }
00551     }
00552 
00553     if ( !found ) {
00554       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00555       Event *event = d->mCalendar.event( (*cacheIt)->uid() );
00556       if ( event ) {
00557         d->mCalendar.deleteEvent( event );
00558       }
00559     }
00560   }
00561 
00562   calendar.close();
00563 }
00564 
00565 void ResourceCached::cleanUpTodoCache( const Todo::List &todoList )
00566 {
00567   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00568 
00569   if ( KStandardDirs::exists( cacheFile() ) ) {
00570     calendar.load( cacheFile() );
00571   } else {
00572     return;
00573   }
00574 
00575   Todo::List list = calendar.todos();
00576   Todo::List::ConstIterator cacheIt, it;
00577   for ( cacheIt = list.constBegin(); cacheIt != list.constEnd(); ++cacheIt ) {
00578 
00579     bool found = false;
00580     for ( it = todoList.constBegin(); it != todoList.constEnd(); ++it ) {
00581       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00582         found = true;
00583       }
00584     }
00585 
00586     if ( !found ) {
00587       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00588       Todo *todo = d->mCalendar.todo( (*cacheIt)->uid() );
00589       if ( todo ) {
00590         d->mCalendar.deleteTodo( todo );
00591       }
00592     }
00593   }
00594 
00595   calendar.close();
00596 }
00597 
00598 KRES::IdMapper &ResourceCached::idMapper()
00599 {
00600   return d->mIdMapper;
00601 }
00602 
00603 QString ResourceCached::cacheFile() const
00604 {
00605   return KStandardDirs::locateLocal( "cache", "kcal/kresources/" + identifier() );
00606 }
00607 
00608 QString ResourceCached::changesCacheFile( const QString &type ) const
00609 {
00610   return KStandardDirs::locateLocal( "cache", "kcal/changescache/" + identifier() + '_' + type );
00611 }
00612 
00613 void ResourceCached::saveChangesCache( const QMap<Incidence *, bool> &map, const QString &type )
00614 {
00615   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00616 
00617   bool isEmpty = true;
00618   QMap<Incidence *,bool>::ConstIterator it;
00619   for ( it = map.begin(); it != map.end(); ++it ) {
00620     isEmpty = false;
00621     calendar.addIncidence( it.key()->clone() );
00622   }
00623 
00624   if ( !isEmpty ) {
00625     calendar.save( changesCacheFile( type ) );
00626   } else {
00627     QFile file( changesCacheFile( type ) );
00628     file.remove();
00629   }
00630 
00631   calendar.close();
00632 }
00633 
00634 void ResourceCached::saveChangesCache()
00635 {
00636   saveChangesCache( d->mAddedIncidences, "added" );
00637   saveChangesCache( d->mDeletedIncidences, "deleted" );
00638   saveChangesCache( d->mChangedIncidences, "changed" );
00639 }
00640 
00641 void ResourceCached::loadChangesCache( QMap<Incidence *, bool> &map, const QString &type )
00642 {
00643   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00644 
00645   if ( KStandardDirs::exists( changesCacheFile( type ) ) ) {
00646     calendar.load( changesCacheFile( type ) );
00647   } else {
00648     return;
00649   }
00650 
00651   const Incidence::List list = calendar.incidences();
00652   Incidence::List::ConstIterator it;
00653   for ( it = list.begin(); it != list.end(); ++it ) {
00654     map.insert( (*it)->clone(), true );
00655   }
00656 
00657   calendar.close();
00658 }
00659 
00660 void ResourceCached::loadChangesCache()
00661 {
00662   loadChangesCache( d->mAddedIncidences, "added" );
00663   loadChangesCache( d->mDeletedIncidences, "deleted" );
00664   loadChangesCache( d->mChangedIncidences, "changed" );
00665 }
00666 
00667 void ResourceCached::calendarIncidenceAdded( Incidence *i )
00668 {
00669   kDebug() << i->uid();
00670 
00671   QMap<Incidence *,bool>::ConstIterator it;
00672   it = d->mAddedIncidences.constFind( i );
00673   if ( it == d->mAddedIncidences.constEnd() ) {
00674     d->mAddedIncidences.insert( i, true );
00675   }
00676 
00677   checkForAutomaticSave();
00678 }
00679 
00680 void ResourceCached::calendarIncidenceChanged( Incidence *i )
00681 {
00682   kDebug() << i->uid();
00683 
00684   QMap<Incidence *,bool>::ConstIterator it;
00685   it = d->mChangedIncidences.constFind( i );
00686   // FIXME: If you modify an added incidence, there's no need to add it to d->mChangedIncidences!
00687   if ( it == d->mChangedIncidences.constEnd() ) {
00688     d->mChangedIncidences.insert( i, true );
00689   }
00690 
00691   checkForAutomaticSave();
00692 }
00693 
00694 void ResourceCached::calendarIncidenceDeleted( Incidence *i )
00695 {
00696   kDebug() << i->uid();
00697 
00698   QMap<Incidence *,bool>::ConstIterator it;
00699   it = d->mDeletedIncidences.constFind( i );
00700   if ( it == d->mDeletedIncidences.constEnd() ) {
00701     d->mDeletedIncidences.insert( i, true );
00702   }
00703 
00704   checkForAutomaticSave();
00705 }
00706 
00707 Incidence::List ResourceCached::addedIncidences() const
00708 {
00709   Incidence::List added;
00710   QMap<Incidence *,bool>::ConstIterator it;
00711   for ( it = d->mAddedIncidences.constBegin(); it != d->mAddedIncidences.constEnd(); ++it ) {
00712     added.append( it.key() );
00713   }
00714   return added;
00715 }
00716 
00717 Incidence::List ResourceCached::changedIncidences() const
00718 {
00719   Incidence::List changed;
00720   QMap<Incidence *,bool>::ConstIterator it;
00721   for ( it = d->mChangedIncidences.constBegin(); it != d->mChangedIncidences.constEnd(); ++it ) {
00722     changed.append( it.key() );
00723   }
00724   return changed;
00725 }
00726 
00727 Incidence::List ResourceCached::deletedIncidences() const
00728 {
00729   Incidence::List deleted;
00730   QMap<Incidence *,bool>::ConstIterator it;
00731   for ( it = d->mDeletedIncidences.constBegin(); it != d->mDeletedIncidences.constEnd(); ++it ) {
00732     deleted.append( it.key() );
00733   }
00734   return deleted;
00735 }
00736 
00737 Incidence::List ResourceCached::allChanges() const
00738 {
00739   Incidence::List changes;
00740   QMap<Incidence *,bool>::ConstIterator it;
00741   for ( it = d->mAddedIncidences.constBegin(); it != d->mAddedIncidences.constEnd(); ++it ) {
00742     changes.append( it.key() );
00743   }
00744   for ( it = d->mChangedIncidences.constBegin(); it != d->mChangedIncidences.constEnd(); ++it ) {
00745     changes.append( it.key() );
00746   }
00747   for ( it = d->mDeletedIncidences.constBegin(); it != d->mDeletedIncidences.constEnd(); ++it ) {
00748     changes.append( it.key() );
00749   }
00750   return changes;
00751 }
00752 
00753 bool ResourceCached::hasChanges() const
00754 {
00755   return !( d->mAddedIncidences.isEmpty() && d->mChangedIncidences.isEmpty() &&
00756             d->mDeletedIncidences.isEmpty() );
00757 }
00758 
00759 void ResourceCached::clearChange( Incidence *incidence )
00760 {
00761   clearChange( incidence->uid() );
00762 }
00763 
00764 void ResourceCached::clearChange( const QString &uid )
00765 {
00766   QMap<Incidence *, bool>::Iterator it;
00767 
00768   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00769     if ( it.key()->uid() == uid ) {
00770       d->mAddedIncidences.erase( it );
00771       break;
00772     }
00773   }
00774 
00775   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00776     if ( it.key()->uid() == uid ) {
00777       d->mChangedIncidences.erase( it );
00778       break;
00779     }
00780   }
00781 
00782   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00783     if ( it.key()->uid() == uid ) {
00784       d->mDeletedIncidences.erase( it );
00785       break;
00786     }
00787   }
00788 }
00789 
00790 void ResourceCached::enableChangeNotification()
00791 {
00792   d->mCalendar.registerObserver( this );
00793 }
00794 
00795 void ResourceCached::disableChangeNotification()
00796 {
00797   d->mCalendar.unregisterObserver( this );
00798 }
00799 
00800 void ResourceCached::slotReload()
00801 {
00802   if ( !isActive() ) {
00803     return;
00804   }
00805 
00806   kDebug();
00807 
00808   load( SyncCache );
00809 }
00810 
00811 void ResourceCached::slotSave()
00812 {
00813   if ( !isActive() ) {
00814     return;
00815   }
00816 
00817   kDebug();
00818 
00819   save( SyncCache );
00820 }
00821 
00822 void ResourceCached::checkForAutomaticSave()
00823 {
00824   if ( d->mSavePolicy == SaveAlways )  {
00825     kDebug() << "save now";
00826     d->mSavePending = true;
00827     d->mSaveTimer.setSingleShot( true );
00828     d->mSaveTimer.start( 1 * 1000 ); // 1 second
00829   } else if ( d->mSavePolicy == SaveDelayed ) {
00830     kDebug() << "save delayed";
00831     d->mSavePending = true;
00832     d->mSaveTimer.setSingleShot( true );
00833     d->mSaveTimer.start( 15 * 1000 ); // 15 seconds
00834   }
00835 }
00836 
00837 bool ResourceCached::checkForReload()
00838 {
00839   if ( d->mReloadPolicy == ReloadNever ) {
00840     return false;
00841   }
00842   if ( d->mReloadPolicy == ReloadOnStartup ) {
00843     return !d->mReloaded;
00844   }
00845   return true;
00846 }
00847 
00848 bool ResourceCached::checkForSave()
00849 {
00850   if ( d->mSavePolicy == SaveNever ) {
00851     return false;
00852   }
00853   return true;
00854 }
00855 
00856 void ResourceCached::addInfoText( QString &txt ) const
00857 {
00858   if ( d->mLastLoad.isValid() ) {
00859     txt += "<br>";
00860     txt += i18n( "Last loaded: %1",
00861                  KGlobal::locale()->formatDateTime( d->mLastLoad.toUtc().dateTime() ) );
00862   }
00863   if ( d->mLastSave.isValid() ) {
00864     txt += "<br>";
00865     txt += i18n( "Last saved: %1",
00866                  KGlobal::locale()->formatDateTime( d->mLastSave.toUtc().dateTime() ) );
00867   }
00868 }
00869 
00870 void ResourceCached::doClose()
00871 {
00872   if ( d->mSavePending ) {
00873     d->mSaveTimer.stop();
00874   }
00875   if ( d->mSavePending  ||  d->mSavePolicy == SaveOnExit  ||  d->mSavePolicy == SaveInterval ) {
00876     save( SyncCache );
00877   }
00878   d->mCalendar.close();
00879 }
00880 
00881 bool ResourceCached::doOpen()
00882 {
00883   kDebug() << "Opening resource" << resourceName();
00884   return true;
00885 }
00886 
00887 void KCal::ResourceCached::setOwner( const Person &owner )
00888 {
00889   d->mCalendar.setOwner( owner );
00890 }
00891 
00892 Person KCal::ResourceCached::owner() const
00893 {
00894   return d->mCalendar.owner();
00895 }

KCal Library

Skip menu "KCal Library"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

KDE-PIM Libraries

Skip menu "KDE-PIM Libraries"
  • akonadi
  • kabc
  • kblog
  • kcal
  • kimap
  • kioslave
  •   imap4
  •   mbox
  • kldap
  • kmime
  • kpimidentities
  •   richtextbuilders
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal