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