calendarresources.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include <stdlib.h>
00022
00023 #include <qdatetime.h>
00024 #include <qstring.h>
00025 #include <qptrlist.h>
00026
00027 #include <kdebug.h>
00028 #include <kstandarddirs.h>
00029 #include <klocale.h>
00030
00031 #include "vcaldrag.h"
00032 #include "vcalformat.h"
00033 #include "icalformat.h"
00034 #include "exceptions.h"
00035 #include "incidence.h"
00036 #include "journal.h"
00037 #include "filestorage.h"
00038
00039 #include <kresources/manager.h>
00040 #include <kresources/selectdialog.h>
00041 #include <kabc/lock.h>
00042
00043 #include "resourcecalendar.h"
00044 #include "resourcelocal.h"
00045
00046 #include "calendarresources.h"
00047
00048 using namespace KCal;
00049
00050 ResourceCalendar *CalendarResources::StandardDestinationPolicy::destination( Incidence * )
00051 {
00052 return resourceManager()->standardResource();
00053 }
00054
00055 ResourceCalendar *CalendarResources::AskDestinationPolicy::destination( Incidence * )
00056 {
00057 QPtrList<KRES::Resource> list;
00058
00059 CalendarResourceManager::ActiveIterator it;
00060 for( it = resourceManager()->activeBegin();
00061 it != resourceManager()->activeEnd(); ++it ) {
00062 if ( !(*it)->readOnly() )
00063 list.append( *it );
00064 }
00065
00066 KRES::Resource *r;
00067 r = KRES::SelectDialog::getResource( list, mParent );
00068 return static_cast<ResourceCalendar *>( r );
00069 }
00070
00071 CalendarResources::CalendarResources()
00072 : Calendar()
00073 {
00074 init();
00075 }
00076
00077 CalendarResources::CalendarResources(const QString &timeZoneId)
00078 : Calendar(timeZoneId)
00079 {
00080 init();
00081 }
00082
00083 void CalendarResources::init()
00084 {
00085 kdDebug(5800) << "CalendarResources::init" << endl;
00086
00087 mManager = new CalendarResourceManager( "calendar" );
00088 mManager->readConfig( 0 );
00089 mManager->addObserver( this );
00090
00091 if ( !mManager->standardResource() ) {
00092 kdDebug(5800) << "Warning! No standard resource yet." << endl;
00093 }
00094
00095
00096 CalendarResourceManager::Iterator i1;
00097 for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) {
00098 (*i1)->setTimeZoneId( timeZoneId() );
00099 }
00100
00101
00102 CalendarResourceManager::ActiveIterator it;
00103 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00104 kdDebug(5800) << "Opening resource " + (*it)->resourceName() << endl;
00105 bool result = (*it)->open();
00106 result = (*it)->load();
00107
00108 connectResource( *it );
00109 }
00110
00111 mStandardPolicy = new StandardDestinationPolicy( mManager );
00112 mAskPolicy = new AskDestinationPolicy( mManager );
00113 mDestinationPolicy = mStandardPolicy;
00114
00115 mOpen = true;
00116 }
00117
00118
00119 CalendarResources::~CalendarResources()
00120 {
00121 kdDebug(5800) << "CalendarResources::destructor" << endl;
00122
00123 close();
00124
00125 delete mManager;
00126 }
00127
00128 void CalendarResources::setStandardDestinationPolicy()
00129 {
00130 mDestinationPolicy = mStandardPolicy;
00131 }
00132
00133 void CalendarResources::setAskDestinationPolicy()
00134 {
00135 mDestinationPolicy = mAskPolicy;
00136 }
00137
00138 void CalendarResources::close()
00139 {
00140 kdDebug(5800) << "CalendarResources::close" << endl;
00141
00142 if ( mOpen ) {
00143 CalendarResourceManager::ActiveIterator it;
00144 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00145 (*it)->close();
00146 }
00147
00148 setModified( false );
00149 mOpen = false;
00150 }
00151 }
00152
00153 void CalendarResources::save()
00154 {
00155 kdDebug(5800) << "CalendarResources::save()" << endl;
00156
00157 if ( mOpen ) {
00158 CalendarResourceManager::ActiveIterator it;
00159 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00160 (*it)->save();
00161 }
00162
00163 setModified( false );
00164 }
00165 }
00166
00167 bool CalendarResources::isSaving()
00168 {
00169 CalendarResourceManager::ActiveIterator it;
00170 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00171 if ( (*it)->isSaving() ) {
00172 return true;
00173 }
00174 }
00175
00176 return false;
00177 }
00178
00179 bool CalendarResources::addIncidence( Incidence *incidence )
00180 {
00181 kdDebug(5800) << "CalendarResources::addIncidence" << endl;
00182
00183 ResourceCalendar *resource = mDestinationPolicy->destination( incidence );
00184
00185 if ( resource ) {
00186 resource->addIncidence( incidence );
00187 mResourceMap[ incidence ] = resource;
00188 } else {
00189 kdDebug(5800) << "CalendarResources::addIncidence(): no resource" << endl;
00190 return false;
00191 }
00192
00193 setModified( true );
00194
00195 return true;
00196 }
00197
00198 bool CalendarResources::addEvent( Event *event )
00199 {
00200 return addIncidence( event );
00201 }
00202
00203 bool CalendarResources::addEvent(Event *anEvent, ResourceCalendar *resource)
00204 {
00205 bool validRes = false;
00206 CalendarResourceManager::ActiveIterator it;
00207 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00208 if ( (*it) == resource ) validRes = true;
00209 }
00210 if ( validRes ) {
00211 resource->addEvent( anEvent );
00212 mResourceMap[anEvent] = resource;
00213 } else {
00214 return false;
00215 }
00216
00217 return true;
00218 }
00219
00220 void CalendarResources::deleteEvent(Event *event)
00221 {
00222 kdDebug(5800) << "CalendarResources::deleteEvent" << endl;
00223
00224 if ( mResourceMap.find(event)!=mResourceMap.end() ) {
00225 mResourceMap[event]->deleteEvent( event );
00226 mResourceMap.remove( event );
00227 } else {
00228 CalendarResourceManager::ActiveIterator it;
00229 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00230 (*it)->deleteEvent( event );
00231 }
00232 }
00233
00234 setModified( true );
00235 }
00236
00237
00238 Event *CalendarResources::event( const QString &uid )
00239 {
00240
00241
00242 CalendarResourceManager::ActiveIterator it;
00243 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00244 Event* event = (*it)->event( uid );
00245 if ( event )
00246 {
00247 mResourceMap[event] = *it;
00248 return event;
00249 }
00250 }
00251
00252
00253 return 0;
00254 }
00255
00256 bool CalendarResources::addTodo( Todo *todo )
00257 {
00258 kdDebug(5800) << "CalendarResources::addTodo" << endl;
00259
00260 return addIncidence( todo );
00261 }
00262
00263 bool CalendarResources::addTodo(Todo *todo, ResourceCalendar *resource)
00264 {
00265 bool validRes = false;
00266 CalendarResourceManager::ActiveIterator it;
00267 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00268 if ( (*it) == resource ) validRes = true;
00269 }
00270 if ( validRes ) {
00271 resource->addTodo( todo );
00272 mResourceMap[todo] = resource;
00273 } else {
00274 return false;
00275 }
00276
00277 return true;
00278 }
00279
00280 void CalendarResources::deleteTodo(Todo *todo)
00281 {
00282 kdDebug(5800) << "CalendarResources::deleteTodo" << endl;
00283
00284 Q_ASSERT(todo);
00285
00286 if ( mResourceMap.find(todo)!=mResourceMap.end() ) {
00287 mResourceMap[todo]->deleteTodo( todo );
00288 mResourceMap.remove( todo );
00289 } else {
00290 CalendarResourceManager::ActiveIterator it;
00291 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00292 (*it)->deleteTodo( todo );
00293 }
00294 }
00295
00296 setModified( true );
00297 }
00298
00299 Todo::List CalendarResources::rawTodos()
00300 {
00301
00302
00303 Todo::List result;
00304
00305 CalendarResourceManager::ActiveIterator it;
00306 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00307
00308
00309 Todo::List todos = (*it)->rawTodos();
00310 Todo::List::ConstIterator it2;
00311 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00312
00313 result.append( *it2 );
00314 mResourceMap[ *it2 ] = *it;
00315 }
00316 }
00317
00318 return result;
00319 }
00320
00321 Todo *CalendarResources::todo( const QString &uid )
00322 {
00323 kdDebug(5800) << "CalendarResources::todo(uid)" << endl;
00324
00325 CalendarResourceManager::ActiveIterator it;
00326 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00327 Todo *todo = (*it)->todo( uid );
00328 if ( todo ) {
00329 mResourceMap[todo] = *it;
00330 return todo;
00331 }
00332 }
00333
00334
00335 return 0;
00336 }
00337
00338 Todo::List CalendarResources::todos( const QDate &date )
00339 {
00340
00341
00342 Todo::List result;
00343
00344 CalendarResourceManager::ActiveIterator it;
00345 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00346 Todo::List todos = (*it)->todos( date );
00347 Todo::List::ConstIterator it2;
00348 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) {
00349 result.append( *it2 );
00350 mResourceMap[ *it2 ] = *it;
00351 }
00352 }
00353
00354 return result;
00355 }
00356
00357
00358 Alarm::List CalendarResources::alarmsTo( const QDateTime &to )
00359 {
00360 kdDebug(5800) << "CalendarResources::alarmsTo" << endl;
00361
00362 Alarm::List result;
00363 CalendarResourceManager::ActiveIterator it;
00364 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00365 Alarm::List list = (*it)->alarmsTo( to );
00366 Alarm::List::Iterator it;
00367 for ( it = list.begin(); it != list.end(); ++it )
00368 result.append( *it );
00369 }
00370 return result;
00371 }
00372
00373 Alarm::List CalendarResources::alarms( const QDateTime &from, const QDateTime &to )
00374 {
00375
00376
00377
00378 Alarm::List result;
00379 CalendarResourceManager::ActiveIterator it;
00380 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00381 Alarm::List list = (*it)->alarms( from, to );
00382 Alarm::List::Iterator it;
00383 for ( it = list.begin(); it != list.end(); ++it )
00384 result.append( *it );
00385 }
00386 return result;
00387 }
00388
00389
00390
00391
00392
00393
00394 Event::List CalendarResources::rawEventsForDate( const QDate &qd, bool sorted )
00395 {
00396
00397
00398 Event::List result;
00399 CalendarResourceManager::ActiveIterator it;
00400 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00401
00402
00403 Event::List list = (*it)->rawEventsForDate( qd, sorted );
00404
00405 Event::List::ConstIterator it2;
00406 if ( sorted ) {
00407 Event::List::Iterator insertionPoint = result.begin();
00408 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00409 while ( insertionPoint != result.end() &&
00410 (*insertionPoint)->dtStart().time() <= (*it2)->dtStart().time() )
00411 insertionPoint++;
00412 result.insert( insertionPoint, *it2 );
00413 mResourceMap[ *it2 ] = *it;
00414 }
00415 } else {
00416 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00417 result.append( *it2 );
00418 mResourceMap[ *it2 ] = *it;
00419 }
00420 }
00421 }
00422
00423 return result;
00424 }
00425
00426 Event::List CalendarResources::rawEvents( const QDate &start, const QDate &end,
00427 bool inclusive )
00428 {
00429 kdDebug(5800) << "CalendarResources::rawEvents(start,end,inclusive)" << endl;
00430
00431 Event::List result;
00432 CalendarResourceManager::ActiveIterator it;
00433 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00434 Event::List list = (*it)->rawEvents( start, end, inclusive );
00435 Event::List::ConstIterator it2;
00436 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00437 result.append( *it2 );
00438 mResourceMap[ *it2 ] = *it;
00439 }
00440 }
00441 return result;
00442 }
00443
00444 Event::List CalendarResources::rawEventsForDate(const QDateTime &qdt)
00445 {
00446 kdDebug(5800) << "CalendarResources::rawEventsForDate(qdt)" << endl;
00447
00448
00449 Event::List result;
00450 CalendarResourceManager::ActiveIterator it;
00451 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00452 Event::List list = (*it)->rawEventsForDate( qdt );
00453 Event::List::ConstIterator it2;
00454 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00455 result.append( *it2 );
00456 mResourceMap[ *it2 ] = *it;
00457 }
00458 }
00459 return result;
00460 }
00461
00462 Event::List CalendarResources::rawEvents()
00463 {
00464 kdDebug(5800) << "CalendarResources::rawEvents()" << endl;
00465
00466 Event::List result;
00467 CalendarResourceManager::ActiveIterator it;
00468 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00469 Event::List list = (*it)->rawEvents();
00470 Event::List::ConstIterator it2;
00471 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) {
00472 result.append( *it2 );
00473 mResourceMap[ *it2 ] = *it;
00474 }
00475 }
00476 return result;
00477 }
00478
00479
00480 bool CalendarResources::addJournal( Journal *journal )
00481 {
00482 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl;
00483
00484 return addIncidence( journal );
00485 }
00486
00487 void CalendarResources::deleteJournal( Journal *journal )
00488 {
00489 kdDebug(5800) << "CalendarResources::deleteJournal" << endl;
00490
00491 if ( mResourceMap.find(journal)!=mResourceMap.end() ) {
00492 mResourceMap[journal]->deleteJournal( journal );
00493 mResourceMap.remove( journal );
00494 } else {
00495 CalendarResourceManager::ActiveIterator it;
00496 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00497 (*it)->deleteJournal( journal );
00498 }
00499 }
00500
00501 setModified( true );
00502 }
00503
00504 bool CalendarResources::addJournal(Journal *journal, ResourceCalendar *resource)
00505 {
00506 bool validRes = false;
00507 CalendarResourceManager::ActiveIterator it;
00508 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00509 if ( (*it) == resource ) validRes = true;
00510 }
00511 if ( validRes ) {
00512 resource->addJournal( journal );
00513 mResourceMap[journal] = resource;
00514 } else {
00515 return false;
00516 }
00517
00518 return true;
00519 }
00520
00521 Journal *CalendarResources::journal(const QDate &date)
00522 {
00523 kdDebug(5800) << "CalendarResources::journal() " << date.toString() << endl;
00524 kdDebug(5800) << "FIXME: what to do with the multiple journals from multiple calendar resources?" << endl;
00525
00526
00527
00528
00529
00530 if ( mManager->standardResource() ) {
00531 Journal* journal = mManager->standardResource()->journal( date );
00532 if ( journal ) {
00533 mResourceMap[journal] = mManager->standardResource();
00534 return journal;
00535 }
00536 }
00537 CalendarResourceManager::ActiveIterator it;
00538 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00539 Journal* journal = (*it)->journal( date );
00540 if ( journal ) {
00541 mResourceMap[journal] = *it;
00542 return journal;
00543 }
00544 }
00545
00546 return 0;
00547 }
00548
00549 Journal *CalendarResources::journal(const QString &uid)
00550 {
00551 kdDebug(5800) << "CalendarResources::journal(uid)" << endl;
00552
00553 CalendarResourceManager::ActiveIterator it;
00554 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00555 Journal* journal = (*it)->journal( uid );
00556 if ( journal ) {
00557 mResourceMap[journal] = *it;
00558 return journal;
00559 }
00560 }
00561
00562
00563 return 0;
00564 }
00565
00566 Journal::List CalendarResources::journals()
00567 {
00568 kdDebug(5800) << "CalendarResources::journals()" << endl;
00569
00570 Journal::List result;
00571 CalendarResourceManager::ActiveIterator it;
00572 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) {
00573 Journal::List list = (*it)->journals();
00574 Journal::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
00584 void CalendarResources::incidenceUpdated( IncidenceBase * )
00585 {
00586 kdDebug(5800) << "CalendarResources::incidenceUpdated( IncidenceBase * ): Not yet implemented\n";
00587 }
00588
00589 void CalendarResources::connectResource( ResourceCalendar *resource )
00590 {
00591 connect( resource, SIGNAL( resourceChanged( ResourceCalendar * ) ),
00592 SIGNAL( calendarChanged() ) );
00593 connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ),
00594 SIGNAL( calendarSaved() ) );
00595 }
00596
00597 ResourceCalendar *CalendarResources::resource(Incidence *inc)
00598 {
00599 if ( mResourceMap.find( inc ) != mResourceMap.end() ) {
00600 return mResourceMap[ inc ];
00601 }
00602 return 0;
00603 }
00604
00605 void CalendarResources::resourceAdded( ResourceCalendar *resource )
00606 {
00607 kdDebug(5800) << "Resource added: " << resource->resourceName() << endl;
00608
00609 if ( !resource->isActive() ) return;
00610
00611 if ( resource->open() ) {
00612 resource->load();
00613 }
00614
00615 connectResource( resource );
00616
00617 emit signalResourceAdded( resource );
00618 }
00619
00620 void CalendarResources::resourceModified( ResourceCalendar *resource )
00621 {
00622 kdDebug(5800) << "Resource modified: " << resource->resourceName() << endl;
00623
00624 emit signalResourceModified( resource );
00625 }
00626
00627 void CalendarResources::resourceDeleted( ResourceCalendar *resource )
00628 {
00629 kdDebug(5800) << "Resource deleted: " << resource->resourceName() << endl;
00630
00631 emit signalResourceDeleted( resource );
00632 }
00633
00634 void CalendarResources::doSetTimeZoneId( const QString &tzid )
00635 {
00636
00637
00638 CalendarResourceManager::Iterator i1;
00639 for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) {
00640 (*i1)->setTimeZoneId( tzid );
00641 }
00642 }
00643
00644 CalendarResources::Ticket *CalendarResources::requestSaveTicket( ResourceCalendar *resource )
00645 {
00646 kdDebug(5800) << "CalendarResources::requestSaveTicket()" << endl;
00647
00648 KABC::Lock *lock = resource->lock();
00649 if ( !lock ) return 0;
00650 if ( lock->lock() ) return new Ticket( resource );
00651 else return 0;
00652 }
00653
00654 bool CalendarResources::save( Ticket *ticket )
00655 {
00656 if ( !ticket || !ticket->resource() ) return false;
00657
00658 if ( ticket->resource()->save() ) {
00659 releaseSaveTicket( ticket );
00660 return true;
00661 }
00662
00663 return false;
00664 }
00665
00666 void CalendarResources::releaseSaveTicket( Ticket *ticket )
00667 {
00668 ticket->resource()->lock()->unlock();
00669 delete ticket;
00670 }
00671
00672 bool CalendarResources::beginChange( Incidence *incidence )
00673 {
00674 kdDebug(5800) << "CalendarResources::beginChange()" << endl;
00675
00676 ResourceCalendar *r = resource( incidence );
00677 if ( !r ) {
00678 r = mDestinationPolicy->destination( incidence );
00679 if ( !r ) {
00680 kdError() << "Unable to get destination resource." << endl;
00681 return false;
00682 }
00683 mResourceMap[ incidence ] = r;
00684 }
00685
00686 int count = incrementChangeCount( r );
00687 if ( count == 1 ) {
00688 Ticket *ticket = requestSaveTicket( r );
00689 if ( !ticket ) {
00690 kdDebug(5800) << "CalendarResources::beginChange(): unable to get ticket."
00691 << endl;
00692 decrementChangeCount( r );
00693 return false;
00694 } else {
00695 mTickets[ r ] = ticket;
00696 }
00697 }
00698
00699 return true;
00700 }
00701
00702 bool CalendarResources::endChange( Incidence *incidence )
00703 {
00704 kdDebug(5800) << "CalendarResource::endChange()" << endl;
00705
00706 ResourceCalendar *r = resource( incidence );
00707 if ( !r ) return false;
00708
00709 int count = decrementChangeCount( r );
00710
00711 if ( count == 0 ) {
00712 bool ok = save( mTickets[ r ] );
00713 if ( ok ) {
00714 mTickets.remove( r );
00715 } else {
00716 return false;
00717 }
00718 }
00719
00720 return true;
00721 }
00722
00723 int CalendarResources::incrementChangeCount( ResourceCalendar *r )
00724 {
00725 if ( !mChangeCounts.contains( r ) ) {
00726 mChangeCounts.insert( r, 0 );
00727 }
00728
00729 int count = mChangeCounts[ r ];
00730 ++count;
00731 mChangeCounts[ r ] = count;
00732
00733 return count;
00734 }
00735
00736 int CalendarResources::decrementChangeCount( ResourceCalendar *r )
00737 {
00738 if ( !mChangeCounts.contains( r ) ) {
00739 kdError() << "No change count for resource." << endl;
00740 return 0;
00741 }
00742
00743 int count = mChangeCounts[ r ];
00744 --count;
00745 if ( count < 0 ) {
00746 kdError() << "Can't decrement change count. It already is 0." << endl;
00747 count = 0;
00748 }
00749 mChangeCounts[ r ] = count;
00750
00751 return count;
00752 }
00753
00754 #include "calendarresources.moc"
This file is part of the documentation for libkcal Library Version 3.2.2.