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