• Skip to content
  • Skip to link menu
KDE 4.0 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 <kdebug.h>
00034 #include <klocale.h>
00035 #include <kurl.h>
00036 #include <kstandarddirs.h>
00037 #include <kconfiggroup.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(5800) << "ResourceCached::setSavePolicy(): start save timer (interval"
00205               << d->mSaveInterval << "minutes).";
00206     d->mSaveTimer.start( d->mSaveInterval * 60 * 1000 ); // n minutes
00207   } else {
00208     d->mSaveTimer.stop();
00209   }
00210 }
00211 
00212 void ResourceCached::setupReloadTimer()
00213 {
00214   if ( d->mReloadPolicy == ReloadInterval ) {
00215     kDebug(5800) << "ResourceCached::setSavePolicy(): start reload timer (interval"
00216                  << d->mReloadInterval << "minutes)";
00217     d->mReloadTimer.start( d->mReloadInterval * 60 * 1000 ); // n minutes
00218   } else {
00219     d->mReloadTimer.stop();
00220   }
00221 }
00222 
00223 void ResourceCached::writeConfig( KConfigGroup &group )
00224 {
00225   group.writeEntry( "ReloadPolicy", d->mReloadPolicy );
00226   group.writeEntry( "ReloadInterval", d->mReloadInterval );
00227 
00228   group.writeEntry( "SavePolicy", d->mSavePolicy );
00229   group.writeEntry( "SaveInterval", d->mSaveInterval );
00230 
00231   group.writeEntry( "LastLoad", d->mLastLoad.toUtc().dateTime() );
00232   group.writeEntry( "LastSave", d->mLastSave.toUtc().dateTime() );
00233 }
00234 
00235 bool ResourceCached::addEvent( Event *event )
00236 {
00237   return d->mCalendar.addEvent( event );
00238 }
00239 
00240 // probably not really efficient, but...it works for now.
00241 bool ResourceCached::deleteEvent( Event *event )
00242 {
00243   kDebug(5800) << "ResourceCached::deleteEvent";
00244 
00245   return d->mCalendar.deleteEvent( event );
00246 }
00247 
00248 void ResourceCached::deleteAllEvents()
00249 {
00250   d->mCalendar.deleteAllEvents();
00251 }
00252 
00253 Event *ResourceCached::event( const QString &uid )
00254 {
00255   return d->mCalendar.event( uid );
00256 }
00257 
00258 Event::List ResourceCached::rawEventsForDate( const QDate &qd, const KDateTime::Spec &timespec,
00259                                               EventSortField sortField,
00260                                               SortDirection sortDirection )
00261 {
00262   Event::List list = d->mCalendar.rawEventsForDate( qd, timespec, sortField, sortDirection );
00263 
00264   return list;
00265 }
00266 
00267 Event::List ResourceCached::rawEvents( const QDate &start, const QDate &end,
00268                                        const KDateTime::Spec &timespec, bool inclusive )
00269 {
00270   return d->mCalendar.rawEvents( start, end, timespec, inclusive );
00271 }
00272 
00273 Event::List ResourceCached::rawEventsForDate( const KDateTime &kdt )
00274 {
00275   return d->mCalendar.rawEventsForDate( kdt );
00276 }
00277 
00278 Event::List ResourceCached::rawEvents( EventSortField sortField, SortDirection sortDirection )
00279 {
00280   return d->mCalendar.rawEvents( sortField, sortDirection );
00281 }
00282 
00283 bool ResourceCached::addTodo( Todo *todo )
00284 {
00285   return d->mCalendar.addTodo( todo );
00286 }
00287 
00288 bool ResourceCached::deleteTodo( Todo *todo )
00289 {
00290   return d->mCalendar.deleteTodo( todo );
00291 }
00292 
00293 void ResourceCached::deleteAllTodos()
00294 {
00295   d->mCalendar.deleteAllTodos();
00296 }
00297 
00298 bool ResourceCached::deleteJournal( Journal *journal )
00299 {
00300   return d->mCalendar.deleteJournal( journal );
00301 }
00302 
00303 void ResourceCached::deleteAllJournals()
00304 {
00305   d->mCalendar.deleteAllJournals();
00306 }
00307 
00308 Todo::List ResourceCached::rawTodos( TodoSortField sortField, SortDirection sortDirection )
00309 {
00310   return d->mCalendar.rawTodos( sortField, sortDirection );
00311 }
00312 
00313 Todo *ResourceCached::todo( const QString &uid )
00314 {
00315   return d->mCalendar.todo( uid );
00316 }
00317 
00318 Todo::List ResourceCached::rawTodosForDate( const QDate &date )
00319 {
00320   return d->mCalendar.rawTodosForDate( date );
00321 }
00322 
00323 bool ResourceCached::addJournal( Journal *journal )
00324 {
00325   kDebug(5800) << "Adding Journal on" << journal->dtStart().toString();
00326 
00327   return d->mCalendar.addJournal( journal );
00328 }
00329 
00330 Journal *ResourceCached::journal( const QString &uid )
00331 {
00332   return d->mCalendar.journal( uid );
00333 }
00334 
00335 Journal::List ResourceCached::rawJournals( JournalSortField sortField, SortDirection sortDirection )
00336 {
00337   return d->mCalendar.rawJournals( sortField, sortDirection );
00338 }
00339 
00340 Journal::List ResourceCached::rawJournalsForDate( const QDate &date )
00341 {
00342   return d->mCalendar.rawJournalsForDate( date );
00343 }
00344 
00345 Alarm::List ResourceCached::alarmsTo( const KDateTime &to )
00346 {
00347   return d->mCalendar.alarmsTo( to );
00348 }
00349 
00350 Alarm::List ResourceCached::alarms( const KDateTime &from, const KDateTime &to )
00351 {
00352 //  kDebug(5800) << "ResourceCached::alarms(" << from.toString() << "-" << to.toString() << ")";
00353 
00354   return d->mCalendar.alarms( from, to );
00355 }
00356 
00357 void ResourceCached::setTimeSpec( const KDateTime::Spec &timeSpec )
00358 {
00359   d->mCalendar.setTimeSpec( timeSpec );
00360 }
00361 
00362 KDateTime::Spec ResourceCached::timeSpec() const
00363 {
00364   return d->mCalendar.timeSpec();
00365 }
00366 
00367 void ResourceCached::setTimeZoneId( const QString &tzid )
00368 {
00369   d->mCalendar.setTimeZoneId( tzid );
00370 }
00371 
00372 QString ResourceCached::timeZoneId() const
00373 {
00374   return d->mCalendar.timeZoneId();
00375 }
00376 
00377 void ResourceCached::shiftTimes( const KDateTime::Spec &oldSpec, const KDateTime::Spec &newSpec )
00378 {
00379   d->mCalendar.shiftTimes( oldSpec, newSpec );
00380 }
00381 
00382 void ResourceCached::clearChanges()
00383 {
00384   d->mAddedIncidences.clear();
00385   d->mChangedIncidences.clear();
00386   d->mDeletedIncidences.clear();
00387 }
00388 
00389 bool ResourceCached::load( CacheAction action )
00390 {
00391   kDebug(5800) << "Loading resource" << resourceName();
00392 
00393   setReceivedLoadError( false );
00394 
00395   bool success = true;
00396   if ( !isOpen() ) {
00397     success = open();
00398   }
00399   if ( success ) {
00400     bool update = false;
00401     switch ( action ) {
00402     case DefaultCache:
00403       if ( !d->mReloaded && !d->mInhibitReload ) {
00404         update = checkForReload();
00405       }
00406       break;
00407     case NoSyncCache:
00408       break;
00409     case SyncCache:
00410       update = true;
00411       break;
00412     }
00413     success = doLoad( update );
00414   }
00415   if ( !success && !receivedLoadError() ) {
00416     loadError();
00417   }
00418 
00419   // If the resource is read-only, we need to set its incidences to read-only,
00420   // too. This can't be done at a lower-level, since the read-only setting
00421   // happens at this level
00422   if ( !noReadOnlyOnLoad() && readOnly() ) {
00423     Incidence::List incidences( rawIncidences() );
00424     Incidence::List::Iterator it;
00425     for ( it = incidences.begin(); it != incidences.end(); ++it ) {
00426       (*it)->setReadOnly( true );
00427     }
00428   }
00429 
00430   kDebug(5800) << "Done loading resource" << resourceName();
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   d->mSavePending = false;
00462   if ( saveInhibited() ) {
00463     return true;
00464   }
00465   if ( !readOnly() ) {
00466     kDebug(5800) << "Save resource" << resourceName();
00467 
00468     setReceivedSaveError( false );
00469 
00470     if ( !isOpen() ) {
00471       return true;
00472     }
00473     bool upload = false;
00474     switch ( action ) {
00475     case DefaultCache:
00476       upload = checkForSave();
00477       break;
00478     case NoSyncCache:
00479       break;
00480     case SyncCache:
00481       upload = true;
00482       break;
00483     }
00484     bool success = incidence ? doSave( upload, incidence ) : doSave( upload );
00485     if ( !success && !receivedSaveError() ) {
00486       saveError();
00487     }
00488     return success;
00489   } else {
00490     // Read-only, just don't save...
00491     kDebug(5800) << "Don't save read-only resource" << resourceName();
00492     return true;
00493   }
00494 }
00495 
00496 bool ResourceCached::save( Incidence *incidence )
00497 {
00498   return save( SyncCache, incidence );
00499 }
00500 
00501 bool ResourceCached::doSave( bool syncCache, Incidence *incidence )
00502 {
00503   Q_UNUSED( incidence );
00504   return doSave( syncCache );
00505 }
00506 
00507 void ResourceCached::saveToCache()
00508 {
00509   kDebug(5800) << "ResourceCached::saveToCache():" << cacheFile();
00510 
00511   setIdMapperIdentifier();
00512   d->mIdMapper.save();
00513 
00514   d->mCalendar.save( cacheFile() );
00515 }
00516 
00517 void ResourceCached::setIdMapperIdentifier()
00518 {
00519   d->mIdMapper.setIdentifier( type() + '_' + identifier() );
00520 }
00521 
00522 void ResourceCached::clearCache()
00523 {
00524   d->mCalendar.close();
00525 }
00526 
00527 void ResourceCached::cleanUpEventCache( const Event::List &eventList )
00528 {
00529   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00530 
00531   if ( KStandardDirs::exists( cacheFile() ) ) {
00532     calendar.load( cacheFile() );
00533   } else {
00534     return;
00535   }
00536 
00537   Event::List list = calendar.events();
00538   Event::List::ConstIterator cacheIt, it;
00539   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00540     bool found = false;
00541     for ( it = eventList.begin(); it != eventList.end(); ++it ) {
00542       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00543         found = true;
00544       }
00545     }
00546 
00547     if ( !found ) {
00548       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00549       Event *event = d->mCalendar.event( (*cacheIt)->uid() );
00550       if ( event ) {
00551         d->mCalendar.deleteEvent( event );
00552       }
00553     }
00554   }
00555 
00556   calendar.close();
00557 }
00558 
00559 void ResourceCached::cleanUpTodoCache( const Todo::List &todoList )
00560 {
00561   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00562 
00563   if ( KStandardDirs::exists( cacheFile() ) ) {
00564     calendar.load( cacheFile() );
00565   } else {
00566     return;
00567   }
00568 
00569   Todo::List list = calendar.todos();
00570   Todo::List::ConstIterator cacheIt, it;
00571   for ( cacheIt = list.begin(); cacheIt != list.end(); ++cacheIt ) {
00572 
00573     bool found = false;
00574     for ( it = todoList.begin(); it != todoList.end(); ++it ) {
00575       if ( (*it)->uid() == (*cacheIt)->uid() ) {
00576         found = true;
00577       }
00578     }
00579 
00580     if ( !found ) {
00581       d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) );
00582       Todo *todo = d->mCalendar.todo( (*cacheIt)->uid() );
00583       if ( todo ) {
00584         d->mCalendar.deleteTodo( todo );
00585       }
00586     }
00587   }
00588 
00589   calendar.close();
00590 }
00591 
00592 KRES::IdMapper &ResourceCached::idMapper()
00593 {
00594   return d->mIdMapper;
00595 }
00596 
00597 QString ResourceCached::cacheFile() const
00598 {
00599   return KStandardDirs::locateLocal( "cache", "kcal/kresources/" + identifier() );
00600 }
00601 
00602 QString ResourceCached::changesCacheFile( const QString &type ) const
00603 {
00604   return KStandardDirs::locateLocal( "cache", "kcal/changescache/" + identifier() + '_' + type );
00605 }
00606 
00607 void ResourceCached::saveChangesCache( const QMap<Incidence *, bool> &map, const QString &type )
00608 {
00609   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00610 
00611   bool isEmpty = true;
00612   QMap<Incidence *,bool>::ConstIterator it;
00613   for ( it = map.begin(); it != map.end(); ++it ) {
00614     isEmpty = false;
00615     calendar.addIncidence( it.key()->clone() );
00616   }
00617 
00618   if ( !isEmpty ) {
00619     calendar.save( changesCacheFile( type ) );
00620   } else {
00621     QFile file( changesCacheFile( type ) );
00622     file.remove();
00623   }
00624 
00625   calendar.close();
00626 }
00627 
00628 void ResourceCached::saveChangesCache()
00629 {
00630   saveChangesCache( d->mAddedIncidences, "added" );
00631   saveChangesCache( d->mDeletedIncidences, "deleted" );
00632   saveChangesCache( d->mChangedIncidences, "changed" );
00633 }
00634 
00635 void ResourceCached::loadChangesCache( QMap<Incidence *, bool> &map, const QString &type )
00636 {
00637   CalendarLocal calendar ( QLatin1String( "UTC" ) );
00638 
00639   if ( KStandardDirs::exists( changesCacheFile( type ) ) ) {
00640     calendar.load( changesCacheFile( type ) );
00641   } else {
00642     return;
00643   }
00644 
00645   const Incidence::List list = calendar.incidences();
00646   Incidence::List::ConstIterator it;
00647   for ( it = list.begin(); it != list.end(); ++it ) {
00648     map.insert( (*it)->clone(), true );
00649   }
00650 
00651   calendar.close();
00652 }
00653 
00654 void ResourceCached::loadChangesCache()
00655 {
00656   loadChangesCache( d->mAddedIncidences, "added" );
00657   loadChangesCache( d->mDeletedIncidences, "deleted" );
00658   loadChangesCache( d->mChangedIncidences, "changed" );
00659 }
00660 
00661 void ResourceCached::calendarIncidenceAdded( Incidence *i )
00662 {
00663 #if 1
00664   kDebug(5800) << "ResourceCached::calendarIncidenceAdded():"
00665             << i->uid();
00666 #endif
00667 
00668   QMap<Incidence *,bool>::ConstIterator it;
00669   it = d->mAddedIncidences.find( i );
00670   if ( it == d->mAddedIncidences.end() ) {
00671     d->mAddedIncidences.insert( i, true );
00672   }
00673 
00674   checkForAutomaticSave();
00675 }
00676 
00677 void ResourceCached::calendarIncidenceChanged( Incidence *i )
00678 {
00679 #if 1
00680   kDebug(5800) << "ResourceCached::calendarIncidenceChanged():"
00681             << i->uid();
00682 #endif
00683 
00684   QMap<Incidence *,bool>::ConstIterator it;
00685   it = d->mChangedIncidences.find( i );
00686   // FIXME: If you modify an added incidence, there's no need to add it to d->mChangedIncidences!
00687   if ( it == d->mChangedIncidences.end() ) {
00688     d->mChangedIncidences.insert( i, true );
00689   }
00690 
00691   checkForAutomaticSave();
00692 }
00693 
00694 void ResourceCached::calendarIncidenceDeleted( Incidence *i )
00695 {
00696 #if 1
00697   kDebug(5800) << "ResourceCached::calendarIncidenceDeleted():"
00698             << i->uid();
00699 #endif
00700 
00701   QMap<Incidence *,bool>::ConstIterator it;
00702   it = d->mDeletedIncidences.find( i );
00703   if ( it == d->mDeletedIncidences.end() ) {
00704     d->mDeletedIncidences.insert( i, true );
00705   }
00706 
00707   checkForAutomaticSave();
00708 }
00709 
00710 Incidence::List ResourceCached::addedIncidences() const
00711 {
00712   Incidence::List added;
00713   QMap<Incidence *,bool>::ConstIterator it;
00714   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00715     added.append( it.key() );
00716   }
00717   return added;
00718 }
00719 
00720 Incidence::List ResourceCached::changedIncidences() const
00721 {
00722   Incidence::List changed;
00723   QMap<Incidence *,bool>::ConstIterator it;
00724   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00725     changed.append( it.key() );
00726   }
00727   return changed;
00728 }
00729 
00730 Incidence::List ResourceCached::deletedIncidences() const
00731 {
00732   Incidence::List deleted;
00733   QMap<Incidence *,bool>::ConstIterator it;
00734   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00735     deleted.append( it.key() );
00736   }
00737   return deleted;
00738 }
00739 
00740 Incidence::List ResourceCached::allChanges() const
00741 {
00742   Incidence::List changes;
00743   QMap<Incidence *,bool>::ConstIterator it;
00744   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00745     changes.append( it.key() );
00746   }
00747   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00748     changes.append( it.key() );
00749   }
00750   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00751     changes.append( it.key() );
00752   }
00753   return changes;
00754 }
00755 
00756 bool ResourceCached::hasChanges() const
00757 {
00758   return !( d->mAddedIncidences.isEmpty() && d->mChangedIncidences.isEmpty() &&
00759             d->mDeletedIncidences.isEmpty() );
00760 }
00761 
00762 void ResourceCached::clearChange( Incidence *incidence )
00763 {
00764   clearChange( incidence->uid() );
00765 }
00766 
00767 void ResourceCached::clearChange( const QString &uid )
00768 {
00769   QMap<Incidence *, bool>::Iterator it;
00770 
00771   for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) {
00772     if ( it.key()->uid() == uid ) {
00773       d->mAddedIncidences.erase( it );
00774       break;
00775     }
00776   }
00777 
00778   for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) {
00779     if ( it.key()->uid() == uid ) {
00780       d->mChangedIncidences.erase( it );
00781       break;
00782     }
00783   }
00784 
00785   for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) {
00786     if ( it.key()->uid() == uid ) {
00787       d->mDeletedIncidences.erase( it );
00788       break;
00789     }
00790   }
00791 }
00792 
00793 void ResourceCached::enableChangeNotification()
00794 {
00795   d->mCalendar.registerObserver( this );
00796 }
00797 
00798 void ResourceCached::disableChangeNotification()
00799 {
00800   d->mCalendar.unregisterObserver( this );
00801 }
00802 
00803 void ResourceCached::slotReload()
00804 {
00805   if ( !isActive() ) {
00806     return;
00807   }
00808 
00809   kDebug(5800) << "ResourceCached::slotReload()";
00810 
00811   load( SyncCache );
00812 }
00813 
00814 void ResourceCached::slotSave()
00815 {
00816   if ( !isActive() ) {
00817     return;
00818   }
00819 
00820   kDebug(5800) << "ResourceCached::slotSave()";
00821 
00822   save( SyncCache );
00823 }
00824 
00825 void ResourceCached::checkForAutomaticSave()
00826 {
00827   if ( d->mSavePolicy == SaveAlways )  {
00828     kDebug(5800) << "ResourceCached::checkForAutomaticSave(): save now";
00829     d->mSavePending = true;
00830     d->mSaveTimer.setSingleShot( true );
00831     d->mSaveTimer.start( 1 * 1000 ); // 1 second
00832   } else if ( d->mSavePolicy == SaveDelayed ) {
00833     kDebug(5800) << "ResourceCached::checkForAutomaticSave(): save delayed";
00834     d->mSavePending = true;
00835     d->mSaveTimer.setSingleShot( true );
00836     d->mSaveTimer.start( 15 * 1000 ); // 15 seconds
00837   }
00838 }
00839 
00840 bool ResourceCached::checkForReload()
00841 {
00842   if ( d->mReloadPolicy == ReloadNever ) {
00843     return false;
00844   }
00845   if ( d->mReloadPolicy == ReloadOnStartup ) {
00846     return !d->mReloaded;
00847   }
00848   return true;
00849 }
00850 
00851 bool ResourceCached::checkForSave()
00852 {
00853   if ( d->mSavePolicy == SaveNever ) {
00854     return false;
00855   }
00856   return true;
00857 }
00858 
00859 void ResourceCached::addInfoText( QString &txt ) const
00860 {
00861   if ( d->mLastLoad.isValid() ) {
00862     txt += "<br>";
00863     txt += i18n( "Last loaded: %1",
00864                  KGlobal::locale()->formatDateTime( d->mLastLoad.toUtc().dateTime() ) );
00865   }
00866   if ( d->mLastSave.isValid() ) {
00867     txt += "<br>";
00868     txt += i18n( "Last saved: %1",
00869                  KGlobal::locale()->formatDateTime( d->mLastSave.toUtc().dateTime() ) );
00870   }
00871 }
00872 
00873 void ResourceCached::doClose()
00874 {
00875   if ( d->mSavePending ) {
00876     d->mSaveTimer.stop();
00877   }
00878   if ( d->mSavePending  ||  d->mSavePolicy == SaveOnExit  ||  d->mSavePolicy == SaveInterval ) {
00879     save( SyncCache );
00880   }
00881   d->mCalendar.close();
00882 }
00883 
00884 bool ResourceCached::doOpen()
00885 {
00886   kDebug(5800) << "Opening resource" << resourceName();
00887   return true;
00888 }
00889 
00890 void KCal::ResourceCached::setOwner( const Person &owner )
00891 {
00892   d->mCalendar.setOwner( owner );
00893 }
00894 
00895 Person KCal::ResourceCached::owner() const
00896 {
00897   return d->mCalendar.owner();
00898 }

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"
  • 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.5
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