libkcal Library API Documentation

calendarresources.cpp

00001 /* 00002 This file is part of libkcal. 00003 Copyright (c) 2003 Cornelius Schumacher <schumacher@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00018 Boston, MA 02111-1307, USA. 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->addObserver( this ); 00089 00090 mStandardPolicy = new StandardDestinationPolicy( mManager ); 00091 mAskPolicy = new AskDestinationPolicy( mManager ); 00092 mDestinationPolicy = mStandardPolicy; 00093 } 00094 00095 CalendarResources::~CalendarResources() 00096 { 00097 // kdDebug(5800) << "CalendarResources::destructor" << endl; 00098 00099 close(); 00100 00101 delete mManager; 00102 } 00103 00104 void CalendarResources::readConfig( KConfig *config ) 00105 { 00106 mManager->readConfig( config ); 00107 00108 CalendarResourceManager::Iterator it; 00109 for ( it = mManager->begin(); it != mManager->end(); ++it ) { 00110 connectResource( *it ); 00111 } 00112 } 00113 00114 void CalendarResources::load() 00115 { 00116 kdDebug(5800) << "CalendarResources::load()" << endl; 00117 00118 if ( !mManager->standardResource() ) { 00119 kdDebug(5800) << "Warning! No standard resource yet." << endl; 00120 } 00121 00122 // set the timezone for all resources. Otherwise we'll have those terrible tz 00123 // troubles ;-(( 00124 CalendarResourceManager::Iterator i1; 00125 for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) { 00126 (*i1)->setTimeZoneId( timeZoneId() ); 00127 } 00128 00129 // Open all active resources 00130 CalendarResourceManager::ActiveIterator it; 00131 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00132 (*it)->load(); 00133 } 00134 00135 mOpen = true; 00136 } 00137 00138 void CalendarResources::setStandardDestinationPolicy() 00139 { 00140 mDestinationPolicy = mStandardPolicy; 00141 } 00142 00143 void CalendarResources::setAskDestinationPolicy() 00144 { 00145 mDestinationPolicy = mAskPolicy; 00146 } 00147 00148 void CalendarResources::close() 00149 { 00150 kdDebug(5800) << "CalendarResources::close" << endl; 00151 00152 if ( mOpen ) { 00153 CalendarResourceManager::ActiveIterator it; 00154 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00155 (*it)->close(); 00156 } 00157 00158 setModified( false ); 00159 mOpen = false; 00160 } 00161 } 00162 00163 void CalendarResources::save() 00164 { 00165 kdDebug(5800) << "CalendarResources::save()" << endl; 00166 00167 if ( mOpen ) { 00168 CalendarResourceManager::ActiveIterator it; 00169 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00170 (*it)->save(); 00171 } 00172 00173 setModified( false ); 00174 } 00175 } 00176 00177 bool CalendarResources::isSaving() 00178 { 00179 CalendarResourceManager::ActiveIterator it; 00180 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00181 if ( (*it)->isSaving() ) { 00182 return true; 00183 } 00184 } 00185 00186 return false; 00187 } 00188 00189 bool CalendarResources::addIncidence( Incidence *incidence ) 00190 { 00191 kdDebug(5800) << "CalendarResources::addIncidence" << endl; 00192 00193 ResourceCalendar *resource = mDestinationPolicy->destination( incidence ); 00194 00195 if ( resource ) { 00196 resource->addIncidence( incidence ); 00197 mResourceMap[ incidence ] = resource; 00198 } else { 00199 kdDebug(5800) << "CalendarResources::addIncidence(): no resource" << endl; 00200 return false; 00201 } 00202 00203 setModified( true ); 00204 00205 return true; 00206 } 00207 00208 bool CalendarResources::addEvent( Event *event ) 00209 { 00210 return addIncidence( event ); 00211 } 00212 00213 bool CalendarResources::addEvent( Event *anEvent, ResourceCalendar *resource ) 00214 { 00215 bool validRes = false; 00216 CalendarResourceManager::ActiveIterator it; 00217 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00218 if ( (*it) == resource ) validRes = true; 00219 } 00220 if ( validRes ) { 00221 resource->addEvent( anEvent ); 00222 mResourceMap[anEvent] = resource; 00223 } else { 00224 return false; 00225 } 00226 00227 return true; 00228 } 00229 00230 void CalendarResources::deleteEvent( Event *event ) 00231 { 00232 kdDebug(5800) << "CalendarResources::deleteEvent" << endl; 00233 00234 if ( mResourceMap.find( event ) != mResourceMap.end() ) { 00235 mResourceMap[event]->deleteEvent( event ); 00236 mResourceMap.remove( event ); 00237 } else { 00238 CalendarResourceManager::ActiveIterator it; 00239 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00240 (*it)->deleteEvent( event ); 00241 } 00242 } 00243 00244 setModified( true ); 00245 } 00246 00247 00248 Event *CalendarResources::event( const QString &uid ) 00249 { 00250 // kdDebug(5800) << "CalendarResources::event(): " << uid << endl; 00251 00252 CalendarResourceManager::ActiveIterator it; 00253 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00254 Event* event = (*it)->event( uid ); 00255 if ( event ) 00256 { 00257 mResourceMap[event] = *it; 00258 return event; 00259 } 00260 } 00261 00262 // Not found 00263 return 0; 00264 } 00265 00266 bool CalendarResources::addTodo( Todo *todo ) 00267 { 00268 kdDebug(5800) << "CalendarResources::addTodo" << endl; 00269 00270 return addIncidence( todo ); 00271 } 00272 00273 bool CalendarResources::addTodo(Todo *todo, ResourceCalendar *resource) 00274 { 00275 bool validRes = false; 00276 CalendarResourceManager::ActiveIterator it; 00277 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00278 if ( (*it) == resource ) validRes = true; 00279 } 00280 if ( validRes ) { 00281 resource->addTodo( todo ); 00282 mResourceMap[todo] = resource; 00283 } else { 00284 return false; 00285 } 00286 00287 return true; 00288 } 00289 00290 void CalendarResources::deleteTodo( Todo *todo ) 00291 { 00292 kdDebug(5800) << "CalendarResources::deleteTodo" << endl; 00293 00294 Q_ASSERT(todo); 00295 00296 if ( mResourceMap.find(todo) != mResourceMap.end() ) { 00297 mResourceMap[todo]->deleteTodo( todo ); 00298 mResourceMap.remove( todo ); 00299 } else { 00300 CalendarResourceManager::ActiveIterator it; 00301 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00302 (*it)->deleteTodo( todo ); 00303 } 00304 } 00305 00306 setModified( true ); 00307 } 00308 00309 Todo::List CalendarResources::rawTodos() 00310 { 00311 // kdDebug(5800) << "CalendarResources::rawTodos()" << endl; 00312 00313 Todo::List result; 00314 00315 CalendarResourceManager::ActiveIterator it; 00316 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00317 // kdDebug(5800) << "Getting raw todos from '" << (*it)->resourceName() 00318 // << "'" << endl; 00319 Todo::List todos = (*it)->rawTodos(); 00320 Todo::List::ConstIterator it2; 00321 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) { 00322 // kdDebug(5800) << "Adding todo to result" << endl; 00323 result.append( *it2 ); 00324 mResourceMap[ *it2 ] = *it; 00325 } 00326 } 00327 00328 return result; 00329 } 00330 00331 Todo *CalendarResources::todo( const QString &uid ) 00332 { 00333 kdDebug(5800) << "CalendarResources::todo(uid)" << endl; 00334 00335 CalendarResourceManager::ActiveIterator it; 00336 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00337 Todo *todo = (*it)->todo( uid ); 00338 if ( todo ) { 00339 mResourceMap[todo] = *it; 00340 return todo; 00341 } 00342 } 00343 00344 // not found 00345 return 0; 00346 } 00347 00348 Todo::List CalendarResources::rawTodosForDate( const QDate &date ) 00349 { 00350 // kdDebug(5800) << "CalendarResources::rawTodosforDate(date)" << endl; 00351 00352 Todo::List result; 00353 00354 CalendarResourceManager::ActiveIterator it; 00355 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00356 Todo::List todos = (*it)->rawTodosForDate( date ); 00357 Todo::List::ConstIterator it2; 00358 for ( it2 = todos.begin(); it2 != todos.end(); ++it2 ) { 00359 result.append( *it2 ); 00360 mResourceMap[ *it2 ] = *it; 00361 } 00362 } 00363 00364 return result; 00365 } 00366 00367 00368 Alarm::List CalendarResources::alarmsTo( const QDateTime &to ) 00369 { 00370 kdDebug(5800) << "CalendarResources::alarmsTo" << endl; 00371 00372 Alarm::List result; 00373 CalendarResourceManager::ActiveIterator it; 00374 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00375 Alarm::List list = (*it)->alarmsTo( to ); 00376 Alarm::List::Iterator it; 00377 for ( it = list.begin(); it != list.end(); ++it ) 00378 result.append( *it ); 00379 } 00380 return result; 00381 } 00382 00383 Alarm::List CalendarResources::alarms( const QDateTime &from, const QDateTime &to ) 00384 { 00385 // kdDebug(5800) << "CalendarResources::alarms(" << from.toString() << " - " 00386 // << to.toString() << ")" << endl; 00387 00388 Alarm::List result; 00389 CalendarResourceManager::ActiveIterator it; 00390 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00391 Alarm::List list = (*it)->alarms( from, to ); 00392 Alarm::List::Iterator it; 00393 for ( it = list.begin(); it != list.end(); ++it ) 00394 result.append( *it ); 00395 } 00396 return result; 00397 } 00398 00399 /****************************** PROTECTED METHODS ****************************/ 00400 00401 00402 // taking a QDate, this function will look for an eventlist in the dict 00403 // with that date attached - 00404 Event::List CalendarResources::rawEventsForDate( const QDate &qd, bool sorted ) 00405 { 00406 // kdDebug(5800) << "CalendarResources::rawEventsForDate()" << endl; 00407 00408 Event::List result; 00409 CalendarResourceManager::ActiveIterator it; 00410 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00411 // kdDebug(5800) << "Getting events from '" << (*it)->resourceName() << "'" 00412 // << endl; 00413 Event::List list = (*it)->rawEventsForDate( qd, sorted ); 00414 00415 Event::List::ConstIterator it2; 00416 if ( sorted ) { 00417 Event::List::Iterator insertionPoint = result.begin(); 00418 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00419 while ( insertionPoint != result.end() && 00420 (*insertionPoint)->dtStart().time() <= (*it2)->dtStart().time() ) 00421 insertionPoint++; 00422 result.insert( insertionPoint, *it2 ); 00423 mResourceMap[ *it2 ] = *it; 00424 } 00425 } else { 00426 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00427 result.append( *it2 ); 00428 mResourceMap[ *it2 ] = *it; 00429 } 00430 } 00431 } 00432 00433 return result; 00434 } 00435 00436 Event::List CalendarResources::rawEvents( const QDate &start, const QDate &end, 00437 bool inclusive ) 00438 { 00439 kdDebug(5800) << "CalendarResources::rawEvents(start,end,inclusive)" << endl; 00440 00441 Event::List result; 00442 CalendarResourceManager::ActiveIterator it; 00443 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00444 Event::List list = (*it)->rawEvents( start, end, inclusive ); 00445 Event::List::ConstIterator it2; 00446 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00447 result.append( *it2 ); 00448 mResourceMap[ *it2 ] = *it; 00449 } 00450 } 00451 return result; 00452 } 00453 00454 Event::List CalendarResources::rawEventsForDate(const QDateTime &qdt) 00455 { 00456 kdDebug(5800) << "CalendarResources::rawEventsForDate(qdt)" << endl; 00457 00458 // TODO: Remove the code duplication by the resourcemap iteration block. 00459 Event::List result; 00460 CalendarResourceManager::ActiveIterator it; 00461 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00462 Event::List list = (*it)->rawEventsForDate( qdt ); 00463 Event::List::ConstIterator it2; 00464 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00465 result.append( *it2 ); 00466 mResourceMap[ *it2 ] = *it; 00467 } 00468 } 00469 return result; 00470 } 00471 00472 Event::List CalendarResources::rawEvents() 00473 { 00474 kdDebug(5800) << "CalendarResources::rawEvents()" << endl; 00475 00476 Event::List result; 00477 CalendarResourceManager::ActiveIterator it; 00478 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00479 Event::List list = (*it)->rawEvents(); 00480 Event::List::ConstIterator it2; 00481 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00482 result.append( *it2 ); 00483 mResourceMap[ *it2 ] = *it; 00484 } 00485 } 00486 return result; 00487 } 00488 00489 00490 bool CalendarResources::addJournal( Journal *journal ) 00491 { 00492 kdDebug(5800) << "Adding Journal on " << journal->dtStart().toString() << endl; 00493 00494 return addIncidence( journal ); 00495 } 00496 00497 void CalendarResources::deleteJournal( Journal *journal ) 00498 { 00499 kdDebug(5800) << "CalendarResources::deleteJournal" << endl; 00500 00501 if ( mResourceMap.find(journal)!=mResourceMap.end() ) { 00502 mResourceMap[journal]->deleteJournal( journal ); 00503 mResourceMap.remove( journal ); 00504 } else { 00505 CalendarResourceManager::ActiveIterator it; 00506 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00507 (*it)->deleteJournal( journal ); 00508 } 00509 } 00510 00511 setModified( true ); 00512 } 00513 00514 bool CalendarResources::addJournal(Journal *journal, ResourceCalendar *resource) 00515 { 00516 bool validRes = false; 00517 CalendarResourceManager::ActiveIterator it; 00518 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00519 if ( (*it) == resource ) validRes = true; 00520 } 00521 if ( validRes ) { 00522 resource->addJournal( journal ); 00523 mResourceMap[journal] = resource; 00524 } else { 00525 return false; 00526 } 00527 00528 return true; 00529 } 00530 00531 Journal *CalendarResources::journal(const QDate &date) 00532 { 00533 kdDebug(5800) << "CalendarResources::journal() " << date.toString() << endl; 00534 kdDebug(5800) << "FIXME: what to do with the multiple journals from multiple calendar resources?" << endl; 00535 00536 // If we're on a private resource, return that journal. 00537 // Else, first see if the standard resource has a journal for this date. If it has, return that journal. 00538 // If not, check all resources for a journal for this date. 00539 00540 if ( mManager->standardResource() ) { 00541 Journal* journal = mManager->standardResource()->journal( date ); 00542 if ( journal ) { 00543 mResourceMap[journal] = mManager->standardResource(); 00544 return journal; 00545 } 00546 } 00547 CalendarResourceManager::ActiveIterator it; 00548 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00549 Journal* journal = (*it)->journal( date ); 00550 if ( journal ) { 00551 mResourceMap[journal] = *it; 00552 return journal; 00553 } 00554 } 00555 00556 return 0; 00557 } 00558 00559 Journal *CalendarResources::journal(const QString &uid) 00560 { 00561 kdDebug(5800) << "CalendarResources::journal(uid)" << endl; 00562 00563 CalendarResourceManager::ActiveIterator it; 00564 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00565 Journal* journal = (*it)->journal( uid ); 00566 if ( journal ) { 00567 mResourceMap[journal] = *it; 00568 return journal; 00569 } 00570 } 00571 00572 // not found 00573 return 0; 00574 } 00575 00576 Journal::List CalendarResources::journals() 00577 { 00578 kdDebug(5800) << "CalendarResources::journals()" << endl; 00579 00580 Journal::List result; 00581 CalendarResourceManager::ActiveIterator it; 00582 for ( it = mManager->activeBegin(); it != mManager->activeEnd(); ++it ) { 00583 Journal::List list = (*it)->journals(); 00584 Journal::List::ConstIterator it2; 00585 for ( it2 = list.begin(); it2 != list.end(); ++it2 ) { 00586 result.append( *it2 ); 00587 mResourceMap[ *it2 ] = *it; 00588 } 00589 } 00590 return result; 00591 } 00592 00593 00594 void CalendarResources::incidenceUpdated( IncidenceBase * ) 00595 { 00596 kdDebug(5800) << "CalendarResources::incidenceUpdated( IncidenceBase * ): Not yet implemented\n"; 00597 } 00598 00599 void CalendarResources::connectResource( ResourceCalendar *resource ) 00600 { 00601 connect( resource, SIGNAL( resourceChanged( ResourceCalendar * ) ), 00602 SIGNAL( calendarChanged() ) ); 00603 connect( resource, SIGNAL( resourceSaved( ResourceCalendar * ) ), 00604 SIGNAL( calendarSaved() ) ); 00605 00606 connect( resource, SIGNAL( resourceLoadError( ResourceCalendar *, 00607 const QString & ) ), 00608 SLOT( slotLoadError( ResourceCalendar *, const QString & ) ) ); 00609 connect( resource, SIGNAL( resourceSaveError( ResourceCalendar *, 00610 const QString & ) ), 00611 SLOT( slotSaveError( ResourceCalendar *, const QString & ) ) ); 00612 } 00613 00614 ResourceCalendar *CalendarResources::resource(Incidence *inc) 00615 { 00616 if ( mResourceMap.find( inc ) != mResourceMap.end() ) { 00617 return mResourceMap[ inc ]; 00618 } 00619 return 0; 00620 } 00621 00622 void CalendarResources::resourceAdded( ResourceCalendar *resource ) 00623 { 00624 kdDebug(5800) << "Resource added: " << resource->resourceName() << endl; 00625 00626 if ( !resource->isActive() ) return; 00627 00628 if ( resource->open() ) { 00629 resource->load(); 00630 } 00631 00632 connectResource( resource ); 00633 00634 emit signalResourceAdded( resource ); 00635 } 00636 00637 void CalendarResources::resourceModified( ResourceCalendar *resource ) 00638 { 00639 kdDebug(5800) << "Resource modified: " << resource->resourceName() << endl; 00640 00641 emit signalResourceModified( resource ); 00642 } 00643 00644 void CalendarResources::resourceDeleted( ResourceCalendar *resource ) 00645 { 00646 kdDebug(5800) << "Resource deleted: " << resource->resourceName() << endl; 00647 00648 emit signalResourceDeleted( resource ); 00649 } 00650 00651 void CalendarResources::doSetTimeZoneId( const QString &tzid ) 00652 { 00653 // set the timezone for all resources. Otherwise we'll have those terrible 00654 // tz troubles ;-(( 00655 CalendarResourceManager::Iterator i1; 00656 for ( i1 = mManager->begin(); i1 != mManager->end(); ++i1 ) { 00657 (*i1)->setTimeZoneId( tzid ); 00658 } 00659 } 00660 00661 CalendarResources::Ticket *CalendarResources::requestSaveTicket( ResourceCalendar *resource ) 00662 { 00663 kdDebug(5800) << "CalendarResources::requestSaveTicket()" << endl; 00664 00665 KABC::Lock *lock = resource->lock(); 00666 if ( !lock ) return 0; 00667 if ( lock->lock() ) return new Ticket( resource ); 00668 else return 0; 00669 } 00670 00671 bool CalendarResources::save( Ticket *ticket ) 00672 { 00673 kdDebug(5800) << "CalendarResources::save( Ticket *)" << endl; 00674 00675 if ( !ticket || !ticket->resource() ) return false; 00676 00677 kdDebug(5800) << "tick " << ticket->resource()->resourceName() << endl; 00678 00679 if ( ticket->resource()->save() ) { 00680 releaseSaveTicket( ticket ); 00681 return true; 00682 } 00683 00684 return false; 00685 } 00686 00687 void CalendarResources::releaseSaveTicket( Ticket *ticket ) 00688 { 00689 ticket->resource()->lock()->unlock(); 00690 delete ticket; 00691 } 00692 00693 bool CalendarResources::beginChange( Incidence *incidence ) 00694 { 00695 kdDebug(5800) << "CalendarResources::beginChange()" << endl; 00696 00697 ResourceCalendar *r = resource( incidence ); 00698 if ( !r ) { 00699 r = mDestinationPolicy->destination( incidence ); 00700 if ( !r ) { 00701 kdError() << "Unable to get destination resource." << endl; 00702 return false; 00703 } 00704 mResourceMap[ incidence ] = r; 00705 } 00706 00707 int count = incrementChangeCount( r ); 00708 if ( count == 1 ) { 00709 Ticket *ticket = requestSaveTicket( r ); 00710 if ( !ticket ) { 00711 kdDebug(5800) << "CalendarResources::beginChange(): unable to get ticket." 00712 << endl; 00713 decrementChangeCount( r ); 00714 return false; 00715 } else { 00716 mTickets[ r ] = ticket; 00717 } 00718 } 00719 00720 return true; 00721 } 00722 00723 bool CalendarResources::endChange( Incidence *incidence ) 00724 { 00725 kdDebug(5800) << "CalendarResource::endChange()" << endl; 00726 00727 ResourceCalendar *r = resource( incidence ); 00728 if ( !r ) return false; 00729 00730 int count = decrementChangeCount( r ); 00731 00732 if ( count == 0 ) { 00733 bool ok = save( mTickets[ r ] ); 00734 if ( ok ) { 00735 mTickets.remove( r ); 00736 } else { 00737 return false; 00738 } 00739 } 00740 00741 return true; 00742 } 00743 00744 int CalendarResources::incrementChangeCount( ResourceCalendar *r ) 00745 { 00746 if ( !mChangeCounts.contains( r ) ) { 00747 mChangeCounts.insert( r, 0 ); 00748 } 00749 00750 int count = mChangeCounts[ r ]; 00751 ++count; 00752 mChangeCounts[ r ] = count; 00753 00754 return count; 00755 } 00756 00757 int CalendarResources::decrementChangeCount( ResourceCalendar *r ) 00758 { 00759 if ( !mChangeCounts.contains( r ) ) { 00760 kdError() << "No change count for resource." << endl; 00761 return 0; 00762 } 00763 00764 int count = mChangeCounts[ r ]; 00765 --count; 00766 if ( count < 0 ) { 00767 kdError() << "Can't decrement change count. It already is 0." << endl; 00768 count = 0; 00769 } 00770 mChangeCounts[ r ] = count; 00771 00772 return count; 00773 } 00774 00775 void CalendarResources::slotLoadError( ResourceCalendar *, const QString &err ) 00776 { 00777 emit signalErrorMessage( err ); 00778 } 00779 00780 void CalendarResources::slotSaveError( ResourceCalendar *, const QString &err ) 00781 { 00782 emit signalErrorMessage( err ); 00783 } 00784 00785 #include "calendarresources.moc"
KDE Logo
This file is part of the documentation for libkcal Library Version 3.3.0.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Aug 27 12:49:07 2004 by doxygen 1.3.8 written by Dimitri van Heesch, © 1997-2003