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