-rw-r--r-- | korganizer/outgoingdialog.cpp | 394 |
1 files changed, 394 insertions, 0 deletions
diff --git a/korganizer/outgoingdialog.cpp b/korganizer/outgoingdialog.cpp new file mode 100644 index 0000000..4eb64f3 --- a/dev/null +++ b/korganizer/outgoingdialog.cpp | |||
@@ -0,0 +1,394 @@ | |||
1 | /* | ||
2 | This file is part of KOrganizer. | ||
3 | Copyright (c) 2001 Cornelius Schumacher <schumacher@kde.org> | ||
4 | |||
5 | This program is free software; you can redistribute it and/or modify | ||
6 | it under the terms of the GNU General Public License as published by | ||
7 | the Free Software Foundation; either version 2 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | This program is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU General Public License | ||
16 | along with this program; if not, write to the Free Software | ||
17 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
18 | */ | ||
19 | |||
20 | #include <kdebug.h> | ||
21 | |||
22 | #include <qfile.h> | ||
23 | #include <qdir.h> | ||
24 | #include <qtextstream.h> | ||
25 | |||
26 | #include <kglobal.h> | ||
27 | #include <klocale.h> | ||
28 | #include <ktempfile.h> | ||
29 | #include <kstandarddirs.h> | ||
30 | |||
31 | #include <libkcal/event.h> | ||
32 | #include <libkcal/freebusy.h> | ||
33 | //#include <libkcal/imipscheduler.h> | ||
34 | #include <libkcal/dummyscheduler.h> | ||
35 | #include <libkcal/icalformat.h> | ||
36 | #include <libkcal/calendar.h> | ||
37 | |||
38 | #ifndef KORG_NOMAIL | ||
39 | #include "mailscheduler.h" | ||
40 | #endif | ||
41 | |||
42 | #include "koprefs.h" | ||
43 | #include "outgoingdialog.h" | ||
44 | #include "koeventviewerdialog.h" | ||
45 | #include "docprefs.h" | ||
46 | |||
47 | ScheduleItemOut::ScheduleItemOut(QListView *parent,IncidenceBase *ev, | ||
48 | Scheduler::Method method, | ||
49 | const QString &recipients) | ||
50 | : QListViewItem(parent) | ||
51 | { | ||
52 | mIncidence = ev; | ||
53 | mMethod = method; | ||
54 | mRecipients = recipients; | ||
55 | |||
56 | // kdDebug() << "ScheduleItemOut: setting the summary" << endl; | ||
57 | //Set the summary | ||
58 | if(ev->type() != "FreeBusy") { | ||
59 | Incidence *incidence = static_cast<Incidence *>(ev); | ||
60 | setText(0,incidence->summary()); | ||
61 | } else { | ||
62 | setText(0,i18n("Free Busy Object")); | ||
63 | } | ||
64 | |||
65 | // kdDebug() << "ScheduleItemOut: checking if the object is an event" << endl; | ||
66 | //If the object is an event | ||
67 | if(ev->type()=="Event") { | ||
68 | Event *event = static_cast<Event *>(ev); | ||
69 | |||
70 | setText(1,event->dtStartDateStr()); | ||
71 | if (event->doesFloat()) { //If the event floats set the start and end times to no time | ||
72 | setText(2,i18n("no time")); | ||
73 | setText(4,i18n("no time")); | ||
74 | } else { //If it does not float | ||
75 | setText(2,event->dtStartTimeStr()); | ||
76 | if (event->hasDuration()) { | ||
77 | setText(4,event->dtEndTimeStr()); | ||
78 | } else { | ||
79 | setText(4,i18n("no time")); | ||
80 | } | ||
81 | } | ||
82 | if (event->hasEndDate()) { | ||
83 | setText(3,event->dtEndDateStr()); | ||
84 | } | ||
85 | else { | ||
86 | setText(3,i18n("no time")); | ||
87 | } | ||
88 | } | ||
89 | |||
90 | //If the object is an Todo | ||
91 | if(ev->type()=="Todo") { | ||
92 | Todo *event = static_cast<Todo *>(ev); | ||
93 | if (event->hasStartDate()) { | ||
94 | setText(1,event->dtStartDateStr()); | ||
95 | if (!event->doesFloat()) { | ||
96 | setText(2,event->dtStartTimeStr()); | ||
97 | } | ||
98 | } | ||
99 | if (event->hasDueDate()) { | ||
100 | setText(3,event->dtDueDateStr()); | ||
101 | if (!event->doesFloat()) { | ||
102 | setText(4,event->dtDueTimeStr()); | ||
103 | } | ||
104 | } | ||
105 | } | ||
106 | |||
107 | // kdDebug() << "ScheduleItemOut: checking if the object is a FreeBusy object" << endl; | ||
108 | //If the object is a freebusy object | ||
109 | if(ev->type() == "FreeBusy") { | ||
110 | FreeBusy *freebusy = static_cast<FreeBusy *>(ev); | ||
111 | |||
112 | setText(1,freebusy->dtStartDateStr()); | ||
113 | setText(2,freebusy->dtStartTimeStr()); | ||
114 | //Must try and get this to the users local settings | ||
115 | setText(3,KGlobal::locale()->formatDate( freebusy->dtEnd().date() ) ); | ||
116 | setText(4,KGlobal::locale()->formatTime( freebusy->dtEnd().time() ) ); | ||
117 | } | ||
118 | |||
119 | // kdDebug() << "ScheduleItemOut: Setting the method" << endl; | ||
120 | //Set the Method | ||
121 | setText(5,Scheduler::translatedMethodName(mMethod)); | ||
122 | } | ||
123 | |||
124 | OutgoingDialog::OutgoingDialog(Calendar *calendar,QWidget* parent, | ||
125 | const char* name,bool modal, | ||
126 | WFlags fl) | ||
127 | : OutgoingDialog_base(parent,name,modal,fl) | ||
128 | { | ||
129 | mCalendar = calendar; | ||
130 | |||
131 | mFormat = new ICalFormat; | ||
132 | |||
133 | if (KOPrefs::instance()->mIMIPScheduler == KOPrefs::IMIPDummy ) { | ||
134 | mScheduler = new DummyScheduler(mCalendar); | ||
135 | } else { | ||
136 | #ifndef KORG_NOMAIL | ||
137 | mScheduler = new MailScheduler(mCalendar); | ||
138 | #else | ||
139 | mScheduler = new DummyScheduler(mCalendar); | ||
140 | #endif | ||
141 | } | ||
142 | mMessageListView->setColumnAlignment(1,AlignHCenter); | ||
143 | mMessageListView->setColumnAlignment(2,AlignHCenter); | ||
144 | mMessageListView->setColumnAlignment(3,AlignHCenter); | ||
145 | mMessageListView->setColumnAlignment(4,AlignHCenter); | ||
146 | QObject::connect(mMessageListView,SIGNAL(doubleClicked(QListViewItem *)), | ||
147 | this,SLOT(showEvent(QListViewItem *))); | ||
148 | mDocPrefs = new DocPrefs("groupschedule"); | ||
149 | loadMessages(); | ||
150 | } | ||
151 | |||
152 | OutgoingDialog::~OutgoingDialog() | ||
153 | { | ||
154 | delete mDocPrefs; | ||
155 | delete mFormat; | ||
156 | } | ||
157 | |||
158 | bool OutgoingDialog::addMessage(IncidenceBase *incidence,Scheduler::Method method) | ||
159 | { | ||
160 | kdDebug() << "Outgoing::addMessage" << "Method:" << method << endl; | ||
161 | if (method == Scheduler::Publish) return false; | ||
162 | if( mDocPrefs ) { | ||
163 | if (method != Scheduler::Cancel) { | ||
164 | mDocPrefs->writeEntry( incidence->uid()+"-scheduled", true ); | ||
165 | } else { | ||
166 | if (!mDocPrefs->readBoolEntry(incidence->uid()+"-scheduled") ) | ||
167 | return true; | ||
168 | } | ||
169 | } | ||
170 | |||
171 | if (KOPrefs::instance()->mIMIPSend == KOPrefs::IMIPOutbox) { | ||
172 | new ScheduleItemOut(mMessageListView,incidence,method); | ||
173 | saveMessage(incidence,method); | ||
174 | emit numMessagesChanged(mMessageListView->childCount()); | ||
175 | } | ||
176 | else { | ||
177 | mScheduler->performTransaction(incidence,method); | ||
178 | } | ||
179 | return true; | ||
180 | } | ||
181 | |||
182 | bool OutgoingDialog::addMessage(IncidenceBase *incidence,Scheduler::Method method, | ||
183 | const QString &recipients) | ||
184 | { | ||
185 | //if (method != Scheduler::Publish) return false; | ||
186 | if( mDocPrefs ) { | ||
187 | if (method != Scheduler::Cancel) { | ||
188 | mDocPrefs->writeEntry( incidence->uid()+"-scheduled", true ); | ||
189 | } else { | ||
190 | if (!mDocPrefs->readBoolEntry(incidence->uid()+"-scheduled") ) | ||
191 | return true; | ||
192 | } | ||
193 | } | ||
194 | if (KOPrefs::instance()->mIMIPSend == KOPrefs::IMIPOutbox) { | ||
195 | new ScheduleItemOut(mMessageListView,incidence,method,recipients); | ||
196 | saveMessage(incidence,method,recipients); | ||
197 | emit numMessagesChanged(mMessageListView->childCount()); | ||
198 | } | ||
199 | else { | ||
200 | mScheduler->performTransaction(incidence,method,recipients); | ||
201 | } | ||
202 | return true; | ||
203 | } | ||
204 | |||
205 | void OutgoingDialog::send() | ||
206 | { | ||
207 | kdDebug() << "OutgoingDialog::send" << endl; | ||
208 | ScheduleItemOut *item = (ScheduleItemOut *)(mMessageListView->firstChild()); | ||
209 | while(item) { | ||
210 | bool success; | ||
211 | if (item->method() == Scheduler::Publish) { | ||
212 | success = mScheduler->publish(item->event(),item->recipients()); | ||
213 | } else { | ||
214 | success = mScheduler->performTransaction(item->event(),item->method()); | ||
215 | } | ||
216 | ScheduleItemOut *oldItem = item; | ||
217 | item = (ScheduleItemOut *)(item->nextSibling()); | ||
218 | if (success) { | ||
219 | deleteMessage(oldItem->event()); | ||
220 | delete (oldItem->event()); | ||
221 | delete oldItem; | ||
222 | } | ||
223 | } | ||
224 | |||
225 | emit numMessagesChanged(mMessageListView->childCount()); | ||
226 | } | ||
227 | |||
228 | void OutgoingDialog::deleteItem() | ||
229 | { | ||
230 | ScheduleItemOut *item = (ScheduleItemOut *)(mMessageListView->selectedItem()); | ||
231 | if(!item) | ||
232 | return; | ||
233 | deleteMessage(item->event()); | ||
234 | delete(item->event()); | ||
235 | mMessageListView->takeItem(item); | ||
236 | emit numMessagesChanged(mMessageListView->childCount()); | ||
237 | } | ||
238 | |||
239 | void OutgoingDialog::showEvent(QListViewItem *qitem) | ||
240 | { | ||
241 | ScheduleItemOut *item = (ScheduleItemOut *)qitem; | ||
242 | Event *event = 0; | ||
243 | Todo *todo = 0; | ||
244 | if ( item->event()->type()=="Event" ) { | ||
245 | event = static_cast<Event *>(item->event()); | ||
246 | } | ||
247 | if ( item->event()->type()=="Todo" ) { | ||
248 | todo = static_cast<Todo *>(item->event()); | ||
249 | } | ||
250 | QString sendText; | ||
251 | if (event || todo) { | ||
252 | KOEventViewerDialog *eventViewer = new KOEventViewerDialog(this); | ||
253 | if (event) eventViewer->setEvent(event); | ||
254 | if (todo) eventViewer->setTodo(todo); | ||
255 | sendText = "<hr><h4>"+i18n("Event will be sent to:")+"</h4>"; | ||
256 | switch (item->method()) { | ||
257 | case Scheduler::Publish: { | ||
258 | sendText += item->recipients(); | ||
259 | break; } | ||
260 | case Scheduler::Request: { | ||
261 | sendText += i18n("All attendees"); | ||
262 | break; } | ||
263 | case Scheduler::Refresh: { | ||
264 | sendText += i18n("All attendees"); | ||
265 | break; } | ||
266 | case Scheduler::Cancel: { | ||
267 | sendText += i18n("All attendees"); | ||
268 | break; } | ||
269 | case Scheduler::Add: { | ||
270 | sendText += i18n("All attendees"); | ||
271 | break; } | ||
272 | case Scheduler::Reply: { | ||
273 | sendText += i18n("The organizer %1").arg(item->event()->organizer()); | ||
274 | break; } | ||
275 | case Scheduler::Counter: { | ||
276 | sendText += i18n("The organizer %1").arg(item->event()->organizer()); | ||
277 | break; } | ||
278 | case Scheduler::Declinecounter: { | ||
279 | sendText += i18n("All attendees"); | ||
280 | break; } | ||
281 | case Scheduler::NoMethod: { | ||
282 | sendText += ""; | ||
283 | break; } | ||
284 | default: | ||
285 | sendText = ""; | ||
286 | } | ||
287 | eventViewer->addText(sendText); | ||
288 | eventViewer->show(); | ||
289 | } | ||
290 | } | ||
291 | |||
292 | bool OutgoingDialog::saveMessage(IncidenceBase *incidence,Scheduler::Method method, | ||
293 | const QString &recipients) | ||
294 | { | ||
295 | KTempFile ktfile(locateLocal("data","korganizer/outgoing/"),"ics"); | ||
296 | QString messageText = mFormat->createScheduleMessage(incidence,method); | ||
297 | QTextStream *qts = ktfile.textStream(); | ||
298 | *qts << messageText; | ||
299 | *qts << "METHOD-BEGIN:" << endl << method << endl << ":METHOD-END" << endl; | ||
300 | *qts << "RECIPIENTS-BEGIN:" << endl << recipients << endl << ":RECIPIENTS-END" << endl; | ||
301 | mMessageMap[incidence]=ktfile.name(); | ||
302 | |||
303 | return true; | ||
304 | } | ||
305 | |||
306 | bool OutgoingDialog::deleteMessage(IncidenceBase *incidence) | ||
307 | { | ||
308 | QFile f( mMessageMap[incidence] ); | ||
309 | mMessageMap.remove(incidence); | ||
310 | if ( !f.exists() ) return false; | ||
311 | else | ||
312 | return f.remove(); | ||
313 | } | ||
314 | |||
315 | void OutgoingDialog::loadMessages() | ||
316 | { | ||
317 | Scheduler::Method method; | ||
318 | QString recipients; | ||
319 | |||
320 | QString outgoingDirName = locateLocal("data","korganizer/outgoing"); | ||
321 | QDir outgoingDir(outgoingDirName); | ||
322 | QStringList outgoing = outgoingDir.entryList(QDir::Files); | ||
323 | QStringList::ConstIterator it; | ||
324 | for(it = outgoing.begin(); it != outgoing.end(); ++it) { | ||
325 | kdDebug() << "-- File: " << (*it) << endl; | ||
326 | QFile f(outgoingDirName + "/" + (*it)); | ||
327 | bool inserted = false; | ||
328 | QMap<IncidenceBase*, QString>::Iterator iter; | ||
329 | for ( iter = mMessageMap.begin(); iter != mMessageMap.end(); ++iter ) { | ||
330 | if (iter.data() == outgoingDirName + "/" + (*it)) inserted = true; | ||
331 | } | ||
332 | if (!inserted) { | ||
333 | if (!f.open(IO_ReadOnly)) { | ||
334 | kdDebug() << "OutgoingDialog::loadMessage(): Can't open file'" | ||
335 | << (*it) << "'" << endl; | ||
336 | } else { | ||
337 | QTextStream t(&f); | ||
338 | QString messageString = t.read(); | ||
339 | ScheduleMessage *message = mFormat->parseScheduleMessage(mCalendar, | ||
340 | messageString); | ||
341 | int begin_pos = messageString.find("METHOD-BEGIN:"); | ||
342 | begin_pos = messageString.find('\n',begin_pos)+1; | ||
343 | QString meth = messageString.mid(begin_pos,1); | ||
344 | switch (meth.toInt()) { | ||
345 | case 0:method=Scheduler::Publish; break; | ||
346 | case 1:method=Scheduler::Request; break; | ||
347 | case 2:method=Scheduler::Refresh; break; | ||
348 | case 3:method=Scheduler::Cancel; break; | ||
349 | case 4:method=Scheduler::Add; break; | ||
350 | case 5:method=Scheduler::Reply; break; | ||
351 | case 6:method=Scheduler::Counter; break; | ||
352 | case 7:method=Scheduler::Declinecounter; break; | ||
353 | default :method=Scheduler::NoMethod; break; | ||
354 | } | ||
355 | begin_pos = messageString.find("RECIPIENTS-BEGIN:"); | ||
356 | begin_pos = messageString.find('\n',begin_pos)+1; | ||
357 | int end_pos = messageString.find(":RECIPIENTS-END",begin_pos)-1; | ||
358 | recipients = messageString.mid(begin_pos, end_pos-begin_pos); | ||
359 | kdDebug() << "Outgoing::loadMessage(): Recipients: " << recipients << endl; | ||
360 | |||
361 | if (message) { | ||
362 | bool inserted = false; | ||
363 | QMap<IncidenceBase*, QString>::Iterator iter; | ||
364 | for ( iter = mMessageMap.begin(); iter != mMessageMap.end(); ++iter ) { | ||
365 | if (iter.data() == outgoingDirName + "/" + (*it)) inserted = true; | ||
366 | } | ||
367 | if (!inserted) { | ||
368 | kdDebug() << "OutgoingDialog::loadMessage(): got message '" | ||
369 | << (*it) << "'" << endl; | ||
370 | IncidenceBase *inc = message->event(); | ||
371 | new ScheduleItemOut(mMessageListView,inc,method,recipients); | ||
372 | mMessageMap[message->event()]=outgoingDirName + "/" + (*it); | ||
373 | } | ||
374 | } else { | ||
375 | QString errorMessage; | ||
376 | if (mFormat->exception()) { | ||
377 | errorMessage = mFormat->exception()->message(); | ||
378 | } | ||
379 | kdDebug() << "OutgoingDialog::loadMessage(): Error parsing " | ||
380 | "message: " << errorMessage << endl; | ||
381 | } | ||
382 | f.close(); | ||
383 | } | ||
384 | } | ||
385 | } | ||
386 | emit numMessagesChanged(mMessageListView->childCount()); | ||
387 | } | ||
388 | |||
389 | void OutgoingDialog::setDocumentId( const QString &id ) | ||
390 | { | ||
391 | mDocPrefs->setDoc( id ); | ||
392 | } | ||
393 | |||
394 | #include "outgoingdialog.moc" | ||