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 const QString oldUid = calInc->uid();
00205 if ( !visitor.assign( calInc, newInc ) ) {
00206 kdError(5800) << "assigning different incidence types" << endl;
00207 } else {
00208 calInc->setUid( oldUid );
00209 calInc->setSchedulingID( newInc->uid() );
00210 res = true;
00211 }
00212 }
00213 }
00214 break;
00215 case ScheduleMessage::Obsolete:
00216 res = true;
00217 break;
00218 default:
00219 break;
00220 }
00221 deleteTransaction( newIncBase );
00222 return res;
00223 }
00224
00225 bool Scheduler::acceptRequest( IncidenceBase *incidence,
00226 ScheduleMessage::Status status,
00227 const QString &attendee )
00228 {
00229 Incidence *inc = static_cast<Incidence *>(incidence);
00230 if ( !inc )
00231 return false;
00232 if (inc->type()=="FreeBusy") {
00233
00234 return true;
00235 }
00236
00237 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00238 kdDebug(5800) << "Scheduler::acceptRequest status=" << ScheduleMessage::statusName( status ) << ": found " << existingIncidences.count() << " incidences with schedulingID " << inc->schedulingID() << endl;
00239 Incidence::List::ConstIterator incit = existingIncidences.begin();
00240 for ( ; incit != existingIncidences.end() ; ++incit ) {
00241 Incidence* const i = *incit;
00242 kdDebug(5800) << "Considering this found event ("
00243 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00244 << ") :" << mFormat->toString( i ) << endl;
00245
00246 if ( i->isReadOnly() )
00247 continue;
00248 if ( i->revision() <= inc->revision() ) {
00249
00250 bool isUpdate = true;
00251
00252
00253
00254
00255
00256 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00257
00258
00259
00260 const KCal::Attendee::List attendees = i->attendees();
00261 KCal::Attendee::List::ConstIterator ait;
00262 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00263 if( (*ait)->email() == attendee && (*ait)->status() == Attendee::NeedsAction ) {
00264
00265
00266 kdDebug(5800) << "ignoring " << i->uid() << " since I'm still NeedsAction there" << endl;
00267 isUpdate = false;
00268 break;
00269 }
00270 }
00271 if ( isUpdate ) {
00272 if ( i->revision() == inc->revision() &&
00273 i->lastModified() > inc->lastModified() ) {
00274
00275 kdDebug(5800) << "This isn't an update - the found incidence was modified more recently" << endl;
00276 deleteTransaction(incidence);
00277 return false;
00278 }
00279 kdDebug(5800) << "replacing existing incidence " << i->uid() << endl;
00280 bool res = true;
00281 AssignmentVisitor visitor;
00282 const QString oldUid = i->uid();
00283 if ( !visitor.assign( i, inc ) ) {
00284 kdError(5800) << "assigning different incidence types" << endl;
00285 res = false;
00286 } else {
00287 i->setUid( oldUid );
00288 i->setSchedulingID( inc->uid() );
00289 }
00290 deleteTransaction( incidence );
00291 return res;
00292 }
00293 } else {
00294
00295 kdDebug(5800) << "This isn't an update - the found incidence has a bigger revision number" << endl;
00296 deleteTransaction(incidence);
00297 return false;
00298 }
00299 }
00300
00301
00302 inc->setSchedulingID( inc->uid() );
00303 inc->setUid( CalFormat::createUniqueId() );
00304
00305
00306 if ( existingIncidences.count() > 0 || inc->revision() == 0 ||
00307 KMessageBox::warningYesNo( 0,
00308 i18n("The event, task or journal to be updated could not be found. "
00309 "Maybe it has already been deleted, or the calendar that "
00310 "contains it is disabled. Press 'Store' to create a new "
00311 "one or 'Throw away' to discard this update." ),
00312 i18n("Discard this update?"), i18n("Store"), i18n("Throw away") ) == KMessageBox::Yes ) {
00313 kdDebug(5800) << "Storing new incidence with scheduling uid=" << inc->schedulingID() << " and uid=" << inc->uid() << endl;
00314 mCalendar->addIncidence(inc);
00315 }
00316 deleteTransaction(incidence);
00317 return true;
00318 }
00319
00320 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status )
00321 {
00322 deleteTransaction(incidence);
00323 return false;
00324 }
00325
00326 bool Scheduler::acceptCancel( IncidenceBase *incidence,
00327 ScheduleMessage::Status status,
00328 const QString &attendee )
00329 {
00330 Incidence *inc = static_cast<Incidence *>( incidence );
00331 if ( !inc ) {
00332 return false;
00333 }
00334
00335 if ( inc->type() == "FreeBusy" ) {
00336
00337 return true;
00338 }
00339
00340 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00341 kdDebug(5800) << "Scheduler::acceptCancel="
00342 << ScheduleMessage::statusName( status )
00343 << ": found " << existingIncidences.count()
00344 << " incidences with schedulingID " << inc->schedulingID()
00345 << endl;
00346
00347 bool ret = false;
00348 Incidence::List::ConstIterator incit = existingIncidences.begin();
00349 for ( ; incit != existingIncidences.end() ; ++incit ) {
00350 Incidence *i = *incit;
00351 kdDebug(5800) << "Considering this found event ("
00352 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00353 << ") :" << mFormat->toString( i ) << endl;
00354
00355
00356 if ( i->isReadOnly() ) {
00357 continue;
00358 }
00359
00360
00361
00362
00363
00364
00365
00366 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00367
00368
00369
00370
00371 bool isMine = true;
00372 const KCal::Attendee::List attendees = i->attendees();
00373 KCal::Attendee::List::ConstIterator ait;
00374 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00375 if ( (*ait)->email() == attendee &&
00376 (*ait)->status() == Attendee::NeedsAction ) {
00377
00378
00379 kdDebug(5800) << "ignoring " << i->uid()
00380 << " since I'm still NeedsAction there" << endl;
00381 isMine = false;
00382 break;
00383 }
00384 }
00385
00386 if ( isMine ) {
00387 kdDebug(5800) << "removing existing incidence " << i->uid() << endl;
00388 if ( i->type() == "Event" ) {
00389 Event *event = mCalendar->event( i->uid() );
00390 ret = ( event && mCalendar->deleteEvent( event ) );
00391 } else if ( i->type() == "Todo" ) {
00392 Todo *todo = mCalendar->todo( i->uid() );
00393 ret = ( todo && mCalendar->deleteTodo( todo ) );
00394 }
00395 deleteTransaction( incidence );
00396 return ret;
00397 }
00398 }
00399
00400
00401 if ( existingIncidences.count() > 0 && inc->revision() > 0 ) {
00402 KMessageBox::information(
00403 0,
00404 i18n( "The event or task could not be removed from your calendar. "
00405 "Maybe it has already been deleted or is not owned by you. "
00406 "Or it might belong to a read-only or disabled calendar." ) );
00407 }
00408 deleteTransaction( incidence );
00409 return ret;
00410 }
00411
00412 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status )
00413 {
00414 const IncidenceBase *toDelete = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00415
00416 bool ret = true;
00417 if ( toDelete ) {
00418 if ( toDelete->type() == "Event" ) {
00419 Event *event = mCalendar->event( toDelete->uid() );
00420 ret = ( event && mCalendar->deleteEvent( event ) );
00421 } else if ( toDelete->type() == "Todo" ) {
00422 Todo *todo = mCalendar->todo( toDelete->uid() );
00423 ret = ( todo && mCalendar->deleteTodo( todo ) );
00424 }
00425 } else {
00426
00427
00428 Incidence *inc = static_cast<Incidence *>( incidence );
00429 if ( inc->revision() > 0 ) {
00430 ret = false;
00431 }
00432 }
00433
00434 if ( !ret ) {
00435 KMessageBox::information(
00436 0,
00437 i18n( "The event or task to be canceled could not be removed from your calendar. "
00438 "Maybe it has already been deleted or is not owned by you. "
00439 "Or it might belong to a read-only or disabled calendar." ) );
00440 }
00441 deleteTransaction(incidence);
00442 return ret;
00443 }
00444
00445 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00446 {
00447 deleteTransaction(incidence);
00448 return false;
00449 }
00450
00451
00452
00453
00454
00455
00456
00457 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status , Method method)
00458 {
00459 if(incidence->type()=="FreeBusy") {
00460 return acceptFreeBusy(incidence, method);
00461 }
00462 bool ret = false;
00463 Event *ev = mCalendar->event(incidence->uid());
00464 Todo *to = mCalendar->todo(incidence->uid());
00465
00466
00467 if ( !ev && !to ) {
00468 const Incidence::List list = mCalendar->incidences();
00469 for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00470 if ( (*it)->schedulingID() == incidence->uid() ) {
00471 ev = dynamic_cast<Event*>( *it );
00472 to = dynamic_cast<Todo*>( *it );
00473 break;
00474 }
00475 }
00476 }
00477
00478 if (ev || to) {
00479
00480 kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00481 Attendee::List attendeesIn = incidence->attendees();
00482 Attendee::List attendeesEv;
00483 Attendee::List attendeesNew;
00484 if (ev) attendeesEv = ev->attendees();
00485 if (to) attendeesEv = to->attendees();
00486 Attendee::List::ConstIterator inIt;
00487 Attendee::List::ConstIterator evIt;
00488 for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00489 Attendee *attIn = *inIt;
00490 bool found = false;
00491 for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00492 Attendee *attEv = *evIt;
00493 if (attIn->email().lower()==attEv->email().lower()) {
00494
00495 kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00496 attEv->setStatus(attIn->status());
00497 attEv->setDelegate(attIn->delegate());
00498 attEv->setDelegator(attIn->delegator());
00499 ret = true;
00500 found = true;
00501 }
00502 }
00503 if ( !found && attIn->status() != Attendee::Declined )
00504 attendeesNew.append( attIn );
00505 }
00506
00507 bool attendeeAdded = false;
00508 for ( Attendee::List::ConstIterator it = attendeesNew.constBegin(); it != attendeesNew.constEnd(); ++it ) {
00509 Attendee* attNew = *it;
00510 QString msg = i18n("%1 wants to attend %2 but was not invited.").arg( attNew->fullName() )
00511 .arg( ev ? ev->summary() : to->summary() );
00512 if ( !attNew->delegator().isEmpty() )
00513 msg = i18n("%1 wants to attend %2 on behalf of %3.").arg( attNew->fullName() )
00514 .arg( ev ? ev->summary() : to->summary() )
00515 .arg( attNew->delegator() );
00516 if ( KMessageBox::questionYesNo( 0, msg, i18n("Uninvited attendee"),
00517 KGuiItem(i18n("Accept Attendance")), KGuiItem(i18n("Reject Attendance")) )
00518 != KMessageBox::Yes )
00519 {
00520 KCal::Incidence *cancel = dynamic_cast<Incidence*>( incidence );
00521 if ( cancel )
00522 cancel->addComment( i18n( "The organizer rejected your attendance at this meeting." ) );
00523 performTransaction( cancel ? cancel : incidence, Scheduler::Cancel, attNew->fullName() );
00524 delete cancel;
00525 continue;
00526 }
00527
00528 Attendee *a = new Attendee( attNew->name(), attNew->email(), attNew->RSVP(),
00529 attNew->status(), attNew->role(), attNew->uid() );
00530 a->setDelegate( attNew->delegate() );
00531 a->setDelegator( attNew->delegator() );
00532 if ( ev )
00533 ev->addAttendee( a );
00534 else if ( to )
00535 to->addAttendee( a );
00536 ret = true;
00537 attendeeAdded = true;
00538 }
00539
00540
00541 if ( attendeeAdded ) {
00542 if ( ev ) {
00543 ev->setRevision( ev->revision() + 1 );
00544 performTransaction( ev, Scheduler::Request );
00545 }
00546 if ( to ) {
00547 to->setRevision( ev->revision() + 1 );
00548 performTransaction( to, Scheduler::Request );
00549 }
00550 }
00551
00552 if ( ret ) {
00553
00554
00555 if ( ev )
00556 ev->updated();
00557 else if ( to )
00558 to->updated();
00559 }
00560 if ( to ) {
00561
00562
00563 Todo *update = dynamic_cast<Todo*> ( incidence );
00564 Q_ASSERT( update );
00565 if ( update && ( to->percentComplete() != update->percentComplete() ) ) {
00566 to->setPercentComplete( update->percentComplete() );
00567 to->updated();
00568 }
00569 }
00570 } else
00571 kdError(5800) << "No incidence for scheduling\n";
00572 if (ret) deleteTransaction(incidence);
00573 return ret;
00574 }
00575
00576 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status )
00577 {
00578
00579 deleteTransaction(incidence);
00580 return false;
00581 }
00582
00583 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00584 {
00585 deleteTransaction(incidence);
00586 return false;
00587 }
00588
00589 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00590 {
00591 if ( !d->mFreeBusyCache ) {
00592 kdError() << "KCal::Scheduler: no FreeBusyCache." << endl;
00593 return false;
00594 }
00595
00596 FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00597
00598 kdDebug(5800) << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00599
00600 Person from;
00601 if(method == Scheduler::Publish) {
00602 from = freebusy->organizer();
00603 }
00604 if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00605 Attendee *attendee = freebusy->attendees().first();
00606 from = attendee->email();
00607 }
00608
00609 if ( !d->mFreeBusyCache->saveFreeBusy( freebusy, from ) ) return false;
00610
00611 deleteTransaction(incidence);
00612 return true;
00613 }