Make Message-ID string generation less confusing.
[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 <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         gchar buf[256];
1225         GSList *cur;
1226         PrefsAccount *account = NULL;
1227         gboolean res = FALSE;
1228         gboolean found_att = FALSE;
1229         Folder *folder = folder_find_from_name ("vCalendar", vcal_folder_get_class());
1230         gboolean redisp = FALSE;
1231
1232         if (meet->uid == NULL && meet->visible && 
1233             !check_attendees_availability(meet, FALSE, TRUE)) {
1234                 return FALSE;
1235         }
1236
1237         if (folder) {
1238                 MainWindow *mainwin = mainwindow_get_mainwindow();
1239                 if (mainwin->summaryview->folder_item == folder->inbox) {
1240                         redisp = TRUE;
1241                         summary_show(mainwin->summaryview, NULL);
1242                 }
1243         }
1244         gtk_widget_set_sensitive(meet->save_btn, FALSE);
1245         gtk_widget_set_sensitive(meet->avail_btn, FALSE);
1246         if (meet->window->window)
1247                 gdk_window_set_cursor(meet->window->window, watch_cursor);
1248
1249         organizer       = get_organizer(meet);
1250         account         = account_find_from_address(organizer, FALSE);
1251
1252         if(account == NULL) {
1253                 debug_print("can't get account from address %s\n", organizer);
1254                 g_free(organizer);
1255                 return FALSE;
1256         }
1257
1258         organizer_name  = get_organizer_name(meet);
1259
1260         if (meet->uid) {
1261                 uid     = g_strdup(meet->uid);
1262         } else {
1263                 uid     = prefs_account_generate_msgid(account);
1264         }
1265
1266         dtstart         = get_date(meet, TRUE);
1267         dtend           = get_date(meet, FALSE);
1268         location        = get_location(meet);
1269         summary         = get_summary(meet);
1270         description     = get_description(meet);
1271         
1272         event = vcal_manager_new_event(uid, organizer, organizer_name, location, summary, description,
1273                                         dtstart, dtend, NULL, tzid, NULL, meet->method, 
1274                                         meet->sequence,
1275                                         ICAL_VEVENT_COMPONENT);
1276         
1277         vcal_manager_update_answer(event, organizer, organizer_name, 
1278                                    ICAL_PARTSTAT_ACCEPTED,
1279                                    ICAL_CUTYPE_INDIVIDUAL);
1280         
1281         for (cur = meet->attendees; cur && cur->data; cur = cur->next) {
1282                 VCalAttendee *attendee = (VCalAttendee *)cur->data;
1283                 gchar *email = gtk_editable_get_chars(GTK_EDITABLE(attendee->address), 0, -1);
1284                 gint index = 0;
1285                 gchar *orig_email = email;
1286                 gchar *name = NULL;
1287                 enum icalparameter_cutype cutype = ICAL_CUTYPE_INDIVIDUAL;
1288                 enum icalparameter_partstat status = ICAL_PARTSTAT_NEEDSACTION;
1289                 
1290                 index = gtk_combo_box_get_active(GTK_COMBO_BOX(attendee->cutype));
1291                 
1292                 cutype = ICAL_CUTYPE_INDIVIDUAL + index;
1293                 if (attendee->status) {
1294                         if(!strcmp(attendee->status, "accepted"))
1295                                 status = ICAL_PARTSTAT_ACCEPTED;
1296                         if(!strcmp(attendee->status, "tentatively accepted"))
1297                                 status = ICAL_PARTSTAT_TENTATIVE;
1298                         if(!strcmp(attendee->status, "declined"))
1299                                 status = ICAL_PARTSTAT_DECLINED;
1300                         g_free(attendee->status);                       
1301                 }
1302                 if (strlen(email)) {
1303                         if (strstr(email, " <")) {
1304                                 name = email;
1305                                 email = strstr(email," <") + 2;
1306                                 *(strstr(name," <")) = '\0';
1307                                 if (strstr(email, ">"))
1308                                         *(strstr(email, ">")) = '\0';
1309                         } 
1310                         
1311                         vcal_manager_update_answer(event, email, name, 
1312                                         status, cutype);
1313                                         
1314                         found_att = strcmp(email, organizer);
1315                 }
1316                 g_free(orig_email);
1317         }
1318         
1319         if (found_att)
1320                 res = vcal_manager_request(account, event);
1321         else
1322                 res = TRUE;
1323         g_free(uid);
1324         g_free(organizer);
1325         g_free(organizer_name);
1326         g_free(dtstart);
1327         g_free(dtend);
1328         g_free(description);
1329         g_free(location);
1330         g_free(summary);
1331         vcal_manager_free_event(event);
1332
1333         gtk_widget_set_sensitive(meet->save_btn, TRUE);
1334         gtk_widget_set_sensitive(meet->avail_btn, avail_btn_can_be_sensitive());
1335         if (meet->window->window)
1336                 gdk_window_set_cursor(meet->window->window, NULL);
1337
1338         if (res) {
1339                 vcal_destroy(meet);
1340         } else {
1341                 alertpanel_error(_("Could not send the meeting invitation.\n"
1342                                    "Check the recipients."));
1343         }
1344
1345         if (folder)
1346                 folder_item_scan(folder->inbox);
1347
1348         if (folder && redisp) {
1349                 MainWindow *mainwin = mainwindow_get_mainwindow();
1350                 summary_show(mainwin->summaryview, folder->inbox);
1351         }
1352
1353         return res;
1354 }
1355
1356 static GList *get_predefined_times(void)
1357 {
1358         int h,m;
1359         GList *times = NULL;
1360         for (h = 0; h < 24; h++) {
1361                 for (m = 0; m < 60; m += 15) {
1362                         gchar *tmp = g_strdup_printf("%02d:%02d", h, m);
1363                         times = g_list_append(times, tmp);
1364                 }
1365         }
1366         return times;
1367 }
1368
1369 static VCalMeeting *vcal_meeting_create_real(VCalEvent *event, gboolean visible)
1370 {
1371         VCalMeeting *meet = g_new0(VCalMeeting, 1);
1372         GtkTextBuffer *buffer = NULL;
1373         GtkWidget *date_hbox, *date_vbox, *save_hbox, *label, *hbox;
1374         gchar *s = NULL;
1375         int i = 0, num = 0;
1376         GtkWidget *scrolledwin;
1377         GList *times = NULL;
1378         GList *accounts;
1379         gchar *time_text = NULL;
1380 #ifdef GENERIC_UMPC
1381         GtkWidget *notebook;
1382         GtkWidget *maemo_vbox0;
1383 #endif
1384
1385         if (!watch_cursor)
1386                 watch_cursor = gdk_cursor_new(GDK_WATCH);
1387
1388         meet->visible = visible;
1389
1390         meet->window            = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "vcal_meeting_gtk");
1391 #ifndef GENERIC_UMPC
1392         meet->table             = gtk_table_new(7, 2, FALSE);
1393 #else
1394         meet->table1            = gtk_table_new(4, 2, FALSE);
1395         meet->table2            = gtk_table_new(2, 2, FALSE);
1396 #endif
1397         meet->who               = gtk_combo_box_new_text();
1398         
1399         meet->start_c           = gtk_calendar_new();
1400         meet->end_c             = gtk_calendar_new();
1401
1402         meet->avail_evtbox  = gtk_event_box_new();
1403         meet->avail_img = gtk_image_new_from_stock
1404                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1405
1406         times = get_predefined_times();
1407
1408 #if !GTK_CHECK_VERSION(2, 24, 0)
1409         meet->start_time = gtk_combo_box_entry_new_text();
1410 #else
1411         meet->start_time = gtk_combo_box_text_new_with_entry();
1412 #endif
1413         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->start_time), -1);
1414 #if !GTK_CHECK_VERSION(2, 24, 0)        
1415         combobox_set_popdown_strings(GTK_COMBO_BOX(meet->start_time), times);
1416 #else   
1417         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(meet->start_time), times);
1418 #endif
1419         
1420 #if !GTK_CHECK_VERSION(2, 24, 0)
1421         meet->end_time = gtk_combo_box_entry_new_text();
1422 #else
1423         meet->end_time = gtk_combo_box_text_new_with_entry();
1424 #endif
1425         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->end_time), -1);
1426 #if !GTK_CHECK_VERSION(2, 24, 0)        
1427         combobox_set_popdown_strings(GTK_COMBO_BOX(meet->end_time), times);
1428 #else   
1429         combobox_set_popdown_strings(GTK_COMBO_BOX_TEXT(meet->end_time), times);
1430 #endif
1431
1432         list_free_strings(times);
1433         g_list_free(times);
1434
1435         meet->location          = gtk_entry_new();
1436         meet->summary           = gtk_entry_new();
1437         meet->description       = gtk_text_view_new();
1438         buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(meet->description));
1439         gtk_text_view_set_editable(GTK_TEXT_VIEW(meet->description), TRUE);
1440         gtk_text_buffer_add_selection_clipboard(buffer, gtk_clipboard_get(GDK_SELECTION_PRIMARY));
1441
1442         scrolledwin = gtk_scrolled_window_new(NULL, NULL);
1443         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwin),
1444                                        GTK_POLICY_AUTOMATIC,
1445                                        GTK_POLICY_AUTOMATIC);
1446         gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolledwin),
1447                                             GTK_SHADOW_IN);
1448         gtk_container_add(GTK_CONTAINER(scrolledwin), meet->description);
1449
1450         if (event) {
1451                 meet->uid = g_strdup(event->uid);
1452                 meet->sequence = event->sequence + 1;
1453                 meet->method = (event->method == ICAL_METHOD_CANCEL ?
1454                                 ICAL_METHOD_CANCEL:ICAL_METHOD_REQUEST);
1455
1456                 gtk_entry_set_text(GTK_ENTRY(meet->location), event->location);
1457                 gtk_entry_set_text(GTK_ENTRY(meet->summary), event->summary);   
1458                 gtk_text_buffer_set_text(buffer, event->description, -1);       
1459         } else 
1460                 meet->method = ICAL_METHOD_REQUEST;
1461         
1462         meet->save_btn          = gtk_button_new_with_label(_("Save & Send"));
1463         meet->avail_btn         = gtk_button_new_with_label(_("Check availability"));
1464
1465         meet->total_avail_evtbox  = gtk_event_box_new();
1466         meet->total_avail_img   = gtk_image_new_from_stock
1467                         (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_SMALL_TOOLBAR);
1468         meet->total_avail_msg = gtk_label_new("");
1469         
1470         gtk_widget_set_size_request(meet->total_avail_evtbox, 18, 16);
1471         gtk_event_box_set_visible_window(GTK_EVENT_BOX(meet->total_avail_evtbox), FALSE);
1472         gtk_container_add (GTK_CONTAINER(meet->total_avail_evtbox), meet->total_avail_img);
1473
1474         g_signal_connect(G_OBJECT(meet->save_btn), "clicked",
1475                          G_CALLBACK(send_meeting_cb), meet);
1476
1477         g_signal_connect(G_OBJECT(meet->avail_btn), "clicked",
1478                          G_CALLBACK(check_avail_cb), meet);
1479
1480         g_signal_connect(G_OBJECT(meet->window), "destroy",
1481                          G_CALLBACK(destroy_meeting_cb), meet);
1482         g_signal_connect(G_OBJECT(meet->window), "key_press_event",
1483                          G_CALLBACK(meeting_key_pressed), meet);
1484         
1485
1486         gtk_widget_set_size_request(meet->description, -1, 100);
1487         gtk_text_view_set_wrap_mode(GTK_TEXT_VIEW(meet->description), GTK_WRAP_WORD);
1488         
1489         if (!event || (event && !event->dtstart && !event->dtend)) {
1490                 time_t t = time (NULL)+ 3600;
1491                 struct tm buft1, buft2;
1492                 struct tm *lt = localtime_r (&t, &buft1);
1493                 mktime(lt);
1494                 gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1495                                         lt->tm_mday);
1496                 gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1497                                         lt->tm_mon, lt->tm_year + 1900);
1498         
1499                 time_text = g_strdup_printf("%02d:%02d", lt->tm_hour, 0);
1500                 combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1501                 g_free(time_text);
1502
1503                 t += 3600;
1504                 lt = localtime_r(&t, &buft2);
1505                 
1506                 gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1507                                         lt->tm_mday);
1508                 gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1509                                         lt->tm_mon, lt->tm_year + 1900);
1510
1511                 time_text = g_strdup_printf("%02d:%02d", lt->tm_hour, 0);
1512                 combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1513                 g_free(time_text);
1514
1515         } else {
1516                 gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1517                                         get_dtdate(event->dtstart, DAY));
1518                 gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1519                                         get_dtdate(event->dtend, DAY));
1520
1521                 gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1522                                         get_dtdate(event->dtstart, MONTH)-1,
1523                                         get_dtdate(event->dtstart, YEAR));
1524                 gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1525                                         get_dtdate(event->dtend, MONTH)-1,
1526                                         get_dtdate(event->dtend, YEAR));
1527
1528                 num = get_list_item_num(get_dtdate(event->dtstart, HOUR), 
1529                                         get_dtdate(event->dtstart, MINUTE));
1530                 if (num > -1) {
1531                         time_text = g_strdup_printf("%02d:%02d", get_dtdate(event->dtstart, HOUR), 
1532                                                                  get_dtdate(event->dtstart, MINUTE));
1533                         combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1534                         g_free(time_text);
1535                 } else {
1536                         gchar *tmp = g_strdup_printf("%02d:%02d",
1537                                         get_dtdate(event->dtstart, HOUR), 
1538                                         get_dtdate(event->dtstart, MINUTE));
1539                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->start_time))),
1540                                            tmp);
1541                         g_free(tmp);
1542                 }
1543
1544                 num = get_list_item_num(get_dtdate(event->dtend, HOUR), 
1545                                         get_dtdate(event->dtend, MINUTE));
1546                 if (num > -1) {
1547                         time_text = g_strdup_printf("%02d:%02d", get_dtdate(event->dtend, HOUR), 
1548                                                                  get_dtdate(event->dtend, MINUTE));
1549                         combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1550                         g_free(time_text);
1551                 } else {
1552                         gchar *tmp = g_strdup_printf("%02d:%02d",
1553                                         get_dtdate(event->dtend, HOUR), 
1554                                         get_dtdate(event->dtend, MINUTE));
1555                         gtk_entry_set_text(GTK_ENTRY(gtk_bin_get_child(GTK_BIN(meet->end_time))),
1556                                            tmp);
1557                         g_free(tmp);
1558                 }
1559         }
1560
1561         g_signal_connect(G_OBJECT(meet->start_c), "day-selected",
1562                          G_CALLBACK(meeting_start_changed), meet);
1563         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(meet->start_time))),
1564                          "changed",
1565                          G_CALLBACK(meeting_start_changed),
1566                          meet);
1567
1568         g_signal_connect(G_OBJECT(meet->end_c), "day-selected",
1569                          G_CALLBACK(meeting_end_changed), meet);
1570         g_signal_connect(G_OBJECT(gtk_bin_get_child(GTK_BIN(meet->end_time))),
1571                          "changed",
1572                          G_CALLBACK(meeting_end_changed),
1573                          meet);
1574
1575 #ifndef GENERIC_UMPC
1576         gtk_widget_set_size_request(meet->start_time, 80, -1);
1577         gtk_widget_set_size_request(meet->end_time, 80, -1);
1578 #else
1579         gtk_widget_set_size_request(meet->start_time, 120, -1);
1580         gtk_widget_set_size_request(meet->end_time, 120, -1);
1581 #endif
1582         
1583         date_hbox = gtk_hbox_new(FALSE, 6);
1584         date_vbox = gtk_vbox_new(FALSE, 6);
1585         hbox = gtk_hbox_new(FALSE, 6);
1586         label = gtk_label_new(g_strconcat("<b>",_("Starts at:"),"</b> ",NULL));
1587         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1588         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1589         
1590         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1591         gtk_box_pack_start(GTK_BOX(hbox), meet->start_time, FALSE, FALSE, 0);
1592         label = gtk_label_new(g_strconcat("<b> ",_("on:"),"</b>",NULL));
1593         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1594         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1595         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1596         gtk_box_pack_start(GTK_BOX(date_vbox), hbox, FALSE, FALSE, 0);
1597         gtk_box_pack_start(GTK_BOX(date_vbox), meet->start_c, FALSE, FALSE, 0);
1598         gtk_box_pack_start(GTK_BOX(date_hbox), date_vbox, FALSE, FALSE, 0);
1599
1600 #ifndef GENERIC_UMPC
1601         label = gtk_label_new(" "); 
1602 #else
1603         label = gtk_label_new(""); 
1604 #endif
1605         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1606         gtk_box_pack_start(GTK_BOX(date_hbox), label, TRUE, TRUE, 0);
1607
1608         date_vbox = gtk_vbox_new(FALSE, 6);
1609         hbox = gtk_hbox_new(FALSE, 6);
1610         label = gtk_label_new(g_strconcat("<b>",_("Ends at:"),"</b> ", NULL));
1611         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1612         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1613         
1614         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1615         gtk_box_pack_start(GTK_BOX(hbox), meet->end_time, FALSE, FALSE, 0);
1616         label = gtk_label_new(g_strconcat("<b> ",_("on:"),"</b>",NULL));
1617         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1618         gtk_label_set_use_markup(GTK_LABEL(label), TRUE);
1619         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1620         gtk_box_pack_start(GTK_BOX(date_vbox), hbox, FALSE, FALSE, 0);
1621         gtk_box_pack_start(GTK_BOX(date_vbox), meet->end_c, FALSE, FALSE, 0);
1622         gtk_box_pack_start(GTK_BOX(date_hbox), date_vbox, FALSE, FALSE, 0);
1623
1624         meet->attendees_vbox = gtk_vbox_new(FALSE, 6);
1625         gtk_widget_show_all(meet->attendees_vbox);
1626         if (!event) {
1627                 attendee_add(meet, NULL, NULL, NULL, NULL, TRUE);
1628         } else {
1629                 gboolean firstadd = TRUE;
1630                 GSList *list = vcal_manager_get_answers_emails(event);
1631                 while (list && list->data) {
1632                         gchar *address = (gchar *)list->data;
1633                         gchar *name = vcal_manager_get_attendee_name(event, address);
1634                         gchar *answer = vcal_manager_get_reply_text_for_attendee(event, address);
1635                         gchar *type = vcal_manager_get_cutype_text_for_attendee(event, address);
1636                         if (strcmp(event->organizer, address)) {
1637                                 attendee_add(meet, address, name, answer, type, firstadd);
1638                                 firstadd = FALSE;
1639                         }
1640                         g_free(name);
1641                         g_free(answer);
1642                         g_free(type);
1643                         list = list->next;
1644                 } 
1645                 
1646                 if (firstadd == TRUE)
1647                         attendee_add(meet, NULL, NULL, NULL, NULL, TRUE);
1648         }
1649
1650         if (!event) {
1651                 gtk_window_set_title(GTK_WINDOW(meet->window), _("New meeting"));
1652         } else {
1653                 gchar *title = g_strdup_printf(_("%s - Edit meeting"),
1654                         event->summary);
1655                 gtk_window_set_title(GTK_WINDOW(meet->window), title);
1656                 g_free(title);
1657         }
1658         address_completion_start(meet->window);
1659         
1660         accounts = account_get_list();
1661         g_return_val_if_fail(accounts != NULL, NULL);
1662
1663         for (i = 0; accounts != NULL; accounts = accounts->next) {
1664                 PrefsAccount *ac = (PrefsAccount *)accounts->data;
1665                 
1666                 if (ac->protocol == A_NNTP) {
1667                         continue;
1668                 }
1669                 if (!event && ac == account_get_cur_account()) {
1670                         num = i;
1671                 }
1672                 else if (event && !strcmp(ac->address, event->organizer))
1673                         num = i;
1674
1675                 meet->avail_accounts = g_slist_append(meet->avail_accounts, ac);
1676                 
1677                 if (ac->name)
1678                         s = g_strdup_printf("%s: %s <%s>",
1679                                                ac->account_name,
1680                                                ac->name, ac->address);
1681                 else
1682                         s = g_strdup_printf("%s: %s",
1683                                                ac->account_name, ac->address);
1684
1685                 gtk_combo_box_append_text(GTK_COMBO_BOX(meet->who), s);
1686                 g_free(s);
1687                 i++;
1688         }
1689         gtk_combo_box_set_active(GTK_COMBO_BOX(meet->who), num);
1690         
1691         save_hbox = gtk_hbox_new(FALSE, 6);
1692         gtk_box_pack_start(GTK_BOX(save_hbox), meet->save_btn, FALSE, FALSE, 0);
1693         gtk_box_pack_start(GTK_BOX(save_hbox), meet->avail_btn, FALSE, FALSE, 0);
1694         gtk_box_pack_start(GTK_BOX(save_hbox), meet->total_avail_evtbox, FALSE, FALSE, 0);
1695         gtk_box_pack_start(GTK_BOX(save_hbox), meet->total_avail_msg, FALSE, FALSE, 0);
1696         
1697         hbox = gtk_hbox_new(FALSE, 6);
1698         gtk_box_pack_start(GTK_BOX(hbox), meet->avail_evtbox, FALSE, FALSE, 0);
1699         gtk_box_pack_start(GTK_BOX(hbox), meet->who, TRUE, TRUE, 0);
1700
1701         gtk_widget_set_size_request(meet->avail_evtbox, 18, 16);
1702         gtk_event_box_set_visible_window(GTK_EVENT_BOX(meet->avail_evtbox), FALSE);
1703         gtk_container_add (GTK_CONTAINER(meet->avail_evtbox), meet->avail_img);
1704
1705 #ifndef GENERIC_UMPC
1706         TABLE_ADD_LINE(_("Organizer:"), hbox, FALSE);
1707         TABLE_ADD_LINE(_("Summary:"), meet->summary, TRUE);
1708         TABLE_ADD_LINE(_("Time:"), date_hbox, TRUE);
1709         TABLE_ADD_LINE(_("Location:"), meet->location, TRUE);
1710         TABLE_ADD_LINE(_("Description:"), scrolledwin, TRUE);
1711         TABLE_ADD_LINE(_("Attendees:"), meet->attendees_vbox, FALSE);
1712         TABLE_ADD_LINE("", save_hbox, TRUE);
1713         
1714         gtk_widget_set_size_request(meet->window, -1, -1);
1715         gtk_container_add(GTK_CONTAINER(meet->window), meet->table);
1716 #else
1717         TABLE_ADD_LINE(_("Organizer:"), hbox, FALSE, TRUE);
1718         TABLE_ADD_LINE(_("Summary:"), meet->summary, TRUE, TRUE);
1719         TABLE_ADD_LINE(_("Location:"), meet->location, FALSE, TRUE);
1720         TABLE_ADD_LINE(_("Description:"), scrolledwin, TRUE, TRUE);
1721         TABLE_ADD_LINE(_("Attendees:"), meet->attendees_vbox, FALSE, TRUE);
1722         TABLE_ADD_LINE("", date_hbox, TRUE, FALSE);
1723         
1724         notebook = gtk_notebook_new ();
1725         gtk_notebook_set_show_border (GTK_NOTEBOOK (notebook), FALSE);
1726         gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
1727                         meet->table1,
1728                         gtk_label_new_with_mnemonic(_("Event:")));
1729                         
1730         gtk_notebook_append_page(GTK_NOTEBOOK(notebook),
1731                         meet->table2,
1732                         gtk_label_new_with_mnemonic(_("Time:")));
1733         gtk_widget_show (notebook);
1734         
1735         maemo_vbox0 = gtk_vbox_new(FALSE, 3);
1736         gtk_box_pack_start(GTK_BOX(maemo_vbox0), notebook, TRUE, TRUE, 0);
1737         gtk_box_pack_start(GTK_BOX(maemo_vbox0), save_hbox, FALSE, FALSE, 0);
1738         
1739         gtk_widget_set_size_request(meet->window, -1, -1);
1740         gtk_container_add (GTK_CONTAINER (meet->window), maemo_vbox0);
1741         
1742         maemo_connect_key_press_to_mainwindow(GTK_WINDOW(meet->window));
1743 #endif
1744         if (visible) {
1745                 GSList *cur;
1746                 gtk_widget_show_all(meet->window);
1747                 for (cur = meet->attendees; cur; cur = cur->next) {
1748                         gtk_widget_hide(((VCalAttendee *)cur->data)->avail_img);
1749                 }
1750                 gtk_widget_hide(meet->avail_img);
1751                 gtk_widget_hide(meet->total_avail_img);
1752                 gtk_widget_set_sensitive(meet->avail_btn, avail_btn_can_be_sensitive());
1753         }
1754         return meet;
1755 }
1756
1757 VCalMeeting *vcal_meeting_create(VCalEvent *event)
1758 {
1759         return vcal_meeting_create_real(event, TRUE);
1760 }
1761
1762 VCalMeeting *vcal_meeting_create_with_start(VCalEvent *event, struct tm *sdate)
1763 {
1764         VCalMeeting *meet = vcal_meeting_create(event);
1765         int num = -1;
1766         gtk_calendar_select_day(GTK_CALENDAR(meet->start_c),
1767                                 sdate->tm_mday);
1768         gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1769                                 sdate->tm_mday);
1770
1771         gtk_calendar_select_month(GTK_CALENDAR(meet->start_c),
1772                                 sdate->tm_mon, sdate->tm_year+1900);
1773         gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1774                                 sdate->tm_mon, sdate->tm_year+1900);
1775
1776         if (sdate->tm_hour != 0) {
1777                 num = get_list_item_num(sdate->tm_hour, 0);
1778                 if (num > -1) {
1779                         gchar *time_text = g_strdup_printf("%02d:%02d", sdate->tm_hour, 0);
1780                         combobox_select_by_text(GTK_COMBO_BOX(meet->start_time), time_text);
1781                         g_free(time_text);
1782                 } 
1783                 if (sdate->tm_hour < 23) {
1784                         num = get_list_item_num(sdate->tm_hour+1, 0);
1785                         if (num > -1) {
1786                                 gchar *time_text = g_strdup_printf("%02d:%02d", sdate->tm_hour+1, 0);
1787                                 combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1788                                 g_free(time_text);
1789                         } 
1790                 } else {
1791                         struct tm tm_tomorrow;
1792                         gchar *time_text;
1793                         tm_tomorrow.tm_mday = sdate->tm_mday;
1794                         tm_tomorrow.tm_mon = sdate->tm_mon;
1795                         tm_tomorrow.tm_wday = sdate->tm_wday;
1796                         tm_tomorrow.tm_year = sdate->tm_year+1900;
1797                         tm_tomorrow.tm_hour = sdate->tm_hour;
1798                         orage_move_day(&tm_tomorrow, +1);
1799                         gtk_calendar_select_day(GTK_CALENDAR(meet->end_c),
1800                                                 tm_tomorrow.tm_mday);
1801                         gtk_calendar_select_month(GTK_CALENDAR(meet->end_c),
1802                                                 tm_tomorrow.tm_mon, tm_tomorrow.tm_year);
1803                         
1804                         time_text = g_strdup_printf("%02d:%02d", 0, 0);
1805                         combobox_select_by_text(GTK_COMBO_BOX(meet->end_time), time_text);
1806                         g_free(time_text);
1807                 }
1808         }
1809         return meet;
1810 }
1811
1812 VCalMeeting *vcal_meeting_create_hidden(VCalEvent *event)
1813 {
1814         return vcal_meeting_create_real(event, FALSE);
1815 }
1816
1817 gboolean vcal_meeting_send(VCalMeeting *meet)
1818 {
1819         return send_meeting_cb(NULL, meet);
1820 }
1821
1822 gboolean vcal_meeting_alert_check(gpointer data)
1823 {
1824         GSList *events = NULL, *cur = NULL;
1825
1826         if (!vcalprefs.alert_enable)
1827                 return TRUE;
1828
1829         events = vcal_folder_get_waiting_events();
1830
1831         for (cur = events; cur; cur = cur->next) {
1832                 VCalEvent *event = (VCalEvent *)cur->data;
1833                 time_t start, end, current;
1834                 gboolean warn = FALSE;
1835
1836                 tzset();
1837
1838                 start = icaltime_as_timet(icaltime_from_string(event->dtstart));
1839                 end = icaltime_as_timet(icaltime_from_string(event->dtend));
1840                 current = time(NULL);
1841                 
1842                 if (start - current <= (vcalprefs.alert_delay*60) 
1843                 &&  start - current + 60 > (vcalprefs.alert_delay*60)) {
1844                         warn = TRUE;
1845                 } else if (event->postponed - current <= (vcalprefs.alert_delay*60)
1846                 &&         event->postponed - current + 60 > (vcalprefs.alert_delay*60)) {
1847                         warn = TRUE;
1848                 }
1849                 if (warn) {
1850                         time_t tmpt = icaltime_as_timet((icaltime_from_string(event->dtstart)));
1851                         gchar *estart = NULL;
1852                         AlertValue aval;
1853                         int length = (end - start) / 60;
1854                         gchar *duration = NULL, *hours = NULL, *minutes = NULL;
1855                         gchar *message = NULL;
1856                         gchar *title = NULL;
1857                         gchar *label = NULL;
1858                         int postpone_min = 0;
1859
1860                         tzset();
1861
1862                         estart = g_strdup(ctime(&tmpt));
1863
1864                         if (length >= 60)
1865                                 hours = g_strdup_printf(ngettext("%d hour", "%d hours", 
1866                                                 (length/60) > 1 ? 2 : 1), length/60);
1867                         if (length%60)
1868                                 minutes = g_strdup_printf(ngettext("%d minute", "%d minutes",
1869                                                 length%60), length%60);
1870
1871                         duration = g_strdup_printf("%s%s%s",
1872                                         hours?hours:"",
1873                                         hours && minutes ? " ":"",
1874                                         minutes?minutes:"");
1875
1876                         g_free(hours);
1877                         g_free(minutes);
1878
1879                         title = g_strdup_printf(_("Upcoming event: %s"), event->summary);
1880                         message = g_strdup_printf(_("You have a meeting or event soon.\n"
1881                                          "It starts at %s and ends %s later.\n"
1882                      "Location: %s\n"
1883                                          "More information:\n\n"
1884                                          "%s"),
1885                                                 estart,
1886                                                 duration,
1887                                                 event->location?event->location:"",
1888                                                 event->description);
1889
1890                         g_free(duration);
1891                         g_free(estart);
1892
1893                         postpone_min = (vcalprefs.alert_delay/2 > 15) ? 15: (vcalprefs.alert_delay/2);
1894                         if (postpone_min == 0)
1895                                 postpone_min = 1;
1896
1897                         label = g_strdup_printf(ngettext("Remind me in %d minute", "Remind me in %d minutes",
1898                                                  postpone_min > 1 ? 2:1), 
1899                                                  postpone_min);
1900                         aval = alertpanel_full(title, message,
1901                                         label, GTK_STOCK_OK, NULL, FALSE,
1902                                         NULL, ALERT_NOTICE, G_ALERTDEFAULT);
1903                         g_free(label);
1904
1905                         g_free(title);
1906                         g_free(message);
1907
1908                         if (aval == G_ALERTDEFAULT) {
1909                                 if (event->postponed == 0)
1910                                         event->postponed = start + (postpone_min*60);
1911                                 else
1912                                         event->postponed += (postpone_min*60);
1913                         } else {
1914                                 event->postponed = (time_t)0;
1915                         }
1916                         vcal_manager_save_event(event, FALSE);
1917                 }
1918                 
1919                 vcal_manager_free_event((VCalEvent *)cur->data);
1920         }
1921         
1922         g_slist_free(events);
1923
1924         return TRUE;
1925 }
1926
1927 void multisync_export(void)
1928 {
1929         GSList *list = NULL;
1930         gchar *path = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
1931                                 "vcalendar", G_DIR_SEPARATOR_S, 
1932                                 "multisync", NULL);
1933         GSList *files = NULL;
1934         GSList *cur = NULL;
1935         gchar *file = NULL;
1936         gchar *tmp = NULL;
1937         gint i = 0;
1938         icalcomponent *calendar = NULL;
1939         FILE *fp;
1940
1941         if (is_dir_exist(path) && remove_dir_recursive(path) < 0) {
1942                 g_free(path);
1943                 return;
1944         }
1945         if (make_dir(path) != 0) {
1946                 g_free(path);
1947                 return;
1948         }
1949         
1950         list = vcal_folder_get_waiting_events();
1951         for (cur = list; cur; cur = cur->next) {
1952                 VCalEvent *event = (VCalEvent *)cur->data;
1953                 file = g_strdup_printf("multisync%lld-%d",
1954                                 (long long)time(NULL), i);
1955
1956                 i++;
1957
1958                 calendar = 
1959                         icalcomponent_vanew(
1960                             ICAL_VCALENDAR_COMPONENT,
1961                             icalproperty_new_version("2.0"),
1962                             icalproperty_new_prodid(
1963                                  "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
1964                             icalproperty_new_calscale("GREGORIAN"),
1965                             (void*)0
1966                     );  
1967                 vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
1968                 tmp = g_strconcat(path, G_DIR_SEPARATOR_S, file, NULL);
1969                 str_write_to_file(icalcomponent_as_ical_string(calendar), tmp);
1970                 g_free(tmp);
1971                 files = g_slist_append(files, file);
1972                 vcal_manager_free_event(event);
1973                 icalcomponent_free(calendar);
1974         }
1975
1976         g_slist_free(list);
1977         
1978         file = g_strconcat(path, G_DIR_SEPARATOR_S, "backup_entries", NULL);
1979         fp = g_fopen(file, "wb");
1980         g_free(file);
1981         if (fp) {
1982                 for (cur = files; cur; cur = cur->next) {
1983                         file = (char *)cur->data;
1984                         if (fprintf(fp, "1 1 %s\n", file) < 0)
1985                                 perror(file);
1986                         g_free(file);
1987                 }
1988                 if (fclose(fp) == EOF)
1989                         perror(file);
1990         } else {
1991                 perror(file);
1992         }
1993         g_free(path);
1994         g_slist_free(files);
1995 }
1996
1997 gboolean vcal_meeting_export_calendar(const gchar *path, 
1998                                 const gchar *user, const gchar *pass,
1999                                 gboolean automatic)
2000 {
2001         GSList *list = vcal_folder_get_waiting_events();
2002         GSList *subs = NULL;
2003         GSList *cur;
2004         icalcomponent *calendar = NULL;
2005         gchar *file = NULL;
2006         gchar *tmpfile = get_tmp_file();
2007         gchar *internal_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2008                                 "vcalendar", G_DIR_SEPARATOR_S, 
2009                                 "internal.ics", NULL);
2010
2011         gboolean res = TRUE;
2012         long filesize = 0;
2013         
2014         multisync_export();
2015
2016         if (vcalprefs.export_subs && vcalprefs.export_enable)
2017                 subs = vcal_folder_get_webcal_events();
2018
2019         if (g_slist_length(list) == 0 && g_slist_length(subs) == 0) {
2020                 g_slist_free(list);
2021                 g_slist_free(subs);
2022                 if (!automatic) {
2023                         alertpanel_full(_("Empty calendar"),
2024                                         _("There is nothing to export."),
2025                                         GTK_STOCK_OK, NULL, NULL, FALSE,
2026                                         NULL, ALERT_NOTICE, G_ALERTDEFAULT);
2027                         return FALSE;
2028                 } else {
2029                         str_write_to_file("", tmpfile);
2030                         goto putfile;
2031                 }
2032         }
2033         
2034         calendar = 
2035                 icalcomponent_vanew(
2036                     ICAL_VCALENDAR_COMPONENT,
2037                     icalproperty_new_version("2.0"),
2038                     icalproperty_new_prodid(
2039                          "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2040                     icalproperty_new_calscale("GREGORIAN"),
2041                     (void*)0
2042             );  
2043
2044         for (cur = list; cur; cur = cur->next) {
2045                 VCalEvent *event = (VCalEvent *)cur->data;
2046                 vcal_manager_event_dump(event, FALSE, FALSE, calendar, FALSE);
2047                 vcal_manager_free_event(event);
2048         }
2049
2050         if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
2051                 g_warning("can't export internal cal");
2052         }
2053         
2054         g_free(internal_file);
2055
2056         for (cur = subs; cur; cur = cur->next) {
2057                 /* Not to be freed */
2058                 icalcomponent *event = (icalcomponent *)cur->data;
2059                 vcal_manager_icalevent_dump(event, NULL, calendar);
2060         }
2061
2062         if (vcalprefs.export_enable || path == NULL) {
2063                 if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
2064                         alertpanel_error(_("Could not export the calendar."));
2065                         g_free(tmpfile);
2066                         icalcomponent_free(calendar);
2067                         g_slist_free(list);
2068                         g_slist_free(subs);
2069                         return FALSE;
2070                 }
2071                 filesize = strlen(icalcomponent_as_ical_string(calendar));
2072         }
2073
2074         icalcomponent_free(calendar);
2075         
2076 putfile:
2077         g_slist_free(list);
2078         g_slist_free(subs);
2079
2080         if (!path && !automatic)
2081                 file = filesel_select_file_save(_("Export calendar to ICS"), NULL);
2082         else
2083                 file = g_strdup(path);
2084
2085         if (automatic && (!path || strlen(path) == 0 || !vcalprefs.export_enable)) {
2086                 g_free(tmpfile);
2087                 g_free(file);
2088                 return TRUE;
2089         }
2090
2091         if (file 
2092         && strncmp(file, "http://", 7) 
2093         && strncmp(file, "https://", 8)
2094         && strncmp(file, "webcal://", 9)
2095         && strncmp(file, "webcals://", 10)
2096         && strncmp(file, "ftp://", 6)) {
2097                 gchar *afile = NULL;
2098                 if (file[0] != G_DIR_SEPARATOR)
2099                         afile=g_strdup_printf("%s%s%s", get_home_dir(), 
2100                                         G_DIR_SEPARATOR_S, file);
2101                 else
2102                         afile=g_strdup(file);
2103                 if (move_file(tmpfile, afile, TRUE) != 0) {
2104                         log_error(LOG_PROTOCOL, _("Couldn't export calendar to '%s'\n"),
2105                                 afile);
2106                         res = FALSE;
2107                 }
2108                 g_free(afile);
2109                 g_free(file);
2110         } else if (file) {
2111                 FILE *fp = g_fopen(tmpfile, "rb");
2112                 if (!strncmp(file, "webcal", 6)) {
2113                         gchar *tmp = g_strdup_printf("http%s", file+6);
2114                         g_free(file);
2115                         file = tmp;
2116                 }
2117                 if (fp) {
2118                         res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
2119                         fclose(fp);
2120                 }
2121                 g_free(file);
2122         }
2123         g_free(tmpfile);
2124         return res;
2125 }
2126
2127 gboolean vcal_meeting_export_freebusy(const gchar *path, const gchar *user,
2128                                 const gchar *pass)
2129 {
2130         GSList *list = vcal_folder_get_waiting_events();
2131         GSList *cur;
2132         icalcomponent *calendar = NULL, *timezone = NULL, *tzc = NULL, *vfreebusy = NULL;
2133         gchar *file = NULL;
2134         gchar *tmpfile = get_tmp_file();
2135         gchar *internal_file = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
2136                                 "vcalendar", G_DIR_SEPARATOR_S, 
2137                                 "internal.ifb", NULL);
2138         time_t whole_start = time(NULL);
2139         time_t whole_end = whole_start + (60*60*24*365);
2140         gboolean res = TRUE;
2141         struct icaltimetype itt_start, itt_end;
2142         long filesize = 0;
2143         
2144         multisync_export();
2145
2146         calendar = 
2147                 icalcomponent_vanew(
2148                     ICAL_VCALENDAR_COMPONENT,
2149                     icalproperty_new_version("2.0"),
2150                     icalproperty_new_prodid(
2151                          "-//Claws Mail//NONSGML Claws Mail Calendar//EN"),
2152                     icalproperty_new_calscale("GREGORIAN"),
2153                     (void*)0
2154             );  
2155
2156         timezone = icalcomponent_new(ICAL_VTIMEZONE_COMPONENT);
2157         
2158         icalcomponent_add_property(timezone,
2159                 icalproperty_new_tzid("UTC"));
2160         
2161         tzc = icalcomponent_new(ICAL_XSTANDARD_COMPONENT);
2162         icalcomponent_add_property(tzc,
2163                 icalproperty_new_dtstart(
2164                         icaltime_from_string("19700101T000000")));
2165         icalcomponent_add_property(tzc,
2166                 icalproperty_new_tzoffsetfrom(0.0));
2167         icalcomponent_add_property(tzc,
2168                 icalproperty_new_tzoffsetto(0.0));
2169         icalcomponent_add_property(tzc,
2170                 icalproperty_new_tzname("Greenwich meridian time"));
2171
2172         icalcomponent_add_component(timezone, tzc);
2173
2174         icalcomponent_add_component(calendar, timezone);
2175
2176         itt_start = icaltime_from_timet(whole_start, FALSE);
2177         itt_end = icaltime_from_timet(whole_end, FALSE);
2178         itt_start.second = itt_start.minute = itt_start.hour = 0;
2179         itt_end.second = 59; itt_end.minute = 59; itt_end.hour = 23;
2180
2181
2182         vfreebusy = 
2183             icalcomponent_vanew(
2184                 ICAL_VFREEBUSY_COMPONENT,
2185                 icalproperty_vanew_dtstart(itt_start, 0),
2186                 icalproperty_vanew_dtend(itt_end, 0),
2187                 (void*)0
2188                 );
2189
2190         debug_print("DTSTART:%s\nDTEND:%s\n",
2191                 icaltime_as_ical_string(itt_start),
2192                 icaltime_as_ical_string(itt_end));
2193
2194         for (cur = list; cur; cur = cur->next) {
2195                 VCalEvent *event = (VCalEvent *)cur->data;
2196                 icalproperty *prop;
2197                 struct icalperiodtype ipt;
2198         
2199                 ipt.start = icaltime_from_string(event->dtstart);
2200                 ipt.end = icaltime_from_string(event->dtend);
2201                 ipt.duration = icaltime_subtract(ipt.end, ipt.start);
2202                 if (icaltime_as_timet(ipt.start) <= icaltime_as_timet(itt_end) 
2203                  && icaltime_as_timet(ipt.end) >= icaltime_as_timet(itt_start)) {
2204                         prop = icalproperty_new_freebusy(ipt);
2205                         icalcomponent_add_property(vfreebusy, prop);
2206                 }
2207                 vcal_manager_free_event(event);
2208         }
2209
2210         icalcomponent_add_component(calendar, vfreebusy);
2211         
2212         if (str_write_to_file(icalcomponent_as_ical_string(calendar), internal_file) < 0) {
2213                 g_warning("can't export freebusy");
2214         }
2215         
2216         g_free(internal_file);
2217
2218         if (vcalprefs.export_freebusy_enable) {
2219                 if (str_write_to_file(icalcomponent_as_ical_string(calendar), tmpfile) < 0) {
2220                         alertpanel_error(_("Could not export the freebusy info."));
2221                         g_free(tmpfile);
2222                         icalcomponent_free(calendar);
2223                         g_slist_free(list);
2224                         return FALSE;
2225                 }
2226                 filesize = strlen(icalcomponent_as_ical_string(calendar));
2227         }
2228
2229         icalcomponent_free(calendar);
2230         g_slist_free(list);
2231         
2232         if ((!path || strlen(path) == 0 || !vcalprefs.export_freebusy_enable)) {
2233                 g_free(tmpfile);
2234                 return TRUE;
2235         }
2236         file = g_strdup(path);
2237
2238
2239         if (file 
2240         && strncmp(file, "http://", 7) 
2241         && strncmp(file, "https://", 8)
2242         && strncmp(file, "webcal://", 9)
2243         && strncmp(file, "webcals://", 10)
2244         && strncmp(file, "ftp://", 6)) {
2245                 gchar *afile = NULL;
2246                 if (file[0] != G_DIR_SEPARATOR)
2247                         afile=g_strdup_printf("%s%s%s", get_home_dir(), 
2248                                         G_DIR_SEPARATOR_S, file);
2249                 else
2250                         afile=g_strdup(file);
2251                 if (move_file(tmpfile, file, TRUE) != 0) {
2252                         log_error(LOG_PROTOCOL, _("Couldn't export free/busy to '%s'\n"),
2253                                 afile);
2254                         res = FALSE;
2255                 }
2256                 g_free(afile);
2257                 g_free(file);
2258         } else if (file) {
2259                 FILE *fp = g_fopen(tmpfile, "rb");
2260                 if (!strncmp(file, "webcal", 6)) {
2261                         gchar *tmp = g_strdup_printf("http%s", file+6);
2262                         g_free(file);
2263                         file = tmp;
2264                 }
2265                 if (fp) {
2266                         res = vcal_curl_put(file, fp, filesize, user, (pass != NULL ? pass : ""));
2267                         fclose(fp);
2268                 }
2269                 g_free(file);
2270         }
2271         g_free(tmpfile);
2272         return res;
2273 }