libkcal Library API Documentation

scheduler.cpp

00001 /*
00002     This file is part of libkcal.
00003     Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org>
00004 
00005     This library is free software; you can redistribute it and/or
00006     modify it under the terms of the GNU Library General Public
00007     License as published by the Free Software Foundation; either
00008     version 2 of the License, or (at your option) any later version.
00009 
00010     This library is distributed in the hope that it will be useful,
00011     but WITHOUT ANY WARRANTY; without even the implied warranty of
00012     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013     Library General Public License for more details.
00014 
00015     You should have received a copy of the GNU Library General Public License
00016     along with this library; see the file COPYING.LIB.  If not, write to
00017     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00018     Boston, MA 02111-1307, USA.
00019 */
00020 
00021 #include <qdir.h>
00022 #include <qfile.h>
00023 #include <qtextstream.h>
00024 
00025 #include <klocale.h>
00026 #include <kdebug.h>
00027 #include <kstandarddirs.h>
00028 
00029 #include "event.h"
00030 #include "todo.h"
00031 #include "freebusy.h"
00032 #include "icalformat.h"
00033 #include "calendar.h"
00034 
00035 #include "scheduler.h"
00036 
00037 using namespace KCal;
00038 
00039 ScheduleMessage::ScheduleMessage(IncidenceBase *incidence,int method,ScheduleMessage::Status status)
00040 {
00041   mIncidence = incidence;
00042   mMethod = method;
00043   mStatus = status;
00044 }
00045 
00046 QString ScheduleMessage::statusName(ScheduleMessage::Status status)
00047 {
00048   switch (status) {
00049     case PublishUpdate:
00050 // TODO: Add string when i18n freeze is over.
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 Scheduler::Scheduler(Calendar *calendar)
00066 {
00067   mCalendar = calendar;
00068   mFormat = new ICalFormat();
00069   mFormat->setTimeZone( calendar->timeZoneId(), !calendar->isLocalTime() );
00070 }
00071 
00072 Scheduler::~Scheduler()
00073 {
00074   delete mFormat;
00075 }
00076 
00077 bool Scheduler::acceptTransaction(IncidenceBase *incidence,Method method,ScheduleMessage::Status status)
00078 {
00079   kdDebug(5800) << "Scheduler::acceptTransaction, method="
00080                 << methodName( method ) << endl;
00081 
00082   switch (method) {
00083     case Publish:
00084       return acceptPublish(incidence, status, method);
00085     case Request:
00086       return acceptRequest(incidence, status);
00087     case Add:
00088       return acceptAdd(incidence, status);
00089     case Cancel:
00090       return acceptCancel(incidence, status);
00091     case Declinecounter:
00092       return acceptDeclineCounter(incidence, status);
00093     case Reply:
00094       return acceptReply(incidence, status, method);
00095     case Refresh:
00096       return acceptRefresh(incidence, status);
00097     case Counter:
00098       return acceptCounter(incidence, status);
00099     default:
00100       deleteTransaction(incidence);
00101       return false;
00102   }
00103   deleteTransaction(incidence);
00104   return false;
00105 }
00106 
00107 QString Scheduler::methodName(Method method)
00108 {
00109   switch (method) {
00110     case Publish:
00111       return QString::fromLatin1("Publish");
00112     case Request:
00113       return QString::fromLatin1("Request");
00114     case Refresh:
00115       return QString::fromLatin1("Refresh");
00116     case Cancel:
00117       return QString::fromLatin1("Cancel");
00118     case Add:
00119       return QString::fromLatin1("Add");
00120     case Reply:
00121       return QString::fromLatin1("Reply");
00122     case Counter:
00123       return QString::fromLatin1("Counter");
00124     case Declinecounter:
00125       return QString::fromLatin1("Decline Counter");
00126     default:
00127       return QString::fromLatin1("Unknown");
00128   }
00129 }
00130 
00131 QString Scheduler::translatedMethodName(Method method)
00132 {
00133   switch (method) {
00134     case Publish:
00135       return i18n("Publish");
00136     case Request:
00137       return i18n("Request");
00138     case Refresh:
00139       return i18n("Refresh");
00140     case Cancel:
00141       return i18n("Cancel");
00142     case Add:
00143       return i18n("Add");
00144     case Reply:
00145       return i18n("Reply");
00146     case Counter:
00147       return i18n("counter proposal","Counter");
00148     case Declinecounter:
00149       return i18n("decline counter proposal","Decline Counter");
00150     default:
00151       return i18n("Unknown");
00152   }
00153 }
00154 
00155 bool Scheduler::deleteTransaction(IncidenceBase *)
00156 {
00157   return true;
00158 }
00159 
00160 bool Scheduler::acceptPublish( IncidenceBase *incidence,
00161                                ScheduleMessage::Status status, Method method )
00162 {
00163   if( incidence->type() == "FreeBusy" ) {
00164     return acceptFreeBusy( incidence, method );
00165   }
00166   kdDebug() << "Scheduler::acceptPublish, status="
00167             << ScheduleMessage::statusName( status ) << endl;
00168   Incidence *inc = static_cast<Incidence *>( incidence );
00169   Event *even = mCalendar->event( incidence->uid() );
00170   switch ( status ) {
00171     case ScheduleMessage::Unknown:
00172     case ScheduleMessage::PublishNew:
00173     case ScheduleMessage::PublishUpdate:
00174       if ( even ) {
00175         if ( even->revision() <= inc->revision() ) {
00176       if ( even->revision() == inc->revision() &&
00177           even->lastModified() > inc->lastModified() ) {
00178         deleteTransaction( incidence );
00179         return false;
00180       }
00181       mCalendar->deleteEvent( even );
00182     } else {
00183       deleteTransaction( incidence );
00184       return false;
00185     }
00186       }
00187       mCalendar->addIncidence( inc );
00188       deleteTransaction( incidence );
00189       return true;
00190     case ScheduleMessage::Obsolete:
00191       return true;
00192     default:
00193       deleteTransaction( incidence );
00194       return false;
00195   }
00196 }
00197 
00198 bool Scheduler::acceptRequest(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00199 {
00200   Incidence *inc = static_cast<Incidence *>(incidence);
00201   if (inc->type()=="FreeBusy") {
00202     // reply to this request is handled in korganizer's incomingdialog
00203     return true;
00204   } else {
00205     Event *even = mCalendar->event(incidence->uid());
00206     if (even) {
00207       if ( even->revision()<=inc->revision() ) {
00208         if ( even->revision()==inc->revision() &&
00209              even->lastModified()>inc->lastModified()) {
00210           deleteTransaction(incidence);
00211           return false;
00212         }
00213         mCalendar->deleteEvent(even);
00214       } else {
00215         deleteTransaction(incidence);
00216         return false;
00217       }
00218     } else {
00219       Todo *todo = mCalendar->todo(incidence->uid());
00220       if (todo) {
00221         if ( todo->revision()<=inc->revision() ) {
00222           if ( todo->revision()==inc->revision() &&
00223                todo->lastModified()>inc->lastModified()) {
00224             deleteTransaction(incidence);
00225             return false;
00226           }
00227           mCalendar->deleteTodo(todo);
00228         } else {
00229           deleteTransaction(incidence);
00230           return false;
00231         }
00232       }
00233     }
00234   }
00235   mCalendar->addIncidence(inc);
00236   deleteTransaction(incidence);
00237   return true;
00238 }
00239 
00240 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00241 {
00242   deleteTransaction(incidence);
00243   return false;
00244 }
00245 
00246 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00247 {
00248   bool ret = false;
00249   Event *even = mCalendar->event(incidence->uid());
00250   if (even) {
00251     mCalendar->deleteEvent(even);
00252     ret = true;
00253   } else {
00254     Todo *todo = mCalendar->todo(incidence->uid());
00255     if (todo) {
00256       mCalendar->deleteTodo(todo);
00257       ret = true;
00258     }
00259   }
00260   deleteTransaction(incidence);
00261   return ret;
00262 }
00263 
00264 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00265 {
00266   deleteTransaction(incidence);
00267   return false;
00268 }
00269 
00270 //bool Scheduler::acceptFreeBusy(Incidence *incidence,ScheduleMessage::Status status)
00271 //{
00272 //  deleteTransaction(incidence);
00273 //  return false;
00274 //}
00275 
00276 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status /* status */, Method method)
00277 {
00278   if(incidence->type()=="FreeBusy") {
00279     return acceptFreeBusy(incidence, method);
00280   }
00281   bool ret = false;
00282   Event *ev = mCalendar->event(incidence->uid());
00283   Todo *to = mCalendar->todo(incidence->uid());
00284   if (ev || to) {
00285     //get matching attendee in calendar
00286     kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00287     Attendee::List attendeesIn = incidence->attendees();
00288     Attendee::List attendeesEv;
00289     if (ev) attendeesEv = ev->attendees();
00290     if (to) attendeesEv = to->attendees();
00291     Attendee::List::ConstIterator inIt;
00292     Attendee::List::ConstIterator evIt;
00293     for ( inIt = attendeesIn.begin(); inIt != attendeesIn.end(); ++inIt ) {
00294       Attendee *attIn = *inIt;
00295       for ( evIt = attendeesEv.begin(); evIt != attendeesEv.end(); ++evIt ) {
00296         Attendee *attEv = *evIt;;
00297         if (attIn->email()==attEv->email()) {
00298           //update attendee-info
00299           kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00300           attEv->setStatus(attIn->status());
00301       attEv->setRSVP(false);
00302       // better to not update the sequence number with replys
00303           //if (ev) ev->setRevision(ev->revision()+1);
00304       //if (to) to->setRevision(to->revision()+1);
00305           ret = true;
00306         }
00307       }
00308     }
00309   }
00310   if (ret) deleteTransaction(incidence);
00311   return ret;
00312 }
00313 
00314 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00315 {
00316   // handled in korganizer's IncomingDialog
00317   deleteTransaction(incidence);
00318   return false;
00319 }
00320 
00321 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status /* status */)
00322 {
00323   deleteTransaction(incidence);
00324   return false;
00325 }
00326 
00327 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00328 {
00329   FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00330 
00331   kdDebug() << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDir() << endl;
00332 
00333   QString from;
00334   if(method == Scheduler::Publish) {
00335     from = freebusy->organizer();
00336   }
00337   if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00338     Attendee *attendee = freebusy->attendees().first();
00339     from = attendee->email();
00340   }
00341 
00342   QDir freeBusyDirectory(freeBusyDir());
00343   if (!freeBusyDirectory.exists()) {
00344     kdDebug() << "Directory " << freeBusyDir() << " does not exist!" << endl;
00345     kdDebug() << "Creating directory: " << freeBusyDir() << endl;
00346     
00347     if(!freeBusyDirectory.mkdir(freeBusyDir(), true)) {
00348       kdDebug() << "Could not create directory: " << freeBusyDir() << endl;
00349       return false;
00350     }
00351   }
00352 
00353   QString filename(freeBusyDir());
00354   filename += "/";
00355   filename += from;
00356   filename += ".ifb";
00357   QFile f(filename);
00358 
00359   kdDebug() << "acceptFreeBusy: filename" << filename << endl;
00360 
00361   freebusy->clearAttendees();
00362   freebusy->setOrganizer(from);
00363 
00364   QString messageText = mFormat->createScheduleMessage(freebusy, Publish);
00365 
00366   if (!f.open(IO_ReadWrite)) {
00367    kdDebug() << "acceptFreeBusy: Can't open:" << filename << " for writing" << endl;
00368    return false;
00369   }
00370   QTextStream t(&f);
00371   t << messageText;
00372   f.close();
00373 
00374   deleteTransaction(incidence);
00375   return true;
00376 }
00377 
KDE Logo
This file is part of the documentation for libkcal Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:36:23 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003