scheduler.cpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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 PublishNew:
00050 return i18n("Publish");
00051 case Obsolete:
00052 return i18n("Obsolete");
00053 case RequestNew:
00054 return i18n("New Request");
00055 case RequestUpdate:
00056 return i18n("Updated Request");
00057 default:
00058 return i18n("Unknown Status: %1").arg(QString::number(status));
00059 }
00060 }
00061
00062 Scheduler::Scheduler(Calendar *calendar)
00063 {
00064 mCalendar = calendar;
00065 mFormat = new ICalFormat();
00066 }
00067
00068 Scheduler::~Scheduler()
00069 {
00070 delete mFormat;
00071 }
00072
00073 bool Scheduler::acceptTransaction(IncidenceBase *incidence,Method method,ScheduleMessage::Status status)
00074 {
00075 kdDebug() << "Scheduler::acceptTransaction " << endl;
00076 switch (method) {
00077 case Publish:
00078 return acceptPublish(incidence, status, method);
00079 case Request:
00080 return acceptRequest(incidence, status);
00081 case Add:
00082 return acceptAdd(incidence, status);
00083 case Cancel:
00084 return acceptCancel(incidence, status);
00085 case Declinecounter:
00086 return acceptDeclineCounter(incidence, status);
00087 case Reply:
00088 return acceptReply(incidence, status, method);
00089 case Refresh:
00090 return acceptRefresh(incidence, status);
00091 case Counter:
00092 return acceptCounter(incidence, status);
00093 default:
00094 deleteTransaction(incidence);
00095 return false;
00096 }
00097 deleteTransaction(incidence);
00098 return false;
00099 }
00100
00101 QString Scheduler::methodName(Method method)
00102 {
00103 switch (method) {
00104 case Publish:
00105 return i18n("Publish");
00106 case Request:
00107 return i18n("Request");
00108 case Refresh:
00109 return i18n("Refresh");
00110 case Cancel:
00111 return i18n("Cancel");
00112 case Add:
00113 return i18n("Add");
00114 case Reply:
00115 return i18n("Reply");
00116 case Counter:
00117 return i18n("counter proposal","Counter");
00118 case Declinecounter:
00119 return i18n("decline counter proposal","Decline Counter");
00120 default:
00121 return i18n("Unknown");
00122 }
00123 }
00124
00125 bool Scheduler::deleteTransaction(IncidenceBase *)
00126 {
00127 return true;
00128 }
00129
00130 bool Scheduler::acceptPublish(IncidenceBase *incidence,ScheduleMessage::Status status, Method method)
00131 {
00132 if(incidence->type()=="FreeBusy") {
00133 return acceptFreeBusy(incidence, method);
00134 }
00135 switch (status) {
00136 case ScheduleMessage::PublishNew:
00137 if (!mCalendar->event(incidence->uid())) {
00138 Incidence *inc = static_cast<Incidence *>(incidence);
00139 mCalendar->addIncidence(inc);
00140 deleteTransaction(incidence);
00141 }
00142 return true;
00143 case ScheduleMessage::Obsolete:
00144 return true;
00145 default:
00146 deleteTransaction(incidence);
00147 return false;
00148 }
00149 deleteTransaction(incidence);
00150 return false;
00151 }
00152
00153 bool Scheduler::acceptRequest(IncidenceBase *incidence,ScheduleMessage::Status status)
00154 {
00155 Incidence *inc = static_cast<Incidence *>(incidence);
00156 if (inc->type()=="FreeBusy") {
00157
00158 return true;
00159 } else {
00160 Event *even = mCalendar->event(incidence->uid());
00161 if (even) {
00162 if ( even->revision()<=inc->revision() ) {
00163 if ( even->revision()==inc->revision() &&
00164 even->lastModified()>inc->lastModified()) {
00165 deleteTransaction(incidence);
00166 return false;
00167 }
00168 mCalendar->deleteEvent(even);
00169 } else {
00170 deleteTransaction(incidence);
00171 return false;
00172 }
00173 } else {
00174 Todo *todo = mCalendar->todo(incidence->uid());
00175 if (todo) {
00176 if ( todo->revision()<=inc->revision() ) {
00177 if ( todo->revision()==inc->revision() &&
00178 todo->lastModified()>inc->lastModified()) {
00179 deleteTransaction(incidence);
00180 return false;
00181 }
00182 mCalendar->deleteTodo(todo);
00183 } else {
00184 deleteTransaction(incidence);
00185 return false;
00186 }
00187 }
00188 }
00189 }
00190 mCalendar->addIncidence(inc);
00191 deleteTransaction(incidence);
00192 return true;
00193 }
00194
00195 bool Scheduler::acceptAdd(IncidenceBase *incidence,ScheduleMessage::Status status)
00196 {
00197 deleteTransaction(incidence);
00198 return false;
00199 }
00200
00201 bool Scheduler::acceptCancel(IncidenceBase *incidence,ScheduleMessage::Status status)
00202 {
00203 bool ret = false;
00204 Event *even = mCalendar->event(incidence->uid());
00205 if (even) {
00206 mCalendar->deleteEvent(even);
00207 ret = true;
00208 } else {
00209 Todo *todo = mCalendar->todo(incidence->uid());
00210 if (todo) {
00211 mCalendar->deleteTodo(todo);
00212 ret = true;
00213 }
00214 }
00215 deleteTransaction(incidence);
00216 return ret;
00217 }
00218
00219 bool Scheduler::acceptDeclineCounter(IncidenceBase *incidence,ScheduleMessage::Status status)
00220 {
00221 deleteTransaction(incidence);
00222 return false;
00223 }
00224
00225
00226
00227
00228
00229
00230
00231 bool Scheduler::acceptReply(IncidenceBase *incidence,ScheduleMessage::Status status, Method method)
00232 {
00233 if(incidence->type()=="FreeBusy") {
00234 return acceptFreeBusy(incidence, method);
00235 }
00236 bool ret = false;
00237 Event *ev = mCalendar->event(incidence->uid());
00238 Todo *to = mCalendar->todo(incidence->uid());
00239 if (ev || to) {
00240
00241 kdDebug(5800) << "Scheduler::acceptTransaction match found!" << endl;
00242 QPtrList<Attendee> attendeesIn = incidence->attendees();
00243 QPtrList<Attendee> attendeesEv;
00244 if (ev) attendeesEv = ev->attendees();
00245 if (to) attendeesEv = to->attendees();
00246 Attendee *attIn;
00247 Attendee *attEv;
00248 for ( attIn = attendeesIn.first(); attIn; attIn = attendeesIn.next() ) {
00249 for ( attEv = attendeesEv.first(); attEv; attEv = attendeesEv.next() ) {
00250 if (attIn->email()==attEv->email()) {
00251
00252 kdDebug(5800) << "Scheduler::acceptTransaction update attendee" << endl;
00253 attEv->setStatus(attIn->status());
00254
00255
00256
00257 ret = true;
00258 }
00259 }
00260 }
00261 }
00262 if (ret) deleteTransaction(incidence);
00263 return ret;
00264 }
00265
00266 bool Scheduler::acceptRefresh(IncidenceBase *incidence,ScheduleMessage::Status status)
00267 {
00268
00269 deleteTransaction(incidence);
00270 return false;
00271 }
00272
00273 bool Scheduler::acceptCounter(IncidenceBase *incidence,ScheduleMessage::Status status)
00274 {
00275 deleteTransaction(incidence);
00276 return false;
00277 }
00278
00279 bool Scheduler::acceptFreeBusy(IncidenceBase *incidence, Method method)
00280 {
00281 FreeBusy *freebusy = static_cast<FreeBusy *>(incidence);
00282
00283 QString freeBusyDirName = locateLocal("appdata","freebusy");
00284 kdDebug() << "acceptFreeBusy:: freeBusyDirName: " << freeBusyDirName << endl;
00285
00286 QString from;
00287 if(method == Scheduler::Publish) {
00288 from = freebusy->organizer();
00289 }
00290 if((method == Scheduler::Reply) && (freebusy->attendeeCount() == 1)) {
00291 Attendee *attendee = freebusy->attendees().first();
00292 from = attendee->email();
00293 }
00294
00295 QDir freeBusyDir(freeBusyDirName);
00296 if (!freeBusyDir.exists()) {
00297 kdDebug() << "Directory " << freeBusyDirName << " does not exist!" << endl;
00298 kdDebug() << "Creating directory: " << freeBusyDirName << endl;
00299
00300 if(!freeBusyDir.mkdir(freeBusyDirName, TRUE)) {
00301 kdDebug() << "Could not create directory: " << freeBusyDirName << endl;
00302 return false;
00303 }
00304 }
00305
00306 QString filename(freeBusyDirName);
00307 filename += "/";
00308 filename += from;
00309 filename += ".ifb";
00310 QFile f(filename);
00311
00312 kdDebug() << "acceptFreeBusy: filename" << filename << endl;
00313
00314 freebusy->clearAttendees();
00315 freebusy->setOrganizer(from);
00316
00317 QString messageText = mFormat->createScheduleMessage(freebusy, Publish);
00318
00319 if (!f.open(IO_ReadWrite)) {
00320 kdDebug() << "acceptFreeBusy: Can't open:" << filename << " for writing" << endl;
00321 return false;
00322 }
00323 QTextStream t(&f);
00324 t << messageText;
00325 f.close();
00326
00327 deleteTransaction(incidence);
00328 return true;
00329 }
This file is part of the documentation for kdelibs Version 3.1.5.