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