00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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 ), mAddingInProgress( false )
00063 {
00064 }
00065
00066 ResourceCalendar *mLastUsedResource;
00067 bool mAddingInProgress;
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
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
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
00178
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
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
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 ResourceCalendar *oldResource = 0;
00292 if ( mResourceMap.contains( incidence ) ) {
00293 oldResource = mResourceMap[incidence];
00294 }
00295 mResourceMap[incidence] = resource;
00296 if ( validRes && beginChange( incidence, resource, subresource ) &&
00297 resource->addIncidence( incidence, subresource ) ) {
00298
00299 incidence->registerObserver( this );
00300 notifyIncidenceAdded( incidence );
00301 setModified( true );
00302 endChange( incidence, resource, subresource );
00303 return true;
00304 } else {
00305 if ( oldResource )
00306 mResourceMap[incidence] = oldResource;
00307 else
00308 mResourceMap.remove( incidence );
00309 }
00310
00311 return false;
00312 }
00313
00314 bool CalendarResources::addIncidence( Incidence *incidence,
00315 ResourceCalendar *resource )
00316 {
00317 return addIncidence( incidence, resource, QString() );
00318 }
00319
00320 bool CalendarResources::addIncidence( Incidence *incidence )
00321 {
00322 kdDebug(5800) << "CalendarResources::addIncidence" << this << endl;
00323
00324 clearException();
00325
00326 ResourceCalendar *resource = d->mLastUsedResource;
00327
00328 if ( !d->mAddingInProgress || d->mLastUsedResource == 0 ) {
00329 resource = mDestinationPolicy->destination( incidence );
00330 d->mLastUsedResource = resource;
00331 }
00332
00333 if ( resource ) {
00334 mResourceMap[ incidence ] = resource;
00335
00336 if ( beginChange( incidence, resource, QString() ) &&
00337 resource->addIncidence( incidence ) ) {
00338 incidence->registerObserver( this );
00339 notifyIncidenceAdded( incidence );
00340
00341
00342 mResourceMap[ incidence ] = resource;
00343 setModified( true );
00344 endChange( incidence, resource, QString() );
00345 return true;
00346 } else {
00347 if ( resource->exception() ) {
00348 mException = new ErrorFormat( resource->exception()->errorCode() );
00349 }
00350 mResourceMap.remove( incidence );
00351 }
00352 } else {
00353 mException = new ErrorFormat( ErrorFormat::UserCancel );
00354 }
00355
00356 return false;
00357 }
00358
00359 bool CalendarResources::addEvent( Event *event )
00360 {
00361 kdDebug(5800) << "CalendarResources::addEvent" << endl;
00362 return addIncidence( event );
00363 }
00364
00365 bool CalendarResources::addEvent( Event *Event, ResourceCalendar *resource )
00366 {
00367 return addIncidence( Event, resource, QString() );
00368 }
00369
00370 bool CalendarResources::addEvent( Event *Event, ResourceCalendar *resource,
00371 const QString &subresource )
00372 {
00373 return addIncidence( Event, resource, subresource );
00374 }
00375
00376 bool CalendarResources::deleteEvent( Event *event )
00377 {
00378 kdDebug(5800) << "CalendarResources::deleteEvent" << endl;
00379
00380 bool status;
00381 if ( mResourceMap.find( event ) != mResourceMap.end() ) {
00382 status = mResourceMap[event]->deleteEvent( event );
00383 if ( status )
00384 mPendingDeleteFromResourceMap = true;
00385 } else {
00386 status = false;
00387 CalendarResourceManager::ActiveIterator it;
00388 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00389 status = (*it)->deleteEvent( event ) || status;
00390 }
00391 }
00392
00393 if ( status ) {
00394 notifyIncidenceDeleted( event );
00395 }
00396
00397 setModified( status );
00398 return status;
00399 }
00400
00401 Event *CalendarResources::event( const QString &uid )
00402 {
00403 CalendarResourceManager::ActiveIterator it;
00404 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00405 Event* event = (*it)->event( uid );
00406 if ( event ) {
00407 mResourceMap[event] = *it;
00408 return event;
00409 }
00410 }
00411
00412
00413 return 0;
00414 }
00415
00416 bool CalendarResources::addTodo( Todo *todo )
00417 {
00418 kdDebug(5800) << "CalendarResources::addTodo" << endl;
00419 return addIncidence( todo );
00420 }
00421
00422 bool CalendarResources::addTodo( Todo *todo, ResourceCalendar *resource )
00423 {
00424 return addIncidence( todo, resource, QString() );
00425 }
00426
00427 bool CalendarResources::addTodo( Todo *todo, ResourceCalendar *resource,
00428 const QString &subresource )
00429 {
00430 return addIncidence( todo, resource, subresource );
00431 }
00432
00433 bool CalendarResources::deleteTodo( Todo *todo )
00434 {
00435 kdDebug(5800) << "CalendarResources::deleteTodo" << endl;
00436
00437 bool status;
00438 if ( mResourceMap.find( todo ) != mResourceMap.end() ) {
00439 status = mResourceMap[todo]->deleteTodo( todo );
00440 if ( status )
00441 mPendingDeleteFromResourceMap = true;
00442 } else {
00443 CalendarResourceManager::ActiveIterator it;
00444 status = false;
00445 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00446 status = (*it)->deleteTodo( todo ) || status;
00447 }
00448 }
00449
00450 setModified( status );
00451 return status;
00452 }
00453
00454 Todo::List CalendarResources::rawTodos( TodoSortField sortField,
00455 SortDirection sortDirection )
00456 {
00457 Todo::List result;
00458
00459 CalendarResourceManager::ActiveIterator it;
00460 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00461 Todo::List todos = (*it)->rawTodos( TodoSortUnsorted );
00462 Todo::List::ConstIterator it2;
00463 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00464 result.append( *it2 );
00465 mResourceMap[ *it2 ] = *it;
00466 }
00467 }
00468 return sortTodos( &result, sortField, sortDirection );
00469 }
00470
00471 Todo *CalendarResources::todo( const QString &uid )
00472 {
00473 kdDebug(5800) << "CalendarResources::todo(uid)" << endl;
00474
00475 CalendarResourceManager::ActiveIterator it;
00476 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00477 Todo *todo = (*it)->todo( uid );
00478 if ( todo ) {
00479 mResourceMap[todo] = *it;
00480 return todo;
00481 }
00482 }
00483
00484
00485 return 0;
00486 }
00487
00488 Todo::List CalendarResources::rawTodosForDate( const QDate &date )
00489 {
00490 Todo::List result;
00491
00492 CalendarResourceManager::ActiveIterator it;
00493 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00494 Todo::List todos = (*it)->rawTodosForDate( date );
00495 Todo::List::ConstIterator it2;
00496 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00497 result.append( *it2 );
00498 mResourceMap[ *it2 ] = *it;
00499 }
00500 }
00501
00502 return result;
00503 }
00504
00505 Alarm::List CalendarResources::alarmsTo( const QDateTime &to )
00506 {
00507 kdDebug(5800) << "CalendarResources::alarmsTo" << endl;
00508
00509 Alarm::List result;
00510 CalendarResourceManager::ActiveIterator resit;
00511 for ( resit = mManager->activeBegin(); resit != mManager->activeEnd(); ++resit ) {
00512 Alarm::List list = (*resit)->alarmsTo( to );
00513 Alarm::List::Iterator alarmit;
00514 for ( alarmit = list.begin(); alarmit != list.end(); ++alarmit )
00515 result.append( *alarmit );
00516 }
00517 return result;
00518 }
00519
00520 Alarm::List CalendarResources::alarms( const QDateTime &from,
00521 const QDateTime &to )
00522 {
00523 Alarm::List result;
00524 CalendarResourceManager::ActiveIterator resit;
00525 for ( resit = mManager->activeBegin(); resit != mManager->activeEnd(); ++resit ) {
00526 Alarm::List list = (*resit)->alarms( from, to );
00527 Alarm::List::Iterator alarmit;
00528 for ( alarmit = list.begin(); alarmit != list.end(); ++alarmit )
00529 result.append( *alarmit );
00530 }
00531 return result;
00532 }
00533
00534 bool CalendarResources::hasCalendarResources()
00535 {
00536 return mDestinationPolicy->hasCalendarResources();
00537 }
00538
00539
00540
00541 Event::List CalendarResources::rawEventsForDate( const QDate &date,
00542 EventSortField sortField,
00543 SortDirection sortDirection )
00544 {
00545 Event::List result;
00546 CalendarResourceManager::ActiveIterator it;
00547 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00548 Event::List list = (*it)->rawEventsForDate( date );
00549 Event::List::ConstIterator it2;
00550 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00551 result.append( *it2 );
00552 mResourceMap[ *it2 ] = *it;
00553 }
00554 }
00555 return sortEventsForDate( &result, date, sortField, sortDirection );
00556 }
00557
00558 Event::List CalendarResources::rawEvents( const QDate &start, const QDate &end,
00559 bool inclusive )
00560 {
00561 kdDebug(5800) << "CalendarResources::rawEvents(start,end,inclusive)" << endl;
00562
00563 Event::List result;
00564 CalendarResourceManager::ActiveIterator it;
00565 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00566 Event::List list = (*it)->rawEvents( start, end, inclusive );
00567 Event::List::ConstIterator it2;
00568 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00569 result.append( *it2 );
00570 mResourceMap[ *it2 ] = *it;
00571 }
00572 }
00573 return result;
00574 }
00575
00576 Event::List CalendarResources::rawEventsForDate( const QDateTime &qdt )
00577 {
00578 kdDebug(5800) << "CalendarResources::rawEventsForDate(qdt)" << endl;
00579
00580
00581 Event::List result;
00582 CalendarResourceManager::ActiveIterator it;
00583 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00584 Event::List list = (*it)->rawEventsForDate( qdt );
00585 Event::List::ConstIterator it2;
00586 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00587 result.append( *it2 );
00588 mResourceMap[ *it2 ] = *it;
00589 }
00590 }
00591 return result;
00592 }
00593
00594 Event::List CalendarResources::rawEvents( EventSortField sortField,
00595 SortDirection sortDirection )
00596 {
00597 kdDebug(5800) << "CalendarResources::rawEvents()" << endl;
00598
00599 Event::List result;
00600 CalendarResourceManager::ActiveIterator it;
00601 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00602 Event::List list = (*it)->rawEvents( EventSortUnsorted );
00603 Event::List::ConstIterator it2;
00604 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00605 result.append( *it2 );
00606 mResourceMap[ *it2 ] = *it;
00607 }
00608 }
00609 return sortEvents( &result, sortField, sortDirection );
00610 }
00611
00612
00613 bool CalendarResources::addJournal( Journal *journal )
00614 {
00615 kdDebug(5800) << "CalendarResources::addJournal" << endl;
00616 return addIncidence( journal );
00617 }
00618
00619 bool CalendarResources::addJournal( Journal *journal, ResourceCalendar *resource )
00620 {
00621 return addIncidence( journal, resource, QString() );
00622 }
00623
00624
00625 bool CalendarResources::addJournal( Journal *journal, ResourceCalendar *resource,
00626 const QString &subresource )
00627 {
00628 return addIncidence( journal, resource, subresource );
00629 }
00630
00631
00632 bool CalendarResources::deleteJournal( Journal *journal )
00633 {
00634 kdDebug(5800) << "CalendarResources::deleteJournal" << endl;
00635
00636 bool status;
00637 if ( mResourceMap.find( journal ) != mResourceMap.end() ) {
00638 status = mResourceMap[journal]->deleteJournal( journal );
00639 if ( status )
00640 mPendingDeleteFromResourceMap = true;
00641 } else {
00642 CalendarResourceManager::ActiveIterator it;
00643 status = false;
00644 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00645 status = (*it)->deleteJournal( journal ) || status;
00646 }
00647 }
00648
00649 setModified( status );
00650 return status;
00651 }
00652
00653 Journal *CalendarResources::journal( const QString &uid )
00654 {
00655 kdDebug(5800) << "CalendarResources::journal(uid)" << endl;
00656
00657 CalendarResourceManager::ActiveIterator it;
00658 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00659 Journal* journal = (*it)->journal( uid );
00660 if ( journal ) {
00661 mResourceMap[journal] = *it;
00662 return journal;
00663 }
00664 }
00665
00666
00667 return 0;
00668 }
00669
00670 Journal::List CalendarResources::rawJournals( JournalSortField sortField,
00671 SortDirection sortDirection )
00672 {
00673 kdDebug(5800) << "CalendarResources::rawJournals()" << endl;
00674
00675 Journal::List result;
00676 CalendarResourceManager::ActiveIterator it;
00677 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00678 Journal::List journals = (*it)->rawJournals( JournalSortUnsorted );
00679 Journal::List::ConstIterator it2;
00680 for ( it2 = journals.begin(); it2 != journals.end(); ++it2 ) {
00681 result.append( *it2 );
00682 mResourceMap[ *it2 ] = *it;
00683 }
00684 }
00685 return sortJournals( &result, sortField, sortDirection );
00686 }
00687
00688 Journal::List CalendarResources::rawJournalsForDate( const QDate &date )
00689 {
00690
00691 Journal::List result;
00692
00693 CalendarResourceManager::ActiveIterator it;
00694 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00695 Journal::List journals = (*it)->rawJournalsForDate( date );
00696 Journal::List::ConstIterator it2;
00697 for ( it2 = journals.begin(); it2 != journals.end(); ++it2 ) {
00698 result.append( *it2 );
00699 mResourceMap[ *it2 ] = *it;
00700 }
00701 }
00702 return result;
00703 }
00704
00705 void CalendarResources::connectResource( ResourceCalendar *resource )
00706 {
00707 connect( resource, SIGNAL( resourceChanged( ResourceCalendar * ) ),
00708 SIGNAL( calendarChanged() ) );
00709 connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00710 SIGNAL( calendarSaved() ) );
00711
00712 connect( resource, SIGNAL( resourceLoadError( ResourceCalendar *,
00713 const QString & ) ),
00714 SLOT( slotLoadError( ResourceCalendar *, const QString & ) ) );
00715 connect( resource, SIGNAL( resourceSaveError( ResourceCalendar *,
00716 const QString & ) ),
00717 SLOT( slotSaveError( ResourceCalendar *, const QString & ) ) );
00718 }
00719
00720 ResourceCalendar *CalendarResources::resource( Incidence *incidence )
00721 {
00722 if ( mResourceMap.find( incidence ) != mResourceMap.end() ) {
00723 return mResourceMap[ incidence ];
00724 }
00725 return 0;
00726 }
00727
00728 void CalendarResources::resourceAdded( ResourceCalendar *resource )
00729 {
00730 kdDebug(5800) << "Resource added: " << resource->resourceName() << endl;
00731
00732 if ( !resource->isActive() )
00733 return;
00734
00735 if ( resource->open() ) {
00736 resource->load();
00737 }
00738
00739 connectResource( resource );
00740
00741 emit signalResourceAdded( resource );
00742 }
00743
00744 void CalendarResources::resourceModified( ResourceCalendar *resource )
00745 {
00746 kdDebug(5800) << "Resource modified: " << resource->resourceName() << endl;
00747
00748 emit signalResourceModified( resource );
00749 }
00750
00751 void CalendarResources::resourceDeleted( ResourceCalendar *resource )
00752 {
00753 kdDebug(5800) << "Resource deleted: " << resource->resourceName() << endl;
00754
00755 emit signalResourceDeleted( resource );
00756 }
00757
00758 void CalendarResources::doSetTimeZoneId( const QString &timeZoneId )
00759 {
00760
00761
00762 CalendarResourceManager::Iterator i1;
00763 for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) {
00764 (*i1)->setTimeZoneId( timeZoneId );
00765 }
00766 }
00767
00768 void CalendarResources::setTimeZoneIdViewOnly( const QString &timeZoneId )
00769 {
00770 reload( timeZoneId );
00771 }
00772
00773 CalendarResources::Ticket
00774 *CalendarResources::requestSaveTicket( ResourceCalendar *resource )
00775 {
00776 kdDebug(5800) << "CalendarResources::requestSaveTicket()" << endl;
00777
00778 KABC::Lock *lock = resource->lock();
00779 if ( !lock )
00780 return 0;
00781 if ( lock->lock() )
00782 return new Ticket( resource );
00783 else
00784 return 0;
00785 }
00786
00787 bool CalendarResources::save( Ticket *ticket, Incidence *incidence )
00788 {
00789 kdDebug(5800) << "CalendarResources::save( Ticket *)" << endl;
00790
00791 if ( !ticket || !ticket->resource() )
00792 return false;
00793
00794 kdDebug(5800) << "tick " << ticket->resource()->resourceName() << endl;
00795
00796
00797 if ( ticket->resource()->save( incidence ) ) {
00798 releaseSaveTicket( ticket );
00799 return true;
00800 }
00801
00802 return false;
00803 }
00804
00805 void CalendarResources::releaseSaveTicket( Ticket *ticket )
00806 {
00807 ticket->resource()->lock()->unlock();
00808 delete ticket;
00809 }
00810
00811 bool CalendarResources::beginChange( Incidence *incidence )
00812 {
00813 return beginChange( incidence, 0, QString() );
00814 }
00815
00816 bool CalendarResources::beginChange( Incidence *incidence,
00817 ResourceCalendar *res,
00818 const QString &subres )
00819 {
00820 Q_UNUSED( subres );
00821
00822 kdDebug(5800) << "CalendarResources::beginChange()" << endl;
00823
00824 if ( !res ) {
00825 res = resource( incidence );
00826 }
00827 if ( !res ) {
00828 res = mDestinationPolicy->destination( incidence );
00829 if ( !res ) {
00830 kdError() << "Unable to get destination resource." << endl;
00831 return false;
00832 }
00833 mResourceMap[ incidence ] = res;
00834 }
00835 mPendingDeleteFromResourceMap = false;
00836
00837 int count = incrementChangeCount( res );
00838 if ( count == 1 ) {
00839 Ticket *ticket = requestSaveTicket( res );
00840 if ( !ticket ) {
00841 kdDebug(5800) << "CalendarResources::beginChange(): unable to get ticket."
00842 << endl;
00843 decrementChangeCount( res );
00844 return false;
00845 } else {
00846 mTickets[ res ] = ticket;
00847 }
00848 }
00849
00850 return true;
00851 }
00852
00853 bool CalendarResources::endChange( Incidence *incidence )
00854 {
00855 return endChange( incidence, 0, QString() );
00856 }
00857
00858 bool CalendarResources::endChange( Incidence *incidence,
00859 ResourceCalendar *res,
00860 const QString &subres )
00861 {
00862 Q_UNUSED( subres );
00863
00864 kdDebug(5800) << "CalendarResource::endChange()" << endl;
00865
00866 if ( !res ) {
00867 res = resource( incidence );
00868 }
00869 if ( !res )
00870 return false;
00871
00872 int count = decrementChangeCount( res );
00873
00874 if ( mPendingDeleteFromResourceMap ) {
00875 mResourceMap.remove( incidence );
00876 mPendingDeleteFromResourceMap = false;
00877 }
00878
00879 if ( count == 0 ) {
00880 bool ok = save( mTickets[ res ], incidence );
00881 if ( ok ) {
00882 mTickets.remove( res );
00883 } else {
00884 return false;
00885 }
00886 }
00887
00888 return true;
00889 }
00890
00891 void CalendarResources::beginAddingIncidences()
00892 {
00893 d->mAddingInProgress = true;
00894 }
00895
00896 void CalendarResources::endAddingIncidences()
00897 {
00898 d->mAddingInProgress = false;
00899 d->mLastUsedResource = 0;
00900 }
00901
00902 int CalendarResources::incrementChangeCount( ResourceCalendar *r )
00903 {
00904 if ( !mChangeCounts.contains( r ) ) {
00905 mChangeCounts.insert( r, 0 );
00906 }
00907
00908 int count = mChangeCounts[ r ];
00909 ++count;
00910 mChangeCounts[ r ] = count;
00911
00912 return count;
00913 }
00914
00915 int CalendarResources::decrementChangeCount( ResourceCalendar *r )
00916 {
00917 if ( !mChangeCounts.contains( r ) ) {
00918 kdError() << "No change count for resource." << endl;
00919 return 0;
00920 }
00921
00922 int count = mChangeCounts[ r ];
00923 --count;
00924 if ( count < 0 ) {
00925 kdError() << "Can't decrement change count. It already is 0." << endl;
00926 count = 0;
00927 }
00928 mChangeCounts[ r ] = count;
00929
00930 return count;
00931 }
00932
00933 void CalendarResources::slotLoadError( ResourceCalendar *, const QString &err )
00934 {
00935 emit signalErrorMessage( err );
00936 }
00937
00938 void CalendarResources::slotSaveError( ResourceCalendar *, const QString &err )
00939 {
00940 emit signalErrorMessage( err );
00941 }
00942
00943 #include "calendarresources.moc"