4c63245c0f94f6d73e4d63252878e6b5986adeb2
[claws.git] / src / plugins / vcalendar / vcal_meeting_gtk.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2013 Colin Leroy <colin@colino.net> and 
4  * the Claws Mail team
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #include "claws-features.h"
24 #endif
25
26 #include <stddef.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29
30 #include "defs.h"
31
32 #ifdef USE_PTHREAD
33 #include <pthread.h>
34 #endif
35 #include <libical/ical.h>
36 #include <gtk/gtk.h>
37 #include <gdk/gdkkeysyms.h>
38 #include <curl/curl.h>
39 #include <curl/curlver.h>
40 #include "combobox.h"
41
42 #include "vcalendar.h"
43 #include "vcal_folder.h"
44 #include "vcal_manager.h"
45 #include "vcal_meeting_gtk.h"
46 #include "vcal_prefs.h"
47 #include "common-views.h"
48 #include "mainwindow.h"
49 #include "prefs_account.h"
50 #include "account.h"
51 #include "filesel.h"
52 #include "alertpanel.h"
53 #include "addr_compl.h"
54 #include "gtkutils.h"
55 #include "log.h"
56 #include "utils.h"
57
58 struct _VCalMeeting
59 {
60         gchar     *uid;
61         gint       sequence;
62         gint       method;
63         GtkWidget *window;
64 #ifndef GENERIC_UMPC
65         GtkWidget *table;
66 #else
67         GtkWidget *table1;
68         GtkWidget *table2;
69 #endif
70         GtkWidget *type;
71         GtkWidget *who;
72         GtkWidget *avail_evtbox;
73         GtkWidget *avail_img;
74         GtkWidget *start_c;
75         GtkWidget *start_time;
76         GtkWidget *end_c;
77         GtkWidget *end_time;
78         GtkWidget *location;
79         GtkWidget *summary;
80         GtkWidget *description;
81         GSList    *attendees;
82         GtkWidget *attendees_vbox;
83         GtkWidget *save_btn;
84         GtkWidget *avail_btn;
85         GSList    *avail_accounts;
86         GtkWidget *total_avail_evtbox;
87         GtkWidget *total_avail_img;
88         GtkWidget *total_avail_msg;
89         PrefsAccount *account;
90         gboolean visible;
91 };
92
93 struct _VCalAttendee {
94         GtkWidget *address;
95         GtkWidget *remove_btn;
96         GtkWidget *add_btn;
97         GtkWidget *cutype;
98         GtkWidget *hbox;
99         VCalMeeting *meet;
100         gchar *status;
101         GtkWidget *avail_evtbox;
102         GtkWidget *avail_img;
103         gchar *cached_contents;
104         gboolean org;
105 };
106
107 static GdkCursor *watch_cursor = NULL;
108
109 VCalAttendee *attendee_add(VCalMeeting *meet, gchar *address, gchar *name, gchar *partstat, gchar *cutype, gboolean first);
110
111 #ifndef GENERIC_UMPC
112 #define TABLE_ADD_LINE(label_text, widget, do_space) {                          \
113         gchar *tmpstr = g_strdup_printf("<span weight=\"bold\">%s</span>",      \
114                                 label_text?label_text:"");                      \
115         GtkWidget *label = NULL;                                                \
116         GtkWidget *spacer = NULL;                                               \
117         GtkWidget *s_hbox = NULL;                                               \
118         if (do_space) {                                                         \
119                 spacer = gtk_label_new("");                                     \
120                 gtk_widget_set_size_request(spacer, 18, 16);                            \
121                 s_hbox = gtk_hbox_new(FALSE, 6);                                \
122                 gtk_box_pack_start(GTK_BOX(s_hbox), spacer, FALSE, FALSE, 0);   \
123                 gtk_box_pack_start(GTK_BOX(s_hbox), widget, TRUE, TRUE, 0);     \
124         }                                                                       \
125         if (label_text) {                                                       \
126                 label = gtk_label_new(tmpstr);                                  \
127                 g_free(tmpstr);                                                 \
128                 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);             \
129                 gtk_misc_set_alignment (GTK_MISC(label), 1, 0.5);               \
130                 gtk_table_attach (GTK_TABLE (meet->table),                      \
131                                   label, 0, 1, i, i+1,                          \
132                                   GTK_FILL, GTK_FILL, 6, 6);                    \
133                 gtk_table_attach (GTK_TABLE (meet->table),                      \
134                                   do_space?s_hbox:widget, 1, 2, i, i+1,         \
135                                   GTK_FILL|GTK_EXPAND, GTK_FILL, 6, 6);         \
136                 if (GTK_IS_LABEL(widget)) {                                     \
137                         gtk_label_set_use_markup(GTK_LABEL (widget), TRUE);     \
138                         gtk_misc_set_alignment (GTK_MISC(widget),0, 0);         \
139                         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);       \
140                 }                                                               \
141         } else {                                                                \
142                 g_free(tmpstr);                                                 \
143                 gtk_table_attach (GTK_TABLE (meet->table),                      \
144                                   do_space?s_hbox:widget, 0, 2, i, i+1,         \
145                                   GTK_FILL|GTK_EXPAND, GTK_FILL, 6, 6);         \
146         }                                                                       \
147         i++;                                                                    \
148 }
149 #else
150 #define TABLE_ADD_LINE(label_text, widget, do_space, intable1) {                        \
151         gchar *tmpstr = g_strdup_printf("<span weight=\"bold\">%s</span>",      \
152                                 label_text?label_text:"");                      \
153         GtkWidget *label = NULL;                                                \
154         GtkWidget *spacer = NULL;                                               \
155         GtkWidget *s_hbox = NULL;                                               \
156         if (do_space) {                                                         \
157                 spacer = gtk_label_new("");                                     \
158                 gtk_widget_set_size_request(spacer, 18, 16);                            \
159                 s_hbox = gtk_hbox_new(FALSE, 6);                                \
160                 gtk_box_pack_start(GTK_BOX(s_hbox), spacer, FALSE, FALSE, 0);   \
161                 gtk_box_pack_start(GTK_BOX(s_hbox), widget, TRUE, TRUE, 0);     \
162         }                                                                       \
163         if (label_text) {                                                       \
164                 label = gtk_label_new(tmpstr);                                  \
165                 g_free(tmpstr);                                                 \
166                 gtk_label_set_use_markup (GTK_LABEL (label), TRUE);             \
167                 gtk_misc_set_alignment (GTK_MISC(label), 1, 0.5);               \
168                 if(intable1)    {                                               \
169                         gtk_table_attach (GTK_TABLE (meet->table1),             \
170                                           label, 0, 1, i, i+1,                  \
171                                           GTK_FILL, GTK_FILL, 1, 1);            \
172                 }                                                               \
173                 else    {                                                       \
174                         gtk_table_attach (GTK_TABLE (meet->table2),             \
175                                           label, 0, 1, i, i+1,                  \
176                                           GTK_FILL, GTK_FILL, 1, 1);            \
177                 }                                                               \
178                 if(intable1)    {                                               \
179                         gtk_table_attach (GTK_TABLE (meet->table1),             \
180                                           do_space?s_hbox:widget, 1, 2, i, i+1, \
181                                           GTK_FILL|GTK_EXPAND, GTK_FILL, 1, 1); \
182                 }                                                               \
183                 else    {                                                       \
184                         gtk_table_attach (GTK_TABLE (meet->table2),             \
185                                           do_space?s_hbox:widget, 1, 2, i, i+1, \
186                                           GTK_FILL|GTK_EXPAND, GTK_FILL, 1, 1); \
187                 }                                                               \
188                 if (GTK_IS_LABEL(widget)) {                                     \
189                         gtk_label_set_use_markup(GTK_LABEL (widget), TRUE);     \
190                         gtk_misc_set_alignment (GTK_MISC(widget),0, 0);         \
191                         gtk_label_set_line_wrap(GTK_LABEL(widget), TRUE);       \
192                 }                                                               \
193         } else {                                                                \
194                 g_free(tmpstr);                                                 \
195                 if(intable1)    {                                               \
196                         gtk_table_attach (GTK_TABLE (meet->table1),             \
197                                           do_space?s_hbox:widget, 0, 2, i, i+1, \
198                                           GTK_FILL|GTK_EXPAND, GTK_FILL, 1, 1); \
199                 }                                                               \
200                 else    {                                                       \
201                         gtk_table_attach (GTK_TABLE (meet->table2),             \
202                                           do_space?s_hbox:widget, 0, 2, i, i+1, \
203                                           GTK_FILL|GTK_EXPAND, GTK_FILL, 1, 1); \
204                 }                                                               \
205         }                                                                       \
206         i++;                                                                    \
207 }
208 #endif
209 enum {
210         DAY,
211         MONTH,
212         YEAR,
213         HOUR,
214         MINUTE
215 };
216
217 static gboolean avail_btn_can_be_sensitive(void)
218 {
219         if (vcalprefs.freebusy_get_url == NULL
220         ||  *vcalprefs.freebusy_get_url == '\0')
221                 return FALSE;
222         else 
223                 return TRUE;
224 }
225
226 static gint get_dtdate(const gchar *str, gint field)
227 {
228         time_t t = icaltime_as_timet((icaltime_from_string(str)));
229         struct tm buft;
230         struct tm *lt;
231         
232         tzset();
233
234 #ifdef G_OS_WIN32
235         if (t < 0)
236                 t = 1;
237 #endif
238         lt = localtime_r(&t, &buft);
239
240         switch(field){
241         case DAY:
242                 return lt->tm_mday;
243         case MONTH:
244                 return lt->tm_mon + 1;
245         case YEAR:
246                 return lt->tm_year + 1900;
247         case HOUR:
248                 return lt->tm_hour;
249         case MINUTE:
250                 return lt->tm_min;
251         }
252         return -1;
253
254 }
255
256 static gboolean add_btn_cb(GtkButton *widget, gpointer data)
257 {
258         VCalAttendee *attendee = (VCalAttendee *)data;
259         attendee_add(attendee->meet, NULL, NULL, NULL, NULL, FALSE);
260         return TRUE;
261 }
262
263 static gboolean remove_btn_cb(GtkButton *widget, gpointer data)
264 {
265         VCalAttendee *attendee = (VCalAttendee *)data;
266         gtk_container_remove(GTK_CONTAINER(attendee->meet->attendees_vbox), attendee->hbox);
267         attendee->meet->attendees = g_slist_remove(attendee->meet->attendees, attendee);
268         
269         g_free(attendee->status);
270
271         return TRUE;
272 }
273
274 VCalAttendee *attendee_add(VCalMeeting *meet, gchar *address, gchar *name, gchar *partstat, gchar *cutype, gboolean first)
275 {
276         GtkWidget *att_hbox = gtk_hbox_new(FALSE, 6);
277         VCalAttendee *attendee  = g_new0(VCalAttendee, 1);
278
279         attendee->address       = gtk_entry_new();
280         attendee->cutype        = gtk_combo_box_new_text();
281         attendee->avail_evtbox  = gtk_event_box_new();
282         attendee->avail_img     = gtk_image_new_from_stock
283                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
284
285         gtk_widget_show(attendee->address);
286         gtk_widget_show(attendee->cutype);
287         gtk_widget_show(attendee->avail_evtbox);
288
289         CLAWS_SET_TIP(attendee->address, _("Use <tab> to autocomplete from addressbook"));
290         gtk_widget_set_size_request(attendee->avail_evtbox, 18, 16);
291         gtk_event_box_set_visible_window(GTK_EVENT_BOX(attendee->avail_evtbox), FALSE);
292         gtk_container_add (GTK_CONTAINER(attendee->avail_evtbox), attendee->avail_img);
293
294         if (address) {
295                 gchar *str = g_strdup_printf("%s%s%s%s",
296                                 (name && strlen(name))?name:"",
297                                 (name && strlen(name))?" <":"",
298                                 address,
299                                 (name && strlen(name))?">":"");
300                 gtk_entry_set_text(GTK_ENTRY(attendee->address), str);
301                 g_free(str);
302         }
303
304         if (partstat)
305                 attendee->status = g_strdup(partstat);
306
307         gtk_combo_box_append_text(GTK_COMBO_BOX(attendee->cutype), _("Individual"));
308         gtk_combo_box_append_text(GTK_COMBO_BOX(attendee->cutype), _("Group"));
309         gtk_combo_box_append_text(GTK_COMBO_BOX(attendee->cutype), _("Resource"));
310         gtk_combo_box_append_text(GTK_COMBO_BOX(attendee->cutype), _("Room"));
311         
312         gtk_combo_box_set_active(GTK_COMBO_BOX(attendee->cutype), 0);
313         
314         if (cutype) {
315                 if (!strcmp(cutype, "group"))
316                         gtk_combo_box_set_active(GTK_COMBO_BOX(attendee->cutype), 1);
317                 if (!strcmp(cutype, "resource"))
318                         gtk_combo_box_set_active(GTK_COMBO_BOX(attendee->cutype), 2);
319                 if (!strcmp(cutype, "room"))
320                         gtk_combo_box_set_active(GTK_COMBO_BOX(attendee->cutype), 3);
321         }
322         
323         attendee->add_btn       = gtk_button_new_with_label(_("Add..."));
324         attendee->remove_btn    = gtk_button_new_with_label(_("Remove"));
325         attendee->meet          = meet;
326         attendee->hbox          = att_hbox;
327
328         gtk_widget_show(attendee->add_btn);
329         gtk_widget_show(attendee->remove_btn);
330         gtk_widget_show(attendee->hbox);
331
332         gtk_box_pack_start(GTK_BOX(attendee->hbox), attendee->avail_evtbox, FALSE, FALSE, 0);
333         gtk_widget_set_sensitive(attendee->remove_btn, !first);
334         meet->attendees         = g_slist_append(meet->attendees, attendee);
335         
336         g_signal_connect(G_OBJECT(attendee->remove_btn), "clicked",
337                          G_CALLBACK(remove_btn_cb), attendee);
338         g_signal_connect(G_OBJECT(attendee->add_btn), "clicked",
339                          G_CALLBACK(add_btn_cb), attendee);
340         
341         gtk_box_pack_start(GTK_BOX(att_hbox), attendee->address, FALSE, FALSE, 0);
342         gtk_box_pack_start(GTK_BOX(att_hbox), attendee->cutype, FALSE, FALSE, 0);
343         gtk_box_pack_start(GTK_BOX(att_hbox), attendee->add_btn, FALSE, FALSE, 0);
344         gtk_box_pack_start(GTK_BOX(att_hbox), attendee->remove_btn, FALSE, FALSE, 0);
345         gtk_box_pack_start(GTK_BOX(meet->attendees_vbox), att_hbox, FALSE, FALSE, 0);
346         address_completion_register_entry(GTK_ENTRY(attendee->address), FALSE);
347 #ifndef GENERIC_UMPC
348         gtk_widget_set_size_request(attendee->address, 320, -1);
349 #else
350         gtk_widget_set_size_request(attendee->address, 220, -1);
351 #endif
352         return attendee;
353 }
354
355 static gchar *get_organizer(VCalMeeting *meet)
356 {
357         int index = gtk_combo_box_get_active(GTK_COMBO_BOX(meet->who));
358         int i = 0;
359         GSList *cur = meet->avail_accounts;
360         while (i < index && cur && cur->data) {
361                 debug_print("%d:skipping %s\n",i,((PrefsAccount *)(cur->data))->address);
362                 cur = cur->next;
363                 i++;
364         }
365         if (cur && cur->data)
366                 return g_strdup(((PrefsAccount *)(cur->data))->address);
367         else
368                 return g_strdup("");
369 }
370
371 static gchar *get_organizer_name(VCalMeeting *meet)
372 {
373         int index = gtk_combo_box_get_active(GTK_COMBO_BOX(meet->who));
374         int i = 0;
375         GSList *cur = meet->avail_accounts;
376         while (i < index && cur && cur->data) {
377                 debug_print("%d:skipping %s\n",i,((PrefsAccount *)(cur->data))->address);
378                 cur = cur->next;
379                 i++;
380         }
381         if (cur && cur->data)
382                 return g_strdup(((PrefsAccount *)(cur->data))->name);
383         else
384                 return g_strdup("");
385 }
386
387 static void get_time_from_combo(GtkWidget *combo, int *h, int *m)
388 {
389         gchar *tmp;
390         gchar **parts;
391
392         if (!h || !m) 
393                 return;
394
395         tmp = gtk_editable_get_chars(GTK_EDITABLE(gtk_bin_get_child(GTK_BIN(combo))), 0, -1);
396         parts = g_strsplit(tmp, ":", 2);
397         if (parts[0] && parts[1] && *parts[0] && *parts[1]) {
398                 *h = atoi(parts[0]);
399                 *m = atoi(parts[1]);
400         }
401         g_strfreev(parts);
402         g_free(tmp);
403 }
404
405 static int get_current_gmt_offset(void)
406 {
407         time_t now = time(NULL);
408         struct tm gmt;
409         struct tm local;
410         
411         tzset();
412
413 #ifdef G_OS_WIN32
414         if (now < 0)
415                 now = 1;
416 #endif
417         gmtime_r(& now, & gmt);
418         localtime_r(& now, & local);
419         
420         local.tm_isdst = 0;
421         return mktime(&local)-mktime(&gmt);
422 }
423
424 static int get_gmt_offset_at_time(time_t then)
425 {
426         struct tm gmt;
427         struct tm local;
428         
429         tzset();
430
431 #ifdef G_OS_WIN32
432         if (then < 0)
433                 then = 1;
434 #endif
435         gmtime_r(& then, & gmt);
436         localtime_r(& then, & local);
437         
438         local.tm_isdst = 0;
439         return mktime(&local)-mktime(&gmt);
440 }
441
442 static gchar *get_date(VCalMeeting *meet, int start) 
443 {
444         struct tm *lt;
445         time_t t;
446         guint d, m, y;
447         int dst_offset = 0;
448         struct tm buft;
449
450         tzset();
451
452         t = time(NULL);
453 #ifdef G_OS_WIN32
454         if (t < 0)
455                 t = 1;
456 #endif
457         lt = localtime_r(&t, &buft);
458         
459         gtk_calendar_get_date(GTK_CALENDAR(start ? meet->start_c : meet->end_c), &y, &m, &d);
460         lt->tm_mday = d;
461         lt->tm_mon  = m;
462         lt->tm_year = y - 1900;
463         lt->tm_hour = 0;
464         lt->tm_min  = 0;
465         lt->tm_sec  = 0;
466
467         if (start) {
468                 get_time_from_combo(meet->start_time, &lt->tm_hour, &lt->tm_min);
469         } else {
470                 get_time_from_combo(meet->end_time, &lt->tm_hour, &lt->tm_min);
471         }
472
473         debug_print("%d %d %d, %d:%d\n", lt->tm_mday, lt->tm_mon, lt->tm_year, lt->tm_hour, lt->tm_min);
474         t = mktime(lt);
475
476         dst_offset = get_current_gmt_offset() - get_gmt_offset_at_time(t);
477         debug_print("DST change offset to apply to time %d\n", dst_offset);
478         t += dst_offset;
479         debug_print("%s\n", ctime(&t));
480         return g_strdup(icaltime_as_ical_string(icaltime_from_timet(t, FALSE)));
481 }
482
483 static gchar *get_location(VCalMeeting *meet)
484 {
485         return gtk_editable_get_chars(GTK_EDITABLE(meet->location),0, -1);
486 }
487
488 static gchar *get_summary(VCalMeeting *meet) 
489 {
490         return gtk_editable_get_chars(GTK_EDITABLE(meet->summary),0, -1);
491 }
492
493 static gchar *get_description(VCalMeeting *meet) 
494 {
495         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(meet->description));
496         GtkTextIter start, end;
497         
498         gtk_text_buffer_get_start_iter(buffer, &start);
499         gtk_text_buffer_get_end_iter(buffer, &end);
500         return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
501 }
502
503 void vcal_meeting_free(VCalMeeting *meet)
504 {
505         debug_print("freeing meeting\n");
506         g_free(meet->uid);
507         address_completion_end(meet->window);
508         g_slist_free(meet->avail_accounts);
509         g_slist_free(meet->attendees);
510         g_free(meet);
511 }
512
513 static void destroy_meeting_cb(GtkWidget *widget, gpointer data)
514 {
515         VCalMeeting *meet = (VCalMeeting *)data;
516         vcal_meeting_free(meet);
517 }
518
519 static void vcal_destroy(VCalMeeting *meet)
520 {
521         GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(meet->description));
522         gtk_text_buffer_remove_selection_clipboard(buffer, gtk_clipboard_get(GDK_SELECTION_PRIMARY));
523         gtk_widget_destroy(meet->window);
524 }
525
526 static gboolean meeting_key_pressed(GtkWidget *widget,
527                                     GdkEventKey *event,
528                                     gpointer data)
529 {
530         VCalMeeting *meet = (VCalMeeting *)data;
531         
532         if (event && event->keyval == GDK_Escape) {
533                 vcal_destroy(meet);
534         }
535         return FALSE;
536 }
537
538 static int get_list_item_num(int h, int m)
539 {
540         if (m % 15 != 0)
541                 return -1;
542
543         return (h*4 + m/15);
544 }
545
546 static void meeting_end_changed(GtkWidget *widget, gpointer data);
547
548 static void meeting_start_changed(GtkWidget *widget, gpointer data)
549 {
550         VCalMeeting *meet = (VCalMeeting *)data;
551         struct tm start_lt;
552         struct tm end_lt;
553         time_t start_t, end_t;
554         guint d, m, y;
555         int num = -1;
556
557         if (strlen(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->start_time))))) < 5)
558                 return;
559         tzset();
560
561         start_t = time(NULL);
562         end_t = time(NULL);
563 #ifdef G_OS_WIN32
564         if (start_t < 0)
565                 start_t = 1;
566         if (end_t < 0)
567                 end_t = 1;
568 #endif
569         localtime_r(&start_t, &start_lt);
570         localtime_r(&end_t, &end_lt);
571
572         gtk_calendar_get_date(GTK_CALENDAR(meet->start_c), &y, &m, &d);
573         start_lt.tm_mday = d; start_lt.tm_mon  = m; start_lt.tm_year = y - 1900;
574         get_time_from_combo(meet->start_time, &start_lt.tm_hour, &start_lt.tm_min);
575
576         start_t = mktime(&start_lt);
577         debug_print("start %s\n", ctime(&start_t));
578
579         gtk_calendar_get_date(GTK_CALENDAR(meet->end_c), &y, &m, &d);
580         end_lt.tm_mday = d; end_lt.tm_mon  = m; end_lt.tm_year = y - 1900;
581
582         get_time_from_combo(meet->end_time, &end_lt.tm_hour, &end_lt.tm_min);
583         end_t = mktime(&end_lt);
584
585         debug_print("end   %s\n", ctime(&end_t));
586         
587         if (end_t > start_t) {
588                 debug_print("ok\n");
589                 return;
590         }
591         end_t = start_t + 3600;
592
593 #ifdef G_OS_WIN32
594         if (end_t < 0)
595                 end_t = 1;
596 #endif
597         localtime_r(&end_t, &end_lt);
598         debug_print("n %d %d %d, %d:%d\n", end_lt.tm_mday, end_lt.tm_mon, end_lt.tm_year, end_lt.tm_hour, end_lt.tm_min);
599
600         g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(meet->end_time)), meeting_end_changed, meet);
601         g_signal_handlers_block_by_func(meet->end_c, meeting_end_changed, meet);
602
603         gtk_calendar_select_day(GTK_CALENDAR(meet->end_c), end_lt.tm_mday);
604
605         gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
606                                 end_lt.tm_mon,
607                                 end_lt.tm_year + 1900);
608
609         num = get_list_item_num(end_lt.tm_hour, end_lt.tm_min);
610         if (num > -1) {
611                 gchar *time_text = g_strdup_printf("%02d:%02d", end_lt.tm_hour, end_lt.tm_min);
612                 combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
613                 g_free(time_text);
614         } else {
615                 gchar *tmp = g_strdup_printf("%02d:%02d",
616                                 end_lt.tm_hour, 
617                                 end_lt.tm_min);
618                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->end_time))),
619                                    tmp);
620                 g_free(tmp);
621         }
622         g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(meet->end_time)), meeting_end_changed, meet);
623         g_signal_handlers_unblock_by_func(meet->end_c, meeting_end_changed, meet);
624 }
625
626 static void meeting_end_changed(GtkWidget *widget, gpointer data)
627 {
628         VCalMeeting *meet = (VCalMeeting *)data;
629         struct tm start_lt;
630         struct tm end_lt;
631         time_t start_t, end_t;
632         guint d, m, y;
633         int num = -1;
634
635         if (strlen(gtk_entry_get_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->end_time))))) < 5)
636                 return;
637         start_t = time(NULL);
638         end_t = time(NULL);
639
640         tzset();
641
642 #ifdef G_OS_WIN32
643         if (start_t < 0)
644                 start_t = 1;
645         if (end_t < 0)
646                 end_t = 1;
647 #endif
648         localtime_r(&start_t, &start_lt);
649         localtime_r(&end_t, &end_lt);
650         
651         gtk_calendar_get_date(GTK_CALENDAR(meet->start_c), &y, &m, &d);
652         start_lt.tm_mday = d; start_lt.tm_mon  = m; start_lt.tm_year = y - 1900;
653         get_time_from_combo(meet->start_time, &start_lt.tm_hour, &start_lt.tm_min);
654
655         start_t = mktime(&start_lt);
656         debug_print("start %s\n", ctime(&start_t));
657
658         gtk_calendar_get_date(GTK_CALENDAR(meet->end_c), &y, &m, &d);
659         end_lt.tm_mday = d; end_lt.tm_mon  = m; end_lt.tm_year = y - 1900;
660         get_time_from_combo(meet->end_time, &end_lt.tm_hour, &end_lt.tm_min);
661
662         end_t = mktime(&end_lt);
663
664         debug_print("end   %s\n", ctime(&end_t));
665         
666         if (end_t > start_t) {
667                 debug_print("ok\n");
668                 return;
669         }
670         start_t = end_t - 3600;
671         
672         tzset();
673
674 #ifdef G_OS_WIN32
675         if (start_t < 0)
676                 start_t = 1;
677 #endif
678         localtime_r(&start_t, &start_lt);
679         debug_print("n %d %d %d, %d:%d\n", start_lt.tm_mday, start_lt.tm_mon, start_lt.tm_year, start_lt.tm_hour, start_lt.tm_min);
680
681         g_signal_handlers_block_by_func(gtk_bin_get_child(GTK_BIN(meet->start_time)), meeting_start_changed, meet);
682         g_signal_handlers_block_by_func(meet->start_c, meeting_start_changed, meet);
683
684         gtk_calendar_select_day(GTK_CALENDAR(meet->start_c), start_lt.tm_mday);
685
686         gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
687                                 start_lt.tm_mon,
688                                 start_lt.tm_year + 1900);
689
690         num = get_list_item_num(start_lt.tm_hour, start_lt.tm_min);
691         if (num > -1) {
692                 gchar *time_text = g_strdup_printf("%02d:%02d", start_lt.tm_hour, start_lt.tm_min);
693                 combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
694                 g_free(time_text);
695         } else {
696                 gchar *tmp = g_strdup_printf("%02d:%02d",
697                                 start_lt.tm_hour, 
698                                 start_lt.tm_min);
699                 gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->start_time))),
700                                    tmp);
701                 g_free(tmp);
702         }
703
704         g_signal_handlers_unblock_by_func(gtk_bin_get_child(GTK_BIN(meet->start_time)), meeting_start_changed, meet);
705         g_signal_handlers_unblock_by_func(meet->start_c, meeting_start_changed, meet);
706 }
707
708 static void att_update_icon(VCalMeeting *meet, VCalAttendee *attendee, gint avail, gchar *text)
709 {
710         const gchar *icon = GTK_STOCK_DIALOG_INFO;
711
712         switch (avail) {
713                 case 0:  icon = GTK_STOCK_DIALOG_WARNING;       break;
714                 case 1:  icon = GTK_STOCK_DIALOG_INFO;          break;
715                 default: icon = GTK_STOCK_DIALOG_QUESTION;      break;
716         }
717         if (!gtk_entry_get_text(GTK_ENTRY(attendee->address)) 
718          || strlen(gtk_entry_get_text(GTK_ENTRY(attendee->address)))==0) {
719                 if (attendee->avail_img) {
720                         gtk_widget_hide(attendee->avail_img);
721                 }
722                 CLAWS_SET_TIP(attendee->avail_evtbox, NULL);
723         } else if (attendee->avail_img) {
724                 gtk_image_set_from_stock
725                         (GTK_IMAGE(attendee->avail_img), 
726                         icon, 
727                         GTK_ICON_SIZE_SMALL_TOOLBAR);
728                 gtk_widget_show(attendee->avail_img);
729                 CLAWS_SET_TIP(attendee->avail_evtbox, text);
730         }
731 }
732
733 gboolean attendee_available(VCalAttendee *attendee, const gchar *dtstart, const gchar *dtend, const gchar *contents)
734 {
735         icalcomponent *toplvl, *vfreebusy;
736         icalproperty *busyprop;
737         struct icaltimetype start = icaltime_from_string(dtstart);
738         struct icaltimetype end = icaltime_from_string(dtend);
739         gboolean result = TRUE;
740         
741
742         if (contents == NULL)
743                 return TRUE;
744
745         toplvl = icalcomponent_new_from_string((gchar *)contents);
746         
747         if (toplvl == NULL)
748                 return TRUE;
749
750         vfreebusy = icalcomponent_get_first_component(toplvl, ICAL_VFREEBUSY_COMPONENT);
751         while (vfreebusy && icalcomponent_isa(vfreebusy) != ICAL_VFREEBUSY_COMPONENT)
752                 vfreebusy = icalcomponent_get_next_component(toplvl, ICAL_VFREEBUSY_COMPONENT);
753         
754         if (vfreebusy) {
755                 busyprop = icalcomponent_get_first_property(vfreebusy, ICAL_FREEBUSY_PROPERTY);
756                 while (busyprop) {
757                         struct icalperiodtype ipt = icalproperty_get_freebusy(busyprop);
758                         
759                         if ( icaltime_compare(start, ipt.end) >= 0 || icaltime_compare(end, ipt.start) <= 0 ) {
760                                 result = TRUE;
761                         } else {
762                                 result = FALSE;
763                                 break;
764                         }
765                         busyprop = icalcomponent_get_next_property(vfreebusy, ICAL_FREEBUSY_PROPERTY);
766                 }
767         }
768
769         icalcomponent_free(toplvl);
770         return result;
771 }
772
773 static gchar *get_avail_msg(const gchar *unavailable_persons, gboolean multiple, 
774         gboolean short_version, gint offset_before, gint offset_after)
775 {
776         gchar *msg, *intro = NULL, *outro = NULL, *before = NULL, *after = NULL;
777
778         if (multiple)
779                 intro = g_strdup(_("The following people are busy at the time of your planned meeting:\n- "));
780         else if (!strcmp(unavailable_persons, _("You")))
781                 intro = g_strdup(_("You are busy at the time of your planned meeting"));
782         else
783                 intro = g_strdup_printf(_("%s is busy at the time of your planned meeting"), unavailable_persons);
784         if (offset_before == 3600)
785                 before = g_strdup_printf(_("%d hour sooner"), offset_before/3600);
786         else if (offset_before > 3600 && offset_before%3600 == 0)
787                 before = g_strdup_printf(_("%d hours sooner"), offset_before/3600);
788         else if (offset_before > 3600)
789                 before = g_strdup_printf(_("%d hours and %d minutes sooner"), offset_before/3600, (offset_before%3600)/60);
790         else if (offset_before == 1800)
791                 before = g_strdup_printf(_("%d minutes sooner"), offset_before/60);
792         else
793                 before = NULL;
794         
795         if (offset_after == 3600)
796                 after = g_strdup_printf(_("%d hour later"), offset_after/3600);
797         else if (offset_after > 3600 && offset_after%3600 == 0)
798                 after = g_strdup_printf(_("%d hours later"), offset_after/3600);
799         else if (offset_after > 3600)
800                 after = g_strdup_printf(_("%d hours and %d minutes later"), offset_after/3600, (offset_after%3600)/60);
801         else if (offset_after == 1800)
802                 after = g_strdup_printf(_("%d minutes later"), offset_after/60);
803         else
804                 after = NULL;
805         
806         if (multiple) {
807                 if (before && after)
808                         outro = g_strdup_printf(_("\n\nEveryone would be available %s or %s."), before, after);
809                 else if (before || after)
810                         outro = g_strdup_printf(_("\n\nEveryone would be available %s."), before?before:after);
811                 else
812                         outro = g_strdup_printf(_("\n\nIt isn't possible to have this meeting with everyone "
813                                                 "in the previous or next 6 hours."));
814         } else {
815                 if (short_version) {
816                         if (before && after)
817                                 outro = g_markup_printf_escaped(_("would be available %s or %s"), before, after);
818                         else if (before || after)
819                                 outro = g_markup_printf_escaped(_("would be available %s"), before?before:after);
820                         else
821                                 outro = g_strdup_printf(_("not available"));
822                 } else {
823                         if (before && after)
824                                 outro = g_markup_printf_escaped(_(", but would be available %s or %s."), before, after);
825                         else if (before || after)
826                                 outro = g_markup_printf_escaped(_(", but would be available %s."), before?before:after);
827                         else
828                                 outro = g_strdup_printf(_(", and isn't available "
829                                                         "in the previous or next 6 hours."));
830                 }
831         }
832         if (multiple && short_version)
833                 msg = g_strconcat(outro+2, NULL);
834         else if (multiple)
835                 msg = g_strconcat(intro, unavailable_persons, outro, NULL);
836         else if (short_version)
837                 msg = g_strdup(outro);
838         else
839                 msg = g_strconcat(intro, outro, NULL);
840         g_free(intro);
841         g_free(outro);
842         g_free(before);
843         g_free(after);
844         return msg;
845 }
846
847 static gboolean find_availability(const gchar *dtstart, const gchar *dtend, GSList *attendees, gboolean for_send, VCalMeeting *meet)
848 {
849         GSList *cur;
850         gint offset = -1800, offset_before = 0, offset_after = 0;
851         gboolean found = FALSE;
852         gchar *unavailable_persons = NULL;
853         gchar *msg = NULL;
854         struct icaltimetype start = icaltime_from_string(dtstart);
855         struct icaltimetype end = icaltime_from_string(dtend);
856         AlertValue val = G_ALERTALTERNATE;
857         gint total = 0;
858         GHashTable *avail_table_avail = g_hash_table_new(NULL, g_direct_equal);
859         GHashTable *avail_table_before = g_hash_table_new(NULL, g_direct_equal);
860         GHashTable *avail_table_after = g_hash_table_new(NULL, g_direct_equal);
861         
862         for (cur = attendees; cur; cur = cur->next) {
863                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
864                 if (!attendee_available(attendee, icaltime_as_ical_string(start), icaltime_as_ical_string(end),
865                                 attendee->cached_contents)) {
866                         gchar *mail = NULL;
867                         
868                         if (attendee->org)
869                                 mail = g_strdup(_("You"));
870                         else
871                                 mail = gtk_editable_get_chars(GTK_EDITABLE(attendee->address), 0, -1);
872
873                         if (unavailable_persons == NULL) {
874                                 unavailable_persons = g_markup_printf_escaped("%s", mail);
875                         } else {
876                                 gchar *tmp = g_markup_printf_escaped("%s,\n- %s", unavailable_persons, mail);
877                                 g_free(unavailable_persons);
878                                 unavailable_persons = tmp;
879                         }
880                         total++;
881                         g_free(mail);
882                         att_update_icon(meet, attendee, 0, _("not available"));
883                 } else {
884                         if (attendee->cached_contents != NULL)
885                                 att_update_icon(meet, attendee, 1, _("available"));
886                         else
887                                 att_update_icon(meet, attendee, 2, _("Free/busy retrieval failed"));
888
889                         g_hash_table_insert(avail_table_avail, attendee, GINT_TO_POINTER(1));
890                 }
891         }
892         offset = -1800;
893         found = FALSE;
894         while (!found && offset >= -3600*6) {
895                 gboolean ok = TRUE;
896                 struct icaltimetype new_start = icaltime_from_timet(icaltime_as_timet(start)+offset, FALSE);
897                 struct icaltimetype new_end   = icaltime_from_timet(icaltime_as_timet(end)+offset, FALSE);
898                 for (cur = attendees; cur; cur = cur->next) {
899                         VCalAttendee *attendee = (VCalAttendee *)cur->data;
900                         debug_print("trying %s - %s (offset %d)\n", 
901                                 icaltime_as_ical_string(new_start), icaltime_as_ical_string(new_end), offset);
902                         if (!attendee_available(attendee, icaltime_as_ical_string(new_start), icaltime_as_ical_string(new_end),
903                                         attendee->cached_contents)) {
904                                 ok = FALSE;
905                                 break;
906                         } else {
907                                 if (!g_hash_table_lookup(avail_table_before, attendee)
908                                 &&  !g_hash_table_lookup(avail_table_avail, attendee))
909                                         g_hash_table_insert(avail_table_before, attendee, GINT_TO_POINTER(-offset));
910                         }
911                 }
912                 if (ok) {
913                         found = TRUE;
914                         offset_before = -offset;
915                 }
916                 offset -= 1800;
917         }
918         found = FALSE;
919         offset = 1800;
920         while (!found && offset <= 3600*6) {
921                 gboolean ok = TRUE;
922                 struct icaltimetype new_start = icaltime_from_timet(icaltime_as_timet(start)+offset, FALSE);
923                 struct icaltimetype new_end   = icaltime_from_timet(icaltime_as_timet(end)+offset, FALSE);
924                 for (cur = attendees; cur; cur = cur->next) {
925                         VCalAttendee *attendee = (VCalAttendee *)cur->data;
926                         debug_print("trying %s - %s (offset %d)\n", 
927                                 icaltime_as_ical_string(new_start), icaltime_as_ical_string(new_end), offset);
928                         if (!attendee_available(attendee, icaltime_as_ical_string(new_start), icaltime_as_ical_string(new_end),
929                                         attendee->cached_contents)) {
930                                 ok = FALSE;
931                                 break;
932                         } else {
933                                 if (!g_hash_table_lookup(avail_table_after, attendee)
934                                 &&  !g_hash_table_lookup(avail_table_avail, attendee))
935                                         g_hash_table_insert(avail_table_after, attendee, GINT_TO_POINTER(offset));
936                         }
937                 }
938                 if (ok) {
939                         found = TRUE;
940                         offset_after = offset;
941                 }
942
943                 offset += 1800;
944         }
945         
946         for (cur = attendees; cur; cur = cur->next) {
947                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
948                 gint ok = GPOINTER_TO_INT(g_hash_table_lookup(avail_table_avail, attendee));
949                 gint o_before = GPOINTER_TO_INT(g_hash_table_lookup(avail_table_before, attendee));
950                 gint o_after = GPOINTER_TO_INT(g_hash_table_lookup(avail_table_after, attendee));
951                 if (!o_before && !o_after && !ok) {
952                         att_update_icon(meet, attendee, 0, _("not available"));
953                 } else if ((o_before != 0 || o_after != 0) && !ok) {
954                         if (attendee->org)
955                                 msg = get_avail_msg(_("You"), FALSE, TRUE, o_before, o_after);
956                         else
957                                 msg = get_avail_msg(gtk_entry_get_text(GTK_ENTRY(attendee->address)), FALSE, TRUE, o_before, o_after);
958                         att_update_icon(meet, attendee, 0, msg);
959                         g_free(msg);
960                 }
961                 
962         }
963         g_hash_table_destroy(avail_table_before);
964         g_hash_table_destroy(avail_table_after);
965
966         if (for_send) {
967                 msg = get_avail_msg(unavailable_persons, (total > 1), FALSE, offset_before, offset_after);
968
969                 val = alertpanel_full(_("Not everyone is available"), msg,
970                                         GTK_STOCK_CANCEL, _("Send anyway"), NULL, FALSE,
971                                         NULL, ALERT_QUESTION, G_ALERTDEFAULT);
972                 g_free(msg);
973         }
974         msg = get_avail_msg(unavailable_persons, TRUE, TRUE, offset_before, offset_after);
975         g_free(unavailable_persons);
976         gtk_image_set_from_stock
977                 (GTK_IMAGE(meet->total_avail_img), 
978                 GTK_STOCK_DIALOG_WARNING, 
979                 GTK_ICON_SIZE_SMALL_TOOLBAR);
980         gtk_widget_show(meet->total_avail_img);
981         gtk_label_set_text(GTK_LABEL(meet->total_avail_msg), _("Not everyone is available. "
982                                 "See tooltips for more info..."));
983         CLAWS_SET_TIP(meet->total_avail_evtbox, msg);
984         g_free(msg);
985         return (val == G_ALERTALTERNATE);
986 }
987
988 static gboolean check_attendees_availability(VCalMeeting *meet, gboolean tell_if_ok, gboolean for_send)
989 {
990         GSList *cur;
991         gchar *tmp = NULL;
992         gchar *real_url = NULL;
993         gint num_format = 0;
994         gchar *change_user = NULL, *change_dom = NULL;
995         gchar *dtstart = NULL;
996         gchar *dtend = NULL;
997         gboolean find_avail = FALSE;
998         gboolean res = TRUE, uncertain = FALSE;
999         gchar *organizer = NULL;
1000         VCalAttendee *dummy_org = NULL;
1001         gchar *internal_ifb = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1002                                 "vcalendar", G_DIR_SEPARATOR_S, 
1003                                 "internal.ifb", NULL);
1004         gboolean local_only = FALSE;
1005         GSList *attlist;
1006
1007         if (vcalprefs.freebusy_get_url == NULL
1008         ||  *vcalprefs.freebusy_get_url == '\0') {
1009                 local_only = TRUE;
1010         } else {
1011                 real_url = g_strdup(vcalprefs.freebusy_get_url);
1012                 tmp = real_url;
1013
1014                 while (strchr(tmp, '%')) {
1015                         tmp = strchr(tmp, '%')+1;
1016                         num_format++;
1017                 }
1018                 if (num_format > 2) {
1019                         g_warning("wrong format in %s!", real_url);
1020                         g_free(real_url);
1021                         return FALSE;
1022                 }
1023
1024                 tmp = NULL;
1025                 if (strstr(real_url, "%u") != NULL) {
1026                         change_user = strstr(real_url, "%u");
1027                         *(strstr(real_url, "%u")+1) = 's';
1028                 } 
1029                 if (strstr(real_url, "%d") != NULL) {
1030                         change_dom = strstr(real_url, "%d");
1031                         *(strstr(real_url, "%d")+1) = 's';
1032                 } 
1033                 debug_print("url format %s\n", real_url);
1034         }
1035         dtstart = get_date(meet, TRUE);
1036         dtend = get_date(meet, FALSE);
1037
1038         /* hack to check our own avail. */
1039         organizer = get_organizer(meet);
1040         dummy_org = g_new0(VCalAttendee, 1);
1041         dummy_org->address      = gtk_entry_new();
1042         dummy_org->avail_img    = meet->avail_img;
1043         dummy_org->avail_evtbox = meet->avail_evtbox;
1044         dummy_org->org = TRUE;
1045         gtk_entry_set_text(GTK_ENTRY(dummy_org->address), organizer);
1046         g_free(organizer);
1047         dummy_org->cached_contents = file_read_to_str(internal_ifb);
1048         g_free(internal_ifb);
1049         
1050         if (!local_only) {
1051                 meet->attendees = g_slist_prepend(meet->attendees, dummy_org);
1052                 attlist = meet->attendees;
1053         } else {
1054                 attlist = g_slist_prepend(NULL, dummy_org);
1055         }
1056         
1057         gtk_widget_set_sensitive(meet->save_btn, FALSE);
1058         gtk_widget_set_sensitive(meet->avail_btn, FALSE);
1059
1060         if (meet->window->window)
1061                 gdk_window_set_cursor(meet->window->window, watch_cursor);
1062
1063         for (cur = attlist; cur && cur->data; cur = cur->next) {
1064                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
1065                 gchar *email = gtk_editable_get_chars(GTK_EDITABLE(attendee->address), 0, -1);
1066                 gchar *remail, *user, *domain;
1067                 gchar *contents = NULL;
1068
1069                 if (*email == '\0') {
1070                         g_free(email);
1071                         att_update_icon(meet, attendee, 0, NULL);
1072                         continue;
1073                 }
1074
1075                 if (!local_only) {
1076                         remail = g_strdup(email);
1077
1078                         extract_address(remail);
1079                         if (strrchr(remail, ' '))
1080                                 user = g_strdup(strrchr(remail, ' ')+1);
1081                         else
1082                                 user = g_strdup(remail);
1083                         if (strchr(user, '@')) {
1084                                 domain = g_strdup(strchr(user, '@')+1);
1085                                 *(strchr(user, '@')) = '\0';
1086                         } else {
1087                                 domain = g_strdup("");
1088                         }
1089                         g_free(remail);
1090                         if (change_user && change_dom) {
1091                                 if (change_user < change_dom)
1092                                         tmp = g_strdup_printf(real_url, user, domain);
1093                                 else
1094                                         tmp = g_strdup_printf(real_url, domain, user);
1095                         } else if (change_user) {
1096                                 tmp = g_strdup_printf(real_url, user);
1097                         } else if (change_dom) {
1098                                 tmp = g_strdup_printf(real_url, domain);
1099                         } else {
1100                                 tmp = g_strdup(real_url);
1101                         }
1102                         g_free(user);
1103                         g_free(domain);
1104                         debug_print("url to get %s\n", tmp);
1105                 }
1106
1107                 if (attendee->cached_contents != NULL) {
1108                         contents = attendee->cached_contents;
1109                         attendee->cached_contents = NULL;
1110                 } else if (!local_only) {
1111                         if (strncmp(tmp, "http://", 7) 
1112                         && strncmp(tmp, "https://", 8)
1113                         && strncmp(tmp, "webcal://", 9)
1114                         && strncmp(tmp, "webcals://", 10)
1115                         && strncmp(tmp, "ftp://", 6))
1116                                 contents = file_read_to_str(tmp);
1117                         else {
1118                                 gchar *label = g_strdup_printf(_("Fetching planning for %s..."), email);
1119                                 if (!strncmp(tmp, "webcal", 6)) {
1120                                         gchar *tmp2 = g_strdup_printf("http%s", tmp+6);
1121                                         g_free(tmp);
1122                                         tmp = tmp2;
1123                                 }
1124                                 contents = vcal_curl_read(tmp, label, FALSE, NULL);
1125                                 g_free(label);
1126                         }
1127                 } else {
1128                         contents = NULL;
1129                 }
1130
1131                 g_free(email);
1132                 g_free(tmp);
1133
1134                 if (contents == NULL) {
1135                         uncertain = TRUE;
1136                         att_update_icon(meet, attendee, 2, _("Free/busy retrieval failed"));
1137                         continue;
1138                 }
1139                 else {
1140                         if (!attendee_available(attendee, dtstart, dtend, contents)) {
1141                                 find_avail = TRUE;
1142                                 debug_print("not available!\n");
1143                         } else {
1144                                 debug_print("available!\n");
1145                                 att_update_icon(meet, attendee, 1, _("Available"));
1146                         }
1147                         attendee->cached_contents = contents;
1148                         
1149                 }
1150         }
1151         
1152         if (find_avail) {
1153                 res = find_availability((dtstart), (dtend), attlist, for_send, meet);
1154         } else {
1155                 res = TRUE;
1156                 if (tell_if_ok) {
1157                         if (for_send)
1158                                 alertpanel_notice(_("Everyone is available."));
1159                         else if (!uncertain) {
1160                                 gtk_image_set_from_stock
1161                                         (GTK_IMAGE(meet->total_avail_img), 
1162                                         GTK_STOCK_DIALOG_INFO, 
1163                                         GTK_ICON_SIZE_SMALL_TOOLBAR);
1164                                 gtk_widget_show(meet->total_avail_img);
1165                                 gtk_label_set_text(GTK_LABEL(meet->total_avail_msg), _("Everyone is available."));
1166                                 CLAWS_SET_TIP(meet->total_avail_evtbox, NULL);
1167                         } else {
1168                                 gtk_image_set_from_stock
1169                                         (GTK_IMAGE(meet->total_avail_img), 
1170                                         GTK_STOCK_DIALOG_QUESTION, 
1171                                         GTK_ICON_SIZE_SMALL_TOOLBAR);
1172                                 gtk_widget_show(meet->total_avail_img);
1173                                 gtk_label_set_text(GTK_LABEL(meet->total_avail_msg), _("Everyone is available."));
1174                                 CLAWS_SET_TIP(meet->total_avail_evtbox, _("Everyone seems available, but some free/busy information failed to be retrieved."));
1175                         }
1176                 }
1177         }
1178
1179         for (cur = attlist; cur && cur->data; cur = cur->next) {
1180                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
1181                 g_free(attendee->cached_contents);
1182                 attendee->cached_contents = NULL;
1183         }
1184         gtk_widget_set_sensitive(meet->save_btn, TRUE);
1185         gtk_widget_set_sensitive(meet->avail_btn, avail_btn_can_be_sensitive());
1186         if (meet->window->window)
1187                 gdk_window_set_cursor(meet->window->window, NULL);
1188
1189         if (!local_only)
1190                 meet->attendees = g_slist_remove(meet->attendees, dummy_org);
1191         else
1192                 g_slist_free(attlist);
1193         gtk_widget_destroy(dummy_org->address);
1194         g_free(dummy_org);
1195
1196         if (!local_only)
1197                 g_free(real_url);
1198
1199         g_free(dtstart);
1200         g_free(dtend);
1201         return res;
1202 }
1203
1204 static gboolean check_avail_cb(GtkButton *widget, gpointer data)
1205 {
1206         VCalMeeting *meet = (VCalMeeting *)data;
1207         check_attendees_availability(meet, TRUE, FALSE);
1208         return TRUE;
1209 }
1210
1211 static gboolean send_meeting_cb(GtkButton *widget, gpointer data)
1212 {
1213         VCalMeeting *meet = (VCalMeeting *)data;
1214         gchar *uid = NULL;
1215         gchar *organizer = NULL;
1216         gchar *organizer_name = NULL;
1217         gchar *dtstart = NULL;
1218         gchar *dtend = NULL;
1219         gchar *tzid = NULL;
1220         gchar *location = NULL;
1221         gchar *summary = NULL;
1222         gchar *description = NULL;
1223         VCalEvent *event = NULL;
1224         GSList *cur;
1225         PrefsAccount *account = NULL;
1226         gboolean res = FALSE;
1227         gboolean found_att = FALSE;
1228         Folder *folder = folder_find_from_name (PLUGIN_NAME, vcal_folder_get_class());
1229         gboolean redisp = FALSE;
1230
1231         if (meet->uid == NULL && meet->visible && 
1232             !check_attendees_availability(meet, FALSE, TRUE)) {
1233                 return FALSE;
1234         }
1235
1236         if (folder) {
1237                 MainWindow *mainwin = mainwindow_get_mainwindow();
1238                 if (mainwin->summaryview->folder_item == folder->inbox) {
1239                         redisp = TRUE;
1240                         summary_show(mainwin->summaryview, NULL);
1241                 }
1242         }
1243         gtk_widget_set_sensitive(meet->save_btn, FALSE);
1244         gtk_widget_set_sensitive(meet->avail_btn, FALSE);
1245         if (meet->window->window)
1246                 gdk_window_set_cursor(meet->window->window, watch_cursor);
1247
1248         organizer       = get_organizer(meet);
1249         account         = account_find_from_address(organizer, FALSE);
1250
1251         if(account == NULL) {
1252                 debug_print("can't get account from address %s\n", organizer);
1253                 g_free(organizer);
1254                 return FALSE;
1255         }
1256
1257         organizer_name  = get_organizer_name(meet);
1258
1259         if (meet->uid) {
1260                 uid     = g_strdup(meet->uid);
1261         } else {
1262                 uid     = prefs_account_generate_msgid(account);
1263         }
1264
1265         dtstart         = get_date(meet, TRUE);
1266         dtend           = get_date(meet, FALSE);
1267         location        = get_location(meet);
1268         summary         = get_summary(meet);
1269         description     = get_description(meet);
1270         
1271         event = vcal_manager_new_event(uid, organizer, organizer_name, location, summary, description,
1272                                         dtstart, dtend, NULL, tzid, NULL, meet->method, 
1273                                         meet->sequence,
1274                                         ICAL_VEVENT_COMPONENT);
1275         
1276         vcal_manager_update_answer(event, organizer, organizer_name, 
1277                                    ICAL_PARTSTAT_ACCEPTED,
1278                                    ICAL_CUTYPE_INDIVIDUAL);
1279         
1280         for (cur = meet->attendees; cur && cur->data; cur = cur->next) {
1281                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
1282                 gchar *email = gtk_editable_get_chars(GTK_EDITABLE(attendee->address), 0, -1);
1283                 gint index = 0;
1284                 gchar *orig_email = email;
1285                 gchar *name = NULL;
1286                 enum icalparameter_cutype cutype = ICAL_CUTYPE_INDIVIDUAL;
1287                 enum icalparameter_partstat status = ICAL_PARTSTAT_NEEDSACTION;
1288                 
1289                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(attendee->cutype));
1290                 
1291                 cutype = ICAL_CUTYPE_INDIVIDUAL + index;
1292                 if (attendee->status) {
1293                         if(!strcmp(attendee->status, "accepted"))
1294                                 status = ICAL_PARTSTAT_ACCEPTED;
1295                         if(!strcmp(attendee->status, "tentatively accepted"))
1296                                 status = ICAL_PARTSTAT_TENTATIVE;
1297                         if(!strcmp(attendee->status, "declined"))
1298                                 status = ICAL_PARTSTAT_DECLINED;
1299                         g_free(attendee->status);                       
1300                 }
1301                 if (strlen(email)) {
1302                         if (strstr(email, " <")) {
1303                                 name = email;
1304                                 email = strstr(email," <") + 2;
1305                                 *(strstr(name," <")) = '\0';
1306                                 if (strstr(email, ">"))
1307                                         *(strstr(email, ">")) = '\0';
1308                         } 
1309                         
1310                         vcal_manager_update_answer(event, email, name, 
1311                                         status, cutype);
1312                                         
1313                         found_att = strcmp(email, organizer);
1314                 }
1315                 g_free(orig_email);
1316         }
1317         
1318         if (found_att)
1319                 res = vcal_manager_request(account, event);
1320         else
1321                 res = TRUE;
1322         g_free(uid);
1323         g_free(organizer);
1324         g_free(organizer_name);
1325         g_free(dtstart);
1326         g_free(dtend);
1327         g_free(description);
1328         g_free(location);
1329         g_free(summary);
1330         vcal_manager_free_event(event);
1331
1332         gtk_widget_set_sensitive(meet->save_btn, TRUE);
1333         gtk_widget_set_sensitive(meet->avail_btn, avail_btn_can_be_sensitive());
1334         if (meet->window->window)
1335                 gdk_window_set_cursor(meet->window->window, NULL);
1336
1337         if (res) {
1338                 vcal_destroy(meet);
1339         } else {
1340                 alertpanel_error(_("Could not send the meeting invitation.\n"
1341                                    "Check the recipients."));
1342         }
1343
1344         if (folder)
1345                 folder_item_scan(folder->inbox);
1346
1347         if (folder && redisp) {
1348                 MainWindow *mainwin = mainwindow_get_mainwindow();
1349                 summary_show(mainwin->summaryview, folder->inbox);
1350         }
1351
1352         return res;
1353 }
1354
1355 static GList *get_predefined_times(void)
1356 {
1357         int h,m;
1358         GList *times = NULL;
1359         for (h = 0; h < 24; h++) {
1360                 for (m = 0; m < 60; m += 15) {
1361                         gchar *tmp = g_strdup_printf("%02d:%02d", h, m);
1362                         times = g_list_append(times, tmp);
1363                 }
1364         }
1365         return times;
1366 }
1367
1368 static VCalMeeting *vcal_meeting_create_real(VCalEvent *event, gboolean visible)
1369 {
1370         VCalMeeting *meet = g_new0(VCalMeeting, 1);
1371         GtkTextBuffer *buffer = NULL;
1372         GtkWidget *date_hbox, *date_vbox, *save_hbox, *label, *hbox;
1373         gchar *s = NULL;
1374         int i = 0, num = 0;
1375         GtkWidget *scrolledwin;
1376         GList *times = NULL;
1377         GList *accounts;
1378         gchar *time_text = NULL;
1379 #ifdef GENERIC_UMPC
1380         GtkWidget *notebook;
1381         GtkWidget *maemo_vbox0;
1382 #endif
1383
1384         if (!watch_cursor)
1385                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1386
1387         meet->visible = visible;
1388
1389         meet->window            = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "vcal_meeting_gtk");
1390 #ifndef GENERIC_UMPC
1391         meet->table             = gtk_table_new(7, 2, FALSE);
1392 #else
1393         meet->table1            = gtk_table_new(4, 2, FALSE);
1394         meet->table2            = gtk_table_new(2, 2, FALSE);
1395 #endif
1396         meet->who               = gtk_combo_box_new_text();
1397         
1398         meet->start_c           = gtk_calendar_new();
1399         meet->end_c             = gtk_calendar_new();
1400
1401         meet->avail_evtbox  = gtk_event_box_new();
1402         meet->avail_img = gtk_image_new_from_stock
1403                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1404
1405         times = get_predefined_times();
1406
1407 #if !GTK_CHECK_VERSION(2, 24, 0)
1408         meet->start_time = gtk_combo_box_entry_new_text();
1409 #else
1410         meet->start_time = gtk_combo_box_text_new_with_entry();
1411 #endif
1412         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->start_time), -1);
1413 #if !GTK_CHECK_VERSION(2, 24, 0)        
1414         combobox_set_popdown_strings(GTK_COMBO_BOX(meet->start_time), times);
1415 #else   
1416         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(meet->start_time), times);
1417 #endif
1418         
1419 #if !GTK_CHECK_VERSION(2, 24, 0)
1420         meet->end_time = gtk_combo_box_entry_new_text();
1421 #else
1422         meet->end_time = gtk_combo_box_text_new_with_entry();
1423 #endif
1424         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->end_time), -1);
1425 #if !GTK_CHECK_VERSION(2, 24, 0)        
1426         combobox_set_popdown_strings(GTK_COMBO_BOX(meet->end_time), times);
1427 #else   
1428         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(meet->end_time), times);
1429 #endif
1430
1431         list_free_strings(times);
1432         g_list_free(times);
1433
1434         meet->location          = gtk_entry_new();
1435         meet->summary           = gtk_entry_new();
1436         meet->description       = gtk_text_view_new();
1437         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(meet->description));
1438         gtk_text_view_set_editable(GTK_TEXT_VIEW(meet->description), TRUE);
1439         gtk_text_buffer_add_selection_clipboard(buffer, gtk_clipboard_get(GDK_SELECTION_PRIMARY));
1440
1441         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1442         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1443                                        GTK_POLICY_AUTOMATIC,
1444                                        GTK_POLICY_AUTOMATIC);
1445         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1446                                             GTK_SHADOW_IN);
1447         gtk_container_add(GTK_CONTAINER(scrolledwin), meet->description);
1448
1449         if (event) {
1450                 meet->uid = g_strdup(event->uid);
1451                 meet->sequence = event->sequence + 1;
1452                 meet->method = (event->method == ICAL_METHOD_CANCEL ?
1453                                 ICAL_METHOD_CANCEL:ICAL_METHOD_REQUEST);
1454
1455                 gtk_entry_set_text(GTK_ENTRY(meet->location), event->location);
1456                 gtk_entry_set_text(GTK_ENTRY(meet->summary), event->summary);   
1457                 gtk_text_buffer_set_text(buffer, event->description, -1);       
1458         } else 
1459                 meet->method = ICAL_METHOD_REQUEST;
1460         
1461         meet->save_btn          = gtk_button_new_with_label(_("Save & Send"));
1462         meet->avail_btn         = gtk_button_new_with_label(_("Check availability"));
1463
1464         meet->total_avail_evtbox  = gtk_event_box_new();
1465         meet->total_avail_img   = gtk_image_new_from_stock
1466                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1467         meet->total_avail_msg = gtk_label_new("");
1468         
1469         gtk_widget_set_size_request(meet->total_avail_evtbox, 18, 16);
1470         gtk_event_box_set_visible_window(GTK_EVENT_BOX(meet->total_avail_evtbox), FALSE);
1471         gtk_container_add (GTK_CONTAINER(meet->total_avail_evtbox), meet->total_avail_img);
1472
1473         g_signal_connect(G_OBJECT(meet->save_btn), "clicked",
1474                          G_CALLBACK(send_meeting_cb), meet);
1475
1476         g_signal_connect(G_OBJECT(meet->avail_btn), "clicked",
1477                          G_CALLBACK(check_avail_cb), meet);
1478
1479         g_signal_connect(G_OBJECT(meet->window), "destroy",
1480                          G_CALLBACK(destroy_meeting_cb), meet);
1481         g_signal_connect(G_OBJECT(meet->window), "key_press_event",
1482                          G_CALLBACK(meeting_key_pressed), meet);
1483         
1484
1485         gtk_widget_set_size_request(meet->description, -1, 100);
1486         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(meet->description), GTK_WRAP_WORD);
1487         
1488         if (!event || (event && !event->dtstart && !event->dtend)) {
1489                 time_t t = time (NULL)+ 3600;
1490                 struct tm buft1, buft2;
1491                 struct tm *lt = localtime_r (&t, &buft1);
1492                 mktime(lt);
1493                 gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1494                                         lt->tm_mday);
1495                 gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1496                                         lt->tm_mon, lt->tm_year + 1900);
1497         
1498                 time_text = g_strdup_printf("%02d:%02d", lt->tm_hour, 0);
1499                 combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1500                 g_free(time_text);
1501
1502                 t += 3600;
1503                 lt = localtime_r(&t, &buft2);
1504                 
1505                 gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1506                                         lt->tm_mday);
1507                 gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1508                                         lt->tm_mon, lt->tm_year + 1900);
1509
1510                 time_text = g_strdup_printf("%02d:%02d", lt->tm_hour, 0);
1511                 combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1512                 g_free(time_text);
1513
1514         } else {
1515                 gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1516                                         get_dtdate(event->dtstart, DAY));
1517                 gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1518                                         get_dtdate(event->dtend, DAY));
1519
1520                 gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1521                                         get_dtdate(event->dtstart, MONTH)-1,
1522                                         get_dtdate(event->dtstart, YEAR));
1523                 gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1524                                         get_dtdate(event->dtend, MONTH)-1,
1525                                         get_dtdate(event->dtend, YEAR));
1526
1527                 num = get_list_item_num(get_dtdate(event->dtstart, HOUR), 
1528                                         get_dtdate(event->dtstart, MINUTE));
1529                 if (num > -1) {
1530                         time_text = g_strdup_printf("%02d:%02d", get_dtdate(event->dtstart, HOUR), 
1531                                                                  get_dtdate(event->dtstart, MINUTE));
1532                         combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1533                         g_free(time_text);
1534                 } else {
1535                         gchar *tmp = g_strdup_printf("%02d:%02d",
1536                                         get_dtdate(event->dtstart, HOUR), 
1537                                         get_dtdate(event->dtstart, MINUTE));
1538                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->start_time))),
1539                                            tmp);
1540                         g_free(tmp);
1541                 }
1542
1543                 num = get_list_item_num(get_dtdate(event->dtend, HOUR), 
1544                                         get_dtdate(event->dtend, MINUTE));
1545                 if (num > -1) {
1546                         time_text = g_strdup_printf("%02d:%02d", get_dtdate(event->dtend, HOUR), 
1547                                                                  get_dtdate(event->dtend, MINUTE));
1548                         combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1549                         g_free(time_text);
1550                 } else {
1551                         gchar *tmp = g_strdup_printf("%02d:%02d",
1552                                         get_dtdate(event->dtend, HOUR), 
1553                                         get_dtdate(event->dtend, MINUTE));
1554                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->end_time))),
1555                                            tmp);
1556                         g_free(tmp);
1557                 }
1558         }
1559
1560         g_signal_connect(G_OBJECT(meet->start_c), "day-selected",
1561                          G_CALLBACK(meeting_start_changed), meet);
1562         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(meet->start_time))),
1563                          "changed",
1564                          G_CALLBACK(meeting_start_changed),
1565                          meet);
1566
1567         g_signal_connect(G_OBJECT(meet->end_c), "day-selected",
1568                          G_CALLBACK(meeting_end_changed), meet);
1569         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(meet->end_time))),
1570                          "changed",
1571                          G_CALLBACK(meeting_end_changed),
1572                          meet);
1573
1574 #ifndef GENERIC_UMPC
1575         gtk_widget_set_size_request(meet->start_time, 80, -1);
1576         gtk_widget_set_size_request(meet->end_time, 80, -1);
1577 #else
1578         gtk_widget_set_size_request(meet->start_time, 120, -1);
1579         gtk_widget_set_size_request(meet->end_time, 120, -1);
1580 #endif
1581         
1582         date_hbox = gtk_hbox_new(FALSE, 6);
1583         date_vbox = gtk_vbox_new(FALSE, 6);
1584         hbox = gtk_hbox_new(FALSE, 6);
1585         label = gtk_label_new(g_strconcat("<b>",_("Starts at:"),"</b> ",NULL));
1586         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1587         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1588         
1589         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1590         gtk_box_pack_start(GTK_BOX(hbox), meet->start_time, FALSE, FALSE, 0);
1591         label = gtk_label_new(g_strconcat("<b> ",_("on:"),"</b>",NULL));
1592         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1593         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1594         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1595         gtk_box_pack_start(GTK_BOX(date_vbox), hbox, FALSE, FALSE, 0);
1596         gtk_box_pack_start(GTK_BOX(date_vbox), meet->start_c, FALSE, FALSE, 0);
1597         gtk_box_pack_start(GTK_BOX(date_hbox), date_vbox, FALSE, FALSE, 0);
1598
1599 #ifndef GENERIC_UMPC
1600         label = gtk_label_new(" "); 
1601 #else
1602         label = gtk_label_new(""); 
1603 #endif
1604         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1605         gtk_box_pack_start(GTK_BOX(date_hbox), label, TRUE, TRUE, 0);
1606
1607         date_vbox = gtk_vbox_new(FALSE, 6);
1608         hbox = gtk_hbox_new(FALSE, 6);
1609         label = gtk_label_new(g_strconcat("<b>",_("Ends at:"),"</b> ", NULL));
1610         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1611         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1612         
1613         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1614         gtk_box_pack_start(GTK_BOX(hbox), meet->end_time, FALSE, FALSE, 0);
1615         label = gtk_label_new(g_strconcat("<b> ",_("on:"),"</b>",NULL));
1616         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1617         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1618         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1619         gtk_box_pack_start(GTK_BOX(date_vbox), hbox, FALSE, FALSE, 0);
1620         gtk_box_pack_start(GTK_BOX(date_vbox), meet->end_c, FALSE, FALSE, 0);
1621         gtk_box_pack_start(GTK_BOX(date_hbox), date_vbox, FALSE, FALSE, 0);
1622
1623         meet->attendees_vbox = gtk_vbox_new(FALSE, 6);
1624         gtk_widget_show_all(meet->attendees_vbox);
1625         if (!event) {
1626                 attendee_add(meet, NULL, NULL, NULL, NULL, TRUE);
1627         } else {
1628                 gboolean firstadd = TRUE;
1629                 GSList *list = vcal_manager_get_answers_emails(event);
1630                 while (list && list->data) {
1631                         gchar *address = (gchar *)list->data;
1632                         gchar *name = vcal_manager_get_attendee_name(event, address);
1633                         gchar *answer = vcal_manager_get_reply_text_for_attendee(event, address);
1634                         gchar *type = vcal_manager_get_cutype_text_for_attendee(event, address);
1635                         if (strcmp(event->organizer, address)) {
1636                                 attendee_add(meet, address, name, answer, type, firstadd);
1637                                 firstadd = FALSE;
1638                         }
1639                         g_free(name);
1640                         g_free(answer);
1641                         g_free(type);
1642                         list = list->next;
1643                 } 
1644                 
1645                 if (firstadd == TRUE)
1646                         attendee_add(meet, NULL, NULL, NULL, NULL, TRUE);
1647         }
1648
1649         if (!event) {
1650                 gtk_window_set_title(GTK_WINDOW(meet->window), _("New meeting"));
1651         } else {
1652                 gchar *title = g_strdup_printf(_("%s - Edit meeting"),
1653                         event->summary);
1654                 gtk_window_set_title(GTK_WINDOW(meet->window), title);
1655                 g_free(title);
1656         }
1657         address_completion_start(meet->window);
1658         
1659         accounts = account_get_list();
1660         g_return_val_if_fail(accounts != NULL, NULL);
1661
1662         for (i = 0; accounts != NULL; accounts = accounts->next) {
1663                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
1664                 
1665                 if (ac->protocol == A_NNTP) {
1666                         continue;
1667                 }
1668                 if (!event && ac == account_get_cur_account()) {
1669                         num = i;
1670                 }
1671                 else if (event && !strcmp(ac->address, event->organizer))
1672                         num = i;
1673
1674                 meet->avail_accounts = g_slist_append(meet->avail_accounts, ac);
1675                 
1676                 if (ac->name)
1677                         s = g_strdup_printf("%s: %s <%s>",
1678                                                ac->account_name,
1679                                                ac->name, ac->address);
1680                 else
1681                         s = g_strdup_printf("%s: %s",
1682                                                ac->account_name, ac->address);
1683
1684                 gtk_combo_box_append_text(GTK_COMBO_BOX(meet->who), s);
1685                 g_free(s);
1686                 i++;
1687         }
1688         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->who), num);
1689         
1690         save_hbox = gtk_hbox_new(FALSE, 6);
1691         gtk_box_pack_start(GTK_BOX(save_hbox), meet->save_btn, FALSE, FALSE, 0);
1692         gtk_box_pack_start(GTK_BOX(save_hbox), meet->avail_btn, FALSE, FALSE, 0);
1693         gtk_box_pack_start(GTK_BOX(save_hbox), meet->total_avail_evtbox, FALSE, FALSE, 0);
1694         gtk_box_pack_start(GTK_BOX(save_hbox), meet->total_avail_msg, FALSE, FALSE, 0);
1695         
1696         hbox = gtk_hbox_new(FALSE, 6);
1697         gtk_box_pack_start(GTK_BOX(hbox), meet->avail_evtbox, FALSE, FALSE, 0);
1698         gtk_box_pack_start(GTK_BOX(hbox), meet->who, TRUE, TRUE, 0);
1699
1700         gtk_widget_set_size_request(meet->avail_evtbox, 18, 16);
1701         gtk_event_box_set_visible_window(GTK_EVENT_BOX(meet->avail_evtbox), FALSE);
1702         gtk_container_add (GTK_CONTAINER(meet->avail_evtbox), meet->avail_img);
1703
1704 #ifndef GENERIC_UMPC
1705         TABLE_ADD_LINE(_("Organizer:"), hbox, FALSE);
1706         TABLE_ADD_LINE(_("Summary:"), meet->summary, TRUE);
1707         TABLE_ADD_LINE(_("Time:"), date_hbox, TRUE);
1708         TABLE_ADD_LINE(_("Location:"), meet->location, TRUE);
1709         TABLE_ADD_LINE(_("Description:"), scrolledwin, TRUE);
1710         TABLE_ADD_LINE(_("Attendees:"), meet->attendees_vbox, FALSE);
1711         TABLE_ADD_LINE("", save_hbox, TRUE);
1712         
1713         gtk_widget_set_size_request(meet->window, -1, -1);
1714         gtk_container_add(GTK_CONTAINER(meet->window), meet->table);
1715 #else
1716         TABLE_ADD_LINE(_("Organizer:"), hbox, FALSE, TRUE);
1717         TABLE_ADD_LINE(_("Summary:"), meet->summary, TRUE, TRUE);
1718         TABLE_ADD_LINE(_("Location:"), meet->location, FALSE, TRUE);
1719         TABLE_ADD_LINE(_("Description:"), scrolledwin, TRUE, TRUE);
1720         TABLE_ADD_LINE(_("Attendees:"), meet->attendees_vbox, FALSE, TRUE);
1721         TABLE_ADD_LINE("", date_hbox, TRUE, FALSE);
1722         
1723         notebook = gtk_notebook_new ();
1724         gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
1725         gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
1726                         meet->table1,
1727                         gtk_label_new_with_mnemonic(_("Event:")));
1728                         
1729         gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
1730                         meet->table2,
1731                         gtk_label_new_with_mnemonic(_("Time:")));
1732         gtk_widget_show (notebook);
1733         
1734         maemo_vbox0 = gtk_vbox_new(FALSE, 3);
1735         gtk_box_pack_start(GTK_BOX(maemo_vbox0), notebook, TRUE, TRUE, 0);
1736         gtk_box_pack_start(GTK_BOX(maemo_vbox0), save_hbox, FALSE, FALSE, 0);
1737         
1738         gtk_widget_set_size_request(meet->window, -1, -1);
1739         gtk_container_add (GTK_CONTAINER (meet->window), maemo_vbox0);
1740         
1741         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(meet->window));
1742 #endif
1743         if (visible) {
1744                 GSList *cur;
1745                 gtk_widget_show_all(meet->window);
1746                 for (cur = meet->attendees; cur; cur = cur->next) {
1747                         gtk_widget_hide(((VCalAttendee *)cur->data)->avail_img);
1748                 }
1749                 gtk_widget_hide(meet->avail_img);
1750                 gtk_widget_hide(meet->total_avail_img);
1751                 gtk_widget_set_sensitive(meet->avail_btn, avail_btn_can_be_sensitive());
1752         }
1753         return meet;
1754 }
1755
1756 VCalMeeting *vcal_meeting_create(VCalEvent *event)
1757 {
1758         return vcal_meeting_create_real(event, TRUE);
1759 }
1760
1761 VCalMeeting *vcal_meeting_create_with_start(VCalEvent *event, struct tm *sdate)
1762 {
1763         VCalMeeting *meet = vcal_meeting_create(event);
1764         int num = -1;
1765         gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1766                                 sdate->tm_mday);
1767         gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1768                                 sdate->tm_mday);
1769
1770         gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1771                                 sdate->tm_mon, sdate->tm_year+1900);
1772         gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1773                                 sdate->tm_mon, sdate->tm_year+1900);
1774
1775         if (sdate->tm_hour != 0) {
1776                 num = get_list_item_num(sdate->tm_hour, 0);
1777                 if (num > -1) {
1778                         gchar *time_text = g_strdup_printf("%02d:%02d", sdate->tm_hour, 0);
1779                         combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1780                         g_free(time_text);
1781                 } 
1782                 if (sdate->tm_hour < 23) {
1783                         num = get_list_item_num(sdate->tm_hour+1, 0);
1784                         if (num > -1) {
1785                                 gchar *time_text = g_strdup_printf("%02d:%02d", sdate->tm_hour+1, 0);
1786                                 combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1787                                 g_free(time_text);
1788                         } 
1789                 } else {
1790                         struct tm tm_tomorrow;
1791                         gchar *time_text;
1792                         tm_tomorrow.tm_mday = sdate->tm_mday;
1793                         tm_tomorrow.tm_mon = sdate->tm_mon;
1794                         tm_tomorrow.tm_wday = sdate->tm_wday;
1795                         tm_tomorrow.tm_year = sdate->tm_year+1900;
1796                         tm_tomorrow.tm_hour = sdate->tm_hour;
1797                         orage_move_day(&tm_tomorrow, +1);
1798                         gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1799                                                 tm_tomorrow.tm_mday);
1800                         gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1801                                                 tm_tomorrow.tm_mon, tm_tomorrow.tm_year);
1802                         
1803                         time_text = g_strdup_printf("%02d:%02d", 0, 0);
1804                         combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1805                         g_free(time_text);
1806                 }
1807         }
1808         return meet;
1809 }
1810
1811 VCalMeeting *vcal_meeting_create_hidden(VCalEvent *event)
1812 {
1813         return vcal_meeting_create_real(event, FALSE);
1814 }
1815
1816 gboolean vcal_meeting_send(VCalMeeting *meet)
1817 {
1818         return send_meeting_cb(NULL, meet);
1819 }
1820
1821 gboolean vcal_meeting_alert_check(gpointer data)
1822 {
1823         GSList *events = NULL, *cur = NULL;
1824
1825         if (!vcalprefs.alert_enable)
1826                 return TRUE;
1827
1828         events = vcal_folder_get_waiting_events();
1829
1830         for (cur = events; cur; cur = cur->next) {
1831                 VCalEvent *event = (VCalEvent *)cur->data;
1832                 time_t start, end, current;
1833                 gboolean warn = FALSE;
1834
1835                 tzset();
1836
1837                 start = icaltime_as_timet(icaltime_from_string(event->dtstart));
1838                 end = icaltime_as_timet(icaltime_from_string(event->dtend));
1839                 current = time(NULL);
1840                 
1841                 if (start - current <= (vcalprefs.alert_delay*60) 
1842                 &&  start - current + 60 > (vcalprefs.alert_delay*60)) {
1843                         warn = TRUE;
1844                 } else if (event->postponed - current <= (vcalprefs.alert_delay*60)
1845                 &&         event->postponed - current + 60 > (vcalprefs.alert_delay*60)) {
1846                         warn = TRUE;
1847                 }
1848                 if (warn) {
1849                         time_t tmpt = icaltime_as_timet((icaltime_from_string(event->dtstart)));
1850                         gchar *estart = NULL;
1851                         AlertValue aval;
1852                         int length = (end - start) / 60;
1853                         gchar *duration = NULL, *hours = NULL, *minutes = NULL;
1854                         gchar *message = NULL;
1855                         gchar *title = NULL;
1856                         gchar *label = NULL;
1857                         int postpone_min = 0;
1858
1859                         tzset();
1860
1861                         estart = g_strdup(ctime(&tmpt));
1862
1863                         if (length >= 60)
1864                                 hours = g_strdup_printf(ngettext("%d hour", "%d hours", 
1865                                                 (length/60) > 1 ? 2 : 1), length/60);
1866                         if (length%60)
1867                                 minutes = g_strdup_printf(ngettext("%d minute", "%d minutes",
1868                                                 length%60), length%60);
1869
1870                         duration = g_strdup_printf("%s%s%s",
1871                                         hours?hours:"",
1872                                         hours && minutes ? " ":"",
1873                                         minutes?minutes:"");
1874
1875                         g_free(hours);
1876                         g_free(minutes);
1877
1878                         title = g_strdup_printf(_("Upcoming event: %s"), event->summary);
1879                         message = g_strdup_printf(_("You have a meeting or event soon.\n"
1880                                          "It starts at %s and ends %s later.\n"
1881                      "Location: %s\n"
1882                                          "More information:\n\n"
1883                                          "%s"),
1884                                                 estart,
1885                                                 duration,
1886                                                 event->location?event->location:"",
1887                                                 event->description);
1888
1889                         g_free(duration);
1890                         g_free(estart);
1891
1892                         postpone_min = (vcalprefs.alert_delay/2 > 15) ? 15: (vcalprefs.alert_delay/2);
1893                         if (postpone_min == 0)
1894                                 postpone_min = 1;
1895
1896                         label = g_strdup_printf(ngettext("Remind me in %d minute", "Remind me in %d minutes",
1897                                                  postpone_min > 1 ? 2:1), 
1898                                                  postpone_min);
1899                         aval = alertpanel_full(title, message,
1900                                         label, GTK_STOCK_OK, NULL, FALSE,
1901                                         NULL, ALERT_NOTICE, G_ALERTDEFAULT);
1902                         g_free(label);
1903
1904                         g_free(title);
1905                         g_free(message);
1906
1907                         if (aval == G_ALERTDEFAULT) {
1908                                 if (event->postponed == 0)
1909                                         event->postponed = start + (postpone_min*60);
1910                                 else
1911                                         event->postponed += (postpone_min*60);
1912                         } else {
1913                                 event->postponed = (time_t)0;
1914                         }
1915                         vcal_manager_save_event(event, FALSE);
1916                 }
1917                 
1918                 vcal_manager_free_event((VCalEvent *)cur->data);
1919         }
1920         
1921         g_slist_free(events);
1922
1923         return TRUE;
1924 }
1925
1926 void multisync_export(void)
1927 {
1928         GSList *list = NULL;
1929         gchar *path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1930                                 "vcalendar", G_DIR_SEPARATOR_S, 
1931                                 "multisync", NULL);
1932         GSList *files = NULL;
1933         GSList *cur = NULL;
1934         gchar *file = NULL;
1935         gchar *tmp = NULL;
1936         gint i = 0;
1937         icalcomponent *calendar = NULL;
1938         FILE *fp;
1939
1940         if (is_dir_exist(path) && remove_dir_recursive(path) < 0) {
1941                 g_free(path);
1942                 return;
1943         }
1944         if (make_dir(path) != 0) {
1945                 g_free(path);
1946                 return;
1947         }
1948         
1949         list = vcal_folder_get_waiting_events();
1950         for (cur = list; cur; cur = cur->next) {
1951                 VCalEvent *event = (VCalEvent *)cur->data;
1952                 file = g_strdup_printf("multisync%lld-%d",
1953                                 (long long)time(NULL), i);
1954
1955                 i++;
1956
1957                 calendar = 
1958                         icalcomponent_vanew(
1959                             ICAL_VCALENDAR_COMPONENT,
1960                             icalproperty_new_version("2.0"),
1961                             icalproperty_new_prodid(
1962                                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
1963                             icalproperty_new_calscale("GREGORIAN"),
1964                             (void*)0
1965                     );  
1966                 vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
1967                 tmp = g_strconcat(path, G_DIR_SEPARATOR_S, file, NULL);
1968                 str_write_to_file(icalcomponent_as_ical_string(calendar), tmp);
1969                 g_free(tmp);
1970                 files = g_slist_append(files, file);
1971                 vcal_manager_free_event(event);
1972                 icalcomponent_free(calendar);
1973         }
1974
1975         g_slist_free(list);
1976         
1977         file = g_strconcat(path, G_DIR_SEPARATOR_S, "backup_entries", NULL);
1978         fp = g_fopen(file, "wb");
1979         g_free(file);
1980         if (fp) {
1981                 for (cur = files; cur; cur = cur->next) {
1982                         file = (char *)cur->data;
1983                         if (fprintf(fp, "1 1 %s\n", file) < 0)
1984                                 FILE_OP_ERROR(file, "fprintf");
1985                         g_free(file);
1986                 }
1987                 if (fclose(fp) == EOF)
1988                         FILE_OP_ERROR(file, "fclose");
1989         } else {
1990                 FILE_OP_ERROR(file, "fopen");
1991         }
1992         g_free(path);
1993         g_slist_free(files);
1994 }
1995
1996 gboolean vcal_meeting_export_calendar(const gchar *path, 
1997                                 const gchar *user, const gchar *pass,
1998                                 gboolean automatic)
1999 {
2000         GSList *list = vcal_folder_get_waiting_events();
2001         GSList *subs = NULL;
2002         GSList *cur;
2003         icalcomponent *calendar = NULL;
2004         gchar *file = NULL;
2005         gchar *tmpfile = get_tmp_file();
2006         gchar *internal_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2007                                 "vcalendar", G_DIR_SEPARATOR_S, 
2008                                 "internal.ics", NULL);
2009
2010         gboolean res = TRUE;
2011         long filesize = 0;
2012         
2013         multisync_export();
2014
2015         if (vcalprefs.export_subs && vcalprefs.export_enable)
2016                 subs = vcal_folder_get_webcal_events();
2017
2018         if (g_slist_length(list) == 0 && g_slist_length(subs) == 0) {
2019                 g_slist_free(list);
2020                 g_slist_free(subs);
2021                 if (!automatic) {
2022                         alertpanel_full(_("Empty calendar"),
2023                                         _("There is nothing to export."),
2024                                         GTK_STOCK_OK, NULL, NULL, FALSE,
2025                                         NULL, ALERT_NOTICE, G_ALERTDEFAULT);
2026                         return FALSE;
2027                 } else {
2028                         str_write_to_file("", tmpfile);
2029                         goto putfile;
2030                 }
2031         }
2032         
2033         calendar = 
2034                 icalcomponent_vanew(
2035                     ICAL_VCALENDAR_COMPONENT,
2036                     icalproperty_new_version("2.0"),
2037                     icalproperty_new_prodid(
2038                          "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2039                     icalproperty_new_calscale("GREGORIAN"),
2040                     (void*)0
2041             );  
2042
2043         for (cur = list; cur; cur = cur->next) {
2044                 VCalEvent *event = (VCalEvent *)cur->data;
2045                 vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
2046                 vcal_manager_free_event(event);
2047         }
2048
2049         if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
2050                 g_warning("can't export internal cal");
2051         }
2052         
2053         g_free(internal_file);
2054
2055         for (cur = subs; cur; cur = cur->next) {
2056                 /* Not to be freed */
2057                 icalcomponent *event = (icalcomponent *)cur->data;
2058                 vcal_manager_icalevent_dump(event, NULL, calendar);
2059         }
2060
2061         if (vcalprefs.export_enable || path == NULL) {
2062                 if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
2063                         alertpanel_error(_("Could not export the calendar."));
2064                         g_free(tmpfile);
2065                         icalcomponent_free(calendar);
2066                         g_slist_free(list);
2067                         g_slist_free(subs);
2068                         return FALSE;
2069                 }
2070                 filesize = strlen(icalcomponent_as_ical_string(calendar));
2071         }
2072
2073         icalcomponent_free(calendar);
2074         
2075 putfile:
2076         g_slist_free(list);
2077         g_slist_free(subs);
2078
2079         if (!path && !automatic)
2080                 file = filesel_select_file_save(_("Export calendar to ICS"), NULL);
2081         else
2082                 file = g_strdup(path);
2083
2084         if (automatic && (!path || strlen(path) == 0 || !vcalprefs.export_enable)) {
2085                 g_free(tmpfile);
2086                 g_free(file);
2087                 return TRUE;
2088         }
2089
2090         if (file 
2091         && strncmp(file, "http://", 7) 
2092         && strncmp(file, "https://", 8)
2093         && strncmp(file, "webcal://", 9)
2094         && strncmp(file, "webcals://", 10)
2095         && strncmp(file, "ftp://", 6)) {
2096                 gchar *afile = NULL;
2097                 if (file[0] != G_DIR_SEPARATOR)
2098                         afile=g_strdup_printf("%s%s%s", get_home_dir(), 
2099                                         G_DIR_SEPARATOR_S, file);
2100                 else
2101                         afile=g_strdup(file);
2102                 if (move_file(tmpfile, afile, TRUE) != 0) {
2103                         log_error(LOG_PROTOCOL, _("Couldn't export calendar to '%s'\n"),
2104                                 afile);
2105                         res = FALSE;
2106                 }
2107                 g_free(afile);
2108                 g_free(file);
2109         } else if (file) {
2110                 FILE *fp = g_fopen(tmpfile, "rb");
2111                 if (!strncmp(file, "webcal", 6)) {
2112                         gchar *tmp = g_strdup_printf("http%s", file+6);
2113                         g_free(file);
2114                         file = tmp;
2115                 }
2116                 if (fp) {
2117                         res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
2118                         fclose(fp);
2119                 }
2120                 g_free(file);
2121         }
2122         g_free(tmpfile);
2123         return res;
2124 }
2125
2126 gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
2127                                 const gchar *pass)
2128 {
2129         GSList *list = vcal_folder_get_waiting_events();
2130         GSList *cur;
2131         icalcomponent *calendar = NULL, *timezone = NULL, *tzc = NULL, *vfreebusy = NULL;
2132         gchar *file = NULL;
2133         gchar *tmpfile = get_tmp_file();
2134         gchar *internal_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2135                                 "vcalendar", G_DIR_SEPARATOR_S, 
2136                                 "internal.ifb", NULL);
2137         time_t whole_start = time(NULL);
2138         time_t whole_end = whole_start + (60*60*24*365);
2139         gboolean res = TRUE;
2140         struct icaltimetype itt_start, itt_end;
2141         long filesize = 0;
2142         
2143         multisync_export();
2144
2145         calendar = 
2146                 icalcomponent_vanew(
2147                     ICAL_VCALENDAR_COMPONENT,
2148                     icalproperty_new_version("2.0"),
2149                     icalproperty_new_prodid(
2150                          "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2151                     icalproperty_new_calscale("GREGORIAN"),
2152                     (void*)0
2153             );  
2154
2155         timezone = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT);
2156         
2157         icalcomponent_add_property(timezone,
2158                 icalproperty_new_tzid("UTC"));
2159         
2160         tzc = icalcomponent_new(ICAL_XSTANDARD_COMPONENT);
2161         icalcomponent_add_property(tzc,
2162                 icalproperty_new_dtstart(
2163                         icaltime_from_string("19700101T000000")));
2164         icalcomponent_add_property(tzc,
2165                 icalproperty_new_tzoffsetfrom(0.0));
2166         icalcomponent_add_property(tzc,
2167                 icalproperty_new_tzoffsetto(0.0));
2168         icalcomponent_add_property(tzc,
2169                 icalproperty_new_tzname("Greenwich meridian time"));
2170
2171         icalcomponent_add_component(timezone, tzc);
2172
2173         icalcomponent_add_component(calendar, timezone);
2174
2175         itt_start = icaltime_from_timet(whole_start, FALSE);
2176         itt_end = icaltime_from_timet(whole_end, FALSE);
2177         itt_start.second = itt_start.minute = itt_start.hour = 0;
2178         itt_end.second = 59; itt_end.minute = 59; itt_end.hour = 23;
2179
2180
2181         vfreebusy = 
2182             icalcomponent_vanew(
2183                 ICAL_VFREEBUSY_COMPONENT,
2184                 icalproperty_vanew_dtstart(itt_start, 0),
2185                 icalproperty_vanew_dtend(itt_end, 0),
2186                 (void*)0
2187                 );
2188
2189         debug_print("DTSTART:%s\nDTEND:%s\n",
2190                 icaltime_as_ical_string(itt_start),
2191                 icaltime_as_ical_string(itt_end));
2192
2193         for (cur = list; cur; cur = cur->next) {
2194                 VCalEvent *event = (VCalEvent *)cur->data;
2195                 icalproperty *prop;
2196                 struct icalperiodtype ipt;
2197         
2198                 ipt.start = icaltime_from_string(event->dtstart);
2199                 ipt.end = icaltime_from_string(event->dtend);
2200                 ipt.duration = icaltime_subtract(ipt.end, ipt.start);
2201                 if (icaltime_as_timet(ipt.start) <= icaltime_as_timet(itt_end) 
2202                  && icaltime_as_timet(ipt.end) >= icaltime_as_timet(itt_start)) {
2203                         prop = icalproperty_new_freebusy(ipt);
2204                         icalcomponent_add_property(vfreebusy, prop);
2205                 }
2206                 vcal_manager_free_event(event);
2207         }
2208
2209         icalcomponent_add_component(calendar, vfreebusy);
2210         
2211         if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
2212                 g_warning("can't export freebusy");
2213         }
2214         
2215         g_free(internal_file);
2216
2217         if (vcalprefs.export_freebusy_enable) {
2218                 if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
2219                         alertpanel_error(_("Could not export the freebusy info."));
2220                         g_free(tmpfile);
2221                         icalcomponent_free(calendar);
2222                         g_slist_free(list);
2223                         return FALSE;
2224                 }
2225                 filesize = strlen(icalcomponent_as_ical_string(calendar));
2226         }
2227
2228         icalcomponent_free(calendar);
2229         g_slist_free(list);
2230         
2231         if ((!path || strlen(path) == 0 || !vcalprefs.export_freebusy_enable)) {
2232                 g_free(tmpfile);
2233                 return TRUE;
2234         }
2235         file = g_strdup(path);
2236
2237
2238         if (file 
2239         && strncmp(file, "http://", 7) 
2240         && strncmp(file, "https://", 8)
2241         && strncmp(file, "webcal://", 9)
2242         && strncmp(file, "webcals://", 10)
2243         && strncmp(file, "ftp://", 6)) {
2244                 gchar *afile = NULL;
2245                 if (file[0] != G_DIR_SEPARATOR)
2246                         afile=g_strdup_printf("%s%s%s", get_home_dir(), 
2247                                         G_DIR_SEPARATOR_S, file);
2248                 else
2249                         afile=g_strdup(file);
2250                 if (move_file(tmpfile, file, TRUE) != 0) {
2251                         log_error(LOG_PROTOCOL, _("Couldn't export free/busy to '%s'\n"),
2252                                 afile);
2253                         res = FALSE;
2254                 }
2255                 g_free(afile);
2256                 g_free(file);
2257         } else if (file) {
2258                 FILE *fp = g_fopen(tmpfile, "rb");
2259                 if (!strncmp(file, "webcal", 6)) {
2260                         gchar *tmp = g_strdup_printf("http%s", file+6);
2261                         g_free(file);
2262                         file = tmp;
2263                 }
2264                 if (fp) {
2265                         res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
2266                         fclose(fp);
2267                 }
2268                 g_free(file);
2269         }
2270         g_free(tmpfile);
2271         return res;
2272 }