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