libkcal Library API Documentation

resourcecached.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2003,2004 Cornelius Schumacher <schumacher@kde.org>
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010 
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015 
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 */
00021 
00022 #include <qdatetime.h>
00023 #include <qstring.h>
00024 #include <qptrlist.h>
00025 
00026 #include <kdebug.h>
00027 #include <klocale.h>
00028 #include <kurl.h>
00029 
00030 #include "exceptions.h"
00031 #include "incidence.h"
00032 #include "event.h"
00033 #include "todo.h"
00034 #include "journal.h"
00035 
00036 #include "resourcecached.h"
00037 
00038 using namespace KCal;
00039 
00040 ResourceCached::ResourceCached( const KConfig* config )
00041   : ResourceCalendar( config ), mReloadPolicy( ReloadNever ),
00042     mReloadInterval( 10 ), mReloaded( false ), mSavePolicy( SaveNever ),
00043     mSaveInterval( 10 )
00044 {
00045   connect( &mReloadTimer, SIGNAL( timeout() ), SLOT( slotReload() ) );
00046   connect( &mSaveTimer, SIGNAL( timeout() ), SLOT( slotSave() ) );
00047 }
00048 
00049 ResourceCached::~ResourceCached()
00050 {
00051 }
00052 
00053 void ResourceCached::setReloadPolicy( int i )
00054 {
00055   mReloadPolicy = i;
00056 
00057   setupReloadTimer();
00058 }
00059 
00060 int ResourceCached::reloadPolicy() const
00061 {
00062   return mReloadPolicy;
00063 }
00064 
00065 void ResourceCached::setReloadInterval( int minutes )
00066 {
00067   mReloadInterval = minutes;
00068 }
00069 
00070 int ResourceCached::reloadInterval() const
00071 {
00072   return mReloadInterval;
00073 }
00074 
00075 void ResourceCached::setSavePolicy( int i )
00076 {
00077   mSavePolicy = i;
00078 
00079   setupSaveTimer();
00080 }
00081 
00082 int ResourceCached::savePolicy() const
00083 {
00084   return mSavePolicy;
00085 }
00086 
00087 void ResourceCached::setSaveInterval( int minutes )
00088 {
00089   mSaveInterval = minutes;
00090 }
00091 
00092 int ResourceCached::saveInterval() const
00093 {
00094   return mSaveInterval;
00095 }
00096 
00097 void ResourceCached::readConfig( const KConfig *config )
00098 {
00099   mReloadPolicy = config->readNumEntry( "ReloadPolicy", ReloadNever );
00100   mReloadInterval = config->readNumEntry( "ReloadInterval", 10 );
00101 
00102   mSaveInterval = config->readNumEntry( "SaveInterval", 10 );
00103   mSavePolicy = config->readNumEntry( "SavePolicy", SaveNever );
00104 
00105   mLastLoad = config->readDateTimeEntry( "LastLoad" );
00106   mLastSave = config->readDateTimeEntry( "LastSave" );
00107 
00108   setupSaveTimer();
00109   setupReloadTimer();
00110 }
00111 
00112 void ResourceCached::setupSaveTimer()
00113 {
00114   if ( mSavePolicy == SaveInterval ) {
00115     kdDebug(5800) << "ResourceCached::setSavePolicy(): start save timer (interval "
00116               << mSaveInterval << " minutes)." << endl;
00117     mSaveTimer.start( mSaveInterval * 60 * 1000 ); // n minutes
00118   } else {
00119     mSaveTimer.stop();
00120   }
00121 }
00122 
00123 void ResourceCached::setupReloadTimer()
00124 {
00125   if ( mReloadPolicy == ReloadInterval ) {
00126     kdDebug(5800) << "ResourceCached::setSavePolicy(): start reload timer "
00127                  "(interval " << mReloadInterval << " minutes)" << endl;
00128     mReloadTimer.start( mReloadInterval * 60 * 1000 ); // n minutes
00129   } else {
00130     mReloadTimer.stop();
00131   }
00132 }
00133 
00134 void ResourceCached::writeConfig( KConfig *config )
00135 {
00136   config->writeEntry( "ReloadPolicy", mReloadPolicy );
00137   config->writeEntry( "ReloadInterval", mReloadInterval );
00138 
00139   config->writeEntry( "SavePolicy", mSavePolicy );
00140   config->writeEntry( "SaveInterval", mSaveInterval );
00141 
00142   config->writeEntry( "LastLoad", mLastLoad );
00143   config->writeEntry( "LastSave", mLastSave );
00144 }
00145 
00146 bool ResourceCached::addEvent(Event *event)
00147 {
00148   return mCalendar.addEvent( event );
00149 }
00150 
00151 // probably not really efficient, but...it works for now.
00152 void ResourceCached::deleteEvent( Event *event )
00153 {
00154   kdDebug(5800) << "ResourceCached::deleteEvent" << endl;
00155 
00156   mCalendar.deleteEvent( event );
00157 }
00158 
00159 
00160 Event *ResourceCached::event( const QString &uid )
00161 {
00162   return mCalendar.event( uid );
00163 }
00164 
00165 Event::List ResourceCached::rawEventsForDate( const QDate &qd, bool sorted )
00166 {
00167   Event::List list = mCalendar.rawEventsForDate( qd, sorted );
00168 
00169   return list;
00170 }
00171 
00172 
00173 Event::List ResourceCached::rawEvents( const QDate &start, const QDate &end,
00174                                        bool inclusive )
00175 {
00176   return mCalendar.rawEvents( start, end, inclusive );
00177 }
00178 
00179 Event::List ResourceCached::rawEventsForDate( const QDateTime &qdt )
00180 {
00181   return mCalendar.rawEventsForDate( qdt.date() );
00182 }
00183 
00184 Event::List ResourceCached::rawEvents()
00185 {
00186   return mCalendar.rawEvents();
00187 }
00188 
00189 bool ResourceCached::addTodo( Todo *todo )
00190 {
00191   return mCalendar.addTodo( todo );
00192 }
00193 
00194 void ResourceCached::deleteTodo( Todo *todo )
00195 {
00196   mCalendar.deleteTodo( todo );
00197 }
00198 
00199 void ResourceCached::deleteJournal( Journal *journal )
00200 {
00201   mCalendar.deleteJournal( journal );
00202 }
00203 
00204 
00205 Todo::List ResourceCached::rawTodos()
00206 {
00207   return mCalendar.rawTodos();
00208 }
00209 
00210 Todo *ResourceCached::todo( const QString &uid )
00211 {
00212   return mCalendar.todo( uid );
00213 }
00214 
00215 Todo::List ResourceCached::rawTodosForDate( const QDate &date )
00216 {
00217   return mCalendar.rawTodosForDate( date );
00218 }
00219 
00220 
00221 bool ResourceCached::addJournal( Journal *journal )
00222 {
00223   kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
00224 
00225   return mCalendar.addJournal( journal );
00226 }
00227 
00228 Journal *ResourceCached::journal( const QDate &date )
00229 {
00230 //  kdDebug(5800) << "ResourceCached::journal() " << date.toString() << endl;
00231 
00232   return mCalendar.journal( date );
00233 }
00234 
00235 Journal *ResourceCached::journal( const QString &uid )
00236 {
00237   return mCalendar.journal( uid );
00238 }
00239 
00240 Journal::List ResourceCached::journals()
00241 {
00242   return mCalendar.journals();
00243 }
00244 
00245 
00246 Alarm::List ResourceCached::alarmsTo( const QDateTime &to )
00247 {
00248   return mCalendar.alarmsTo( to );
00249 }
00250 
00251 Alarm::List ResourceCached::alarms( const QDateTime &from, const QDateTime &to )
00252 {
00253 //  kdDebug(5800) << "ResourceCached::alarms(" << from.toString() << " - " << to.toString() << ")\n";
00254 
00255   return mCalendar.alarms( from, to );
00256 }
00257 
00258 
00259 void ResourceCached::setTimeZoneId( const QString& tzid )
00260 {
00261   mCalendar.setTimeZoneId( tzid );
00262 }
00263 
00264 QString ResourceCached::timeZoneId() const
00265 {
00266   return mCalendar.timeZoneId();
00267 }
00268 
00269 void ResourceCached::clearChanges()
00270 {
00271   mAddedIncidences.clear();
00272   mChangedIncidences.clear();
00273   mDeletedIncidences.clear();
00274 }
00275 
00276 void ResourceCached::calendarIncidenceAdded( Incidence *i )
00277 {
00278 #if 1
00279   kdDebug(5800) << "ResourceCached::calendarIncidenceAdded(): "
00280             << i->uid() << endl;
00281 #endif
00282 
00283   QMap<Incidence *,bool>::ConstIterator it;
00284   it = mAddedIncidences.find( i );
00285   if ( it == mAddedIncidences.end() ) {
00286     mAddedIncidences.insert( i, true );
00287   }
00288 
00289   checkForAutomaticSave();
00290 }
00291 
00292 void ResourceCached::calendarIncidenceChanged( Incidence *i )
00293 {
00294 #if 1
00295   kdDebug(5800) << "ResourceCached::calendarIncidenceChanged(): "
00296             << i->uid() << endl;
00297 #endif
00298 
00299   QMap<Incidence *,bool>::ConstIterator it;
00300   it = mChangedIncidences.find( i );
00301   if ( it == mChangedIncidences.end() ) {
00302     mChangedIncidences.insert( i, true );
00303   }
00304 
00305   checkForAutomaticSave();
00306 }
00307 
00308 void ResourceCached::calendarIncidenceDeleted( Incidence *i )
00309 {
00310 #if 1
00311   kdDebug(5800) << "ResourceCached::calendarIncidenceDeleted(): "
00312             << i->uid() << endl;
00313 #endif
00314 
00315   QMap<Incidence *,bool>::ConstIterator it;
00316   it = mDeletedIncidences.find( i );
00317   if ( it == mDeletedIncidences.end() ) {
00318     mDeletedIncidences.insert( i, true );
00319   }
00320 
00321   checkForAutomaticSave();
00322 }
00323 
00324 Incidence::List ResourceCached::addedIncidences() const
00325 {
00326   Incidence::List added;
00327   QMap<Incidence *,bool>::ConstIterator it;
00328   for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
00329     added.append( it.key() );
00330   }
00331   return added;
00332 }
00333 
00334 Incidence::List ResourceCached::changedIncidences() const
00335 {
00336   Incidence::List changed;
00337   QMap<Incidence *,bool>::ConstIterator it;
00338   for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
00339     changed.append( it.key() );
00340   }
00341   return changed;
00342 }
00343 
00344 Incidence::List ResourceCached::deletedIncidences() const
00345 {
00346   Incidence::List deleted;
00347   QMap<Incidence *,bool>::ConstIterator it;
00348   for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
00349     deleted.append( it.key() );
00350   }
00351   return deleted;
00352 }
00353 
00354 Incidence::List ResourceCached::allChanges() const
00355 {
00356   Incidence::List changes;
00357   QMap<Incidence *,bool>::ConstIterator it;
00358   for( it = mAddedIncidences.begin(); it != mAddedIncidences.end(); ++it ) {
00359     changes.append( it.key() );
00360   }
00361   for( it = mChangedIncidences.begin(); it != mChangedIncidences.end(); ++it ) {
00362     changes.append( it.key() );
00363   }
00364   for( it = mDeletedIncidences.begin(); it != mDeletedIncidences.end(); ++it ) {
00365     changes.append( it.key() );
00366   }
00367   return changes;
00368 }
00369 
00370 bool ResourceCached::hasChanges() const
00371 {
00372   return !( mAddedIncidences.isEmpty() && mChangedIncidences.isEmpty() &&
00373             mDeletedIncidences.isEmpty() );
00374 }
00375 
00376 void ResourceCached::clearChange( Incidence *incidence )
00377 {
00378   mAddedIncidences.remove( incidence );
00379   mChangedIncidences.remove( incidence );
00380   mDeletedIncidences.remove( incidence );
00381 }
00382 
00383 void ResourceCached::enableChangeNotification()
00384 {
00385   mCalendar.registerObserver( this );
00386 }
00387 
00388 void ResourceCached::disableChangeNotification()
00389 {
00390   mCalendar.unregisterObserver( this );
00391 }
00392 
00393 void ResourceCached::slotReload()
00394 {
00395   kdDebug(5800) << "ResourceCached::slotReload()" << endl;
00396 
00397   load();
00398 }
00399 
00400 void ResourceCached::slotSave()
00401 {
00402   kdDebug(5800) << "ResourceCached::slotSave()" << endl;
00403 
00404   save();
00405 }
00406 
00407 void ResourceCached::checkForAutomaticSave()
00408 {
00409   if ( mSavePolicy == SaveAlways )  {
00410     kdDebug() << "ResourceCached::checkForAutomaticSave(): save now" << endl;
00411     mSaveTimer.start( 1 * 1000, true ); // 1 second
00412   } else if ( mSavePolicy == SaveDelayed ) {
00413     kdDebug() << "ResourceCached::checkForAutomaticSave(): save delayed"
00414               << endl;
00415     mSaveTimer.start( 15 * 1000, true ); // 15 seconds
00416   }
00417 }
00418 
00419 bool ResourceCached::checkForReload()
00420 {
00421   if ( mReloadPolicy == ReloadNever ) return false;
00422   if ( mReloadPolicy == ReloadOnStartup ) return !mReloaded;
00423   return true;
00424 }
00425 
00426 bool ResourceCached::checkForSave()
00427 {
00428   if ( mSavePolicy == SaveNever ) return false;
00429   return true;
00430 }
00431 
00432 void ResourceCached::addInfoText( QString &txt ) const
00433 {
00434   if ( mLastLoad.isValid() ) {
00435     txt += "<br>";
00436     txt += i18n("Last loaded: %1")
00437            .arg( KGlobal::locale()->formatDateTime( mLastLoad ) );
00438   }
00439   if ( mLastSave.isValid() ) {
00440     txt += "<br>";
00441     txt += i18n("Last saved: %1")
00442            .arg( KGlobal::locale()->formatDateTime( mLastSave ) );
00443   }
00444 }
00445 
00446 #include "resourcecached.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Oct 17 09:52:51 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003