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);
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,ScheduleMessage::Status )
00327 {
00328 const IncidenceBase *toDelete = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00329
00330 bool ret = true;
00331 if ( toDelete ) {
00332 if ( toDelete->type() == "Event" ) {
00333 Event *event = mCalendar->event( toDelete->uid() );
00334 ret = ( event && mCalendar->deleteEvent( event ) );
00335 } else if ( toDelete->type() == "Todo" ) {
00336 Todo *todo = mCalendar->todo( toDelete->uid() );
00337 ret = ( todo && mCalendar->deleteTodo( todo ) );
00338 }
00339 } else {
00340
00341
00342 Incidence *inc = static_cast<Incidence *>( incidence );
00343 if ( inc->revision() > 0 ) {
00344 ret = false;
00345 }
00346 }
00347
00348 if ( !ret ) {
00349 KMessageBox::error(
00350 0,
00351 i18n( "The event or task to be canceled could not be removed from your calendar. "
00352 "Maybe it has already been deleted, or the calendar that "
00353 "contains it is disabled." ) );
00354 }
00355 deleteTransaction(incidence);
00356 return ret;
00357 }
00358
00359 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00360 {
00361 deleteTransaction(incidence);
00362 return false;
00363 }
00364
00365
00366
00367
00368
00369
00370
00371 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status , Method method)
00372 {
00373 if(incidence->type()=="FreeBusy") {
00374 return acceptFreeBusy(incidence, method);
00375 }
00376 bool ret = false;
00377 Event *ev = mCalendar->event(incidence->uid());
00378 Todo *to = mCalendar->todo(incidence->uid());
00379
00380
00381 if ( !ev && !to ) {
00382 const Incidence::List list = mCalendar->incidences();
00383 for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00384 if ( (*it)->schedulingID() == incidence->uid() ) {
00385 ev = dynamic_cast<Event*>( *it );
00386 to = dynamic_cast<Todo*>( *it );
00387 break;
00388 }
00389 }
00390 }
00391
00392 if (ev || to) {
00393
00394 kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00395 Attendee::List attendeesIn = incidence->attendees();
00396 Attendee::List attendeesEv;
00397 Attendee::List attendeesNew;
00398 if (ev) attendeesEv = ev->attendees();
00399 if (to) attendeesEv = to->attendees();
00400 Attendee::List::ConstIterator inIt;
00401 Attendee::List::ConstIterator evIt;
00402 for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00403 Attendee *attIn = *inIt;
00404 bool found = false;
00405 for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00406 Attendee *attEv = *evIt;
00407 if (attIn->email().lower()==attEv->email().lower()) {
00408
00409 kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00410 attEv->setStatus(attIn->status());
00411 attEv->setDelegate(attIn->delegate());
00412 attEv->setDelegator(attIn->delegator());
00413 ret = true;
00414 found = true;
00415 }
00416 }
00417 if ( !found && attIn->status() != Attendee::Declined )
00418 attendeesNew.append( attIn );
00419 }
00420
00421 bool attendeeAdded = false;
00422 for ( Attendee::List::ConstIterator it = attendeesNew.constBegin(); it != attendeesNew.constEnd(); ++it ) {
00423 Attendee* attNew = *it;
00424 QString msg = i18n("%1 wants to attend %2 but was not invited.").arg( attNew->fullName() )
00425 .arg( ev ? ev->summary() : to->summary() );
00426 if ( !attNew->delegator().isEmpty() )
00427 msg = i18n("%1 wants to attend %2 on behalf of %3.").arg( attNew->fullName() )
00428 .arg( ev ? ev->summary() : to->summary() )
00429 .arg( attNew->delegator() );
00430 if ( KMessageBox::questionYesNo( 0, msg, i18n("Uninvited attendee"),
00431 KGuiItem(i18n("Accept Attendance")), KGuiItem(i18n("Reject Attendance")) )
00432 != KMessageBox::Yes )
00433 {
00434 KCal::Incidence *cancel = dynamic_cast<Incidence*>( incidence );
00435 if ( cancel )
00436 cancel->addComment( i18n( "The organizer rejected your attendance at this meeting." ) );
00437 performTransaction( cancel ? cancel : incidence, Scheduler::Cancel, attNew->fullName() );
00438 delete cancel;
00439 continue;
00440 }
00441
00442 Attendee *a = new Attendee( attNew->name(), attNew->email(), attNew->RSVP(),
00443 attNew->status(), attNew->role(), attNew->uid() );
00444 a->setDelegate( attNew->delegate() );
00445 a->setDelegator( attNew->delegator() );
00446 if ( ev )
00447 ev->addAttendee( a );
00448 else if ( to )
00449 to->addAttendee( a );
00450 ret = true;
00451 attendeeAdded = true;
00452 }
00453
00454
00455 if ( attendeeAdded ) {
00456 if ( ev ) {
00457 ev->setRevision( ev->revision() + 1 );
00458 performTransaction( ev, Scheduler::Request );
00459 }
00460 if ( to ) {
00461 to->setRevision( ev->revision() + 1 );
00462 performTransaction( to, Scheduler::Request );
00463 }
00464 }
00465
00466 if ( ret ) {
00467
00468
00469 if ( ev )
00470 ev->updated();
00471 else if ( to )
00472 to->updated();
00473 }
00474 if ( to ) {
00475
00476
00477 Todo *update = dynamic_cast<Todo*> ( incidence );
00478 Q_ASSERT( update );
00479 if ( update && ( to->percentComplete() != update->percentComplete() ) ) {
00480 to->setPercentComplete( update->percentComplete() );
00481 to->updated();
00482 }
00483 }
00484 } else
00485 kdError(5800) << "No incidence for scheduling\n";
00486 if (ret) deleteTransaction(incidence);
00487 return ret;
00488 }
00489
00490 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status )
00491 {
00492
00493 deleteTransaction(incidence);
00494 return false;
00495 }
00496
00497 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status )
00498 {
00499 deleteTransaction(incidence);
00500 return false;
00501 }
00502
00503 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00504 {
00505 if ( !d->mFreeBusyCache ) {
00506 kdError() << "KCal::Scheduler: no FreeBusyCache." << endl;
00507 return false;
00508 }
00509
00510 FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00511
00512 kdDebug(5800) << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00513
00514 Person from;
00515 if(method == Scheduler::Publish) {
00516 from = freebusy->organizer();
00517 }
00518 if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00519 Attendee *attendee = freebusy->attendees().first();
00520 from = attendee->email();
00521 }
00522
00523 if ( !d->mFreeBusyCache->saveFreeBusy( freebusy, from ) ) return false;
00524
00525 deleteTransaction(incidence);
00526 return true;
00527 }