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 "calhelper.h"
00029 #include "event.h"
00030 #include "todo.h"
00031 #include "freebusy.h"
00032 #include "icalformat.h"
00033 #include "calendar.h"
00034 #include "calendarresources.h"
00035 #include "freebusycache.h"
00036 #include "assignmentvisitor.h"
00037
00038 #include "scheduler.h"
00039
00040 using namespace KCal;
00041
00042 ScheduleMessage::ScheduleMessage(IncidenceBase *incidence,int method,ScheduleMessage::Status status)
00043 {
00044 mIncidence = incidence;
00045 mMethod = method;
00046 mStatus = status;
00047 }
00048
00049 QString ScheduleMessage::statusName(ScheduleMessage::Status status)
00050 {
00051 switch (status) {
00052 case PublishUpdate:
00053 return i18n("Updated Publish");
00054 case PublishNew:
00055 return i18n("Publish");
00056 case Obsolete:
00057 return i18n("Obsolete");
00058 case RequestNew:
00059 return i18n("New Request");
00060 case RequestUpdate:
00061 return i18n("Updated Request");
00062 default:
00063 return i18n("Unknown Status: %1").arg(QString::number(status));
00064 }
00065 }
00066
00067 struct Scheduler::Private
00068 {
00069 Private() : mFreeBusyCache( 0 ) {}
00070
00071 FreeBusyCache *mFreeBusyCache;
00072 };
00073
00074 Scheduler::Scheduler(Calendar *calendar)
00075 {
00076 mCalendar = calendar;
00077 mFormat = new ICalFormat();
00078 mFormat->setTimeZone( calendar->timeZoneId(), !calendar->isLocalTime() );
00079
00080 d = new Private;
00081 }
00082
00083 Scheduler::~Scheduler()
00084 {
00085 delete d;
00086
00087 delete mFormat;
00088 }
00089
00090 void Scheduler::setFreeBusyCache( FreeBusyCache *c )
00091 {
00092 d->mFreeBusyCache = c;
00093 }
00094
00095 FreeBusyCache *Scheduler::freeBusyCache() const
00096 {
00097 return d->mFreeBusyCache;
00098 }
00099
00100 bool Scheduler::acceptTransaction( IncidenceBase *incidence,
00101 Method method,
00102 ScheduleMessage::Status status,
00103 const QString &attendee )
00104 {
00105 kdDebug(5800) << "Scheduler::acceptTransaction, method="
00106 << methodName( method ) << endl;
00107
00108 switch (method) {
00109 case Publish:
00110 return acceptPublish(incidence, status, method);
00111 case Request:
00112 return acceptRequest( incidence, status, attendee );
00113 case Add:
00114 return acceptAdd(incidence, status);
00115 case Cancel:
00116 return acceptCancel(incidence, status, attendee );
00117 case Declinecounter:
00118 return acceptDeclineCounter(incidence, status);
00119 case Reply:
00120 return acceptReply(incidence, status, method);
00121 case Refresh:
00122 return acceptRefresh(incidence, status);
00123 case Counter:
00124 return acceptCounter(incidence, status);
00125 default:
00126 break;
00127 }
00128 deleteTransaction(incidence);
00129 return false;
00130 }
00131
00132 QString Scheduler::methodName(Method method)
00133 {
00134 switch (method) {
00135 case Publish:
00136 return QString::fromLatin1("Publish");
00137 case Request:
00138 return QString::fromLatin1("Request");
00139 case Refresh:
00140 return QString::fromLatin1("Refresh");
00141 case Cancel:
00142 return QString::fromLatin1("Cancel");
00143 case Add:
00144 return QString::fromLatin1("Add");
00145 case Reply:
00146 return QString::fromLatin1("Reply");
00147 case Counter:
00148 return QString::fromLatin1("Counter");
00149 case Declinecounter:
00150 return QString::fromLatin1("Decline Counter");
00151 default:
00152 return QString::fromLatin1("Unknown");
00153 }
00154 }
00155
00156 QString Scheduler::translatedMethodName(Method method)
00157 {
00158 switch (method) {
00159 case Publish:
00160 return i18n("Publish");
00161 case Request:
00162 return i18n("Request");
00163 case Refresh:
00164 return i18n("Refresh");
00165 case Cancel:
00166 return i18n("Cancel");
00167 case Add:
00168 return i18n("Add");
00169 case Reply:
00170 return i18n("Reply");
00171 case Counter:
00172 return i18n("counter proposal","Counter");
00173 case Declinecounter:
00174 return i18n("decline counter proposal","Decline Counter");
00175 default:
00176 return i18n("Unknown");
00177 }
00178 }
00179
00180 bool Scheduler::deleteTransaction(IncidenceBase *)
00181 {
00182 return true;
00183 }
00184
00185 bool Scheduler::acceptPublish( IncidenceBase *newIncBase,
00186 ScheduleMessage::Status status, Method method )
00187 {
00188 if( newIncBase->type() == "FreeBusy" ) {
00189 return acceptFreeBusy( newIncBase, method );
00190 }
00191
00192 bool res = false;
00193 kdDebug(5800) << "Scheduler::acceptPublish, status="
00194 << ScheduleMessage::statusName( status ) << endl;
00195 Incidence *newInc = static_cast<Incidence *>( newIncBase );
00196 Incidence *calInc = mCalendar->incidence( newIncBase->uid() );
00197 switch ( status ) {
00198 case ScheduleMessage::Unknown:
00199 case ScheduleMessage::PublishNew:
00200 case ScheduleMessage::PublishUpdate:
00201 if ( calInc && newInc ) {
00202 if ( (newInc->revision() > calInc->revision()) ||
00203 (newInc->revision() == calInc->revision() &&
00204 newInc->lastModified() > calInc->lastModified() ) ) {
00205 AssignmentVisitor visitor;
00206 const QString oldUid = calInc->uid();
00207 if ( !visitor.assign( calInc, newInc ) ) {
00208 kdError(5800) << "assigning different incidence types" << endl;
00209 } else {
00210 calInc->setUid( oldUid );
00211 calInc->setSchedulingID( newInc->uid() );
00212 res = true;
00213 }
00214 }
00215 }
00216 break;
00217 case ScheduleMessage::Obsolete:
00218 res = true;
00219 break;
00220 default:
00221 break;
00222 }
00223 deleteTransaction( newIncBase );
00224 return res;
00225 }
00226
00227 bool Scheduler::acceptRequest( IncidenceBase *incidence,
00228 ScheduleMessage::Status status,
00229 const QString &attendee )
00230 {
00231 Incidence *inc = static_cast<Incidence *>(incidence);
00232 if ( !inc )
00233 return false;
00234 if (inc->type()=="FreeBusy") {
00235
00236 return true;
00237 }
00238
00239 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00240 kdDebug(5800) << "Scheduler::acceptRequest status=" << ScheduleMessage::statusName( status ) << ": found " << existingIncidences.count() << " incidences with schedulingID " << inc->schedulingID() << endl;
00241 Incidence::List::ConstIterator incit = existingIncidences.begin();
00242 for ( ; incit != existingIncidences.end() ; ++incit ) {
00243 Incidence* const i = *incit;
00244 kdDebug(5800) << "Considering this found event ("
00245 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00246 << ") :" << mFormat->toString( i ) << endl;
00247
00248 if ( i->isReadOnly() )
00249 continue;
00250 if ( i->revision() <= inc->revision() ) {
00251
00252 bool isUpdate = true;
00253
00254
00255
00256
00257
00258 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00259
00260
00261
00262 const KCal::Attendee::List attendees = i->attendees();
00263 KCal::Attendee::List::ConstIterator ait;
00264 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00265 if( (*ait)->email() == attendee && (*ait)->status() == Attendee::NeedsAction ) {
00266
00267
00268 kdDebug(5800) << "ignoring " << i->uid() << " since I'm still NeedsAction there" << endl;
00269 isUpdate = false;
00270 break;
00271 }
00272 }
00273 if ( isUpdate ) {
00274 if ( i->revision() == inc->revision() &&
00275 i->lastModified() > inc->lastModified() ) {
00276
00277 kdDebug(5800) << "This isn't an update - the found incidence was modified more recently" << endl;
00278 deleteTransaction(incidence);
00279 return false;
00280 }
00281 kdDebug(5800) << "replacing existing incidence " << i->uid() << endl;
00282 bool res = true;
00283 AssignmentVisitor visitor;
00284 const QString oldUid = i->uid();
00285 if ( !visitor.assign( i, inc ) ) {
00286 kdError(5800) << "assigning different incidence types" << endl;
00287 res = false;
00288 } else {
00289 {
00290
00291
00292
00293
00294
00295 i->setSchedulingID( inc->uid() );
00296 i->setUid( oldUid );
00297 }
00298 }
00299 deleteTransaction( incidence );
00300 return res;
00301 }
00302 } else {
00303
00304 kdDebug(5800) << "This isn't an update - the found incidence has a bigger revision number" << endl;
00305 deleteTransaction(incidence);
00306 return false;
00307 }
00308 }
00309
00310
00311 inc->setSchedulingID( inc->uid() );
00312 inc->setUid( CalFormat::createUniqueId() );
00313
00314 if ( existingIncidences.count() == 0 && inc->revision() > 0 ) {
00315 KMessageBox::information(
00316 0,
00317 i18n( "<qt>"
00318 "You accepted an invitation update, but an earlier version of the "
00319 "item could not be found in your calendar.<p>"
00320 "This may have occurred because:<ul>"
00321 "<li>the organizer did not include you in the original invitation</li>"
00322 "<li>you did not accept the original invitation yet</li>"
00323 "<li>you deleted the original invitation from your calendar</li>"
00324 "<li>you no longer have access to the calendar containing the invitation</li>"
00325 "</ul>"
00326 "This is not a problem, but we thought you should know.</qt>" ),
00327 i18n( "Cannot find invitation to be updated" ), "AcceptCantFindIncidence" );
00328 }
00329 kdDebug(5800) << "Storing new incidence with scheduling uid=" << inc->schedulingID()
00330 << " and uid=" << inc->uid() << endl;
00331
00332 CalendarResources *stdcal = dynamic_cast<CalendarResources *>( mCalendar );
00333 if( stdcal && !stdcal->hasCalendarResources() ) {
00334 KMessageBox::sorry(
00335 0,
00336 i18n( "No calendars found, unable to save the invitation." ) );
00337 return false;
00338 }
00339
00340
00341
00342
00343
00344 QWidget *tmpparent = 0;
00345 if ( stdcal ) {
00346 tmpparent = stdcal->dialogParentWidget();
00347 stdcal->setDialogParentWidget( 0 );
00348 }
00349
00350 TryAgain:
00351 bool success = false;
00352 if ( stdcal ) {
00353 success = stdcal->addIncidence( inc );
00354 } else {
00355 success = mCalendar->addIncidence( inc );
00356 }
00357
00358 if ( !success ) {
00359 ErrorFormat *e = stdcal ? stdcal->exception() : 0;
00360
00361 if ( e && e->errorCode() == KCal::ErrorFormat::UserCancel &&
00362 KMessageBox::warningYesNo(
00363 0,
00364 i18n( "You canceled the save operation. Therefore, the appointment will not be "
00365 "stored in your calendar even though you accepted the invitation. "
00366 "Are you certain you want to discard this invitation? " ),
00367 i18n( "Discard this invitation?" ),
00368 i18n( "Discard" ), i18n( "Go Back to Folder Selection" ) ) == KMessageBox::Yes ) {
00369 KMessageBox::information(
00370 0,
00371 i18n( "The invitation \"%1\" was not saved to your calendar "
00372 "but you are still listed as an attendee for that appointment.\n"
00373 "If you mistakenly accepted the invitation or do not plan to attend, please notify "
00374 "the organizer %2 and ask them to remove you from the attendee list.").
00375 arg( inc->summary(), inc->organizer().fullName() ) );
00376 deleteTransaction( incidence );
00377 return true;
00378 } else {
00379 goto TryAgain;
00380 }
00381
00382
00383
00384 if ( !e ||
00385 ( e && ( e->errorCode() != KCal::ErrorFormat::UserCancel &&
00386 e->errorCode() != KCal::ErrorFormat::NoWritableFound ) ) ) {
00387 QString errMessage = i18n( "Unable to save %1 \"%2\"." ).
00388 arg( i18n( inc->type() ) ).
00389 arg( inc->summary() );
00390 KMessageBox::sorry( 0, errMessage );
00391 }
00392 return false;
00393 }
00394
00395 deleteTransaction( incidence );
00396 return true;
00397 }
00398
00399 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status )
00400 {
00401 deleteTransaction(incidence);
00402 return false;
00403 }
00404
00405 bool Scheduler::acceptCancel( IncidenceBase *incidence,
00406 ScheduleMessage::Status status,
00407 const QString &attendee )
00408 {
00409 Incidence *inc = static_cast<Incidence *>( incidence );
00410 if ( !inc ) {
00411 return false;
00412 }
00413
00414 if ( inc->type() == "FreeBusy" ) {
00415
00416 return true;
00417 }
00418
00419 const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00420 kdDebug(5800) << "Scheduler::acceptCancel="
00421 << ScheduleMessage::statusName( status )
00422 << ": found " << existingIncidences.count()
00423 << " incidences with schedulingID " << inc->schedulingID()
00424 << endl;
00425
00426
00427
00428 Incidence::List myExistingIncidences;
00429 Incidence::List::ConstIterator incit = existingIncidences.begin();
00430 for ( ; incit != existingIncidences.end() ; ++incit ) {
00431 Incidence *i = *incit;
00432 if ( CalHelper::isMyCalendarIncidence( mCalendar, i ) ) {
00433 myExistingIncidences.append( i );
00434 }
00435 }
00436
00437 bool ret = false;
00438 incit = myExistingIncidences.begin();
00439 for ( ; incit != myExistingIncidences.end() ; ++incit ) {
00440 Incidence *i = *incit;
00441 kdDebug(5800) << "Considering this found event ("
00442 << ( i->isReadOnly() ? "readonly" : "readwrite" )
00443 << ") :" << mFormat->toString( i ) << endl;
00444
00445
00446 if ( i->isReadOnly() ) {
00447 continue;
00448 }
00449
00450
00451
00452
00453
00454
00455
00456 kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00457
00458
00459
00460
00461 bool isMine = true;
00462 const KCal::Attendee::List attendees = i->attendees();
00463 KCal::Attendee::List::ConstIterator ait;
00464 for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00465 if ( (*ait)->email() == attendee &&
00466 (*ait)->status() == Attendee::NeedsAction ) {
00467
00468
00469 kdDebug(5800) << "ignoring " << i->uid()
00470 << " since I'm still NeedsAction there" << endl;
00471 isMine = false;
00472 break;
00473 }
00474 }
00475
00476 if ( isMine ) {
00477 kdDebug(5800) << "removing existing incidence " << i->uid() << endl;
00478 if ( i->type() == "Event" ) {
00479 Event *event = mCalendar->event( i->uid() );
00480 ret = ( event && mCalendar->deleteEvent( event ) );
00481 } else if ( i->type() == "Todo" ) {
00482 Todo *todo = mCalendar->todo( i->uid() );
00483 ret = ( todo && mCalendar->deleteTodo( todo ) );
00484 }
00485 deleteTransaction( incidence );
00486 return ret;
00487 }
00488 }
00489
00490
00491 if ( myExistingIncidences.count() > 0 && inc->revision() > 0 ) {
00492 KMessageBox::information(
00493 0,
00494 i18n( "The event or task could not be removed from your calendar. "
00495 "Maybe it has already been deleted or is not owned by you. "
00496 "Or it might belong to a read-only or disabled calendar." ) );
00497 }
00498 deleteTransaction( incidence );
00499 return ret;
00500 }
00501
00502 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status )
00503 {
00504 const IncidenceBase *toDelete = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00505
00506 bool ret = true;
00507 if ( toDelete ) {
00508 if ( toDelete->type() == "Event" ) {
00509 Event *event = mCalendar->event( toDelete->uid() );
00510 ret = ( event && mCalendar->deleteEvent( event ) );
00511 } else if ( toDelete->type() == "Todo" ) {
00512 Todo *todo = mCalendar->todo( toDelete->uid() );
00513 ret = ( todo && mCalendar->deleteTodo( todo ) );
00514 }
00515 } else {
00516
00517
00518 Incidence *inc = static_cast<Incidence *>( incidence );
00519 if ( inc->revision() > 0 ) {
00520 ret = false;
00521 }
00522 }
00523
00524 if ( !ret ) {
00525 KMessageBox::information(
00526 0,
00527 i18n( "The event or task to be canceled could not be removed from your calendar. "
00528 "Maybe it has already been deleted or is not owned by you. "
00529 "Or it might belong to a read-only or disabled calendar." ) );
00530 }
00531 deleteTransaction(incidence);
00532 return ret;
00533 }
00534
00535 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00536 {
00537 deleteTransaction(incidence);
00538 return false;
00539 }
00540
00541
00542
00543
00544
00545
00546
00547 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status , Method method)
00548 {
00549 if(incidence->type()=="FreeBusy") {
00550 return acceptFreeBusy(incidence, method);
00551 }
00552 bool ret = false;
00553 Event *ev = mCalendar->event(incidence->uid());
00554 Todo *to = mCalendar->todo(incidence->uid());
00555
00556
00557 if ( !ev && !to ) {
00558 const Incidence::List list = mCalendar->incidences();
00559 for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00560 if ( (*it)->schedulingID() == incidence->uid() ) {
00561 ev = dynamic_cast<Event*>( *it );
00562 to = dynamic_cast<Todo*>( *it );
00563 break;
00564 }
00565 }
00566 }
00567
00568 if (ev || to) {
00569
00570 kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00571 Attendee::List attendeesIn = incidence->attendees();
00572 Attendee::List attendeesEv;
00573 Attendee::List attendeesNew;
00574 if (ev) attendeesEv = ev->attendees();
00575 if (to) attendeesEv = to->attendees();
00576 Attendee::List::ConstIterator inIt;
00577 Attendee::List::ConstIterator evIt;
00578 for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00579 Attendee *attIn = *inIt;
00580 bool found = false;
00581 for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00582 Attendee *attEv = *evIt;
00583 if (attIn->email().lower()==attEv->email().lower()) {
00584
00585 kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00586 attEv->setStatus(attIn->status());
00587 attEv->setDelegate(attIn->delegate());
00588 attEv->setDelegator(attIn->delegator());
00589 ret = true;
00590 found = true;
00591 }
00592 }
00593 if ( !found && attIn->status() != Attendee::Declined )
00594 attendeesNew.append( attIn );
00595 }
00596
00597 bool attendeeAdded = false;
00598 for ( Attendee::List::ConstIterator it = attendeesNew.constBegin(); it != attendeesNew.constEnd(); ++it ) {
00599 Attendee* attNew = *it;
00600 QString msg = i18n("%1 wants to attend %2 but was not invited.").arg( attNew->fullName() )
00601 .arg( ev ? ev->summary() : to->summary() );
00602 if ( !attNew->delegator().isEmpty() )
00603 msg = i18n("%1 wants to attend %2 on behalf of %3.").arg( attNew->fullName() )
00604 .arg( ev ? ev->summary() : to->summary() )
00605 .arg( attNew->delegator() );
00606 if ( KMessageBox::questionYesNo( 0, msg, i18n("Uninvited attendee"),
00607 KGuiItem(i18n("Accept Attendance")), KGuiItem(i18n("Reject Attendance")) )
00608 != KMessageBox::Yes )
00609 {
00610 KCal::Incidence *cancel = dynamic_cast<Incidence*>( incidence );
00611 if ( cancel )
00612 cancel->addComment( i18n( "The organizer rejected your attendance at this meeting." ) );
00613 performTransaction( cancel ? cancel : incidence, Scheduler::Cancel, attNew->fullName() );
00614 delete cancel;
00615 continue;
00616 }
00617
00618 Attendee *a = new Attendee( attNew->name(), attNew->email(), attNew->RSVP(),
00619 attNew->status(), attNew->role(), attNew->uid() );
00620 a->setDelegate( attNew->delegate() );
00621 a->setDelegator( attNew->delegator() );
00622 if ( ev )
00623 ev->addAttendee( a );
00624 else if ( to )
00625 to->addAttendee( a );
00626 ret = true;
00627 attendeeAdded = true;
00628 }
00629
00630
00631 if ( attendeeAdded ) {
00632 bool sendMail = false;
00633 if ( ev || to ) {
00634 if ( KMessageBox::questionYesNo( 0, i18n( "An attendee was added to the incidence. "
00635 "Do you want to email the attendees an update message?" ),
00636 i18n( "Attendee Added" ), i18n( "Send Messages" ),
00637 i18n( "Do Not Send" ) ) == KMessageBox::Yes ) {
00638 sendMail = true;
00639 }
00640 }
00641
00642 if ( ev ) {
00643 ev->setRevision( ev->revision() + 1 );
00644 if ( sendMail )
00645 performTransaction( ev, Scheduler::Request );
00646 }
00647 if ( to ) {
00648 to->setRevision( to->revision() + 1 );
00649 if ( sendMail )
00650 performTransaction( to, Scheduler::Request );
00651 }
00652 }
00653
00654 if ( ret ) {
00655
00656
00657 if ( ev )
00658 ev->updated();
00659 else if ( to )
00660 to->updated();
00661 }
00662 if ( to ) {
00663
00664
00665 Todo *update = dynamic_cast<Todo*> ( incidence );
00666 Q_ASSERT( update );
00667 if ( update && ( to->percentComplete() != update->percentComplete() ) ) {
00668 to->setPercentComplete( update->percentComplete() );
00669 to->updated();
00670 }
00671 }
00672 } else
00673 kdError(5800) << "No incidence for scheduling\n";
00674 if (ret) deleteTransaction(incidence);
00675 return ret;
00676 }
00677
00678 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status )
00679 {
00680
00681 deleteTransaction(incidence);
00682 return false;
00683 }
00684
00685 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00686 {
00687 deleteTransaction(incidence);
00688 return false;
00689 }
00690
00691 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00692 {
00693 if ( !d->mFreeBusyCache ) {
00694 kdError() << "KCal::Scheduler: no FreeBusyCache." << endl;
00695 return false;
00696 }
00697
00698 FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00699
00700 kdDebug(5800) << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00701
00702 Person from;
00703 if(method == Scheduler::Publish) {
00704 from = freebusy->organizer();
00705 }
00706 if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00707 Attendee *attendee = freebusy->attendees().first();
00708 from = attendee->email();
00709 }
00710
00711 if ( !d->mFreeBusyCache->saveFreeBusy( freebusy, from ) ) return false;
00712
00713 deleteTransaction(incidence);
00714 return true;
00715 }