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