libkcal

calendarresources.cpp

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