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