00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include <klocale.h>
00024 #include <kdebug.h>
00025 #include <kmessagebox.h>
00026 #include <kstandarddirs.h>
00027
00028 #include "event.h"
00029 #include "todo.h"
00030 #include "freebusy.h"
00031 #include "icalformat.h"
00032 #include "calendar.h"
00033 #include "freebusycache.h"
00034 #include "assignmentvisitor.h"
00035
00036 #include "scheduler.h"
00037
00038 using namespace KCal;
00039
00040 ScheduleMessage::ScheduleMessage(IncidenceBase *incidence,int method,ScheduleMessage::Status status)
00041 {
00042 mIncidence = incidence;
00043 mMethod = method;
00044 mStatus = status;
00045 }
00046
00047 QString ScheduleMessage::statusName(ScheduleMessage::Status status)
00048 {
00049 switch (status) {
00050 case PublishUpdate:
00051 return i18n("Updated Publish");
00052 case PublishNew:
00053 return i18n("Publish");
00054 case Obsolete:
00055 return i18n("Obsolete");
00056 case RequestNew:
00057 return i18n("New Request");
00058 case RequestUpdate:
00059 return i18n("Updated Request");
00060 default:
00061 return i18n("Unknown Status: %1").arg(QString::number(status));
00062 }
00063 }
00064
00065 struct Scheduler::Private
00066 {
00067 Private() : mFreeBusyCache( 0 ) {}
00068
00069 FreeBusyCache *mFreeBusyCache;
00070 };
00071
00072 Scheduler::Scheduler(Calendar *calendar)
00073 {
00074 mCalendar = calendar;
00075 mFormat = new ICalFormat();
00076 mFormat->setTimeZone( calendar->timeZoneId(), !calendar->isLocalTime() );
00077
00078 d = new Private;
00079 }
00080
00081 Scheduler::~Scheduler()
00082 {
00083 delete d;
00084
00085 delete mFormat;
00086 }
00087
00088 void Scheduler::setFreeBusyCache( FreeBusyCache *c )
00089 {
00090 d->mFreeBusyCache = c;
00091 }
00092
00093 FreeBusyCache *Scheduler::freeBusyCache() const
00094 {
00095 return d->mFreeBusyCache;
00096 }
00097
00098 bool Scheduler::acceptTransaction( IncidenceBase *incidence,
00099 Method method,
00100 ScheduleMessage::Status status,
00101 const QString &attendee )
00102 {
00103 kdDebug(5800) << "Scheduler::acceptTransaction, method="
00104 << methodName( method ) << endl;
00105
00106 switch (method) {
00107 case Publish:
00108 return acceptPublish(incidence, status, method);
00109 case Request:
00110 return acceptRequest( incidence, status, attendee );
00111 case Add:
00112 return acceptAdd(incidence, status);
00113 case Cancel:
00114 return acceptCancel(incidence, status, attendee );
00115 case Declinecounter:
00116 return acceptDeclineCounter(incidence, status);
00117 case Reply:
00118 return acceptReply(incidence, status, method);
00119 case Refresh:
00120 return acceptRefresh(incidence, status);
00121 case Counter:
00122 return acceptCounter(incidence, status);
00123 default:
00124 break;
00125 }
00126 deleteTransaction(incidence);
00127 return false;
00128 }
00129
00130 QString Scheduler::methodName(Method method)
00131 {
00132 switch (method) {
00133 case Publish:
00134 return QString::fromLatin1("Publish");
00135 case Request:
00136 return QString::fromLatin1("Request");
00137 case Refresh:
00138 return QString::fromLatin1("Refresh");
00139 case Cancel:
00140 return QString::fromLatin1("Cancel");
00141 case Add:
00142 return QString::fromLatin1("Add");
00143 case Reply:
00144 return QString::fromLatin1("Reply");
00145 case Counter:
00146 return QString::fromLatin1("Counter");
00147 case Declinecounter:
00148 return QString::fromLatin1("Decline Counter");
00149 default:
00150 return QString::fromLatin1("Unknown");
00151 }
00152 }
00153
00154 QString Scheduler::translatedMethodName(Method method)
00155 {
00156 switch (method) {
00157 case Publish:
00158 return i18n("Publish");
00159 case Request:
00160 return i18n("Request");
00161 case Refresh:
00162 return i18n("Refresh");
00163 case Cancel:
00164 return i18n("Cancel");
00165 case Add:
00166 return i18n("Add");
00167 case Reply:
00168 return i18n("Reply");
00169 case Counter:
00170 return i18n("counter proposal","Counter");
00171 case Declinecounter:
00172 return i18n("decline counter proposal","Decline Counter");
00173 default:
00174 return i18n("Unknown");
00175 }
00176 }
00177
00178 bool Scheduler::deleteTransaction(IncidenceBase *)
00179 {
00180 return true;
00181 }
00182
00183 bool Scheduler::acceptPublish( IncidenceBase *newIncBase,
00184 ScheduleMessage::Status status, Method method )
00185 {
00186 if( newIncBase->type() == "FreeBusy" ) {
00187 return acceptFreeBusy( newIncBase, method );
00188 }
00189
00190 bool res = false;
00191 kdDebug(5800) << "Scheduler::acceptPublish, status="
00192 << ScheduleMessage::statusName( status ) << endl;
00193 Incidence *newInc = static_cast<Incidence *>( newIncBase );
00194 Incidence *calInc = mCalendar->incidence( newIncBase->uid() );
00195 switch ( status ) {
00196 case ScheduleMessage::Unknown:
00197 case ScheduleMessage::PublishNew:
00198 case ScheduleMessage::PublishUpdate:
00199 if ( calInc && newInc ) {
00200 if ( (newInc->revision() > calInc->revision()) ||
00201 (newInc->revision() == calInc->revision() &&
00202 newInc->lastModified() > calInc->lastModified() ) ) {
00203 AssignmentVisitor visitor;
00204 if ( !visitor.assign( calInc, newInc ) ) {
00205 kdError(5800) << "assigning different incidence types" << endl;
00206 } else {
00207 res = true;
00208 }
00209 }
00210 }
00211 break;
00212 case ScheduleMessage::Obsolete:
00213 res = true;
00214 break;
00215 default:
00216 break;
00217 }
00218 deleteTransaction( newIncBase );
00219 return res;
00220 }
00221
00222 bool Scheduler::acceptRequest( IncidenceBase *incidence,
00223 ScheduleMessage::Status status,
00224 const QString &attendee )
00225 {
00226 Incidence *inc = static_cast<Incidence *>(incidence);
00227 if ( !inc )
00228 return false;
00229 if (inc->type()=="FreeBusy") {
00230
00231 return true;
00232 }
00233
00234 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00235 kdDebug(5800) << "Scheduler::acceptRequest status=" << ScheduleMessage::statusName( status ) << ": found " << existingIncidences.count() << " incidences with schedulingID " << inc->schedulingID() << endl;
00236 Incidence::List::ConstIterator incit = existingIncidences.begin();
00237 for ( ; incit != existingIncidences.end() ; ++incit ) {
00238 Incidence* const i = *incit;
00239 kdDebug(5800) << "Considering this found event ("
00240 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00241 << ") :" << mFormat->toString( i ) << endl;
00242
00243 if ( i->isReadOnly() )
00244 continue;
00245 if ( i->revision() <= inc->revision() ) {
00246
00247 bool isUpdate = true;
00248
00249
00250
00251
00252
00253 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00254
00255
00256
00257 const KCal::Attendee::List attendees = i->attendees();
00258 KCal::Attendee::List::ConstIterator ait;
00259 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00260 if( (*ait)->email() == attendee && (*ait)->status() == Attendee::NeedsAction ) {
00261
00262
00263 kdDebug(5800) << "ignoring " << i->uid() << " since I'm still NeedsAction there" << endl;
00264 isUpdate = false;
00265 break;
00266 }
00267 }
00268 if ( isUpdate ) {
00269 if ( i->revision() == inc->revision() &&
00270 i->lastModified() > inc->lastModified() ) {
00271
00272 kdDebug(5800) << "This isn't an update - the found incidence was modified more recently" << endl;
00273 deleteTransaction(incidence);
00274 return false;
00275 }
00276 kdDebug(5800) << "replacing existing incidence " << i->uid() << endl;
00277 bool res = true;
00278 AssignmentVisitor visitor;
00279 const QString oldUid = i->uid();
00280 if ( !visitor.assign( i, inc ) ) {
00281 kdError(5800) << "assigning different incidence types" << endl;
00282 res = false;
00283 }
00284 i->setUid( oldUid );
00285 i->setSchedulingID( inc->uid() );
00286 deleteTransaction( incidence );
00287 return res;
00288 }
00289 } else {
00290
00291 kdDebug(5800) << "This isn't an update - the found incidence has a bigger revision number" << endl;
00292 deleteTransaction(incidence);
00293 return false;
00294 }
00295 }
00296
00297
00298 inc->setSchedulingID( inc->uid() );
00299 inc->setUid( CalFormat::createUniqueId() );
00300
00301
00302 if ( existingIncidences.count() > 0 || inc->revision() == 0 ||
00303 KMessageBox::warningYesNo( 0,
00304 i18n("The event, task or journal to be updated could not be found. "
00305 "Maybe it has already been deleted, or the calendar that "
00306 "contains it is disabled. Press 'Store' to create a new "
00307 "one or 'Throw away' to discard this update." ),
00308 i18n("Discard this update?"), i18n("Store"), i18n("Throw away") ) == KMessageBox::Yes ) {
00309 kdDebug(5800) << "Storing new incidence with scheduling uid=" << inc->schedulingID() << " and uid=" << inc->uid() << endl;
00310 mCalendar->addIncidence(inc);
00311 }
00312 deleteTransaction(incidence);
00313 return true;
00314 }
00315
00316 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status )
00317 {
00318 deleteTransaction(incidence);
00319 return false;
00320 }
00321
00322 bool Scheduler::acceptCancel( IncidenceBase *incidence,
00323 ScheduleMessage::Status status,
00324 const QString &attendee )
00325 {
00326 Incidence *inc = static_cast<Incidence *>( incidence );
00327 if ( !inc ) {
00328 return false;
00329 }
00330
00331 if ( inc->type() == "FreeBusy" ) {
00332
00333 return true;
00334 }
00335
00336 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00337 kdDebug(5800) << "Scheduler::acceptCancel="
00338 << ScheduleMessage::statusName( status )
00339 << ": found " << existingIncidences.count()
00340 << " incidences with schedulingID " << inc->schedulingID()
00341 << endl;
00342
00343 bool ret = false;
00344 Incidence::List::ConstIterator incit = existingIncidences.begin();
00345 for ( ; incit != existingIncidences.end() ; ++incit ) {
00346 Incidence *i = *incit;
00347 kdDebug(5800) << "Considering this found event ("
00348 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00349 << ") :" << mFormat->toString( i ) << endl;
00350
00351
00352 if ( i->isReadOnly() ) {
00353 continue;
00354 }
00355
00356
00357
00358
00359
00360
00361
00362 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00363
00364
00365
00366
00367 bool isMine = true;
00368 const KCal::Attendee::List attendees = i->attendees();
00369 KCal::Attendee::List::ConstIterator ait;
00370 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00371 if ( (*ait)->email() == attendee &&
00372 (*ait)->status() == Attendee::NeedsAction ) {
00373
00374
00375 kdDebug(5800) << "ignoring " << i->uid()
00376 << " since I'm still NeedsAction there" << endl;
00377 isMine = false;
00378 break;
00379 }
00380 }
00381
00382 if ( isMine ) {
00383 kdDebug(5800) << "removing existing incidence " << i->uid() << endl;
00384 if ( i->type() == "Event" ) {
00385 Event *event = mCalendar->event( i->uid() );
00386 ret = ( event && mCalendar->deleteEvent( event ) );
00387 } else if ( i->type() == "Todo" ) {
00388 Todo *todo = mCalendar->todo( i->uid() );
00389 ret = ( todo && mCalendar->deleteTodo( todo ) );
00390 }
00391 deleteTransaction( incidence );
00392 return ret;
00393 }
00394 }
00395
00396
00397 if ( inc->revision() > 0 ) {
00398 KMessageBox::error(
00399 0,
00400 i18n( "The event or task could not be removed from your calendar. "
00401 "Maybe it has already been deleted or is not owned by you. "
00402 "Or it might belong to a read-only or disabled calendar." ) );
00403 }
00404 deleteTransaction( incidence );
00405 return ret;
00406 }
00407
00408 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status )
00409 {
00410 const IncidenceBase *toDelete = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00411
00412 bool ret = true;
00413 if ( toDelete ) {
00414 if ( toDelete->type() == "Event" ) {
00415 Event *event = mCalendar->event( toDelete->uid() );
00416 ret = ( event && mCalendar->deleteEvent( event ) );
00417 } else if ( toDelete->type() == "Todo" ) {
00418 Todo *todo = mCalendar->todo( toDelete->uid() );
00419 ret = ( todo && mCalendar->deleteTodo( todo ) );
00420 }
00421 } else {
00422
00423
00424 Incidence *inc = static_cast<Incidence *>( incidence );
00425 if ( inc->revision() > 0 ) {
00426 ret = false;
00427 }
00428 }
00429
00430 if ( !ret ) {
00431 KMessageBox::error(
00432 0,
00433 i18n( "The event or task to be canceled could not be removed from your calendar. "
00434 "Maybe it has already been deleted, or the calendar that "
00435 "contains it is disabled." ) );
00436 }
00437 deleteTransaction(incidence);
00438 return ret;
00439 }
00440
00441 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00442 {
00443 deleteTransaction(incidence);
00444 return false;
00445 }
00446
00447
00448
00449
00450
00451
00452
00453 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status , Method method)
00454 {
00455 if(incidence->type()=="FreeBusy") {
00456 return acceptFreeBusy(incidence, method);
00457 }
00458 bool ret = false;
00459 Event *ev = mCalendar->event(incidence->uid());
00460 Todo *to = mCalendar->todo(incidence->uid());
00461
00462
00463 if ( !ev && !to ) {
00464 const Incidence::List list = mCalendar->incidences();
00465 for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00466 if ( (*it)->schedulingID() == incidence->uid() ) {
00467 ev = dynamic_cast<Event*>( *it );
00468 to = dynamic_cast<Todo*>( *it );
00469 break;
00470 }
00471 }
00472 }
00473
00474 if (ev || to) {
00475
00476 kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00477 Attendee::List attendeesIn = incidence->attendees();
00478 Attendee::List attendeesEv;
00479 Attendee::List attendeesNew;
00480 if (ev) attendeesEv = ev->attendees();
00481 if (to) attendeesEv = to->attendees();
00482 Attendee::List::ConstIterator inIt;
00483 Attendee::List::ConstIterator evIt;
00484 for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00485 Attendee *attIn = *inIt;
00486 bool found = false;
00487 for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00488 Attendee *attEv = *evIt;
00489 if (attIn->email().lower()==attEv->email().lower()) {
00490
00491 kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00492 attEv->setStatus(attIn->status());
00493 attEv->setDelegate(attIn->delegate());
00494 attEv->setDelegator(attIn->delegator());
00495 ret = true;
00496 found = true;
00497 }
00498 }
00499 if ( !found && attIn->status() != Attendee::Declined )
00500 attendeesNew.append( attIn );
00501 }
00502
00503 bool attendeeAdded = false;
00504 for ( Attendee::List::ConstIterator it = attendeesNew.constBegin(); it != attendeesNew.constEnd(); ++it ) {
00505 Attendee* attNew = *it;
00506 QString msg = i18n("%1 wants to attend %2 but was not invited.").arg( attNew->fullName() )
00507 .arg( ev ? ev->summary() : to->summary() );
00508 if ( !attNew->delegator().isEmpty() )
00509 msg = i18n("%1 wants to attend %2 on behalf of %3.").arg( attNew->fullName() )
00510 .arg( ev ? ev->summary() : to->summary() )
00511 .arg( attNew->delegator() );
00512 if ( KMessageBox::questionYesNo( 0, msg, i18n("Uninvited attendee"),
00513 KGuiItem(i18n("Accept Attendance")), KGuiItem(i18n("Reject Attendance")) )
00514 != KMessageBox::Yes )
00515 {
00516 KCal::Incidence *cancel = dynamic_cast<Incidence*>( incidence );
00517 if ( cancel )
00518 cancel->addComment( i18n( "The organizer rejected your attendance at this meeting." ) );
00519 performTransaction( cancel ? cancel : incidence, Scheduler::Cancel, attNew->fullName() );
00520 delete cancel;
00521 continue;
00522 }
00523
00524 Attendee *a = new Attendee( attNew->name(), attNew->email(), attNew->RSVP(),
00525 attNew->status(), attNew->role(), attNew->uid() );
00526 a->setDelegate( attNew->delegate() );
00527 a->setDelegator( attNew->delegator() );
00528 if ( ev )
00529 ev->addAttendee( a );
00530 else if ( to )
00531 to->addAttendee( a );
00532 ret = true;
00533 attendeeAdded = true;
00534 }
00535
00536
00537 if ( attendeeAdded ) {
00538 if ( ev ) {
00539 ev->setRevision( ev->revision() + 1 );
00540 performTransaction( ev, Scheduler::Request );
00541 }
00542 if ( to ) {
00543 to->setRevision( ev->revision() + 1 );
00544 performTransaction( to, Scheduler::Request );
00545 }
00546 }
00547
00548 if ( ret ) {
00549
00550
00551 if ( ev )
00552 ev->updated();
00553 else if ( to )
00554 to->updated();
00555 }
00556 if ( to ) {
00557
00558
00559 Todo *update = dynamic_cast<Todo*> ( incidence );
00560 Q_ASSERT( update );
00561 if ( update && ( to->percentComplete() != update->percentComplete() ) ) {
00562 to->setPercentComplete( update->percentComplete() );
00563 to->updated();
00564 }
00565 }
00566 } else
00567 kdError(5800) << "No incidence for scheduling\n";
00568 if (ret) deleteTransaction(incidence);
00569 return ret;
00570 }
00571
00572 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status )
00573 {
00574
00575 deleteTransaction(incidence);
00576 return false;
00577 }
00578
00579 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00580 {
00581 deleteTransaction(incidence);
00582 return false;
00583 }
00584
00585 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00586 {
00587 if ( !d->mFreeBusyCache ) {
00588 kdError() << "KCal::Scheduler: no FreeBusyCache." << endl;
00589 return false;
00590 }
00591
00592 FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00593
00594 kdDebug(5800) << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00595
00596 Person from;
00597 if(method == Scheduler::Publish) {
00598 from = freebusy->organizer();
00599 }
00600 if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00601 Attendee *attendee = freebusy->attendees().first();
00602 from = attendee->email();
00603 }
00604
00605 if ( !d->mFreeBusyCache->saveFreeBusy( freebusy, from ) ) return false;
00606
00607 deleteTransaction(incidence);
00608 return true;
00609 }