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