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 return d->mCalendar.addJournal( journal ); 00324 } 00325 00326 Journal *ResourceCached::journal( const QString &uid ) 00327 { 00328 return d->mCalendar.journal( uid ); 00329 } 00330 00331 Journal::List ResourceCached::rawJournals( JournalSortField sortField, SortDirection sortDirection ) 00332 { 00333 return d->mCalendar.rawJournals( sortField, sortDirection ); 00334 } 00335 00336 Journal::List ResourceCached::rawJournalsForDate( const QDate &date ) 00337 { 00338 return d->mCalendar.rawJournalsForDate( date ); 00339 } 00340 00341 Alarm::List ResourceCached::alarmsTo( const KDateTime &to ) 00342 { 00343 return d->mCalendar.alarmsTo( to ); 00344 } 00345 00346 Alarm::List ResourceCached::alarms( const KDateTime &from, const KDateTime &to ) 00347 { 00348 return d->mCalendar.alarms( from, to ); 00349 } 00350 00351 void ResourceCached::setTimeSpec( const KDateTime::Spec &timeSpec ) 00352 { 00353 d->mCalendar.setTimeSpec( timeSpec ); 00354 } 00355 00356 KDateTime::Spec ResourceCached::timeSpec() const 00357 { 00358 return d->mCalendar.timeSpec(); 00359 } 00360 00361 void ResourceCached::setTimeZoneId( const QString &tzid ) 00362 { 00363 d->mCalendar.setTimeZoneId( tzid ); 00364 } 00365 00366 QString ResourceCached::timeZoneId() const 00367 { 00368 return d->mCalendar.timeZoneId(); 00369 } 00370 00371 void ResourceCached::shiftTimes( const KDateTime::Spec &oldSpec, const KDateTime::Spec &newSpec ) 00372 { 00373 d->mCalendar.shiftTimes( oldSpec, newSpec ); 00374 } 00375 00376 void ResourceCached::clearChanges() 00377 { 00378 d->mAddedIncidences.clear(); 00379 d->mChangedIncidences.clear(); 00380 d->mDeletedIncidences.clear(); 00381 } 00382 00383 bool ResourceCached::load( CacheAction action ) 00384 { 00385 kDebug() << resourceName(); 00386 00387 setReceivedLoadError( false ); 00388 00389 bool success = true; 00390 if ( !isOpen() ) { 00391 success = open(); //krazy:exclude=syscalls open is a class method 00392 } 00393 if ( success ) { 00394 bool update = false; 00395 switch ( action ) { 00396 case DefaultCache: 00397 if ( !d->mReloaded && !d->mInhibitReload ) { 00398 update = checkForReload(); 00399 } 00400 break; 00401 case NoSyncCache: 00402 break; 00403 case SyncCache: 00404 update = true; 00405 break; 00406 } 00407 success = doLoad( update ); 00408 } 00409 if ( !success && !receivedLoadError() ) { 00410 loadError(); 00411 } 00412 00413 // If the resource is read-only, we need to set its incidences to read-only, 00414 // too. This can't be done at a lower-level, since the read-only setting 00415 // happens at this level 00416 if ( !noReadOnlyOnLoad() && readOnly() ) { 00417 Incidence::List incidences( rawIncidences() ); 00418 Incidence::List::Iterator it; 00419 for ( it = incidences.begin(); it != incidences.end(); ++it ) { 00420 (*it)->setReadOnly( true ); 00421 } 00422 } 00423 00424 kDebug() << "Done loading resource" << resourceName(); 00425 00426 if ( success ) { 00427 emit resourceLoaded( this ); 00428 } 00429 00430 return success; 00431 } 00432 00433 bool ResourceCached::load() 00434 { 00435 return load( SyncCache ); 00436 } 00437 00438 bool ResourceCached::loadFromCache() 00439 { 00440 setIdMapperIdentifier(); 00441 d->mIdMapper.load(); 00442 00443 if ( !KStandardDirs::exists( cacheFile() ) ) { 00444 return false; 00445 } 00446 d->mCalendar.load( cacheFile() ); 00447 if ( !noReadOnlyOnLoad() && readOnly() ) { 00448 Incidence::List incidences( rawIncidences() ); 00449 Incidence::List::Iterator it; 00450 for ( it = incidences.begin(); it != incidences.end(); ++it ) { 00451 (*it)->setReadOnly( true ); 00452 } 00453 } 00454 return true; 00455 } 00456 00457 bool ResourceCached::save( CacheAction action, Incidence *incidence ) 00458 { 00459 if ( !incidence && ( d->mSavePolicy == SaveAlways || d->mSavePolicy == SaveDelayed ) ) { 00460 d->mSaveTimer.stop(); // in case it's called manually while save is pending 00461 } 00462 d->mSavePending = false; 00463 if ( saveInhibited() ) { 00464 return true; 00465 } 00466 if ( !readOnly() ) { 00467 kDebug() << "Save resource" << resourceName(); 00468 00469 setReceivedSaveError( false ); 00470 00471 if ( !isOpen() ) { 00472 return true; 00473 } 00474 bool upload = false; 00475 switch ( action ) { 00476 case DefaultCache: 00477 upload = checkForSave(); 00478 break; 00479 case NoSyncCache: 00480 break; 00481 case SyncCache: 00482 upload = true; 00483 break; 00484 } 00485 bool success = incidence ? doSave( upload, incidence ) : doSave( upload ); 00486 if ( !success && !receivedSaveError() ) { 00487 saveError(); 00488 } else { 00489 emit resourceSaved( this ); 00490 } 00491 return success; 00492 } else { 00493 // Read-only, just don't save... 00494 kDebug() << "Don't save read-only resource" << resourceName(); 00495 return true; 00496 } 00497 } 00498 00499 bool ResourceCached::save( Incidence *incidence ) 00500 { 00501 return save( SyncCache, incidence ); 00502 } 00503 00504 bool ResourceCached::doSave( bool syncCache, Incidence *incidence ) 00505 { 00506 Q_UNUSED( incidence ); 00507 return doSave( syncCache ); 00508 } 00509 00510 void ResourceCached::saveToCache() 00511 { 00512 kDebug() << cacheFile(); 00513 00514 setIdMapperIdentifier(); 00515 d->mIdMapper.save(); 00516 00517 d->mCalendar.save( cacheFile() ); 00518 } 00519 00520 void ResourceCached::setIdMapperIdentifier() 00521 { 00522 d->mIdMapper.setIdentifier( type() + '_' + identifier() ); 00523 } 00524 00525 void ResourceCached::clearCache() 00526 { 00527 d->mCalendar.close(); 00528 } 00529 00530 void ResourceCached::cleanUpEventCache( const Event::List &eventList ) 00531 { 00532 CalendarLocal calendar ( QLatin1String( "UTC" ) ); 00533 00534 if ( KStandardDirs::exists( cacheFile() ) ) { 00535 calendar.load( cacheFile() ); 00536 } else { 00537 return; 00538 } 00539 00540 Event::List list = calendar.events(); 00541 Event::List::ConstIterator cacheIt, it; 00542 for ( cacheIt = list.constBegin(); cacheIt != list.constEnd(); ++cacheIt ) { 00543 bool found = false; 00544 for ( it = eventList.begin(); it != eventList.end(); ++it ) { 00545 if ( (*it)->uid() == (*cacheIt)->uid() ) { 00546 found = true; 00547 break; 00548 } 00549 } 00550 00551 if ( !found ) { 00552 d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) ); 00553 Event *event = d->mCalendar.event( (*cacheIt)->uid() ); 00554 if ( event ) { 00555 d->mCalendar.deleteEvent( event ); 00556 } 00557 } 00558 } 00559 00560 calendar.close(); 00561 } 00562 00563 void ResourceCached::cleanUpTodoCache( const Todo::List &todoList ) 00564 { 00565 CalendarLocal calendar ( QLatin1String( "UTC" ) ); 00566 00567 if ( KStandardDirs::exists( cacheFile() ) ) { 00568 calendar.load( cacheFile() ); 00569 } else { 00570 return; 00571 } 00572 00573 Todo::List list = calendar.todos(); 00574 Todo::List::ConstIterator cacheIt, it; 00575 for ( cacheIt = list.constBegin(); cacheIt != list.constEnd(); ++cacheIt ) { 00576 00577 bool found = false; 00578 for ( it = todoList.constBegin(); it != todoList.constEnd(); ++it ) { 00579 if ( (*it)->uid() == (*cacheIt)->uid() ) { 00580 found = true; 00581 } 00582 } 00583 00584 if ( !found ) { 00585 d->mIdMapper.removeRemoteId( d->mIdMapper.remoteId( (*cacheIt)->uid() ) ); 00586 Todo *todo = d->mCalendar.todo( (*cacheIt)->uid() ); 00587 if ( todo ) { 00588 d->mCalendar.deleteTodo( todo ); 00589 } 00590 } 00591 } 00592 00593 calendar.close(); 00594 } 00595 00596 KRES::IdMapper &ResourceCached::idMapper() 00597 { 00598 return d->mIdMapper; 00599 } 00600 00601 QString ResourceCached::cacheFile() const 00602 { 00603 return KStandardDirs::locateLocal( "cache", "kcal/kresources/" + identifier() ); 00604 } 00605 00606 QString ResourceCached::changesCacheFile( const QString &type ) const 00607 { 00608 return KStandardDirs::locateLocal( "cache", "kcal/changescache/" + identifier() + '_' + type ); 00609 } 00610 00611 void ResourceCached::saveChangesCache( const QMap<Incidence *, bool> &map, const QString &type ) 00612 { 00613 CalendarLocal calendar ( QLatin1String( "UTC" ) ); 00614 00615 bool isEmpty = true; 00616 QMap<Incidence *,bool>::ConstIterator it; 00617 for ( it = map.begin(); it != map.end(); ++it ) { 00618 isEmpty = false; 00619 calendar.addIncidence( it.key()->clone() ); 00620 } 00621 00622 if ( !isEmpty ) { 00623 calendar.save( changesCacheFile( type ) ); 00624 } else { 00625 QFile file( changesCacheFile( type ) ); 00626 file.remove(); 00627 } 00628 00629 calendar.close(); 00630 } 00631 00632 void ResourceCached::saveChangesCache() 00633 { 00634 saveChangesCache( d->mAddedIncidences, "added" ); 00635 saveChangesCache( d->mDeletedIncidences, "deleted" ); 00636 saveChangesCache( d->mChangedIncidences, "changed" ); 00637 } 00638 00639 void ResourceCached::loadChangesCache( QMap<Incidence *, bool> &map, const QString &type ) 00640 { 00641 CalendarLocal calendar ( QLatin1String( "UTC" ) ); 00642 00643 if ( KStandardDirs::exists( changesCacheFile( type ) ) ) { 00644 calendar.load( changesCacheFile( type ) ); 00645 } else { 00646 return; 00647 } 00648 00649 const Incidence::List list = calendar.incidences(); 00650 Incidence::List::ConstIterator it; 00651 for ( it = list.begin(); it != list.end(); ++it ) { 00652 map.insert( (*it)->clone(), true ); 00653 } 00654 00655 calendar.close(); 00656 } 00657 00658 void ResourceCached::loadChangesCache() 00659 { 00660 loadChangesCache( d->mAddedIncidences, "added" ); 00661 loadChangesCache( d->mDeletedIncidences, "deleted" ); 00662 loadChangesCache( d->mChangedIncidences, "changed" ); 00663 } 00664 00665 void ResourceCached::calendarIncidenceAdded( Incidence *i ) 00666 { 00667 kDebug() << i->uid(); 00668 00669 QMap<Incidence *,bool>::ConstIterator it; 00670 it = d->mAddedIncidences.constFind( i ); 00671 if ( it == d->mAddedIncidences.constEnd() ) { 00672 d->mAddedIncidences.insert( i, true ); 00673 } 00674 00675 checkForAutomaticSave(); 00676 } 00677 00678 void ResourceCached::calendarIncidenceChanged( Incidence *i ) 00679 { 00680 kDebug() << i->uid(); 00681 00682 QMap<Incidence *,bool>::ConstIterator it; 00683 it = d->mChangedIncidences.constFind( i ); 00684 // FIXME: If you modify an added incidence, there's no need to add it to d->mChangedIncidences! 00685 if ( it == d->mChangedIncidences.constEnd() ) { 00686 d->mChangedIncidences.insert( i, true ); 00687 } 00688 00689 checkForAutomaticSave(); 00690 } 00691 00692 void ResourceCached::calendarIncidenceDeleted( Incidence *i ) 00693 { 00694 kDebug() << i->uid(); 00695 00696 QMap<Incidence *,bool>::ConstIterator it; 00697 it = d->mDeletedIncidences.constFind( i ); 00698 if ( it == d->mDeletedIncidences.constEnd() ) { 00699 d->mDeletedIncidences.insert( i, true ); 00700 } 00701 00702 checkForAutomaticSave(); 00703 } 00704 00705 Incidence::List ResourceCached::addedIncidences() const 00706 { 00707 Incidence::List added; 00708 QMap<Incidence *,bool>::ConstIterator it; 00709 for ( it = d->mAddedIncidences.constBegin(); it != d->mAddedIncidences.constEnd(); ++it ) { 00710 added.append( it.key() ); 00711 } 00712 return added; 00713 } 00714 00715 Incidence::List ResourceCached::changedIncidences() const 00716 { 00717 Incidence::List changed; 00718 QMap<Incidence *,bool>::ConstIterator it; 00719 for ( it = d->mChangedIncidences.constBegin(); it != d->mChangedIncidences.constEnd(); ++it ) { 00720 changed.append( it.key() ); 00721 } 00722 return changed; 00723 } 00724 00725 Incidence::List ResourceCached::deletedIncidences() const 00726 { 00727 Incidence::List deleted; 00728 QMap<Incidence *,bool>::ConstIterator it; 00729 for ( it = d->mDeletedIncidences.constBegin(); it != d->mDeletedIncidences.constEnd(); ++it ) { 00730 deleted.append( it.key() ); 00731 } 00732 return deleted; 00733 } 00734 00735 Incidence::List ResourceCached::allChanges() const 00736 { 00737 Incidence::List changes; 00738 QMap<Incidence *,bool>::ConstIterator it; 00739 for ( it = d->mAddedIncidences.constBegin(); it != d->mAddedIncidences.constEnd(); ++it ) { 00740 changes.append( it.key() ); 00741 } 00742 for ( it = d->mChangedIncidences.constBegin(); it != d->mChangedIncidences.constEnd(); ++it ) { 00743 changes.append( it.key() ); 00744 } 00745 for ( it = d->mDeletedIncidences.constBegin(); it != d->mDeletedIncidences.constEnd(); ++it ) { 00746 changes.append( it.key() ); 00747 } 00748 return changes; 00749 } 00750 00751 bool ResourceCached::hasChanges() const 00752 { 00753 return !( d->mAddedIncidences.isEmpty() && d->mChangedIncidences.isEmpty() && 00754 d->mDeletedIncidences.isEmpty() ); 00755 } 00756 00757 void ResourceCached::clearChange( Incidence *incidence ) 00758 { 00759 clearChange( incidence->uid() ); 00760 } 00761 00762 void ResourceCached::clearChange( const QString &uid ) 00763 { 00764 QMap<Incidence *, bool>::Iterator it; 00765 00766 for ( it = d->mAddedIncidences.begin(); it != d->mAddedIncidences.end(); ++it ) { 00767 if ( it.key()->uid() == uid ) { 00768 d->mAddedIncidences.erase( it ); 00769 break; 00770 } 00771 } 00772 00773 for ( it = d->mChangedIncidences.begin(); it != d->mChangedIncidences.end(); ++it ) { 00774 if ( it.key()->uid() == uid ) { 00775 d->mChangedIncidences.erase( it ); 00776 break; 00777 } 00778 } 00779 00780 for ( it = d->mDeletedIncidences.begin(); it != d->mDeletedIncidences.end(); ++it ) { 00781 if ( it.key()->uid() == uid ) { 00782 d->mDeletedIncidences.erase( it ); 00783 break; 00784 } 00785 } 00786 } 00787 00788 void ResourceCached::enableChangeNotification() 00789 { 00790 d->mCalendar.registerObserver( this ); 00791 } 00792 00793 void ResourceCached::disableChangeNotification() 00794 { 00795 d->mCalendar.unregisterObserver( this ); 00796 } 00797 00798 void ResourceCached::slotReload() 00799 { 00800 if ( !isActive() ) { 00801 return; 00802 } 00803 00804 kDebug(); 00805 00806 load( SyncCache ); 00807 } 00808 00809 void ResourceCached::slotSave() 00810 { 00811 if ( !isActive() ) { 00812 return; 00813 } 00814 00815 kDebug(); 00816 00817 save( SyncCache ); 00818 } 00819 00820 void ResourceCached::checkForAutomaticSave() 00821 { 00822 if ( d->mSavePolicy == SaveAlways ) { 00823 kDebug() << "save now"; 00824 d->mSavePending = true; 00825 d->mSaveTimer.setSingleShot( true ); 00826 d->mSaveTimer.start( 1 * 1000 ); // 1 second 00827 } else if ( d->mSavePolicy == SaveDelayed ) { 00828 kDebug() << "save delayed"; 00829 d->mSavePending = true; 00830 d->mSaveTimer.setSingleShot( true ); 00831 d->mSaveTimer.start( 15 * 1000 ); // 15 seconds 00832 } 00833 } 00834 00835 bool ResourceCached::checkForReload() 00836 { 00837 if ( d->mReloadPolicy == ReloadNever ) { 00838 return false; 00839 } 00840 if ( d->mReloadPolicy == ReloadOnStartup ) { 00841 return !d->mReloaded; 00842 } 00843 return true; 00844 } 00845 00846 bool ResourceCached::checkForSave() 00847 { 00848 if ( d->mSavePolicy == SaveNever ) { 00849 return false; 00850 } 00851 return true; 00852 } 00853 00854 void ResourceCached::addInfoText( QString &txt ) const 00855 { 00856 if ( d->mLastLoad.isValid() ) { 00857 txt += "<br>"; 00858 txt += i18n( "Last loaded: %1", 00859 KGlobal::locale()->formatDateTime( d->mLastLoad.toUtc().dateTime() ) ); 00860 } 00861 if ( d->mLastSave.isValid() ) { 00862 txt += "<br>"; 00863 txt += i18n( "Last saved: %1", 00864 KGlobal::locale()->formatDateTime( d->mLastSave.toUtc().dateTime() ) ); 00865 } 00866 } 00867 00868 void ResourceCached::doClose() 00869 { 00870 if ( d->mSavePending ) { 00871 d->mSaveTimer.stop(); 00872 } 00873 if ( d->mSavePending || d->mSavePolicy == SaveOnExit || d->mSavePolicy == SaveInterval ) { 00874 save( SyncCache ); 00875 } 00876 d->mCalendar.close(); 00877 } 00878 00879 bool ResourceCached::doOpen() 00880 { 00881 kDebug() << "Opening resource" << resourceName(); 00882 return true; 00883 } 00884 00885 void KCal::ResourceCached::setOwner( const Person &owner ) 00886 { 00887 d->mCalendar.setOwner( owner ); 00888 } 00889 00890 Person KCal::ResourceCached::owner() const 00891 { 00892 return d->mCalendar.owner(); 00893 }
This file is part of the KDE documentation.
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:05:30 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
Documentation copyright © 1996-2012 The KDE developers.
Generated on Mon May 14 2012 05:05:30 by doxygen 1.7.5 written by Dimitri van Heesch, © 1997-2006
KDE's Doxygen guidelines are available online.