libkcal

scheduler.cpp

00001 /*
00002     This file is part of libkcal.
00003 
00004     Copyright (c) 2001,2004 Cornelius Schumacher <schumacher@kde.org>
00005     Copyright (C) 2004 Reinhold Kainhofer <reinhold@kainhofer.com>
00006 
00007     This library is free software; you can redistribute it and/or
00008     modify it under the terms of the GNU Library General Public
00009     License as published by the Free Software Foundation; either
00010     version 2 of the License, or (at your option) any later version.
00011 
00012     This library is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     Library General Public License for more details.
00016 
00017     You should have received a copy of the GNU Library General Public License
00018     along with this library; see the file COPYING.LIB.  If not, write to
00019     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00020     Boston, MA 02110-1301, USA.
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     // reply to this request is handled in korganizer's incomingdialog
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     // If it's readonly, we can't possible update it.
00246     if ( i->isReadOnly() )
00247       continue;
00248     if ( i->revision() <= inc->revision() ) {
00249       // The new incidence might be an update for the found one
00250       bool isUpdate = true;
00251       // Code for new invitations:
00252       // If you think we could check the value of "status" to be RequestNew: we can't.
00253       // It comes from a similar check inside libical, where the event is compared to
00254       // other events in the calendar. But if we have another version of the event around
00255       // (e.g. shared folder for a group), the status could be RequestNew, Obsolete or Updated.
00256       kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00257       // This is supposed to be a new request, not an update - however we want to update
00258       // the existing one to handle the "clicking more than once on the invitation" case.
00259       // So check the attendee status of the attendee.
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           // This incidence wasn't created by me - it's probably in a shared folder
00265           // and meant for someone else, ignore it.
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           // This isn't an update - the found incidence was modified more recently
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       // This isn't an update - the found incidence has a bigger revision number
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   // Move the uid to be the schedulingID and make a unique UID
00302   inc->setSchedulingID( inc->uid() );
00303   inc->setUid( CalFormat::createUniqueId() );
00304   // in case this is an update and we didn't find the to-be-updated incidence,
00305   // ask whether we should create a new one, or drop the update
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 /* 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     // reply to this request is handled in korganizer's incomingdialog
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     // If it's readonly, we can't possible remove it.
00356     if ( i->isReadOnly() ) {
00357       continue;
00358     }
00359 
00360     // Code for new invitations:
00361     // We cannot check the value of "status" to be RequestNew because
00362     // "status" comes from a similar check inside libical, where the event
00363     // is compared to other events in the calendar. But if we have another
00364     // version of the event around (e.g. shared folder for a group), the
00365     // status could be RequestNew, Obsolete or Updated.
00366     kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00367 
00368     // This is supposed to be a new request, not an update - however we want
00369     // to update the existing one to handle the "clicking more than once
00370     // on the invitation" case. So check the attendee status of the attendee.
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         // This incidence wasn't created by me - it's probably in a shared
00378         // folder and meant for someone else, ignore it.
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   // in case we didn't find the to-be-removed incidence
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 /* 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     // only complain if we failed to determine the toDelete incidence
00427     // on non-initial request.
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 /* status */)
00446 {
00447   deleteTransaction(incidence);
00448   return false;
00449 }
00450 
00451 //bool Scheduler::acceptFreeBusy(Incidence *incidence,ScheduleMessage::Status status)
00452 //{
00453 //  deleteTransaction(incidence);
00454 //  return false;
00455 //}
00456 
00457 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status /* 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   // try harder to find the correct incidence
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     //get matching attendee in calendar
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           //update attendee-info
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     // send update about new participants
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       // We set at least one of the attendees, so the incidence changed
00554       // Note: This should not result in a sequence number bump
00555       if ( ev )
00556         ev->updated();
00557       else if ( to )
00558         to->updated();
00559     }
00560     if ( to ) {
00561       // for VTODO a REPLY can be used to update the completion status of
00562       // a task. see RFC2446 3.4.3
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 /* status */)
00577 {
00578   // handled in korganizer's IncomingDialog
00579   deleteTransaction(incidence);
00580   return false;
00581 }
00582 
00583 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status /* 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 }
KDE Home | KDE Accessibility Home | Description of Access Keys