Fix some copyright headers
[claws.git] / src / plugins / vcalendar / vcalendar.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2018 Colin Leroy and the Claws Mail team
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 3 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <stddef.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include <libical/ical.h>
30 #include <gtk/gtk.h>
31
32 #if USE_PTHREAD
33 #include "pthread.h"
34 #endif
35
36 #include "folder.h"
37 #include "folder_item_prefs.h"
38 #include "mimeview.h"
39 #include "utils.h"
40 #include "vcalendar.h"
41 #include "vcal_manager.h"
42 #include "vcal_folder.h"
43 #include "vcal_meeting_gtk.h"
44 #include "vcal_interface.h"
45 #include "prefs_account.h"
46 #include "prefs_common.h"
47 #include "menu.h"
48 #include "account.h"
49 #include "codeconv.h"
50 #include "xml.h"
51 #include "xmlprops.h"
52 #include "alertpanel.h"
53 #include "vcal_prefs.h"
54 #include "statusbar.h"
55 #include "timing.h"
56 #include "inc.h"
57
58 MimeViewerFactory vcal_viewer_factory;
59
60 static void create_meeting_from_message_cb_ui(GtkAction *action, gpointer data);
61
62 static GdkColor uri_color = {
63         (gulong)0,
64         (gushort)0,
65         (gushort)0,
66         (gushort)0
67 };
68
69 struct _VCalViewer
70 {
71         MimeViewer mimeviewer;
72
73         gchar     *file;
74         MimeInfo  *mimeinfo;
75         gchar     *tmpfile;
76         VCalEvent *event;
77
78         GtkWidget *scrolledwin;
79         GtkWidget *table;
80         GtkWidget *type;
81         GtkWidget *who;
82         GtkWidget *start;
83         GtkWidget *end;
84         GtkWidget *location;
85         GtkWidget *summary;
86         GtkWidget *description;
87         gchar     *url;
88         GtkWidget *answer;
89         GtkWidget *button;
90         GtkWidget *reedit;
91         GtkWidget *cancel;
92         GtkWidget *uribtn;
93         GtkWidget *attendees;
94         GtkWidget *unavail_box;
95 };
96
97 static GtkActionEntry vcalendar_main_menu[] = {{
98         "Message/CreateMeeting",
99         NULL, N_("Create meeting from message..."), NULL, NULL, G_CALLBACK(create_meeting_from_message_cb_ui)
100 }};
101
102 static void create_meeting_from_message_cb_ui(GtkAction *action, gpointer data)
103 {
104         MainWindow *mainwin = mainwindow_get_mainwindow();
105         SummaryView *summaryview = mainwin->summaryview;
106         GSList *msglist = summary_get_selected_msg_list(summaryview);
107         FolderItem *item = NULL;
108         GSList *cur;
109         gchar *msg;
110         gint total=0;
111
112         if (summary_is_locked(summaryview) || !msglist) {
113                 if (msglist)
114                         g_slist_free(msglist);
115                 return;
116         }
117         total = g_slist_length(msglist);
118         
119         msg = g_strdup_printf(_("You are about to create %d "
120                                        "meetings, one by one. Do you "
121                                        "want to continue?"), 
122                                        total);
123         if (total > 9
124         &&  alertpanel(_("Warning"), msg, GTK_STOCK_CANCEL, "+" GTK_STOCK_YES, NULL)
125             != G_ALERTALTERNATE) {
126                 g_free(msg);
127                 return;
128         }
129         g_free(msg);
130
131         main_window_cursor_wait(summaryview->mainwin);
132         gtk_cmclist_freeze(GTK_CMCLIST(summaryview->ctree));
133         folder_item_update_freeze();
134         inc_lock();
135
136         item = summaryview->folder_item;
137
138         STATUSBAR_PUSH(mainwin, _("Creating meeting..."));
139
140         for (cur = msglist; cur; cur = cur->next) {
141                 MsgInfo *msginfo = procmsg_msginfo_get_full_info((MsgInfo *)cur->data);
142                 VCalEvent *event = NULL;
143                 FILE *fp = NULL;
144
145                 if (MSG_IS_ENCRYPTED(msginfo->flags)) {
146                         fp = procmime_get_first_encrypted_text_content(msginfo);
147                 } else {
148                         fp = procmime_get_first_text_content(msginfo);
149                 }
150                 
151                 if (fp) {
152                         gchar *uid;
153                         time_t t = time(NULL);
154                         time_t t2 = t+3600;
155                         gchar *org = NULL;
156                         gchar *orgname = NULL;
157                         gchar *summary = g_strdup(msginfo->subject ? msginfo->subject:_("no subject"));
158                         gchar *description = file_read_stream_to_str(fp);
159                         gchar *dtstart = g_strdup(icaltime_as_ical_string(icaltime_from_timet_with_zone(t, FALSE, NULL)));
160                         gchar *dtend = g_strdup(icaltime_as_ical_string(icaltime_from_timet_with_zone(t2, FALSE, NULL)));
161                         gchar *recur = NULL;
162                         gchar *tzid = g_strdup("UTC");
163                         gchar *url = NULL;
164                         gint method = ICAL_METHOD_REQUEST;
165                         gint sequence = 1;
166                         PrefsAccount *account = NULL;
167                         
168                         fclose(fp);
169
170                         if (item && item->prefs && item->prefs->enable_default_account)
171                                 account = account_find_from_id(item->prefs->default_account);
172
173                         if (!account) 
174                                 account = account_get_cur_account();
175                         
176                         if (!account)
177                                 goto bail;
178
179                         org = g_strdup(account->address);
180
181                         uid = prefs_account_generate_msgid(account);
182                         
183                         event = vcal_manager_new_event(uid,
184                                         org, NULL, NULL/*location*/, summary, description, 
185                                         dtstart, dtend, recur, tzid, url, method, sequence, 
186                                         ICAL_VTODO_COMPONENT);
187                         g_free(uid);
188                         
189                         /* hack to get default hours */
190                         g_free(event->dtstart);
191                         g_free(event->dtend);
192                         event->dtstart = NULL;
193                         event->dtend = NULL;
194
195                         vcal_meeting_create(event);
196                         vcal_manager_free_event(event);
197                         
198 bail:
199                         g_free(org);
200                         g_free(orgname);
201                         g_free(summary);
202                         g_free(description);
203                         g_free(dtstart);
204                         g_free(dtend);
205                         g_free(recur);
206                         g_free(tzid);
207                         g_free(url);
208                 }
209
210                 procmsg_msginfo_free(&msginfo);
211         }
212
213         statusbar_progress_all(0,0,0);
214         STATUSBAR_POP(mainwin);
215         inc_unlock();
216         folder_item_update_thaw();
217         gtk_cmclist_thaw(GTK_CMCLIST(summaryview->ctree));
218         main_window_cursor_normal(summaryview->mainwin);
219         g_slist_free(msglist);
220 }
221
222 static gchar *get_tmpfile(VCalViewer *vcalviewer)
223 {
224         gchar *tmpfile = NULL;
225         
226         if (!vcalviewer->tmpfile) {
227                 tmpfile = procmime_get_tmp_file_name(vcalviewer->mimeinfo);
228                 debug_print("creating %s\n", tmpfile);
229
230                 if (procmime_get_part(tmpfile, vcalviewer->mimeinfo) < 0) {
231                         g_warning("Can't get mimepart file");   
232                         g_free(tmpfile);
233                         return NULL;
234                 }
235                 vcalviewer->tmpfile = tmpfile;
236         }
237         
238         return vcalviewer->tmpfile;
239 }
240
241 static GtkWidget *vcal_viewer_get_widget(MimeViewer *_mimeviewer)
242 {
243         VCalViewer *vcalviewer = (VCalViewer *) _mimeviewer;
244
245         debug_print("vcal_viewer_get_widget\n");
246         gtk_widget_show_all(vcalviewer->scrolledwin);
247         return vcalviewer->scrolledwin;
248 }
249
250 static void vcal_viewer_clear_viewer(MimeViewer *_mimeviewer)
251 {
252         VCalViewer *vcalviewer = (VCalViewer *) _mimeviewer;
253
254         debug_print("vcal_viewer_clear_viewer\n");
255
256         g_free(vcalviewer->file);
257         vcalviewer->file = NULL;
258         if (vcalviewer->tmpfile) {
259                 debug_print("unlinking %s\n", vcalviewer->tmpfile);
260                 g_unlink(vcalviewer->tmpfile);
261                 g_free(vcalviewer->tmpfile);
262                 vcalviewer->tmpfile = NULL;
263         }
264         vcalviewer->mimeinfo = NULL;
265 }
266
267 static VCalEvent *vcalviewer_get_component(const gchar *file, const gchar *charset)
268 {
269         gchar *compstr = NULL;
270         VCalEvent *event = NULL;
271         FILE *fp;
272         GByteArray *array;
273         gchar buf[BUFSIZ];
274         gint n_read;
275
276         g_return_val_if_fail(file != NULL, NULL);
277
278         if ((fp = g_fopen(file, "rb")) == NULL) {
279                 FILE_OP_ERROR(file, "g_fopen");
280                 return NULL;
281         }
282
283         array = g_byte_array_new();
284
285         while ((n_read = fread(buf, sizeof(gchar), sizeof(buf), fp)) > 0) {
286                 if (n_read < sizeof(buf) && ferror(fp))
287                         break;
288                 g_byte_array_append(array, (guchar *)buf, n_read);
289         }
290
291         if (ferror(fp)) {
292                 FILE_OP_ERROR("file stream", "fread");
293                 g_byte_array_free(array, TRUE);
294                 fclose(fp);
295                 return NULL;
296         }
297
298         buf[0] = '\0';
299         g_byte_array_append(array, (guchar *)buf, 1);
300         compstr = (gchar *)array->data;
301         g_byte_array_free(array, FALSE);
302
303         fclose(fp);     
304
305         if (compstr) {
306                 event = vcal_get_event_from_ical(compstr, charset);
307                 g_free(compstr);
308         }
309         
310         return event;
311 }
312
313 #define GTK_LABEL_SET_TEXT_TRIMMED(label, text) {               \
314         gchar *tmplbl = strretchomp(g_strdup(text));            \
315         gtk_label_set_text(label, tmplbl);                      \
316         g_free(tmplbl);                                         \
317 }
318
319 static void vcalviewer_answer_set_choices(VCalViewer *vcalviewer, VCalEvent *event, icalproperty_method method);
320
321 static void vcalviewer_reset(VCalViewer *vcalviewer) 
322 {
323         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), "-");
324         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), "-");
325         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->location), "-");
326         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->summary), "-");
327         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->description), "-");
328         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), "-");
329         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->end), "-");
330         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->attendees), "-");
331         g_free(vcalviewer->url);
332         vcalviewer->url = NULL;
333         gtk_widget_hide(vcalviewer->uribtn);
334         vcalviewer_answer_set_choices(vcalviewer, NULL, ICAL_METHOD_CANCEL);
335 }
336
337 static void vcalviewer_show_error(VCalViewer *vcalviewer, const gchar *msg)
338 {
339         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), msg);
340 }
341
342 static void vcalviewer_hide_error(VCalViewer *vcalviewer)
343 {
344         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), "-");
345 }
346
347 static void vcalviewer_show_unavailable(VCalViewer *vcalviewer, gboolean visi)
348 {
349         if (visi)
350                 gtk_widget_show_all(vcalviewer->unavail_box);
351         else
352                 gtk_widget_hide(vcalviewer->unavail_box);
353 }
354
355 static void vcalviewer_answer_set_choices(VCalViewer *vcalviewer, VCalEvent *event, icalproperty_method method)
356 {
357         int i = 0;
358         
359         gtk_widget_hide(vcalviewer->reedit);
360         gtk_widget_hide(vcalviewer->cancel);
361         gtk_widget_hide(vcalviewer->answer);
362         gtk_widget_hide(vcalviewer->button);
363
364         for (i = 0; i < 3; i++) 
365                 gtk_combo_box_remove_text(GTK_COMBO_BOX(vcalviewer->answer), 0);
366         
367         vcalviewer_show_unavailable(vcalviewer, FALSE);
368
369         if (method == ICAL_METHOD_REQUEST && event && !event->rec_occurrence) {
370                 PrefsAccount *account = vcal_manager_get_account_from_event(event);
371                 
372                 if (!account)
373                         account = vcal_manager_get_account_from_event(vcalviewer->event);
374
375                 if (!account && event) {
376                         account = account_get_default();
377                         vcal_manager_update_answer(event, account->address, 
378                                         account->name,
379                                         ICAL_PARTSTAT_NEEDSACTION, 
380                                         ICAL_CUTYPE_INDIVIDUAL);
381                 }
382                 if (account) {
383                         gtk_combo_box_append_text(GTK_COMBO_BOX(vcalviewer->answer), _("Accept"));
384                         gtk_combo_box_append_text(GTK_COMBO_BOX(vcalviewer->answer), _("Tentatively accept"));
385                         gtk_combo_box_append_text(GTK_COMBO_BOX(vcalviewer->answer), _("Decline"));
386                         gtk_widget_set_sensitive(vcalviewer->answer, TRUE);
387                         gtk_widget_set_sensitive(vcalviewer->button, TRUE);
388                         gtk_widget_show(vcalviewer->answer);
389                         gtk_widget_show(vcalviewer->button);
390                 } else {
391                         gtk_combo_box_append_text(GTK_COMBO_BOX(vcalviewer->answer), "-");
392                         gtk_widget_set_sensitive(vcalviewer->answer, FALSE);
393                         gtk_widget_set_sensitive(vcalviewer->button, FALSE);
394                 }
395         } else {
396                 gtk_combo_box_append_text(GTK_COMBO_BOX(vcalviewer->answer), "-");
397                 gtk_widget_set_sensitive(vcalviewer->answer, FALSE);
398                 gtk_widget_set_sensitive(vcalviewer->button, FALSE);
399         }
400         gtk_combo_box_set_active(GTK_COMBO_BOX(vcalviewer->answer), 0);
401         
402         if (event && event->method == ICAL_METHOD_REQUEST) {
403                 PrefsAccount *account = vcal_manager_get_account_from_event(event);
404                 gchar *internal_ifb = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
405                                         "vcalendar", G_DIR_SEPARATOR_S, 
406                                         "internal.ifb", NULL);
407                 gchar *myfb = file_read_to_str(internal_ifb);
408                 g_free(internal_ifb);
409                 if (account) {
410                         icalparameter_partstat answer = 
411                                 vcal_manager_get_reply_for_attendee(event, account->address);
412                                 
413                         if (answer == ICAL_PARTSTAT_ACCEPTED)
414                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vcalviewer->answer), 0);
415                         if (answer == ICAL_PARTSTAT_TENTATIVE)
416                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vcalviewer->answer), 1);
417                         if (answer == ICAL_PARTSTAT_DECLINED)
418                                 gtk_combo_box_set_active(GTK_COMBO_BOX(vcalviewer->answer), 2);
419                         if (event->dtstart && event->dtend && myfb && *myfb 
420                         && answer != ICAL_PARTSTAT_ACCEPTED 
421                         && answer != ICAL_PARTSTAT_TENTATIVE) {
422                                 if (!attendee_available(NULL, event->dtstart, event->dtend, myfb))
423                                         vcalviewer_show_unavailable(vcalviewer, TRUE);
424                         }
425                 }
426                 g_free(myfb);
427         }
428
429         g_free(vcalviewer->url);
430         if (event && event->url && *(event->url)) {
431                 vcalviewer->url = g_strdup(event->url);
432                 gtk_widget_show(vcalviewer->uribtn);
433         } else {
434                 vcalviewer->url = NULL;
435                 gtk_widget_hide(vcalviewer->uribtn);
436         }
437 }
438
439 static FolderItem *vcalendar_get_current_item(void)
440 {
441         MainWindow *mainwin = mainwindow_get_mainwindow();
442         FolderItem *item = NULL;
443         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
444         
445         if (mainwin) {
446                 item = mainwin->summaryview->folder_item;
447                 if (item->folder == folder)
448                         return item;
449                 else 
450                         return folder->inbox;
451         } else {
452                 return NULL;
453         }
454 }
455
456 void vcalviewer_display_event (VCalViewer *vcalviewer, VCalEvent *event)
457 {
458         GSList *list = NULL;
459         gchar *attendees = NULL;
460         gboolean mine = FALSE;
461         gchar *label = NULL;
462         gboolean save_evt = FALSE;
463         FolderItem *item = vcalendar_get_current_item();
464         if (!event)
465                 return;
466         if (!vcalviewer)
467                 return;
468
469 /* general */
470         if (event->type == ICAL_VTODO_COMPONENT) {
471
472                 label = g_strjoin(" ", _("You have a Todo item."),
473                                 _("Details follow:"), NULL);
474                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
475
476         } else if (event->method == ICAL_METHOD_REQUEST) {
477
478                 if (account_find_from_address(event->organizer, FALSE)) {
479                         label = g_strjoin(" ", _("You have created a meeting."),
480                                         _("Details follow:"), NULL);
481                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
482                         mine = TRUE;
483                 } else {
484                         label = g_strjoin(" ", _("You have been invited to a meeting."),
485                                                 _("Details follow:"), NULL);
486                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
487                 }
488
489         } else if (event->method == ICAL_METHOD_CANCEL) {
490
491                 label = g_strjoin(" ",
492                                 _("A meeting to which you had been invited has been cancelled."),
493                                 _("Details follow:"), NULL);
494                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
495                 save_evt = TRUE;
496                 vcalendar_refresh_folder_contents(item);
497         } else if (event->method == ICAL_METHOD_REPLY) {
498                 /* don't change label */
499         } else {
500
501                 label = g_strjoin(" ", _("You have been forwarded an appointment."),
502                                 _("Details follow:"), NULL);
503                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
504         }
505
506         g_free(label);
507
508 /* organizer */
509         if (event->orgname && *(event->orgname)
510         &&  event->organizer && *(event->organizer)) {
511                 gchar *addr = g_strconcat(event->orgname, " <", event->organizer, ">", NULL);
512                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), addr);
513                 g_free(addr);
514         } else if (event->organizer && *(event->organizer)) {
515                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), event->organizer);
516         } else {
517                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), "-");
518         }
519
520 /* location */
521         if (event->location && *(event->location)) {
522                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->location), event->location);
523         } else {
524                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->location), "-");
525         }
526
527 /* summary */
528         if (event->summary && *(event->summary)) {
529                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->summary), event->summary);
530         } else {
531                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->summary), "-");
532         }
533
534 /* description */
535         if (event->description && *(event->description)) {
536                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->description), event->description);
537         } else {
538                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->description), "-");
539         }
540
541 /* url */
542         g_free(vcalviewer->url);
543         if (event->url && *(event->url)) {
544                 vcalviewer->url = g_strdup(event->url);
545                 gtk_widget_show(vcalviewer->uribtn);
546         } else {
547                 vcalviewer->url = NULL;
548                 gtk_widget_hide(vcalviewer->uribtn);
549         }
550         
551 /* start */
552         if (event->start && *(event->start)) {
553                 if (event->recur && *(event->recur)) {
554                         gchar *tmp = g_strdup_printf(g_strconcat("%s <span weight=\"bold\">",
555                                                         _("(this event recurs)"),"</span>", NULL),
556                                         event->start);
557                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), tmp);
558                         gtk_label_set_use_markup(GTK_LABEL(vcalviewer->start), TRUE);
559                         g_free(tmp);
560                 } else if (event->rec_occurrence) {
561                         gchar *tmp = g_strdup_printf(g_strconcat("%s <span weight=\"bold\">",
562                                                         _("(this event is part of a recurring event)"),
563                                                         "</span>", NULL),
564                                         event->start);
565                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), tmp);
566                         gtk_label_set_use_markup(GTK_LABEL(vcalviewer->start), TRUE);
567                         g_free(tmp);
568                 } else {
569                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), event->start);
570                 }
571         } else {
572                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), "-");
573         }
574
575 /* end */
576         if (event->end && *(event->end)) {
577                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->end), event->end);
578         } else {
579                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->end), "-");
580         }
581         
582 /* attendees */
583         attendees = g_strdup("");
584         for (list = vcal_manager_get_answers_emails(event); 
585              list && list->data; list = list->next) {
586                 gchar *attendee = (gchar *)list->data;
587                 gchar *name = vcal_manager_get_attendee_name(event, attendee);
588                 gchar *ename = g_markup_printf_escaped("%s", name?name:"");
589                 gchar *eatt = g_markup_printf_escaped("%s", attendee);
590                 icalparameter_partstat acode = vcal_manager_get_reply_for_attendee(event, attendee);
591                 gchar *answer = vcal_manager_get_reply_text_for_attendee(event, attendee);
592                 gchar *type = vcal_manager_get_cutype_text_for_attendee(event, attendee);
593                 gchar *tmp = NULL;
594                 gint e_len, n_len;
595                 tmp = g_strdup_printf(
596                                 "%s%s&lt;%s&gt; (%s, <span%s>%s</span>)", 
597                                 (ename && *(ename))?ename:"",
598                                 (ename && *(ename))?" ":"",
599                                 (eatt && *(eatt))?eatt:"", 
600                                 (type && *(type))?type:"", 
601                                 (acode != ICAL_PARTSTAT_ACCEPTED ? " foreground=\"red\"":""),
602                                 (answer && *(answer))?answer:"");
603                 e_len = strlen(attendees);
604                 n_len = strlen(tmp);
605                 if (e_len) {
606                         attendees = g_realloc(attendees, e_len+n_len+2);
607                         *(attendees+e_len) = '\n';
608                         strcpy(attendees+e_len+1, tmp);
609                 } else {
610                         attendees = g_realloc(attendees, e_len+n_len+1);
611                         strcpy(attendees, tmp);
612                 }
613                 g_free(tmp);
614                 g_free(answer);
615                 g_free(type);
616                 g_free(name);
617                 g_free(ename);
618                 g_free(eatt);
619         }
620         
621         if (attendees && *(attendees)) {
622                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->attendees), attendees);
623         } else {
624                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->attendees), "-");
625         }
626         g_free(attendees);
627         gtk_label_set_use_markup(GTK_LABEL(vcalviewer->attendees), TRUE);
628         
629 /* buttons */
630         if (!mine)
631                 if (event->type != ICAL_VTODO_COMPONENT)
632                         vcalviewer_answer_set_choices(vcalviewer, event, event->method);
633                 else
634                         vcalviewer_answer_set_choices(vcalviewer, event, ICAL_METHOD_PUBLISH);
635         else {
636                 vcalviewer_answer_set_choices(vcalviewer, event, ICAL_METHOD_REPLY);
637                 gtk_widget_show(vcalviewer->reedit);
638                 gtk_widget_show(vcalviewer->cancel);
639         }
640
641 /* save if cancelled */
642         if (save_evt)
643                 vcal_manager_save_event(event, TRUE);
644 }
645
646 gchar *vcalviewer_get_uid_from_mimeinfo(MimeInfo *mimeinfo)
647 {
648         gchar *tmpfile = procmime_get_tmp_file_name(mimeinfo);
649         const gchar *charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
650         gchar *compstr = NULL;
651         gchar *res = NULL;
652         VCalEvent *event = NULL;
653
654         if (!charset)
655                 charset = CS_WINDOWS_1252;
656         
657         if (!strcasecmp(charset, CS_ISO_8859_1))
658                 charset = CS_WINDOWS_1252;
659
660         if (procmime_get_part(tmpfile, mimeinfo) < 0) {
661                 g_warning("Can't get mimepart file");   
662                 g_free(tmpfile);
663                 return NULL;
664         }
665         
666         compstr = file_read_to_str(tmpfile);
667         
668         event = vcal_get_event_from_ical(compstr, charset);
669         if (event)
670                 res = g_strdup(event->uid);
671         
672         vcal_manager_free_event(event);
673
674         debug_print("got uid: %s\n", res);
675         return res;
676
677 }
678
679 static void vcalviewer_get_request_values(VCalViewer *vcalviewer, MimeInfo *mimeinfo, gboolean is_todo) 
680 {
681         VCalEvent *saved_event = NULL;
682         const gchar *charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
683         const gchar *saveme =  procmime_mimeinfo_get_parameter(mimeinfo, "vcalsave");
684         
685         if (!vcalviewer->event)
686                 return;
687
688         if (!charset)
689                 charset = CS_WINDOWS_1252;
690         
691         if (!strcasecmp(charset, CS_ISO_8859_1))
692                 charset = CS_WINDOWS_1252;
693
694         /* see if we have it registered and more recent */
695         saved_event = vcal_manager_load_event(vcalviewer->event->uid);
696         if (saved_event && saved_event->sequence >= vcalviewer->event->sequence) {
697                 charset = CS_INTERNAL;
698                 saved_event->method = vcalviewer->event->method;
699                 vcalviewer_display_event(vcalviewer, saved_event);
700                 vcal_manager_free_event(saved_event);
701                 return;
702         } else if (saved_event) {
703                 vcal_manager_free_event(saved_event);
704         }
705
706         /* load it and register it */
707
708         if (!saveme || strcmp(saveme,"no"))
709                 vcal_manager_save_event(vcalviewer->event, FALSE);
710
711         vcalviewer_display_event(vcalviewer, vcalviewer->event);
712 }
713
714 static void vcalviewer_get_reply_values(VCalViewer *vcalviewer, MimeInfo *mimeinfo) 
715 {
716         const gchar *charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
717         VCalEvent *saved_event = NULL;
718         gchar *attendee = NULL, *label = NULL;
719         Answer *answer = NULL;
720
721         if (!vcalviewer->event)
722                 return;
723
724         if (!charset)
725                 charset = CS_WINDOWS_1252;
726         
727         if (!strcasecmp(charset, CS_ISO_8859_1))
728                 charset = CS_WINDOWS_1252;
729
730         if (!vcalviewer->event->answers || g_slist_length(vcalviewer->event->answers) > 1) {
731                 g_warning("strange, no answers or more than one");
732         } 
733         
734         if (vcalviewer->event->answers) {
735                 answer = (Answer *)vcalviewer->event->answers->data;
736                 attendee = g_strdup(answer->attendee);
737         }
738                 
739
740         if (!answer) {
741                 label = g_strjoin(" ",
742                         _("You have received an answer to an unknown meeting proposal."),
743                         _("Details follow:"), NULL);
744                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
745         } else {
746                 label = g_strdup_printf(_("You have received an answer to a meeting proposal.\n"
747                         "%s has %s the invitation whose details follow:"),
748                         attendee, vcal_manager_answer_get_text(answer->answer));
749                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->type), label);
750                 g_free(attendee);
751         }
752         g_free(label);
753
754         saved_event = vcal_manager_load_event(vcalviewer->event->uid);
755         if (saved_event && answer) {
756                 vcal_manager_update_answer(saved_event, 
757                         answer->attendee, answer->name, answer->answer, answer->cutype);
758                 vcal_manager_save_event(saved_event, TRUE);
759                 saved_event->method = vcalviewer->event->method;
760                 vcalviewer_display_event(vcalviewer, saved_event);
761                 vcal_manager_free_event(saved_event);
762                 return;
763         }
764
765         if (vcalviewer->event->organizer) {
766                 if (vcalviewer->event->orgname) {
767                         gchar *addr = g_strconcat(vcalviewer->event->orgname, " <", 
768                                         vcalviewer->event->organizer, ">", NULL);
769                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), addr);
770                         g_free(addr);
771                 } else {
772                         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who),
773                                 vcalviewer->event->organizer);
774                 }
775         } else {
776                 GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->who), "-");
777         }
778         
779         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->location),
780                 vcalviewer->event->location?vcalviewer->event->location:"-");
781
782         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->summary), 
783                 vcalviewer->event->summary?vcalviewer->event->summary:"-");
784
785         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->description), 
786                 vcalviewer->event->description?vcalviewer->event->description:"-");
787
788         g_free(vcalviewer->url);
789         if (vcalviewer->event->url) {
790                 vcalviewer->url = g_strdup(vcalviewer->event->url);
791                 gtk_widget_show(vcalviewer->uribtn);
792         } else {
793                 vcalviewer->url = NULL;
794                 gtk_widget_hide(vcalviewer->uribtn);
795         }
796
797         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->start), 
798                 vcalviewer->event->start?vcalviewer->event->start:"-");
799         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->end), 
800                 vcalviewer->event->end?vcalviewer->event->end:"-");
801
802         GTK_LABEL_SET_TEXT_TRIMMED(GTK_LABEL(vcalviewer->attendees), "-"); 
803
804         vcalviewer_answer_set_choices(vcalviewer, NULL, ICAL_METHOD_REPLY);
805 }
806
807 static void vcalviewer_get_event(VCalViewer *vcalviewer, MimeInfo *mimeinfo) 
808 {
809         gchar *tmpfile = get_tmpfile(vcalviewer);
810         const gchar *charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
811
812         if (!charset)
813                 charset = CS_WINDOWS_1252;
814         
815         if (!strcasecmp(charset, CS_ISO_8859_1))
816                 charset = CS_WINDOWS_1252;
817         
818         if (vcalviewer->event) {
819                 vcal_manager_free_event(vcalviewer->event);
820                 vcalviewer->event = NULL;
821         }
822         
823         if (!tmpfile) {
824                 vcalviewer_reset(vcalviewer);
825                 vcalviewer_show_error(vcalviewer, _("Error - could not get the calendar MIME part."));
826                 return;
827         }
828
829         vcalviewer->event = vcalviewer_get_component(tmpfile, charset);
830         if (!vcalviewer->event) {
831                 vcalviewer_reset(vcalviewer);
832                 vcalviewer_show_error(vcalviewer, _("Error - no calendar part found."));
833                 return;
834         }
835         
836         if (vcalviewer->event->type == ICAL_VTODO_COMPONENT) {
837                 vcalviewer_get_request_values(vcalviewer, mimeinfo, TRUE);
838         } else if (vcalviewer->event->method == ICAL_METHOD_REQUEST ||
839                    vcalviewer->event->method == ICAL_METHOD_PUBLISH ||
840                    vcalviewer->event->method == ICAL_METHOD_CANCEL) {
841                 vcalviewer_get_request_values(vcalviewer, mimeinfo, FALSE);
842         } else if (vcalviewer->event->method == ICAL_METHOD_REPLY) {
843                 vcalviewer_get_reply_values(vcalviewer, mimeinfo);
844         } else {
845                 vcalviewer_reset(vcalviewer);
846                 vcalviewer_show_error(vcalviewer, _("Error - Unknown calendar component type."));
847         }
848 }
849
850 static VCalViewer *s_vcalviewer = NULL;
851
852 static void vcal_viewer_show_mimepart(MimeViewer *_mimeviewer, const gchar *file, MimeInfo *mimeinfo)
853 {
854         VCalViewer *vcalviewer = (VCalViewer *) _mimeviewer;
855         START_TIMING("");
856
857         s_vcalviewer = vcalviewer;
858
859         if (mimeinfo == NULL) {
860                 vcal_viewer_clear_viewer(_mimeviewer);
861                 END_TIMING();
862                 return;
863         }
864         debug_print("vcal_viewer_show_mimepart : %s\n", file);
865
866         vcal_viewer_clear_viewer(_mimeviewer);
867         gtk_widget_show_all(vcalviewer->scrolledwin);
868         g_free(vcalviewer->file);
869         vcalviewer->file = g_strdup(file);
870         vcalviewer->mimeinfo = mimeinfo;
871         vcalviewer_hide_error(vcalviewer);
872         vcalviewer_get_event(vcalviewer, mimeinfo);
873         GTK_EVENTS_FLUSH();
874         gtk_widget_set_size_request(vcalviewer->description, 
875                 vcalviewer->scrolledwin->allocation.width - 200, -1);
876         gtk_label_set_line_wrap(GTK_LABEL(vcalviewer->location), TRUE);
877         gtk_label_set_line_wrap(GTK_LABEL(vcalviewer->summary), TRUE);
878         gtk_label_set_line_wrap(GTK_LABEL(vcalviewer->description), TRUE);
879         gtk_label_set_line_wrap(GTK_LABEL(vcalviewer->attendees), FALSE);
880         
881         if (prefs_common_get_prefs()->textfont) {
882                 PangoFontDescription *font_desc;
883
884                 font_desc = pango_font_description_from_string
885                                                 (prefs_common_get_prefs()->textfont);
886                 if (font_desc) {
887                         gtk_widget_modify_font(
888                                 vcalviewer->description, font_desc);
889                         pango_font_description_free(font_desc);
890                 }
891         }
892         END_TIMING();   
893 }
894
895 void vcalviewer_reload(FolderItem *item)
896 {
897         if (s_vcalviewer) {
898                 MainWindow *mainwin = mainwindow_get_mainwindow();
899                 Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
900
901                 folder_item_scan(item);
902                 if (mainwin && mainwin->summaryview->folder_item) {
903                         FolderItem *cur = mainwin->summaryview->folder_item;
904                         if (cur->folder == folder)
905                                 folder_item_scan(cur);
906                 }
907                 if (mainwin && mainwin->summaryview->folder_item == item) {
908                         debug_print("reload: %p, %p\n", (MimeViewer *)s_vcalviewer, s_vcalviewer->mimeinfo);
909                         summary_redisplay_msg(mainwin->summaryview);
910                 }
911         }
912 }
913
914 static void vcal_viewer_destroy_viewer(MimeViewer *_mimeviewer)
915 {
916         VCalViewer *vcalviewer = (VCalViewer *) _mimeviewer;
917
918         debug_print("vcal_viewer_destroy_viewer\n");
919
920         if (s_vcalviewer == vcalviewer)
921                 s_vcalviewer = NULL;
922         vcal_viewer_clear_viewer(_mimeviewer);
923         g_free(vcalviewer);
924 }
925
926 static gboolean vcalviewer_reedit_cb(GtkButton *widget, gpointer data)
927 {
928         VCalViewer *vcalviewer = (VCalViewer *)data;
929         gchar * uid = vcalviewer_get_uid_from_mimeinfo(vcalviewer->mimeinfo);
930         VCalEvent *event = NULL;
931         
932         s_vcalviewer = vcalviewer;
933         /* see if we have it registered and more recent */
934         event = vcal_manager_load_event(uid);
935         vcal_meeting_create(event);
936         vcal_manager_free_event(event);
937         return TRUE;
938 }
939
940 static gboolean vcalviewer_uribtn_cb(GtkButton *widget, gpointer data)
941 {
942         VCalViewer *vcalviewer = (VCalViewer *)data;
943
944         if (vcalviewer->url)
945                 open_uri(vcalviewer->url, prefs_common_get_uri_cmd());
946
947         return TRUE;
948 }
949
950 void vcalendar_refresh_folder_contents(FolderItem *item)
951 {
952         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
953         if (folder && item->folder == folder) {
954                 MainWindow *mainwin = mainwindow_get_mainwindow();
955                 folder_item_scan(item);
956                 if (mainwin->summaryview->folder_item == item) {
957                         summary_show(mainwin->summaryview, item);
958                 }
959         }
960 }
961
962 static void send_cancel_notify_toggled_cb(GtkToggleButton *button, gboolean *data)
963 {
964         *data = gtk_toggle_button_get_active(button);
965 }
966
967 void vcalendar_cancel_meeting(FolderItem *item, const gchar *uid)
968 {
969         VCalEvent *event = NULL;
970         VCalMeeting *meet = NULL;
971         gchar *file = NULL;
972         gint val = 0;
973         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
974         gboolean redisp = FALSE;
975         GtkWidget *send_notify_chkbtn = gtk_check_button_new_with_label(_("Send a notification to the attendees"));
976         gboolean send_notify = TRUE;
977
978         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(send_notify_chkbtn), TRUE);
979         gtk_widget_show(send_notify_chkbtn);
980         g_signal_connect(G_OBJECT(send_notify_chkbtn), "toggled", 
981                          G_CALLBACK(send_cancel_notify_toggled_cb),
982                          &send_notify);
983
984         val = alertpanel_full(_("Cancel meeting"),
985                                    _("Are you sure you want to cancel this meeting?"),
986                                    GTK_STOCK_NO, GTK_STOCK_YES, NULL, FALSE,
987                                    send_notify_chkbtn, ALERT_WARNING, G_ALERTDEFAULT);
988
989         if (val != G_ALERTALTERNATE)
990                 return;
991
992         event = vcal_manager_load_event(uid);
993         if (!event)
994                 return;
995         event->method = ICAL_METHOD_CANCEL;
996         
997         if (folder) {
998                 MainWindow *mainwin = mainwindow_get_mainwindow();
999                 if (mainwin->summaryview->folder_item == item) {
1000                         redisp = TRUE;
1001                         summary_show(mainwin->summaryview, NULL);
1002                 }
1003         }
1004         
1005         if (send_notify) {
1006                 meet = vcal_meeting_create_hidden(event);
1007                 if (!vcal_meeting_send(meet)) {
1008                         event->method = ICAL_METHOD_REQUEST;
1009                         vcal_manager_save_event(event, TRUE);
1010                         vcal_manager_free_event(event);
1011                         if (folder)
1012                                 folder_item_scan(item);
1013
1014                         if (folder && redisp) {
1015                                 MainWindow *mainwin = mainwindow_get_mainwindow();
1016                                 summary_show(mainwin->summaryview, item);
1017                         }
1018                         return;
1019                 }
1020         }
1021
1022         vcal_manager_save_event(event, TRUE);
1023         
1024         file = vcal_manager_get_event_file(event->uid);
1025         g_unlink(file);
1026         vcal_manager_free_event(event);
1027         g_free(file);
1028         if (folder)
1029                 folder_item_scan(item);
1030         if (folder && redisp) {
1031                 MainWindow *mainwin = mainwindow_get_mainwindow();
1032                 summary_show(mainwin->summaryview, item);
1033         }
1034
1035         return;
1036 }
1037
1038 static gboolean vcalviewer_cancel_cb(GtkButton *widget, gpointer data)
1039 {
1040         VCalViewer *vcalviewer = (VCalViewer *)data;
1041         FolderItem *item = vcalendar_get_current_item();
1042         gchar * uid = vcalviewer_get_uid_from_mimeinfo(vcalviewer->mimeinfo);
1043         vcalendar_cancel_meeting(item, uid);
1044         return TRUE;
1045 }
1046
1047 static gboolean vcalviewer_action_cb(GtkButton *widget, gpointer data)
1048 {
1049         VCalViewer *vcalviewer = (VCalViewer *)data;
1050         gint index = gtk_combo_box_get_active(GTK_COMBO_BOX(vcalviewer->answer));
1051         icalparameter_partstat reply[3] = {ICAL_PARTSTAT_ACCEPTED, ICAL_PARTSTAT_TENTATIVE, ICAL_PARTSTAT_DECLINED};
1052         PrefsAccount *account = NULL;
1053         VCalEvent *saved_event = NULL, *event = NULL;
1054         debug_print("index chosen %d\n", index);
1055         
1056         if (index < 0 || index > 2) {
1057                 return TRUE;
1058         }
1059         
1060         s_vcalviewer = vcalviewer;
1061         
1062         if (!vcalviewer->event) {
1063                 g_warning("can't get event");
1064                 return TRUE;
1065         }
1066
1067 /* see if we have it registered and more recent */
1068         event = vcalviewer->event;
1069         saved_event = vcal_manager_load_event(vcalviewer->event->uid);
1070         if (saved_event && saved_event->sequence >= vcalviewer->event->sequence) {
1071                 saved_event->method = vcalviewer->event->method;
1072                 event = saved_event;
1073         } else if (saved_event) {
1074                 vcal_manager_free_event(saved_event);
1075                 saved_event = NULL;
1076         }
1077         account = vcal_manager_get_account_from_event(event);
1078         
1079         if (!account) {
1080                 AlertValue val = alertpanel_full(_("No account found"), 
1081                                         _("You have no account matching any attendee.\n"
1082                                             "Do you want to reply anyway?"),
1083                                         GTK_STOCK_CANCEL, g_strconcat("+", _("Reply anyway"), NULL),
1084                                         NULL, FALSE, NULL, ALERT_QUESTION, G_ALERTDEFAULT);
1085                 if (val == G_ALERTALTERNATE) {          
1086                         account = account_get_default();
1087                         vcal_manager_update_answer(event, account->address, 
1088                                         account->name,
1089                                         ICAL_PARTSTAT_NEEDSACTION, 
1090                                         ICAL_CUTYPE_INDIVIDUAL);
1091                 } else {
1092                         if (saved_event)
1093                                 vcal_manager_free_event(saved_event);
1094                         return TRUE;
1095                 }
1096         }
1097         
1098         vcal_manager_update_answer(event, account->address, account->name, reply[index], 0);
1099         
1100         if (event->organizer && *(event->organizer) && 
1101             !vcal_manager_reply(account, event)) {
1102                 g_warning("couldn't send reply");
1103         } else {
1104                 debug_print("no organizer, not sending answer\n");
1105         }
1106         
1107         vcal_manager_save_event(event, TRUE);
1108
1109         vcalviewer_display_event(vcalviewer, event);
1110         if (saved_event)
1111                 vcal_manager_free_event(saved_event);
1112         return TRUE;
1113 };
1114
1115 #define TABLE_ADD_LINE(label_text, widget) {                            \
1116         gchar *tmpstr = g_strdup_printf("<span weight=\"bold\">%s</span>",\
1117                                 label_text);                            \
1118         GtkWidget *label = gtk_label_new(tmpstr);                       \
1119         g_free(tmpstr);                                                 \
1120         gtk_label_set_use_markup (GTK_LABEL (label), TRUE);             \
1121         gtk_misc_set_alignment (GTK_MISC(label), 1, 0);                 \
1122         gtk_table_attach (GTK_TABLE (vcalviewer->table),                \
1123                           label, 0, 1, i, i+1,                          \
1124                           GTK_FILL, GTK_FILL, 6, 6);                    \
1125         gtk_table_attach (GTK_TABLE (vcalviewer->table),                \
1126                           widget, 1, 2, i, i+1,                         \
1127                           GTK_FILL, GTK_FILL, 6, 6);                    \
1128         if (GTK_IS_LABEL(widget)) {                                     \
1129                 gtk_label_set_use_markup(GTK_LABEL (widget), TRUE);     \
1130                 gtk_misc_set_alignment (GTK_MISC(widget),0, 0);         \
1131                 gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);       \
1132         }                                                               \
1133         i++;                                                            \
1134 }
1135
1136 static gchar *vcal_viewer_get_selection(MimeViewer *_viewer)
1137 {
1138         VCalViewer *viewer = (VCalViewer *)_viewer;
1139         if (viewer->summary == NULL)
1140                 return NULL;
1141         if (gtk_label_get_text(GTK_LABEL(viewer->description))
1142         &&  *(gtk_label_get_text(GTK_LABEL(viewer->description))) > 2) {
1143                 gint start, end;
1144                 if (gtk_label_get_selection_bounds(GTK_LABEL(viewer->description), 
1145                                                    &start, &end)) {
1146                         gchar *tmp = g_strdup(gtk_label_get_text(
1147                                                 GTK_LABEL(viewer->description))+start);
1148                         tmp[end-start] = '\0';
1149                         return tmp;
1150                 } else {
1151                         return g_strdup(gtk_label_get_text(GTK_LABEL(viewer->description)));
1152                 }
1153         }
1154         else if (gtk_label_get_text(GTK_LABEL(viewer->summary))
1155         &&  *(gtk_label_get_text(GTK_LABEL(viewer->summary))) > 2)
1156                 return g_strdup(gtk_label_get_text(GTK_LABEL(viewer->summary)));
1157         else 
1158                 return NULL;
1159 }
1160
1161
1162 static gboolean vcal_viewer_scroll_page(MimeViewer *_viewer, gboolean up)
1163 {
1164         VCalViewer *viewer = (VCalViewer *)_viewer;
1165         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1166                 GTK_SCROLLED_WINDOW(viewer->scrolledwin));
1167         
1168         return gtkutils_scroll_page(viewer->table, vadj, up);
1169 }
1170
1171 static void vcal_viewer_scroll_one_line(MimeViewer *_viewer, gboolean up)
1172 {
1173         VCalViewer *viewer = (VCalViewer *)_viewer;
1174         GtkAdjustment *vadj = gtk_scrolled_window_get_vadjustment(
1175                 GTK_SCROLLED_WINDOW(viewer->scrolledwin));
1176         
1177         gtkutils_scroll_one_line(viewer->table, vadj, up);
1178 }
1179
1180 MimeViewer *vcal_viewer_create(void)
1181 {
1182         VCalViewer *vcalviewer;
1183         int i = 0;
1184         GtkWidget *hbox = NULL, *vbox = NULL;
1185         GtkWidget *warning_img;
1186         GtkWidget *warning_label;
1187         
1188         debug_print("Creating vcal view...\n");
1189         vcalviewer = g_new0(VCalViewer, 1);
1190         vcalviewer->mimeviewer.factory = &vcal_viewer_factory;
1191
1192         vcalviewer->mimeviewer.get_widget = vcal_viewer_get_widget;
1193         vcalviewer->mimeviewer.show_mimepart = vcal_viewer_show_mimepart;
1194         vcalviewer->mimeviewer.clear_viewer = vcal_viewer_clear_viewer;
1195         vcalviewer->mimeviewer.destroy_viewer = vcal_viewer_destroy_viewer;
1196         vcalviewer->mimeviewer.get_selection = vcal_viewer_get_selection;
1197         vcalviewer->mimeviewer.scroll_page = vcal_viewer_scroll_page;
1198         vcalviewer->mimeviewer.scroll_one_line = vcal_viewer_scroll_one_line;
1199
1200         vcalviewer->table = gtk_table_new(8, 2, FALSE);
1201         vcalviewer->type = gtk_label_new("meeting");
1202         vcalviewer->who = gtk_label_new("who");
1203         vcalviewer->start = gtk_label_new("start");
1204         vcalviewer->end = gtk_label_new("end");
1205         vcalviewer->location = gtk_label_new("location");
1206         vcalviewer->summary = gtk_label_new("summary");
1207         vcalviewer->description = gtk_label_new("description");
1208         vcalviewer->attendees = gtk_label_new("attendees");
1209
1210         vcalviewer->answer = gtk_combo_box_new_text();
1211         vcalviewer->url = NULL;
1212         vcalviewer->button = gtk_button_new_with_label(_("Answer"));
1213         vcalviewer->reedit = gtk_button_new_with_label(_("Edit meeting..."));
1214         vcalviewer->cancel = gtk_button_new_with_label(_("Cancel meeting..."));
1215         vcalviewer->uribtn = gtk_button_new_with_label(_("Launch website"));
1216         vcalviewer->unavail_box = gtk_hbox_new(FALSE, 6);
1217         warning_img = gtk_image_new_from_stock
1218                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1219         warning_label = gtk_label_new(_("You are already busy at this time."));
1220
1221         gtk_box_pack_start(GTK_BOX(vcalviewer->unavail_box), warning_img, FALSE, FALSE, 0);
1222         gtk_box_pack_start(GTK_BOX(vcalviewer->unavail_box), warning_label, FALSE, FALSE, 0);
1223         
1224         hbox = gtk_hbox_new(FALSE, 6);
1225         gtk_box_pack_start(GTK_BOX(hbox), vcalviewer->answer, FALSE, FALSE, 0);
1226         gtk_box_pack_start(GTK_BOX(hbox), vcalviewer->button, FALSE, FALSE, 0);
1227         gtk_box_pack_start(GTK_BOX(hbox), vcalviewer->reedit, FALSE, FALSE, 0);
1228         gtk_box_pack_start(GTK_BOX(hbox), vcalviewer->cancel, FALSE, FALSE, 0);
1229         gtk_box_pack_start(GTK_BOX(hbox), vcalviewer->uribtn, FALSE, FALSE, 0);
1230         
1231         vbox = gtk_vbox_new(FALSE, 6);
1232         gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
1233         gtk_box_pack_start(GTK_BOX(vbox), vcalviewer->unavail_box, FALSE, FALSE, 0);
1234
1235         vcalviewer_answer_set_choices(vcalviewer, NULL, ICAL_METHOD_REQUEST);
1236
1237         gtk_label_set_selectable(GTK_LABEL(vcalviewer->type), TRUE);
1238         gtk_label_set_selectable(GTK_LABEL(vcalviewer->who), TRUE);
1239         gtk_label_set_selectable(GTK_LABEL(vcalviewer->start), TRUE);
1240         gtk_label_set_selectable(GTK_LABEL(vcalviewer->end), TRUE);
1241         gtk_label_set_selectable(GTK_LABEL(vcalviewer->location), TRUE);
1242         gtk_label_set_selectable(GTK_LABEL(vcalviewer->summary), TRUE);
1243         gtk_label_set_selectable(GTK_LABEL(vcalviewer->description), TRUE);
1244         gtk_label_set_selectable(GTK_LABEL(vcalviewer->attendees), TRUE);
1245
1246         g_signal_connect(G_OBJECT(vcalviewer->button), "clicked",
1247                          G_CALLBACK(vcalviewer_action_cb), vcalviewer);
1248
1249         g_signal_connect(G_OBJECT(vcalviewer->reedit), "clicked",
1250                          G_CALLBACK(vcalviewer_reedit_cb), vcalviewer);
1251
1252         g_signal_connect(G_OBJECT(vcalviewer->cancel), "clicked",
1253                          G_CALLBACK(vcalviewer_cancel_cb), vcalviewer);
1254
1255         g_signal_connect(G_OBJECT(vcalviewer->uribtn), "clicked",
1256                          G_CALLBACK(vcalviewer_uribtn_cb), vcalviewer);
1257
1258         TABLE_ADD_LINE(_("Event:"), vcalviewer->type);
1259         TABLE_ADD_LINE(_("Organizer:"), vcalviewer->who);
1260         TABLE_ADD_LINE(_("Location:"), vcalviewer->location);
1261         TABLE_ADD_LINE(_("Summary:"), vcalviewer->summary);
1262         TABLE_ADD_LINE(_("Starting:"), vcalviewer->start);
1263         TABLE_ADD_LINE(_("Ending:"), vcalviewer->end);
1264         TABLE_ADD_LINE(_("Description:"), vcalviewer->description);
1265         TABLE_ADD_LINE(_("Attendees:"), vcalviewer->attendees);
1266         gtk_label_set_line_wrap(GTK_LABEL(vcalviewer->attendees), FALSE);
1267         TABLE_ADD_LINE(_("Action:"), vbox);
1268         
1269         vcalviewer->scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1270         gtk_scrolled_window_add_with_viewport(
1271                 GTK_SCROLLED_WINDOW(vcalviewer->scrolledwin), 
1272                 vcalviewer->table);
1273         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(vcalviewer->scrolledwin),
1274                                        GTK_POLICY_NEVER,
1275                                        GTK_POLICY_AUTOMATIC);
1276         
1277         gtk_widget_show_all(vcalviewer->scrolledwin);
1278         return (MimeViewer *) vcalviewer;
1279 }
1280
1281 static gchar *content_types[] =
1282         {"text/calendar", NULL};
1283         
1284 MimeViewerFactory vcal_viewer_factory =
1285 {
1286         content_types,
1287         0,
1288         vcal_viewer_create,
1289 };
1290
1291 static gint alert_timeout_tag = 0;
1292 static gint scan_timeout_tag = 0;
1293
1294 static gboolean vcal_webcal_check(gpointer data)
1295 {
1296         Folder *root = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1297
1298         if (prefs_common_get_prefs()->work_offline)
1299                 return TRUE;
1300         
1301         manual_update = FALSE;
1302         folderview_check_new(root);
1303         manual_update = TRUE;
1304         return TRUE;
1305 }
1306
1307 static guint context_menu_id = 0;
1308 static guint main_menu_id = 0;
1309
1310 void vcalendar_init(void)
1311 {
1312         MainWindow *mainwin = mainwindow_get_mainwindow();
1313         Folder *folder = NULL;
1314         gchar *directory = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1315                                 "vcalendar", NULL);
1316         START_TIMING("");
1317         if (!is_dir_exist(directory) && make_dir (directory) != 0) {
1318                 g_free(directory);
1319                 return;
1320         }
1321
1322         g_free(directory);
1323
1324         vcal_prefs_init();
1325
1326         mimeview_register_viewer_factory(&vcal_viewer_factory);
1327         folder_register_class(vcal_folder_get_class());
1328
1329         folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1330         if (!folder) {
1331                 START_TIMING("creating folder");
1332                 folder = folder_new(vcal_folder_get_class(), PLUGIN_NAME, NULL);
1333                 folder->klass->create_tree(folder);
1334                 folder_add(folder);
1335                 folder_scan_tree(folder, TRUE);
1336                 END_TIMING();
1337         }
1338
1339         if (!folder->inbox) {
1340                 folder->klass->create_tree(folder);
1341                 folder_scan_tree(folder, TRUE);
1342         }
1343         if (folder->klass->scan_required(folder, folder->inbox)) {
1344                 START_TIMING("scanning folder");
1345                 folder_item_scan(folder->inbox);
1346                 END_TIMING();
1347         }
1348         
1349         vcal_folder_gtk_init();
1350
1351         alert_timeout_tag = g_timeout_add(60*1000, 
1352                                 (GSourceFunc)vcal_meeting_alert_check, 
1353                                 (gpointer)NULL);
1354         scan_timeout_tag = g_timeout_add(3600*1000, 
1355                                 (GSourceFunc)vcal_webcal_check, 
1356                                 (gpointer)NULL);
1357         if (prefs_common_get_prefs()->enable_color) {
1358                 gtkut_convert_int_to_gdk_color(prefs_common_get_prefs()->color[COL_URI],
1359                                        &uri_color);
1360         }
1361
1362         gtk_action_group_add_actions(mainwin->action_group, vcalendar_main_menu,
1363                         1, (gpointer)mainwin);
1364         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menu/Message", "CreateMeeting", 
1365                           "Message/CreateMeeting", GTK_UI_MANAGER_MENUITEM,
1366                           main_menu_id)
1367         MENUITEM_ADDUI_ID_MANAGER(mainwin->ui_manager, "/Menus/SummaryViewPopup", "CreateMeeting", 
1368                           "Message/CreateMeeting", GTK_UI_MANAGER_MENUITEM,
1369                           context_menu_id)
1370         END_TIMING();
1371 }
1372
1373 void vcalendar_done(void)
1374 {
1375         MainWindow *mainwin = mainwindow_get_mainwindow();
1376         FolderView *folderview = NULL;
1377         FolderItem *fitem = NULL;
1378
1379         icalmemory_free_ring();
1380
1381         vcal_folder_free_data();
1382
1383         if (mainwin == NULL)
1384                 return;
1385
1386         folderview = mainwin->folderview;
1387         fitem = folderview->summaryview->folder_item;
1388
1389         if (fitem && 
1390             fitem->folder->klass == vcal_folder_get_class()) {
1391                 folderview_unselect(folderview);
1392                 summary_clear_all(folderview->summaryview);
1393                 if (fitem->folder->klass->item_closed)
1394                         fitem->folder->klass->item_closed(fitem);
1395         }
1396
1397         mimeview_unregister_viewer_factory(&vcal_viewer_factory);
1398         folder_unregister_class(vcal_folder_get_class());
1399         vcal_folder_gtk_done();
1400         vcal_prefs_done();
1401         g_source_remove(alert_timeout_tag);
1402         alert_timeout_tag = 0;
1403         g_source_remove(scan_timeout_tag);
1404         scan_timeout_tag = 0;
1405
1406         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/CreateMeeting", main_menu_id);
1407         main_menu_id = 0;
1408         MENUITEM_REMUI_MANAGER(mainwin->ui_manager,mainwin->action_group, "Message/CreateMeeting", context_menu_id);
1409         context_menu_id = 0;
1410 }