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 "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     // reply to this request is handled in korganizer's incomingdialog
00236     return true;
00237   }
00238 
00239   Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00240   kdDebug(5800) << "Scheduler::acceptRequest status=" << ScheduleMessage::statusName( status )
00241                 << ": found " << existingIncidences.count() << " incidences with schedulingID "
00242                 << inc->schedulingID() << endl;
00243 
00244 
00245   if ( existingIncidences.count() > 1 ) {
00246     // We must process our own incidences first, so we do a little sort here.
00247     // This situation basically means we're watching a shared calendar from an attendee that
00248     // was also invited, so, kontact is showing two events, on each agenda, which are actually
00249     // the same event.
00250     kdDebug(5800) << "Scheduler::acceptRequest: found more than one existing incidence!" << endl;
00251     Incidence::List existingIncidencesCopy;
00252     Incidence::List::ConstIterator it = existingIncidences.begin();
00253     for ( ; it != existingIncidences.end() ; ++it ) {
00254       Incidence *i = *it;
00255       if ( CalHelper::isMyCalendarIncidence( mCalendar, i ) ) {
00256         existingIncidencesCopy.prepend( i );
00257       } else {
00258         existingIncidencesCopy.append( i );
00259       }
00260     }
00261     existingIncidences = existingIncidencesCopy;
00262   }
00263 
00264   Incidence::List::ConstIterator incit = existingIncidences.begin();
00265   for ( ; incit != existingIncidences.end() ; ++incit ) {
00266     Incidence* const i = *incit;
00267     kdDebug(5800) << "Considering this found event ("
00268                   << ( i->isReadOnly() ? "readonly" : "readwrite" )
00269                   << ") :" << mFormat->toString( i ) << endl;
00270     // If it's readonly, we can't possible update it.
00271     if ( i->isReadOnly() )
00272       continue;
00273     if ( i->revision() <= inc->revision() ) {
00274       // The new incidence might be an update for the found one
00275       bool isUpdate = true;
00276       // Code for new invitations:
00277       // If you think we could check the value of "status" to be RequestNew: we can't.
00278       // It comes from a similar check inside libical, where the event is compared to
00279       // other events in the calendar. But if we have another version of the event around
00280       // (e.g. shared folder for a group), the status could be RequestNew, Obsolete or Updated.
00281       kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00282       // This is supposed to be a new request, not an update - however we want to update
00283       // the existing one to handle the "clicking more than once on the invitation" case.
00284       // So check the attendee status of the attendee.
00285       const KCal::Attendee::List attendees = i->attendees();
00286       KCal::Attendee::List::ConstIterator ait;
00287       for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00288         if( (*ait)->email() == attendee && (*ait)->status() == Attendee::NeedsAction ) {
00289           // This incidence wasn't created by me - it's probably in a shared folder
00290           // and meant for someone else, ignore it.
00291           kdDebug(5800) << "ignoring " << i->uid() << " since I'm still NeedsAction there" << endl;
00292           isUpdate = false;
00293           break;
00294         }
00295       }
00296       if ( isUpdate ) {
00297         if ( i->revision() == inc->revision() &&
00298              i->lastModified() > inc->lastModified() ) {
00299           // This isn't an update - the found incidence was modified more recently
00300           kdDebug(5800) << "This isn't an update - the found incidence was modified more recently" << endl;
00301           deleteTransaction(incidence);
00302           return false;
00303         }
00304         kdDebug(5800) << "replacing existing incidence " << i->uid() << endl;
00305         bool res = true;
00306         AssignmentVisitor visitor;
00307         const QString oldUid = i->uid();
00308         if ( !visitor.assign( i, inc ) ) {
00309           kdError(5800) << "assigning different incidence types" << endl;
00310           res = false;
00311         } else {
00312           {
00313             // issue4579: DON'T change this order. setUid() emits incidenceUpdated().
00314             // setSchedulingID() doesn't. If you change the order, kolab resource will
00315             // catch the signal and think there isn't any schedulingId.
00316             // Possible kcal improvement: create a setUids( schedulingId, uid )
00317             // that changes both members and emits incidenceuIpdated() at the end.
00318             i->setSchedulingID( inc->uid() );
00319             i->setUid( oldUid );
00320           }
00321         }
00322         deleteTransaction( incidence );
00323         return res;
00324       }
00325     } else {
00326       // This isn't an update - the found incidence has a bigger revision number
00327       kdDebug(5800) << "This isn't an update - the found incidence has a bigger revision number" << endl;
00328       deleteTransaction(incidence);
00329       return false;
00330     }
00331   }
00332 
00333   // Move the uid to be the schedulingID and make a unique UID
00334   inc->setSchedulingID( inc->uid() );
00335   inc->setUid( CalFormat::createUniqueId() );
00336   // notify the user in case this is an update and we didn't find the to-be-updated incidence
00337   if ( existingIncidences.count() == 0 && inc->revision() > 0 ) {
00338     KMessageBox::information(
00339       0,
00340       i18n( "<qt>"
00341             "You accepted an invitation update, but an earlier version of the "
00342             "item could not be found in your calendar.<p>"
00343             "This may have occurred because:<ul>"
00344             "<li>the organizer did not include you in the original invitation</li>"
00345             "<li>you did not accept the original invitation yet</li>"
00346             "<li>you deleted the original invitation from your calendar</li>"
00347             "<li>you no longer have access to the calendar containing the invitation</li>"
00348             "</ul>"
00349             "This is not a problem, but we thought you should know.</qt>" ),
00350       i18n( "Cannot find invitation to be updated" ), "AcceptCantFindIncidence" );
00351   }
00352   kdDebug(5800) << "Storing new incidence with scheduling uid=" << inc->schedulingID()
00353                 << " and uid=" << inc->uid() << endl;
00354 
00355   CalendarResources *stdcal = dynamic_cast<CalendarResources *>( mCalendar );
00356   if( stdcal && !stdcal->hasCalendarResources() ) {
00357     KMessageBox::sorry(
00358       0,
00359       i18n( "No calendars found, unable to save the invitation." ) );
00360     return false;
00361   }
00362 
00363   // FIXME: This is a nasty hack, since we need to set a parent for the
00364   //        resource selection dialog. However, we don't have any UI methods
00365   //        in the calendar, only in the CalendarResources::DestinationPolicy
00366   //        So we need to type-cast it and extract it from the CalendarResources
00367   QWidget *tmpparent = 0;
00368   if ( stdcal ) {
00369     tmpparent = stdcal->dialogParentWidget();
00370     stdcal->setDialogParentWidget( 0 );
00371   }
00372 
00373 TryAgain:
00374   bool success = false;
00375   if ( stdcal ) {
00376     success = stdcal->addIncidence( inc );
00377   } else {
00378     success = mCalendar->addIncidence( inc );
00379   }
00380 
00381   if ( !success ) {
00382     ErrorFormat *e = stdcal ? stdcal->exception() : 0;
00383 
00384     if ( e && e->errorCode() == KCal::ErrorFormat::UserCancel &&
00385          KMessageBox::warningYesNo(
00386            0,
00387            i18n( "You canceled the save operation. Therefore, the appointment will not be "
00388                  "stored in your calendar even though you accepted the invitation. "
00389                  "Are you certain you want to discard this invitation? " ),
00390            i18n( "Discard this invitation?" ),
00391            i18n( "Discard" ), i18n( "Go Back to Folder Selection" ) ) == KMessageBox::Yes ) {
00392       KMessageBox::information(
00393         0,
00394         i18n( "The invitation \"%1\" was not saved to your calendar "
00395               "but you are still listed as an attendee for that appointment.\n"
00396               "If you mistakenly accepted the invitation or do not plan to attend, please notify "
00397               "the organizer %2 and ask them to remove you from the attendee list.").
00398         arg( inc->summary(),  inc->organizer().fullName() ) );
00399       deleteTransaction( incidence );
00400       return true;
00401     } else {
00402       goto TryAgain;
00403     }
00404 
00405     // We can have a failure if the user pressed [cancel] in the resource
00406     // selectdialog, so check the exception.
00407     if ( !e ||
00408          ( e && ( e->errorCode() != KCal::ErrorFormat::UserCancel &&
00409                   e->errorCode() != KCal::ErrorFormat::NoWritableFound ) ) ) {
00410       QString errMessage = i18n( "Unable to save %1 \"%2\"." ).
00411                            arg( i18n( inc->type() ) ).
00412                            arg( inc->summary() );
00413       KMessageBox::sorry( 0, errMessage );
00414     }
00415     return false;
00416   }
00417 
00418   deleteTransaction( incidence );
00419   return true;
00420 }
00421 
00422 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00423 {
00424   deleteTransaction(incidence);
00425   return false;
00426 }
00427 
00428 bool Scheduler::acceptCancel( IncidenceBase *incidence,
00429                               ScheduleMessage::Status status,
00430                               const QString &attendee )
00431 {
00432   Incidence *inc = static_cast<Incidence *>( incidence );
00433   if ( !inc ) {
00434     return false;
00435   }
00436 
00437   if ( inc->type() == "FreeBusy" ) {
00438     // reply to this request is handled in korganizer's incomingdialog
00439     return true;
00440   }
00441 
00442   const Incidence::List existingIncidences = mCalendar->incidencesFromSchedulingID( inc->uid() );
00443   kdDebug(5800) << "Scheduler::acceptCancel="
00444                 << ScheduleMessage::statusName( status )
00445                 << ": found " << existingIncidences.count()
00446                 << " incidences with schedulingID " << inc->schedulingID()
00447                 << endl;
00448 
00449   // Remove existing incidences that aren't stored in my calendar as we
00450   // will never attempt to remove those -- even if we have write-access.
00451   Incidence::List myExistingIncidences;
00452   Incidence::List::ConstIterator incit = existingIncidences.begin();
00453   for ( ; incit != existingIncidences.end() ; ++incit ) {
00454     Incidence *i = *incit;
00455     if ( CalHelper::isMyCalendarIncidence( mCalendar, i ) ) {
00456       myExistingIncidences.append( i );
00457     }
00458   }
00459 
00460   bool ret = false;
00461   incit = myExistingIncidences.begin();
00462   for ( ; incit != myExistingIncidences.end() ; ++incit ) {
00463     Incidence *i = *incit;
00464     kdDebug(5800) << "Considering this found event ("
00465                   << ( i->isReadOnly() ? "readonly" : "readwrite" )
00466                   << ") :" << mFormat->toString( i ) << endl;
00467 
00468     // If it's readonly, we can't possible remove it.
00469     if ( i->isReadOnly() ) {
00470       continue;
00471     }
00472 
00473     // Code for new invitations:
00474     // We cannot check the value of "status" to be RequestNew because
00475     // "status" comes from a similar check inside libical, where the event
00476     // is compared to other events in the calendar. But if we have another
00477     // version of the event around (e.g. shared folder for a group), the
00478     // status could be RequestNew, Obsolete or Updated.
00479     kdDebug(5800) << "looking in " << i->uid() << "'s attendees" << endl;
00480 
00481     // This is supposed to be a new request, not an update - however we want
00482     // to update the existing one to handle the "clicking more than once
00483     // on the invitation" case. So check the attendee status of the attendee.
00484     bool isMine = true;
00485     const KCal::Attendee::List attendees = i->attendees();
00486     KCal::Attendee::List::ConstIterator ait;
00487     for ( ait = attendees.begin(); ait != attendees.end(); ++ait ) {
00488       if ( (*ait)->email() == attendee &&
00489            (*ait)->status() == Attendee::NeedsAction ) {
00490         // This incidence wasn't created by me - it's probably in a shared
00491         // folder and meant for someone else, ignore it.
00492         kdDebug(5800) << "ignoring " << i->uid()
00493                       << " since I'm still NeedsAction there" << endl;
00494         isMine = false;
00495         break;
00496       }
00497     }
00498 
00499     if ( isMine ) {
00500       kdDebug(5800) << "removing existing incidence " << i->uid() << endl;
00501       if ( i->type() == "Event" ) {
00502         Event *event = mCalendar->event( i->uid() );
00503         ret = ( event && mCalendar->deleteEvent( event ) );
00504       } else if ( i->type() == "Todo" ) {
00505         Todo *todo = mCalendar->todo( i->uid() );
00506         ret = ( todo && mCalendar->deleteTodo( todo ) );
00507       }
00508       deleteTransaction( incidence );
00509       return ret;
00510     }
00511   }
00512 
00513   // in case we didn't find the to-be-removed incidence
00514   if ( myExistingIncidences.count() > 0 && inc->revision() > 0 ) {
00515     KMessageBox::information(
00516       0,
00517       i18n( "The event or task could not be removed from your calendar. "
00518             "Maybe it has already been deleted or is not owned by you. "
00519             "Or it might belong to a read-only or disabled calendar." ) );
00520   }
00521   deleteTransaction( incidence );
00522   return ret;
00523 }
00524 
00525 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00526 {
00527   const IncidenceBase *toDelete = mCalendar->incidenceFromSchedulingID( incidence->uid() );
00528 
00529   bool ret = true;
00530   if ( toDelete ) {
00531     if ( toDelete->type() == "Event" ) {
00532       Event *event = mCalendar->event( toDelete->uid() );
00533       ret = ( event && mCalendar->deleteEvent( event ) );
00534     } else if ( toDelete->type() == "Todo" ) {
00535       Todo *todo = mCalendar->todo( toDelete->uid() );
00536       ret = ( todo && mCalendar->deleteTodo( todo ) );
00537     }
00538   } else {
00539     // only complain if we failed to determine the toDelete incidence
00540     // on non-initial request.
00541     Incidence *inc = static_cast<Incidence *>( incidence );
00542     if ( inc->revision() > 0 ) {
00543       ret = false;
00544     }
00545   }
00546 
00547   if ( !ret ) {
00548     KMessageBox::information(
00549       0,
00550       i18n( "The event or task to be canceled could not be removed from your calendar. "
00551             "Maybe it has already been deleted or is not owned by you. "
00552             "Or it might belong to a read-only or disabled calendar." ) );
00553   }
00554   deleteTransaction(incidence);
00555   return ret;
00556 }
00557 
00558 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00559 {
00560   deleteTransaction(incidence);
00561   return false;
00562 }
00563 
00564 //bool Scheduler::acceptFreeBusy(Incidence *incidence,ScheduleMessage::Status status)
00565 //{
00566 //  deleteTransaction(incidence);
00567 //  return false;
00568 //}
00569 
00570 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status /* status */, Method method)
00571 {
00572   if(incidence->type()=="FreeBusy") {
00573     return acceptFreeBusy(incidence, method);
00574   }
00575   bool ret = false;
00576   Event *ev = mCalendar->event(incidence->uid());
00577   Todo *to = mCalendar->todo(incidence->uid());
00578 
00579   // try harder to find the correct incidence
00580   if ( !ev && !to ) {
00581     const Incidence::List list = mCalendar->incidences();
00582     for ( Incidence::List::ConstIterator it = list.begin(), end = list.end(); it != end; ++it ) {
00583       if ( (*it)->schedulingID() == incidence->uid() ) {
00584         ev = dynamic_cast<Event*>( *it );
00585         to = dynamic_cast<Todo*>( *it );
00586         break;
00587       }
00588     }
00589   }
00590 
00591   if (ev || to) {
00592     //get matching attendee in calendar
00593     kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00594     Attendee::List attendeesIn = incidence->attendees();
00595     Attendee::List attendeesEv;
00596     Attendee::List attendeesNew;
00597     if (ev) attendeesEv = ev->attendees();
00598     if (to) attendeesEv = to->attendees();
00599     Attendee::List::ConstIterator inIt;
00600     Attendee::List::ConstIterator evIt;
00601     for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00602       Attendee *attIn = *inIt;
00603       bool found = false;
00604       for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00605         Attendee *attEv = *evIt;
00606         if (attIn->email().lower()==attEv->email().lower()) {
00607           //update attendee-info
00608           kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00609           attEv->setStatus(attIn->status());
00610           attEv->setDelegate(attIn->delegate());
00611           attEv->setDelegator(attIn->delegator());
00612           Incidence *incidence = ev ? static_cast<Incidence*>( ev ) :
00613                                       static_cast<Incidence*>( to );
00614           incidence->setFieldDirty( Incidence::FieldAttendees );
00615           ret = true;
00616           found = true;
00617         }
00618       }
00619       if ( !found && attIn->status() != Attendee::Declined )
00620         attendeesNew.append( attIn );
00621     }
00622 
00623     bool attendeeAdded = false;
00624     for ( Attendee::List::ConstIterator it = attendeesNew.constBegin(); it != attendeesNew.constEnd(); ++it ) {
00625       Attendee* attNew = *it;
00626       QString msg = i18n("%1 wants to attend %2 but was not invited.").arg( attNew->fullName() )
00627           .arg( ev ? ev->summary() : to->summary() );
00628       if ( !attNew->delegator().isEmpty() )
00629         msg = i18n("%1 wants to attend %2 on behalf of %3.").arg( attNew->fullName() )
00630             .arg( ev ? ev->summary() : to->summary() )
00631             .arg( attNew->delegator() );
00632       if ( KMessageBox::questionYesNo( 0, msg, i18n("Uninvited attendee"),
00633            KGuiItem(i18n("Accept Attendance")), KGuiItem(i18n("Reject Attendance")) )
00634            != KMessageBox::Yes )
00635       {
00636         KCal::Incidence *cancel = dynamic_cast<Incidence*>( incidence );
00637         if ( cancel )
00638           cancel->addComment( i18n( "The organizer rejected your attendance at this meeting." ) );
00639         performTransaction( cancel ? cancel : incidence, Scheduler::Cancel, attNew->fullName() );
00640         delete cancel;
00641         continue;
00642       }
00643 
00644       Attendee *a = new Attendee( attNew->name(), attNew->email(), attNew->RSVP(),
00645                                   attNew->status(), attNew->role(), attNew->uid() );
00646       a->setDelegate( attNew->delegate() );
00647       a->setDelegator( attNew->delegator() );
00648       if ( ev )
00649         ev->addAttendee( a );
00650       else if ( to )
00651         to->addAttendee( a );
00652       ret = true;
00653       attendeeAdded = true;
00654     }
00655 
00656     // send update about new participants
00657     if ( attendeeAdded ) {
00658       bool sendMail = false;
00659       if ( ev || to ) {
00660         if ( KMessageBox::questionYesNo( 0, i18n( "An attendee was added to the incidence. "
00661                                                   "Do you want to email the attendees an update message?" ),
00662                                          i18n( "Attendee Added" ), i18n( "Send Messages" ),
00663                                          i18n( "Do Not Send" ) ) == KMessageBox::Yes ) {
00664           sendMail = true;
00665         }
00666       }
00667 
00668       if ( ev ) {
00669         ev->setRevision( ev->revision() + 1 );
00670         if ( sendMail )
00671           performTransaction( ev, Scheduler::Request );
00672       }
00673       if ( to ) {
00674         to->setRevision( to->revision() + 1 );
00675         if ( sendMail )
00676           performTransaction( to, Scheduler::Request );
00677       }
00678     }
00679 
00680     if ( ret ) {
00681       // We set at least one of the attendees, so the incidence changed
00682       // Note: This should not result in a sequence number bump
00683       if ( ev )
00684         ev->updated();
00685       else if ( to )
00686         to->updated();
00687     }
00688     if ( to ) {
00689       // for VTODO a REPLY can be used to update the completion status of
00690       // a task. see RFC2446 3.4.3
00691       Todo *update = dynamic_cast<Todo*> ( incidence );
00692       Q_ASSERT( update );
00693       if ( update && ( to->percentComplete() != update->percentComplete() ) ) {
00694         to->setPercentComplete( update->percentComplete() );
00695         to->updated();
00696       }
00697     }
00698   } else
00699     kdError(5800) << "No incidence for scheduling\n";
00700   if (ret) deleteTransaction(incidence);
00701   return ret;
00702 }
00703 
00704 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00705 {
00706   // handled in korganizer's IncomingDialog
00707   deleteTransaction(incidence);
00708   return false;
00709 }
00710 
00711 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00712 {
00713   deleteTransaction(incidence);
00714   return false;
00715 }
00716 
00717 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00718 {
00719   if ( !d->mFreeBusyCache ) {
00720     kdError() << "KCal::Scheduler: no FreeBusyCache." << endl;
00721     return false;
00722   }
00723 
00724   FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00725 
00726   kdDebug(5800) << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00727 
00728   Person from;
00729   if(method == Scheduler::Publish) {
00730     from = freebusy->organizer();
00731   }
00732   if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00733     Attendee *attendee = freebusy->attendees().first();
00734     from = attendee->email();
00735   }
00736 
00737   if ( !d->mFreeBusyCache->saveFreeBusy( freebusy, from ) ) return false;
00738 
00739   deleteTransaction(incidence);
00740   return true;
00741 }
KDE Home | KDE Accessibility Home | Description of Access Keys