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 ), 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
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 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
00165
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
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
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
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
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
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
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
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
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
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
00768
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 kdDebug(5800) << "saved: failure" << endl;
00800 return false;
00801 }
00802
00803 kdDebug(5800) << "tick " << ticket->resource()->resourceName() << endl;
00804
00805
00806 if ( ticket->resource()->save( incidence ) ) {
00807 releaseSaveTicket( ticket );
00808 kdDebug(5800) << "save: success" << endl;
00809 return true;
00810 }
00811 kdDebug(5800) << "save: failure2" << endl;
00812 return false;
00813 }
00814
00815 void CalendarResources::releaseSaveTicket( Ticket *ticket )
00816 {
00817 ticket->resource()->lock()->unlock();
00818 delete ticket;
00819 }
00820
00821 bool CalendarResources::beginChange( Incidence *incidence )
00822 {
00823 return beginChange( incidence, 0, QString() );
00824 }
00825
00826 bool CalendarResources::beginChange( Incidence *incidence,
00827 ResourceCalendar *res,
00828 const QString &subres )
00829 {
00830 Q_UNUSED( subres );
00831
00832 kdDebug(5800) << "CalendarResources::beginChange()" << endl;
00833
00834 if ( !res ) {
00835 res = resource( incidence );
00836 }
00837 if ( !res ) {
00838 res = mDestinationPolicy->destination( incidence );
00839 if ( !res ) {
00840 kdError() << "Unable to get destination resource." << endl;
00841 return false;
00842 }
00843 mResourceMap[ incidence ] = res;
00844 }
00845 mPendingDeleteFromResourceMap = false;
00846
00847 int count = incrementChangeCount( res );
00848 if ( count == 1 ) {
00849 Ticket *ticket = requestSaveTicket( res );
00850 if ( !ticket ) {
00851 kdDebug(5800) << "CalendarResources::beginChange(): unable to get ticket."
00852 << endl;
00853 decrementChangeCount( res );
00854 return false;
00855 } else {
00856 mTickets[ res ] = ticket;
00857 }
00858 }
00859
00860 return true;
00861 }
00862
00863 bool CalendarResources::endChange( Incidence *incidence )
00864 {
00865 return endChange( incidence, 0, QString() );
00866 }
00867
00868 bool CalendarResources::endChange( Incidence *incidence,
00869 ResourceCalendar *res,
00870 const QString &subres )
00871 {
00872 Q_UNUSED( subres );
00873
00874 kdDebug(5800) << "CalendarResource::endChange()" << incidence->dtStart() << endl;
00875
00876 if ( !res ) {
00877 res = resource( incidence );
00878 }
00879 if ( !res )
00880 return false;
00881
00882 int count = decrementChangeCount( res );
00883
00884 if ( mPendingDeleteFromResourceMap ) {
00885 mResourceMap.remove( incidence );
00886 mPendingDeleteFromResourceMap = false;
00887 }
00888
00889 if ( count == 0 ) {
00890 bool ok = save( mTickets[ res ], incidence );
00891 if ( ok ) {
00892 mTickets.remove( res );
00893 } else {
00894 return false;
00895 }
00896 }
00897
00898 return true;
00899 }
00900
00901 void CalendarResources::beginAddingIncidences()
00902 {
00903 kdDebug(5800) << "CalendarResources: beginAddingIncidences() " << endl;
00904 d->mBatchAddingInProgress = true;
00905 }
00906
00907 void CalendarResources::endAddingIncidences()
00908 {
00909 kdDebug(5800) << "CalendarResources: endAddingIncidences() " << endl;
00910 d->mBatchAddingInProgress = false;
00911
00912 if ( d->mLastUsedResource ) {
00913 d->mLastUsedResource->endAddingIncidences();
00914 }
00915
00916 d->mLastUsedResource = 0;
00917 }
00918
00919 int CalendarResources::incrementChangeCount( ResourceCalendar *r )
00920 {
00921 if ( !mChangeCounts.contains( r ) ) {
00922 mChangeCounts.insert( r, 0 );
00923 }
00924
00925 int count = mChangeCounts[ r ];
00926 ++count;
00927 mChangeCounts[ r ] = count;
00928
00929 return count;
00930 }
00931
00932 int CalendarResources::decrementChangeCount( ResourceCalendar *r )
00933 {
00934 if ( !mChangeCounts.contains( r ) ) {
00935 kdError() << "No change count for resource." << endl;
00936 return 0;
00937 }
00938
00939 int count = mChangeCounts[ r ];
00940 --count;
00941 if ( count < 0 ) {
00942 kdError() << "Can't decrement change count. It already is 0." << endl;
00943 count = 0;
00944 }
00945 mChangeCounts[ r ] = count;
00946
00947 return count;
00948 }
00949
00950 void CalendarResources::slotLoadError( ResourceCalendar *, const QString &err )
00951 {
00952 emit signalErrorMessage( err );
00953 }
00954
00955 void CalendarResources::slotSaveError( ResourceCalendar *, const QString &err )
00956 {
00957 emit signalErrorMessage( err );
00958 }
00959
00960 #include "calendarresources.moc"