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