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