libkcal Library API Documentation

calendarresources.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <stdlib.h>
00022 
00023 #include <qdatetime.h>
00024 #include <qstring.h>
00025 #include <qptrlist.h>
00026 
00027 #include <kdebug.h>
00028 #include <kstandarddirs.h>
00029 #include <klocale.h>
00030 
00031 #include "vcaldrag.h"
00032 #include "vcalformat.h"
00033 #include "icalformat.h"
00034 #include "exceptions.h"
00035 #include "incidence.h"
00036 #include "journal.h"
00037 #include "filestorage.h"
00038 
00039 #include <kresources/manager.h>
00040 #include "selectdialog.h"
00041 #include <kabc/lock.h>
00042 
00043 #include "resourcecalendar.h"
00044 #include "resourcelocal.h"
00045 
00046 #include "calendarresources.h"
00047 
00048 using namespace KCal;
00049 
00050 ResourceCalendar *CalendarResources::StandardDestinationPolicy::destination( Incidence * )
00051 {
00052   return resourceManager()->standardResource();
00053 }
00054 
00055 ResourceCalendar *CalendarResources::AskDestinationPolicy::destination( Incidence *incidence )
00056 {
00057   QPtrList<ResourceCalendar> list;
00058 
00059   CalendarResourceManager::ActiveIterator it;
00060   for( it = resourceManager()->activeBegin();
00061        it != resourceManager()->activeEnd(); ++it ) {
00062     if ( !(*it)->readOnly() )
00063       list.append( *it );
00064   }
00065 
00066   return SelectDialog::getResource( list, incidence, mParent );
00067 }
00068 
00069 CalendarResources::CalendarResources()
00070   : Calendar()
00071 {
00072   init();
00073 }
00074 
00075 CalendarResources::CalendarResources(const QString &timeZoneId)
00076   : Calendar(timeZoneId)
00077 {
00078   init();
00079 }
00080 
00081 void CalendarResources::init()
00082 {
00083   kdDebug(5800) << "CalendarResources::init" << endl;
00084 
00085   mManager = new CalendarResourceManager( "calendar" );
00086   mManager->addObserver( this );
00087 
00088   mStandardPolicy = new StandardDestinationPolicy( mManager );
00089   mAskPolicy = new AskDestinationPolicy( mManager );
00090   mDestinationPolicy = mStandardPolicy;
00091 }
00092 
00093 CalendarResources::~CalendarResources()
00094 {
00095 //  kdDebug(5800) << "CalendarResources::destructor" << endl;
00096 
00097   close();
00098 
00099   delete mManager;
00100 }
00101 
00102 void CalendarResources::readConfig( KConfig *config )
00103 {
00104   mManager->readConfig( config );
00105 
00106   CalendarResourceManager::Iterator it;
00107   for ( it = mManager->begin(); it != mManager->end(); ++it ) {
00108     connectResource( *it );
00109   }
00110 }
00111 
00112 void CalendarResources::load()
00113 {
00114   kdDebug(5800) << "CalendarResources::load()" << endl;
00115 
00116   if ( !mManager->standardResource() ) {
00117     kdDebug(5800) << "Warning! No standard resource yet." << endl;
00118   }
00119 
00120   // set the timezone for all resources. Otherwise we'll have those terrible tz
00121   // troubles ;-((
00122   CalendarResourceManager::Iterator i1;
00123   for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) {
00124     (*i1)->setTimeZoneId( timeZoneId() );
00125   }
00126 
00127   // Open all active resources
00128   CalendarResourceManager::ActiveIterator it;
00129   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00130     (*it)->load();
00131   }
00132 
00133   mOpen = true;
00134 }
00135 
00136 void CalendarResources::setStandardDestinationPolicy()
00137 {
00138   mDestinationPolicy = mStandardPolicy;
00139 }
00140 
00141 void CalendarResources::setAskDestinationPolicy()
00142 {
00143   mDestinationPolicy = mAskPolicy;
00144 }
00145 
00146 void CalendarResources::close()
00147 {
00148   kdDebug(5800) << "CalendarResources::close" << endl;
00149 
00150   if ( mOpen ) {
00151     CalendarResourceManager::ActiveIterator it;
00152     for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00153       (*it)->close();
00154     }
00155 
00156     setModified( false );
00157     mOpen = false;
00158   }
00159 }
00160 
00161 void CalendarResources::save()
00162 {
00163   kdDebug(5800) << "CalendarResources::save()" << endl;
00164 
00165   if ( mOpen ) {
00166     CalendarResourceManager::ActiveIterator it;
00167     for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00168       (*it)->save();
00169     }
00170 
00171     setModified( false );
00172   }
00173 }
00174 
00175 bool CalendarResources::isSaving()
00176 {
00177   CalendarResourceManager::ActiveIterator it;
00178   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00179     if ( (*it)->isSaving() ) {
00180       return true;
00181     }
00182   }
00183 
00184   return false;
00185 }
00186 
00187 bool CalendarResources::addIncidence( Incidence *incidence )
00188 {
00189   kdDebug(5800) << "CalendarResources::addIncidence" << endl;
00190 
00191   ResourceCalendar *resource = mDestinationPolicy->destination( incidence );
00192 
00193   if ( resource ) {
00194     if ( resource->addIncidence( incidence ) ) {
00195       mResourceMap[ incidence ] = resource;
00196       setModified( true );
00197       return true;
00198     }
00199   } else
00200     kdDebug(5800) << "CalendarResources::addIncidence(): no resource" << endl;
00201 
00202   return false;
00203 }
00204 
00205 bool CalendarResources::addEvent( Event *event )
00206 {
00207   return addIncidence( event );
00208 }
00209 
00210 bool CalendarResources::addEvent( Event *anEvent, ResourceCalendar *resource )
00211 {
00212   bool validRes = false;
00213   CalendarResourceManager::ActiveIterator it;
00214   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00215     if ( (*it) == resource ) validRes = true;
00216   }
00217   if ( validRes && resource->addEvent( anEvent ) ) {
00218     mResourceMap[anEvent] = resource;
00219     setModified( true );
00220     return true;
00221   }
00222 
00223   return false;
00224 }
00225 
00226 void CalendarResources::deleteEvent( Event *event )
00227 {
00228   kdDebug(5800) << "CalendarResources::deleteEvent" << endl;
00229 
00230   if ( mResourceMap.find( event ) != mResourceMap.end() ) {
00231     mResourceMap[event]->deleteEvent( event );
00232     mResourceMap.remove( event );
00233   } else {
00234     CalendarResourceManager::ActiveIterator it;
00235     for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00236       (*it)->deleteEvent( event );
00237     }
00238   }
00239 
00240   setModified( true );
00241 }
00242 
00243 
00244 Event *CalendarResources::event( const QString &uid )
00245 {
00246 //  kdDebug(5800) << "CalendarResources::event(): " << uid << endl;
00247 
00248   CalendarResourceManager::ActiveIterator it;
00249   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00250     Event* event = (*it)->event( uid );
00251     if ( event )
00252     {
00253       mResourceMap[event] = *it;
00254       return event;
00255     }
00256   }
00257 
00258   // Not found
00259   return 0;
00260 }
00261 
00262 bool CalendarResources::addTodo( Todo *todo )
00263 {
00264   kdDebug(5800) << "CalendarResources::addTodo" << endl;
00265 
00266   return addIncidence( todo );
00267 }
00268 
00269 bool CalendarResources::addTodo(Todo *todo, ResourceCalendar *resource)
00270 {
00271   bool validRes = false;
00272   CalendarResourceManager::ActiveIterator it;
00273   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00274     if ( (*it) == resource ) validRes = true;
00275   }
00276   if ( validRes && resource->addTodo( todo ) ) {
00277     mResourceMap[todo] = resource;
00278     setModified( true );
00279     return true;
00280   }
00281 
00282   return false;
00283 }
00284 
00285 void CalendarResources::deleteTodo( Todo *todo )
00286 {
00287   kdDebug(5800) << "CalendarResources::deleteTodo" << endl;
00288 
00289   Q_ASSERT(todo);
00290 
00291   if ( mResourceMap.find(todo) != mResourceMap.end() ) {
00292     mResourceMap[todo]->deleteTodo( todo );
00293     mResourceMap.remove( todo );
00294   } else {
00295     CalendarResourceManager::ActiveIterator it;
00296     for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00297       (*it)->deleteTodo( todo );
00298     }
00299   }
00300 
00301   setModified( true );
00302 }
00303 
00304 Todo::List CalendarResources::rawTodos()
00305 {
00306 //  kdDebug(5800) << "CalendarResources::rawTodos()" << endl;
00307 
00308   Todo::List result;
00309 
00310   CalendarResourceManager::ActiveIterator it;
00311   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00312 //    kdDebug(5800) << "Getting raw todos from '" << (*it)->resourceName()
00313 //                  << "'" << endl;
00314     Todo::List todos = (*it)->rawTodos();
00315     Todo::List::ConstIterator it2;
00316     for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00317 //      kdDebug(5800) << "Adding todo to result" << endl;
00318       result.append( *it2 );
00319       mResourceMap[ *it2 ] = *it;
00320     }
00321   }
00322 
00323   return result;
00324 }
00325 
00326 Todo *CalendarResources::todo( const QString &uid )
00327 {
00328   //kdDebug(5800) << "CalendarResources::todo(uid)" << endl;
00329 
00330   CalendarResourceManager::ActiveIterator it;
00331   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00332     Todo *todo = (*it)->todo( uid );
00333     if ( todo ) {
00334       mResourceMap[todo] = *it;
00335       return todo;
00336     }
00337   }
00338 
00339   // not found
00340   return 0;
00341 }
00342 
00343 Todo::List CalendarResources::rawTodosForDate( const QDate &date )
00344 {
00345 //  kdDebug(5800) << "CalendarResources::rawTodosforDate(date)" << endl;
00346 
00347   Todo::List result;
00348 
00349   CalendarResourceManager::ActiveIterator it;
00350   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00351     Todo::List todos = (*it)->rawTodosForDate( date );
00352     Todo::List::ConstIterator it2;
00353     for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00354       result.append( *it2 );
00355       mResourceMap[ *it2 ] = *it;
00356     }
00357   }
00358 
00359   return result;
00360 }
00361 
00362 
00363 Alarm::List CalendarResources::alarmsTo( const QDateTime &to )
00364 {
00365   kdDebug(5800) << "CalendarResources::alarmsTo" << endl;
00366 
00367   Alarm::List result;
00368   CalendarResourceManager::ActiveIterator it;
00369   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00370     Alarm::List list = (*it)->alarmsTo( to );
00371     Alarm::List::Iterator it;
00372     for ( it = list.begin(); it != list.end(); ++it )
00373       result.append( *it );
00374   }
00375   return result;
00376 }
00377 
00378 Alarm::List CalendarResources::alarms( const QDateTime &from, const QDateTime &to )
00379 {
00380 //  kdDebug(5800) << "CalendarResources::alarms(" << from.toString() << " - "
00381 //                << to.toString() << ")" << endl;
00382 
00383   Alarm::List result;
00384   CalendarResourceManager::ActiveIterator it;
00385   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00386     Alarm::List list = (*it)->alarms( from, to );
00387     Alarm::List::Iterator it;
00388     for ( it = list.begin(); it != list.end(); ++it )
00389       result.append( *it );
00390   }
00391   return result;
00392 }
00393 
00394 /****************************** PROTECTED METHODS ****************************/
00395 
00396 
00397 // taking a QDate, this function will look for an eventlist in the dict
00398 // with that date attached -
00399 Event::List CalendarResources::rawEventsForDate( const QDate &qd, bool sorted )
00400 {
00401 //  kdDebug(5800) << "CalendarResources::rawEventsForDate()" << endl;
00402 
00403   Event::List result;
00404   CalendarResourceManager::ActiveIterator it;
00405   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00406 //    kdDebug(5800) << "Getting events from '" << (*it)->resourceName() << "'"
00407 //                  << endl;
00408     Event::List list = (*it)->rawEventsForDate( qd, sorted );
00409 
00410     Event::List::ConstIterator it2;
00411     if ( sorted ) {
00412       Event::List::Iterator insertionPoint = result.begin();
00413       for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00414         while ( insertionPoint != result.end() &&
00415                 (*insertionPoint)->dtStart().time() <= (*it2)->dtStart().time() )
00416           insertionPoint++;
00417         result.insert( insertionPoint, *it2 );
00418         mResourceMap[ *it2 ] = *it;
00419       }
00420     } else {
00421       for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00422         result.append( *it2 );
00423         mResourceMap[ *it2 ] = *it;
00424       }
00425     }
00426   }
00427 
00428   return result;
00429 }
00430 
00431 Event::List CalendarResources::rawEvents( const QDate &start, const QDate &end,
00432                                           bool inclusive )
00433 {
00434   kdDebug(5800) << "CalendarResources::rawEvents(start,end,inclusive)" << endl;
00435 
00436   Event::List result;
00437   CalendarResourceManager::ActiveIterator it;
00438   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00439     Event::List list = (*it)->rawEvents( start, end, inclusive );
00440     Event::List::ConstIterator it2;
00441     for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00442       result.append( *it2 );
00443       mResourceMap[ *it2 ] = *it;
00444     }
00445   }
00446   return result;
00447 }
00448 
00449 Event::List CalendarResources::rawEventsForDate(const QDateTime &qdt)
00450 {
00451   kdDebug(5800) << "CalendarResources::rawEventsForDate(qdt)" << endl;
00452 
00453   // TODO: Remove the code duplication by the resourcemap iteration block.
00454   Event::List result;
00455   CalendarResourceManager::ActiveIterator it;
00456   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00457     Event::List list = (*it)->rawEventsForDate( qdt );
00458     Event::List::ConstIterator it2;
00459     for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00460       result.append( *it2 );
00461       mResourceMap[ *it2 ] = *it;
00462     }
00463   }
00464   return result;
00465 }
00466 
00467 Event::List CalendarResources::rawEvents()
00468 {
00469   kdDebug(5800) << "CalendarResources::rawEvents()" << endl;
00470 
00471   Event::List result;
00472   CalendarResourceManager::ActiveIterator it;
00473   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00474     Event::List list = (*it)->rawEvents();
00475     Event::List::ConstIterator it2;
00476     for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00477       result.append( *it2 );
00478       mResourceMap[ *it2 ] = *it;
00479     }
00480   }
00481   return result;
00482 }
00483 
00484 
00485 bool CalendarResources::addJournal( Journal *journal )
00486 {
00487   kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
00488 
00489   return addIncidence( journal );
00490 }
00491 
00492 void CalendarResources::deleteJournal( Journal *journal )
00493 {
00494   kdDebug(5800) << "CalendarResources::deleteJournal" << endl;
00495 
00496   if ( mResourceMap.find(journal)!=mResourceMap.end() ) {
00497     mResourceMap[journal]->deleteJournal( journal );
00498     mResourceMap.remove( journal );
00499   } else {
00500     CalendarResourceManager::ActiveIterator it;
00501     for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00502       (*it)->deleteJournal( journal );
00503     }
00504   }
00505 
00506   setModified( true );
00507 }
00508 
00509 bool CalendarResources::addJournal(Journal *journal, ResourceCalendar *resource)
00510 {
00511   bool validRes = false;
00512   CalendarResourceManager::ActiveIterator it;
00513   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00514     if ( (*it) == resource ) validRes = true;
00515   }
00516   if ( validRes && resource->addJournal( journal ) ) {
00517     mResourceMap[journal] = resource;
00518     setModified( true );
00519     return true;
00520   }
00521 
00522   return false;
00523 }
00524 
00525 Journal *CalendarResources::journal(const QDate &date)
00526 {
00527   kdDebug(5800) << "CalendarResources::journal() " << date.toString() << endl;
00528   kdDebug(5800) << "FIXME: what to do with the multiple journals from multiple calendar resources?" << endl;
00529 
00530   // If we're on a private resource, return that journal.
00531   // Else, first see if the standard resource has a journal for this date. If it has, return that journal.
00532   // If not, check all resources for a journal for this date.
00533 
00534   if ( mManager->standardResource() ) {
00535     Journal* journal = mManager->standardResource()->journal( date );
00536     if ( journal ) {
00537       mResourceMap[journal] = mManager->standardResource();
00538       return journal;
00539     }
00540   }
00541   CalendarResourceManager::ActiveIterator it;
00542   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00543     Journal* journal = (*it)->journal( date );
00544     if ( journal ) {
00545       mResourceMap[journal] = *it;
00546       return journal;
00547     }
00548   }
00549 
00550   return 0;
00551 }
00552 
00553 Journal *CalendarResources::journal(const QString &uid)
00554 {
00555   kdDebug(5800) << "CalendarResources::journal(uid)" << endl;
00556 
00557   CalendarResourceManager::ActiveIterator it;
00558   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00559     Journal* journal = (*it)->journal( uid );
00560     if ( journal ) {
00561       mResourceMap[journal] = *it;
00562       return journal;
00563     }
00564   }
00565 
00566   // not found
00567   return 0;
00568 }
00569 
00570 Journal::List CalendarResources::journals()
00571 {
00572   kdDebug(5800) << "CalendarResources::journals()" << endl;
00573 
00574   Journal::List result;
00575   CalendarResourceManager::ActiveIterator it;
00576   for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00577     Journal::List list = (*it)->journals();
00578     Journal::List::ConstIterator it2;
00579     for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00580       result.append( *it2 );
00581       mResourceMap[ *it2 ] = *it;
00582     }
00583   }
00584   return result;
00585 }
00586 
00587 
00588 void CalendarResources::incidenceUpdated( IncidenceBase * )
00589 {
00590   kdDebug(5800) << "CalendarResources::incidenceUpdated( IncidenceBase * ): Not yet implemented\n";
00591 }
00592 
00593 void CalendarResources::connectResource( ResourceCalendar *resource )
00594 {
00595   connect( resource, SIGNAL( resourceChanged( ResourceCalendar * ) ),
00596            SIGNAL( calendarChanged() ) );
00597   connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00598            SIGNAL( calendarSaved() ) );
00599 
00600   connect( resource, SIGNAL( resourceLoadError( ResourceCalendar *,
00601                                                 const QString & ) ),
00602            SLOT( slotLoadError( ResourceCalendar *, const QString & ) ) );
00603   connect( resource, SIGNAL( resourceSaveError( ResourceCalendar *,
00604                                                 const QString & ) ),
00605            SLOT( slotSaveError( ResourceCalendar *, const QString & ) ) );
00606 }
00607 
00608 ResourceCalendar *CalendarResources::resource(Incidence *inc)
00609 {
00610   if ( mResourceMap.find( inc ) != mResourceMap.end() ) {
00611     return mResourceMap[ inc ];
00612   }
00613   return 0;
00614 }
00615 
00616 void CalendarResources::resourceAdded( ResourceCalendar *resource )
00617 {
00618   kdDebug(5800) << "Resource added: " << resource->resourceName() << endl;
00619 
00620   if ( !resource->isActive() ) return;
00621 
00622   if ( resource->open() ) {
00623     resource->load();
00624   }
00625 
00626   connectResource( resource );
00627 
00628   emit signalResourceAdded( resource );
00629 }
00630 
00631 void CalendarResources::resourceModified( ResourceCalendar *resource )
00632 {
00633   kdDebug(5800) << "Resource modified: " << resource->resourceName() << endl;
00634 
00635   emit signalResourceModified( resource );
00636 }
00637 
00638 void CalendarResources::resourceDeleted( ResourceCalendar *resource )
00639 {
00640   kdDebug(5800) << "Resource deleted: " << resource->resourceName() << endl;
00641 
00642   emit signalResourceDeleted( resource );
00643 }
00644 
00645 void CalendarResources::doSetTimeZoneId( const QString &tzid )
00646 {
00647   // set the timezone for all resources. Otherwise we'll have those terrible
00648   // tz troubles ;-((
00649   CalendarResourceManager::Iterator i1;
00650   for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) {
00651     (*i1)->setTimeZoneId( tzid );
00652   }
00653 }
00654 
00655 CalendarResources::Ticket *CalendarResources::requestSaveTicket( ResourceCalendar *resource )
00656 {
00657   kdDebug(5800) << "CalendarResources::requestSaveTicket()" << endl;
00658 
00659   KABC::Lock *lock = resource->lock();
00660   if ( !lock ) return 0;
00661   if ( lock->lock() ) return new Ticket( resource );
00662   else return 0;
00663 }
00664 
00665 bool CalendarResources::save( Ticket *ticket )
00666 {
00667   kdDebug(5800) << "CalendarResources::save( Ticket *)" << endl;
00668 
00669   if ( !ticket || !ticket->resource() ) return false;
00670 
00671   kdDebug(5800) << "tick " << ticket->resource()->resourceName() << endl;
00672 
00673   if ( ticket->resource()->save() ) {
00674     releaseSaveTicket( ticket );
00675     return true;
00676   }
00677 
00678   return false;
00679 }
00680 
00681 void CalendarResources::releaseSaveTicket( Ticket *ticket )
00682 {
00683   ticket->resource()->lock()->unlock();
00684   delete ticket;
00685 }
00686 
00687 bool CalendarResources::beginChange( Incidence *incidence )
00688 {
00689   kdDebug(5800) << "CalendarResources::beginChange()" << endl;
00690 
00691   ResourceCalendar *r = resource( incidence );
00692   if ( !r ) {
00693     r = mDestinationPolicy->destination( incidence );
00694     if ( !r ) {
00695       kdError() << "Unable to get destination resource." << endl;
00696       return false;
00697     }
00698     mResourceMap[ incidence ] = r;
00699   }
00700 
00701   int count = incrementChangeCount( r );
00702   if ( count == 1 ) {
00703     Ticket *ticket = requestSaveTicket( r );
00704     if ( !ticket ) {
00705       kdDebug(5800) << "CalendarResources::beginChange(): unable to get ticket."
00706                     << endl;
00707       decrementChangeCount( r );
00708       return false;
00709     } else {
00710       mTickets[ r ] = ticket;
00711     }
00712   }
00713 
00714   return true;
00715 }
00716 
00717 bool CalendarResources::endChange( Incidence *incidence )
00718 {
00719   kdDebug(5800) << "CalendarResource::endChange()" << endl;
00720 
00721   ResourceCalendar *r = resource( incidence );
00722   if ( !r ) return false;
00723 
00724   int count = decrementChangeCount( r );
00725 
00726   if ( count == 0 ) {
00727     bool ok = save( mTickets[ r ] );
00728     if ( ok ) {
00729       mTickets.remove( r );
00730     } else {
00731       return false;
00732     }
00733   }
00734 
00735   return true;
00736 }
00737 
00738 int CalendarResources::incrementChangeCount( ResourceCalendar *r )
00739 {
00740   if ( !mChangeCounts.contains( r ) ) {
00741     mChangeCounts.insert( r, 0 );
00742   }
00743 
00744   int count = mChangeCounts[ r ];
00745   ++count;
00746   mChangeCounts[ r ] = count;
00747 
00748   return count;
00749 }
00750 
00751 int CalendarResources::decrementChangeCount( ResourceCalendar *r )
00752 {
00753   if ( !mChangeCounts.contains( r ) ) {
00754     kdError() << "No change count for resource." << endl;
00755     return 0;
00756   }
00757 
00758   int count = mChangeCounts[ r ];
00759   --count;
00760   if ( count < 0 ) {
00761     kdError() << "Can't decrement change count. It already is 0." << endl;
00762     count = 0;
00763   }
00764   mChangeCounts[ r ] = count;
00765 
00766   return count;
00767 }
00768 
00769 void CalendarResources::slotLoadError( ResourceCalendar *, const QString &err )
00770 {
00771   emit signalErrorMessage( err );
00772 }
00773 
00774 void CalendarResources::slotSaveError( ResourceCalendar *, const QString &err )
00775 {
00776   emit signalErrorMessage( err );
00777 }
00778 
00779 #include "calendarresources.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 Thu Oct 4 14:39:37 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003