• Skip to content
  • Skip to link menu
KDE 4.1 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   return success;
00429 }
00430 
00431 bool ResourceCached::load()
00432 {
00433   return load( SyncCache );
00434 }
00435 
00436 bool ResourceCached::loadFromCache()
00437 {
00438   setIdMapperIdentifier();
00439   d->mIdMapper.load();
00440 
00441   if ( !KStandardDirs::exists( cacheFile() ) ) {
00442     return false;
00443   }
00444   d->mCalendar.load( cacheFile() );
00445   if ( !noReadOnlyOnLoad() && readOnly() ) {
00446     Incidence::List incidences( rawIncidences() );
00447     Incidence::List::Iterator it;
00448     for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00449       (*it)->setReadOnly( true );
00450     }
00451   }
00452   return true;
00453 }
00454 
00455 bool ResourceCached::save( CacheAction action, Incidence *incidence )
00456 {
00457   if ( !incidence && ( d->mSavePolicy == SaveAlways || d->mSavePolicy == SaveDelayed ) ) {
00458     d->mSaveTimer.stop();   // in case it's called manually while save is pending
00459   }
00460   d->mSavePending = false;
00461   if ( saveInhibited() ) {
00462     return true;
00463   }
00464   if ( !readOnly() ) {
00465     kDebug() << "Save resource" << resourceName();
00466 
00467     setReceivedSaveError( false );
00468 
00469     if ( !isOpen() ) {
00470       return true;
00471     }
00472     bool upload = false;
00473     switch ( action ) {
00474     case DefaultCache:
00475       upload = checkForSave();
00476       break;
00477     case NoSyncCache:
00478       break;
00479     case SyncCache:
00480       upload = true;
00481       break;
00482     }
00483     bool success = incidence ? doSave( upload, incidence ) : doSave( upload );
00484     if ( !success && !receivedSaveError() ) {
00485       saveError();
00486     }
00487     return success;
00488   } else {
00489     // Read-only, just don't save...
00490     kDebug() << "Don't save read-only resource" << resourceName();
00491     return true;
00492   }
00493 }
00494 
00495 bool ResourceCached::save( Incidence *incidence )
00496 {
00497   return save( SyncCache, incidence );
00498 }
00499 
00500 bool ResourceCached::doSave( bool syncCache, Incidence *incidence )
00501 {
00502   Q_UNUSED( incidence );
00503   return doSave( syncCache );
00504 }
00505 
00506 void ResourceCached::saveToCache()
00507 {
00508   kDebug() << cacheFile();
00509 
00510   setIdMapperIdentifier();
00511   d->mIdMapper.save();
00512 
00513   d->mCalendar.save( cacheFile() );
00514 }
00515 
00516 void ResourceCached::setIdMapperIdentifier()
00517 {
00518   d->mIdMapper.setIdentifier( type() + '_' + identifier() );
00519 }
00520 
00521 void ResourceCached::clearCache()
00522 {
00523   d->mCalendar.close();
00524 }
00525 
00526 void ResourceCached::cleanUpEventCache( const Event::List &eventList )
00527 {
00528   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00529 
00530   if ( KStandardDirs::exists( cacheFile() ) ) {
00531     calendar.load( cacheFile() );
00532   } else {
00533     return;
00534   }
00535 
00536   Event::List list = calendar.events();
00537   Event::List::ConstIterator cacheIt, it;
00538   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00539     bool found = false;
00540     for ( it = eventList.begin(); it != eventList.end(); ++it ) {
00541       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00542         found = true;
00543       }
00544     }
00545 
00546     if ( !found ) {
00547       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00548       Event *event = d->mCalendar.event( (*cacheIt)->uid() );
00549       if ( event ) {
00550         d->mCalendar.deleteEvent( event );
00551       }
00552     }
00553   }
00554 
00555   calendar.close();
00556 }
00557 
00558 void ResourceCached::cleanUpTodoCache( const Todo::List &todoList )
00559 {
00560   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00561 
00562   if ( KStandardDirs::exists( cacheFile() ) ) {
00563     calendar.load( cacheFile() );
00564   } else {
00565     return;
00566   }
00567 
00568   Todo::List list = calendar.todos();
00569   Todo::List::ConstIterator cacheIt, it;
00570   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00571 
00572     bool found = false;
00573     for ( it = todoList.begin(); it != todoList.end(); ++it ) {
00574       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00575         found = true;
00576       }
00577     }
00578 
00579     if ( !found ) {
00580       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00581       Todo *todo = d->mCalendar.todo( (*cacheIt)->uid() );
00582       if ( todo ) {
00583         d->mCalendar.deleteTodo( todo );
00584       }
00585     }
00586   }
00587 
00588   calendar.close();
00589 }
00590 
00591 KRES::IdMapper &ResourceCached::idMapper()
00592 {
00593   return d->mIdMapper;
00594 }
00595 
00596 QString ResourceCached::cacheFile() const
00597 {
00598   return KStandardDirs::locateLocal( "cache", "kcal/kresources/" + identifier() );
00599 }
00600 
00601 QString ResourceCached::changesCacheFile( const QString &type ) const
00602 {
00603   return KStandardDirs::locateLocal( "cache", "kcal/changescache/" + identifier() + '_' + type );
00604 }
00605 
00606 void ResourceCached::saveChangesCache( const QMap<Incidence *, bool> &map, const QString &type )
00607 {
00608   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00609 
00610   bool isEmpty = true;
00611   QMap<Incidence *,bool>::ConstIterator it;
00612   for ( it = map.begin(); it != map.end(); ++it ) {
00613     isEmpty = false;
00614     calendar.addIncidence( it.key()->clone() );
00615   }
00616 
00617   if ( !isEmpty ) {
00618     calendar.save( changesCacheFile( type ) );
00619   } else {
00620     QFile file( changesCacheFile( type ) );
00621     file.remove();
00622   }
00623 
00624   calendar.close();
00625 }
00626 
00627 void ResourceCached::saveChangesCache()
00628 {
00629   saveChangesCache( d->mAddedIncidences, "added" );
00630   saveChangesCache( d->mDeletedIncidences, "deleted" );
00631   saveChangesCache( d->mChangedIncidences, "changed" );
00632 }
00633 
00634 void ResourceCached::loadChangesCache( QMap<Incidence *, bool> &map, const QString &type )
00635 {
00636   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00637 
00638   if ( KStandardDirs::exists( changesCacheFile( type ) ) ) {
00639     calendar.load( changesCacheFile( type ) );
00640   } else {
00641     return;
00642   }
00643 
00644   const Incidence::List list = calendar.incidences();
00645   Incidence::List::ConstIterator it;
00646   for ( it = list.begin(); it != list.end(); ++it ) {
00647     map.insert( (*it)->clone(), true );
00648   }
00649 
00650   calendar.close();
00651 }
00652 
00653 void ResourceCached::loadChangesCache()
00654 {
00655   loadChangesCache( d->mAddedIncidences, "added" );
00656   loadChangesCache( d->mDeletedIncidences, "deleted" );
00657   loadChangesCache( d->mChangedIncidences, "changed" );
00658 }
00659 
00660 void ResourceCached::calendarIncidenceAdded( Incidence *i )
00661 {
00662   kDebug() << i->uid();
00663 
00664   QMap<Incidence *,bool>::ConstIterator it;
00665   it = d->mAddedIncidences.find( i );
00666   if ( it == d->mAddedIncidences.end() ) {
00667     d->mAddedIncidences.insert( i, true );
00668   }
00669 
00670   checkForAutomaticSave();
00671 }
00672 
00673 void ResourceCached::calendarIncidenceChanged( Incidence *i )
00674 {
00675   kDebug() << i->uid();
00676 
00677   QMap<Incidence *,bool>::ConstIterator it;
00678   it = d->mChangedIncidences.find( i );
00679   // FIXME: If you modify an added incidence, there's no need to add it to d->mChangedIncidences!
00680   if ( it == d->mChangedIncidences.end() ) {
00681     d->mChangedIncidences.insert( i, true );
00682   }
00683 
00684   checkForAutomaticSave();
00685 }
00686 
00687 void ResourceCached::calendarIncidenceDeleted( Incidence *i )
00688 {
00689   kDebug() << i->uid();
00690 
00691   QMap<Incidence *,bool>::ConstIterator it;
00692   it = d->mDeletedIncidences.find( i );
00693   if ( it == d->mDeletedIncidences.end() ) {
00694     d->mDeletedIncidences.insert( i, true );
00695   }
00696 
00697   checkForAutomaticSave();
00698 }
00699 
00700 Incidence::List ResourceCached::addedIncidences() const
00701 {
00702   Incidence::List added;
00703   QMap<Incidence *,bool>::ConstIterator it;
00704   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00705     added.append( it.key() );
00706   }
00707   return added;
00708 }
00709 
00710 Incidence::List ResourceCached::changedIncidences() const
00711 {
00712   Incidence::List changed;
00713   QMap<Incidence *,bool>::ConstIterator it;
00714   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00715     changed.append( it.key() );
00716   }
00717   return changed;
00718 }
00719 
00720 Incidence::List ResourceCached::deletedIncidences() const
00721 {
00722   Incidence::List deleted;
00723   QMap<Incidence *,bool>::ConstIterator it;
00724   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00725     deleted.append( it.key() );
00726   }
00727   return deleted;
00728 }
00729 
00730 Incidence::List ResourceCached::allChanges() const
00731 {
00732   Incidence::List changes;
00733   QMap<Incidence *,bool>::ConstIterator it;
00734   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00735     changes.append( it.key() );
00736   }
00737   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00738     changes.append( it.key() );
00739   }
00740   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00741     changes.append( it.key() );
00742   }
00743   return changes;
00744 }
00745 
00746 bool ResourceCached::hasChanges() const
00747 {
00748   return !( d->mAddedIncidences.isEmpty() && d->mChangedIncidences.isEmpty() &&
00749             d->mDeletedIncidences.isEmpty() );
00750 }
00751 
00752 void ResourceCached::clearChange( Incidence *incidence )
00753 {
00754   clearChange( incidence->uid() );
00755 }
00756 
00757 void ResourceCached::clearChange( const QString &uid )
00758 {
00759   QMap<Incidence *, bool>::Iterator it;
00760 
00761   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00762     if ( it.key()->uid() == uid ) {
00763       d->mAddedIncidences.erase( it );
00764       break;
00765     }
00766   }
00767 
00768   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00769     if ( it.key()->uid() == uid ) {
00770       d->mChangedIncidences.erase( it );
00771       break;
00772     }
00773   }
00774 
00775   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00776     if ( it.key()->uid() == uid ) {
00777       d->mDeletedIncidences.erase( it );
00778       break;
00779     }
00780   }
00781 }
00782 
00783 void ResourceCached::enableChangeNotification()
00784 {
00785   d->mCalendar.registerObserver( this );
00786 }
00787 
00788 void ResourceCached::disableChangeNotification()
00789 {
00790   d->mCalendar.unregisterObserver( this );
00791 }
00792 
00793 void ResourceCached::slotReload()
00794 {
00795   if ( !isActive() ) {
00796     return;
00797   }
00798 
00799   kDebug();
00800 
00801   load( SyncCache );
00802 }
00803 
00804 void ResourceCached::slotSave()
00805 {
00806   if ( !isActive() ) {
00807     return;
00808   }
00809 
00810   kDebug();
00811 
00812   save( SyncCache );
00813 }
00814 
00815 void ResourceCached::checkForAutomaticSave()
00816 {
00817   if ( d->mSavePolicy == SaveAlways )  {
00818     kDebug() << "save now";
00819     d->mSavePending = true;
00820     d->mSaveTimer.setSingleShot( true );
00821     d->mSaveTimer.start( 1 * 1000 ); // 1 second
00822   } else if ( d->mSavePolicy == SaveDelayed ) {
00823     kDebug() << "save delayed";
00824     d->mSavePending = true;
00825     d->mSaveTimer.setSingleShot( true );
00826     d->mSaveTimer.start( 15 * 1000 ); // 15 seconds
00827   }
00828 }
00829 
00830 bool ResourceCached::checkForReload()
00831 {
00832   if ( d->mReloadPolicy == ReloadNever ) {
00833     return false;
00834   }
00835   if ( d->mReloadPolicy == ReloadOnStartup ) {
00836     return !d->mReloaded;
00837   }
00838   return true;
00839 }
00840 
00841 bool ResourceCached::checkForSave()
00842 {
00843   if ( d->mSavePolicy == SaveNever ) {
00844     return false;
00845   }
00846   return true;
00847 }
00848 
00849 void ResourceCached::addInfoText( QString &txt ) const
00850 {
00851   if ( d->mLastLoad.isValid() ) {
00852     txt += "<br>";
00853     txt += i18n( "Last loaded: %1",
00854                  KGlobal::locale()->formatDateTime( d->mLastLoad.toUtc().dateTime() ) );
00855   }
00856   if ( d->mLastSave.isValid() ) {
00857     txt += "<br>";
00858     txt += i18n( "Last saved: %1",
00859                  KGlobal::locale()->formatDateTime( d->mLastSave.toUtc().dateTime() ) );
00860   }
00861 }
00862 
00863 void ResourceCached::doClose()
00864 {
00865   if ( d->mSavePending ) {
00866     d->mSaveTimer.stop();
00867   }
00868   if ( d->mSavePending  ||  d->mSavePolicy == SaveOnExit  ||  d->mSavePolicy == SaveInterval ) {
00869     save( SyncCache );
00870   }
00871   d->mCalendar.close();
00872 }
00873 
00874 bool ResourceCached::doOpen()
00875 {
00876   kDebug() << "Opening resource" << resourceName();
00877   return true;
00878 }
00879 
00880 void KCal::ResourceCached::setOwner( const Person &owner )
00881 {
00882   d->mCalendar.setOwner( owner );
00883 }
00884 
00885 Person KCal::ResourceCached::owner() const
00886 {
00887   return d->mCalendar.owner();
00888 }

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
  • kpimutils
  • kresources
  • ktnef
  • kxmlrpcclient
  • mailtransport
  • qgpgme
  • syndication
  •   atom
  •   rdf
  •   rss2
Generated for KDE-PIM Libraries by doxygen 1.5.4
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