aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-itip-control.c
blob: 56276fe668e83ce9cc310b511be904a25acb2ed2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-itip-control.c
 *
 * Copyright (C) 2001  Ximian, Inc.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: JP Rosevear
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <glib.h>
#include <gtk/gtkmisc.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include <libgnomeui/gnome-stock.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-dialog-util.h>
#include <gal/util/e-unicode-i18n.h>
#include <gal/widgets/e-unicode.h>
#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-stream.h>
#include <ical.h>
#include <cal-util/cal-component.h>
#include <cal-util/timeutil.h>
#include <cal-client/cal-client.h>
#include <e-util/e-time-utils.h>
#include <e-util/e-dialog-widgets.h>
#include "calendar-config.h"
#include "itip-utils.h"
#include "e-itip-control.h"

struct _EItipControlPrivate {
    GtkWidget *html;

    GtkWidget *count;
    GtkWidget *next;
    GtkWidget *prev;

    CalClient *event_client;
    CalClient *task_client;

    char *vcalendar;
    CalComponent *comp;
    icalcomponent *main_comp;
    icalcomponent *ical_comp;
    icalcomponent *top_level;
    icalcompiter iter;
    icalproperty_method method;

    int current;
    int total;

    GList *addresses;
    gchar *from_address;
    gchar *my_address;
};

/* HTML Strings */
#define HTML_BODY_START "<body bgcolor=\"#ffffff\" text=\"#000000\" link=\"#336699\">"
#define HTML_SEP        "<hr color=#336699 align=\"left\" width=450>"
#define HTML_BODY_END   "</body>"
#define HTML_FOOTER     "</html>"

/* We don't use these now, as we need to be able to translate the text.
   But I've left these here, in case there are problems with the new code. */
#if 0
#define PUBLISH_OPTIONS "<form><b>Choose an action:</b>&nbsp<select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"U\">Update</option></select>&nbsp &nbsp \
<input TYPE=Submit name=\"ok\" value=\"OK\"></form>"

#define REQUEST_OPTIONS "<form><b>Choose an action:</b>&nbsp<select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"A\">Accept</option> \
<option VALUE=\"T\">Tentatively accept</option> \
<option VALUE=\"D\">Decline</option></select>&nbsp \
<input TYPE=\"checkbox\" name=\"rsvp\" value=\"1\" checked>RSVP&nbsp&nbsp\
<input TYPE=\"submit\" name=\"ok\" value=\"OK\"><br> \
</form>"

#define REQUEST_FB_OPTIONS "<form><b>Choose an action:</b><select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"F\">Send Free/Busy Information</option></select>&nbsp &nbsp \
<input TYPE=Submit name=\"ok\" value=\"OK\"></form>"

#define REPLY_OPTIONS "<form><b>Choose an action:</b><select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"R\">Update respondent status</option></select>&nbsp &nbsp \
<input TYPE=Submit name=\"ok\" value=\"OK\"></form>"

#define REFRESH_OPTIONS "<form><b>Choose an action:</b><select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"S\">Send Latest Information</option></select>&nbsp &nbsp \
<input TYPE=Submit name=\"ok\" value=\"OK\"></form>"

#define CANCEL_OPTIONS "<form><b>Choose an action:</b><select NAME=\"action\" SIZE=\"1\"> \
<option VALUE=\"C\">Cancel</option></select>&nbsp &nbsp \
<input TYPE=Submit name=\"ok\" value=\"OK\"></form>"
#endif

static void class_init  (EItipControlClass   *klass);
static void init    (EItipControl        *itip);
static void destroy (GtkObject               *obj);

static void prev_clicked_cb (GtkWidget *widget, gpointer data);
static void next_clicked_cb (GtkWidget *widget, gpointer data);
static void url_requested_cb (GtkHTML *html, const gchar *url, GtkHTMLStream *handle, gpointer data);
static void ok_clicked_cb (GtkHTML *html, const gchar *method, const gchar *url, const gchar *encoding, gpointer data);

static GtkVBoxClass *parent_class = NULL;


GtkType
e_itip_control_get_type (void)
{
    static GtkType type = 0;

    if (type == 0) {
        static const GtkTypeInfo info =
        {
            "EItipControl",
            sizeof (EItipControl),
            sizeof (EItipControlClass),
            (GtkClassInitFunc) class_init,
            (GtkObjectInitFunc) init,
            /* reserved_1 */ NULL,
            /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };

        type = gtk_type_unique (gtk_vbox_get_type (), &info);
    }

    return type;
}

static void
class_init (EItipControlClass *klass)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS (klass);

    parent_class = gtk_type_class (gtk_vbox_get_type ());

    object_class->destroy = destroy;
}


/* Calendar Server routines */
static void
start_calendar_server_cb (CalClient *cal_client,
              CalClientOpenStatus status,
              gpointer data)
{
    gboolean *success = data;

    if (status == CAL_CLIENT_OPEN_SUCCESS)
        *success = TRUE;
    else
        *success = FALSE;

    gtk_main_quit (); /* end the sub event loop */
}

static CalClient *
start_calendar_server (gchar *uri, gboolean tasks)
{
    CalClient *client;
    char *cal_uri;
    gboolean success;

    client = cal_client_new ();

    if (uri)
        cal_uri = g_strdup (uri);
    else {
        if (tasks)
            cal_uri = g_concat_dir_and_file (
                g_get_home_dir (),
                "evolution/local/Tasks/tasks.ics");
        else
            cal_uri = g_concat_dir_and_file (
                g_get_home_dir (),
                "evolution/local/Calendar/calendar.ics");
    }

    gtk_signal_connect (GTK_OBJECT (client), "cal_opened",
                start_calendar_server_cb, &success);

    if (!cal_client_open_calendar (client, cal_uri, FALSE))
        return NULL;

    /* run a sub event loop to turn cal-client's async load
       notification into a synchronous call */
    gtk_main ();

    if (success)
        return client;

    return NULL;
}

static void
init (EItipControl *itip)
{
    EItipControlPrivate *priv;
    GtkWidget *hbox, *scrolled_window;

    priv = g_new0 (EItipControlPrivate, 1);

    itip->priv = priv;

    /* Addresses */
    priv->addresses = itip_addresses_get ();

    /* Header */
    priv->prev = gnome_stock_button (GNOME_STOCK_BUTTON_PREV);
    gtk_widget_show (priv->prev);
    priv->next = gnome_stock_button (GNOME_STOCK_BUTTON_NEXT);
    gtk_widget_show (priv->next);

    hbox = gtk_hbox_new (FALSE, 0);
    gtk_box_pack_start (GTK_BOX (hbox), priv->prev, FALSE, FALSE, 4);
    gtk_box_pack_start (GTK_BOX (hbox), priv->count, TRUE, TRUE, 4);
    gtk_box_pack_start (GTK_BOX (hbox), priv->next, FALSE, FALSE, 4);
    gtk_widget_show (hbox);

    gtk_signal_connect (GTK_OBJECT (priv->prev), "clicked",
                GTK_SIGNAL_FUNC (prev_clicked_cb), itip);
    gtk_signal_connect (GTK_OBJECT (priv->next), "clicked",
                GTK_SIGNAL_FUNC (next_clicked_cb), itip);

    /* Get the cal clients */
    priv->event_client = start_calendar_server (
        calendar_config_get_default_uri (), FALSE);
    if (priv->event_client == NULL)
        g_warning ("Unable to start calendar client");

    priv->task_client = start_calendar_server (
        calendar_config_get_default_tasks_uri (), FALSE);
    if (priv->task_client == NULL)
        g_warning ("Unable to start calendar client");

    /* Html Widget */
    priv->html = gtk_html_new ();
    gtk_html_set_default_content_type (GTK_HTML (priv->html), 
                       "text/html; charset=utf-8");

    gtk_widget_show (priv->html);

    scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_window),
                    GTK_POLICY_AUTOMATIC,
                    GTK_POLICY_AUTOMATIC);
    gtk_widget_show (scrolled_window);

    gtk_container_add (GTK_CONTAINER (scrolled_window), priv->html);
    gtk_widget_set_usize (scrolled_window, 600, 400);
    gtk_box_pack_start (GTK_BOX (itip), scrolled_window, FALSE, FALSE, 4);

    gtk_signal_connect (GTK_OBJECT (priv->html), "url_requested",
                url_requested_cb, itip);
    gtk_signal_connect (GTK_OBJECT (priv->html), "submit",
                ok_clicked_cb, itip);

}

static void
clean_up (EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    g_free (priv->vcalendar);
    priv->vcalendar = NULL;

    if (priv->comp)
        gtk_object_unref (GTK_OBJECT (priv->comp));
    priv->comp = NULL;

    icalcomponent_free (priv->top_level);
    priv->top_level = NULL;
    icalcomponent_free (priv->main_comp);
    priv->main_comp = NULL;
    priv->ical_comp = NULL;

    priv->current = 0;
    priv->total = 0;

    priv->my_address = NULL;
    g_free (priv->from_address);
    priv->from_address = NULL;
}

static void
destroy (GtkObject *obj)
{
    EItipControl *itip = E_ITIP_CONTROL (obj);
    EItipControlPrivate *priv;

    priv = itip->priv;

    clean_up (itip);

    itip_addresses_free (priv->addresses);
    priv->addresses = NULL;

    gtk_object_unref (GTK_OBJECT (priv->event_client));
    gtk_object_unref (GTK_OBJECT (priv->task_client));

    g_free (priv);

    if (GTK_OBJECT_CLASS (parent_class)->destroy)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (obj);
}

GtkWidget *
e_itip_control_new (void)
{
    return gtk_type_new (E_TYPE_ITIP_CONTROL);
}

static void
find_my_address (EItipControl *itip, icalcomponent *ical_comp)
{
    EItipControlPrivate *priv;
    icalproperty *prop;
    const char *attendee, *text;
    icalvalue *value;

    priv = itip->priv;

    for (prop = icalcomponent_get_first_property (ical_comp, ICAL_ATTENDEE_PROPERTY);
         prop != NULL;
         prop = icalcomponent_get_next_property (ical_comp, ICAL_ATTENDEE_PROPERTY))
    {
        GList *l;

        value = icalproperty_get_value (prop);
        if (!value)
            continue;

        attendee = icalvalue_get_string (value);

        text = itip_strip_mailto (attendee);
        for (l = priv->addresses; l != NULL; l = l->next) {
            ItipAddress *a = l->data;

            if (!strcmp (a->address, text)) {
                priv->my_address = a->address;
                return;
            }
        }
    }
}

static icalproperty *
find_attendee (icalcomponent *ical_comp, const char *address)
{
    icalproperty *prop;
    const char *attendee, *text;
    icalvalue *value;

    g_return_val_if_fail (address != NULL, NULL);

    for (prop = icalcomponent_get_first_property (ical_comp, ICAL_ATTENDEE_PROPERTY);
         prop != NULL;
         prop = icalcomponent_get_next_property (ical_comp, ICAL_ATTENDEE_PROPERTY))
    {
        value = icalproperty_get_value (prop);
        if (!value)
            continue;

        attendee = icalvalue_get_string (value);

        text = itip_strip_mailto (attendee);
        if (strstr (text, address))
            break;
    }

    return prop;
}

static icalparameter_partstat
find_attendee_partstat (icalcomponent *ical_comp, const char *address)
{
    icalproperty *prop;

    prop = find_attendee (ical_comp, address);
    if (prop != NULL) {
        icalparameter *param;

        param = icalproperty_get_first_parameter (prop, ICAL_PARTSTAT_PARAMETER);
        if (param != NULL)
            return icalparameter_get_partstat (param);
    }

    return ICAL_PARTSTAT_NONE;
}

static void
set_label (EItipControl *itip)
{
    EItipControlPrivate *priv;
    gchar *text;

    priv = itip->priv;

    text = g_strdup_printf ("%d of %d", priv->current, priv->total);
    gtk_label_set_text (GTK_LABEL (priv->count), text);

}

static void
set_button_status (EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    if (priv->current == priv->total)
        gtk_widget_set_sensitive (priv->next, FALSE);
    else
        gtk_widget_set_sensitive (priv->next, TRUE);

    if (priv->current == 1)
        gtk_widget_set_sensitive (priv->prev, FALSE);
    else
        gtk_widget_set_sensitive (priv->prev, TRUE);
}

static void
write_label_piece (EItipControl *itip, CalComponentDateTime *dt,
           char *buffer, int size,
           const char *stext, const char *etext)
{
    EItipControlPrivate *priv;
    struct tm tmp_tm;
    char time_buf[64], *time_utf8;
    icaltimezone *zone = NULL;
    char *display_name;

    priv = itip->priv;

    /* UTC times get converted to the current timezone. This is done for
       the COMPLETED property, which is always in UTC, and also because
       Outlook sends simple events as UTC times. */
    if (dt->value->is_utc) {
        char *location = calendar_config_get_timezone ();
        zone = icaltimezone_get_builtin_timezone (location);
        icaltimezone_convert_time (dt->value, icaltimezone_get_utc_timezone (), zone);
    }

    tmp_tm = icaltimetype_to_tm (dt->value);

    if (stext != NULL)
        strcat (buffer, stext);

    e_time_format_date_and_time (&tmp_tm,
                     calendar_config_get_24_hour_format (),
                     FALSE, FALSE,
                     time_buf, sizeof (time_buf));

    time_utf8 = e_utf8_from_locale_string (time_buf);
    strcat (buffer, time_utf8);
    g_free (time_utf8);

    if (!dt->value->is_utc && dt->tzid) {
        zone = icalcomponent_get_timezone (priv->top_level, dt->tzid);
    }

    /* Output timezone after time, e.g. " America/New_York". */
    if (zone) {
        display_name = icaltimezone_get_display_name (zone);
        /* These are ASCII strings, so should be OK as UTF-8.*/
        if (display_name) {
            strcat (buffer, " ");
            strcat (buffer, display_name);
        }
    }

    if (etext != NULL)
        strcat (buffer, etext);
}

static void
set_date_label (EItipControl *itip, GtkHTML *html, GtkHTMLStream *html_stream,
        CalComponent *comp)
{
    EItipControlPrivate *priv;
    CalComponentDateTime datetime;
    static char buffer[1024];
    gboolean wrote = FALSE, task_completed = FALSE;
    CalComponentVType type;

    priv = itip->priv;

    type = cal_component_get_vtype (comp);

    buffer[0] = '\0';
    cal_component_get_dtstart (comp, &datetime);
    if (datetime.value) {
        switch (type) {
        case CAL_COMPONENT_EVENT:
            write_label_piece (itip, &datetime, buffer, 1024,
                       U_("Meeting begins: <b>"),
                       "</b><br>");
            break;
        case CAL_COMPONENT_TODO:
            write_label_piece (itip, &datetime, buffer, 1024,
                       U_("Task begins: <b>"),
                       "</b><br>");
            break;
        case CAL_COMPONENT_FREEBUSY:
            write_label_piece (itip, &datetime, buffer, 1024,
                       U_("Free/Busy info begins: <b>"),
                       "</b><br>");
            break;
        default:
            write_label_piece (itip, &datetime, buffer, 1024, U_("Begins: <b>"), "</b><br>");
        }
        gtk_html_write (html, html_stream, buffer, strlen(buffer));
        wrote = TRUE;
    }
    cal_component_free_datetime (&datetime);

    buffer[0] = '\0';
    cal_component_get_dtend (comp, &datetime);
    if (datetime.value){
        switch (type) {
        case CAL_COMPONENT_EVENT:
            write_label_piece (itip, &datetime, buffer, 1024, U_("Meeting ends: <b>"), "</b><br>");
            break;
        case CAL_COMPONENT_FREEBUSY:
            write_label_piece (itip, &datetime, buffer, 1024, U_("Free/Busy info ends: <b>"),
                       "</b><br>");
            break;
        default:
            write_label_piece (itip, &datetime, buffer, 1024, U_("Ends: <b>"), "</b><br>");
        }
        gtk_html_write (html, html_stream, buffer, strlen (buffer));
        wrote = TRUE;
    }
    cal_component_free_datetime (&datetime);

    buffer[0] = '\0';
    datetime.tzid = NULL;
    cal_component_get_completed (comp, &datetime.value);
    if (type == CAL_COMPONENT_TODO && datetime.value) {
        /* Pass TRUE as is_utc, so it gets converted to the current
           timezone. */
        datetime.value->is_utc = TRUE;
        write_label_piece (itip, &datetime, buffer, 1024, U_("Task Completed: <b>"), "</b><br>");
        gtk_html_write (html, html_stream, buffer, strlen (buffer));
        wrote = TRUE;
        task_completed = TRUE;
    }
    cal_component_free_datetime (&datetime);

    buffer[0] = '\0';
    cal_component_get_due (comp, &datetime);
    if (type == CAL_COMPONENT_TODO && !task_completed && datetime.value) {
        write_label_piece (itip, &datetime, buffer, 1024, U_("Task Due: <b>"), "</b><br>");
        gtk_html_write (html, html_stream, buffer, strlen (buffer));
        wrote = TRUE;
    }

    cal_component_free_datetime (&datetime);

    if (wrote)
        gtk_html_stream_printf (html_stream, "<br>");
}

static void
set_message (GtkHTML *html, GtkHTMLStream *html_stream, const gchar *message, gboolean err)
{
    if (message == NULL)
        return;


    if (err) {
        gtk_html_stream_printf (html_stream, "<b><font color=\"#ff0000\">%s</font></b><br><br>", message);
    } else {
        gtk_html_stream_printf (html_stream, "<b>%s</b><br><br>", message);
    }
}

static void
write_error_html (EItipControl *itip, const gchar *itip_err)
{
    EItipControlPrivate *priv;
    GtkHTMLStream *html_stream;

    priv = itip->priv;

    /* Html widget */
    html_stream = gtk_html_begin (GTK_HTML (priv->html));
    gtk_html_stream_printf (html_stream,
                "<html><head><title>%s</title></head>",
                U_("iCalendar Information"));

    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_BODY_START, strlen(HTML_BODY_START));

    /* The table */
    gtk_html_stream_printf (html_stream, "<table width=450 cellspacing=\"0\" cellpadding=\"4\" border=\"0\">");
    /* The column for the image */
    gtk_html_stream_printf (html_stream, "<tr><td width=48 align=\"center\" valign=\"top\" rowspan=\"8\">");
    /* The image */
    gtk_html_stream_printf (html_stream, "<img src=\"/meeting-request.png\"></td>");

    gtk_html_stream_printf (html_stream, "<td align=\"left\" valign=\"top\">");

    /* Title */
    set_message (GTK_HTML (priv->html), html_stream, U_("iCalendar Error"), TRUE);

    /* Error */
    gtk_html_write (GTK_HTML (priv->html), html_stream, itip_err, strlen(itip_err));

    /* Clean up */
    gtk_html_stream_printf (html_stream, "</td></tr></table>");

    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_BODY_END, strlen(HTML_BODY_END));
    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_FOOTER, strlen(HTML_FOOTER));

    gtk_html_end (GTK_HTML (priv->html), html_stream, GTK_HTML_STREAM_OK);
}

static void
write_html (EItipControl *itip, const gchar *itip_desc, const gchar *itip_title, const gchar *options)
{
    EItipControlPrivate *priv;
    GtkHTMLStream *html_stream;
    CalComponentText text;
    CalComponentOrganizer organizer;
    CalComponentAttendee *attendee;
    GSList *attendees, *l = NULL;
    gchar *html;
    const gchar *const_html;

    priv = itip->priv;

    /* Html widget */
    html_stream = gtk_html_begin (GTK_HTML (priv->html));
    gtk_html_stream_printf (html_stream,
                "<html><head><title>%s</title></head>",
                U_("iCalendar Information"));
    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_BODY_START, strlen(HTML_BODY_START));

    /* The table */
    const_html = "<table width=450 cellspacing=\"0\" cellpadding=\"4\" border=\"0\">";
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    /* The column for the image */
    const_html = "<tr><td width=48 align=\"center\" valign=\"top\" rowspan=\"8\">";
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    /* The image */
    const_html = "<img src=\"/meeting-request.png\"></td>";
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    const_html = "<td align=\"left\" valign=\"top\">";
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    switch (priv->method) {
    case ICAL_METHOD_REFRESH:
    case ICAL_METHOD_REPLY:
        /* An attendee sent this */
        cal_component_get_attendee_list (priv->comp, &attendees);
        if (attendees != NULL) {
            attendee = attendees->data;
            html = g_strdup_printf (itip_desc,
                        attendee->cn ?
                        attendee->cn :
                        itip_strip_mailto (attendee->value));
        } else {
            html = g_strdup_printf (itip_desc, U_("An unknown person"));
        }
        break;
    case ICAL_METHOD_PUBLISH:
    case ICAL_METHOD_REQUEST:
    case ICAL_METHOD_ADD:
    case ICAL_METHOD_CANCEL:
    default:
        /* The organizer sent this */
        cal_component_get_organizer (priv->comp, &organizer);
        if (organizer.value != NULL)
            html = g_strdup_printf (itip_desc,
                        organizer.cn ?
                        organizer.cn :
                        itip_strip_mailto (organizer.value));
        else
            html = g_strdup_printf (itip_desc, U_("An unknown person"));
        break;
    }
    gtk_html_write (GTK_HTML (priv->html), html_stream, html, strlen(html));
    g_free (html);

    /* Describe what the user can do */
    const_html = U_("<br> Please review the following information, "
            "and then select an action from the menu below.");
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    /* Separator */
    gtk_html_write (GTK_HTML (priv->html), html_stream, HTML_SEP, strlen (HTML_SEP));

    /* Title */
    set_message (GTK_HTML (priv->html), html_stream, itip_title, FALSE);

    /* Date information */
    set_date_label (itip, GTK_HTML (priv->html), html_stream, priv->comp);

    /* Summary */
    cal_component_get_summary (priv->comp, &text);
    gtk_html_stream_printf (html_stream, "<b>%s</b> %s<br><br>",
                U_("Summary:"), text.value ? text.value : U_("<i>None</i>"));

    /* Description */
    cal_component_get_description_list (priv->comp, &l);
    if (l)
        text = *((CalComponentText *)l->data);

    if (l && text.value) {
        gtk_html_stream_printf (html_stream, "<b>%s</b> %s",
                    U_("Description:"), text.value);
    }
    cal_component_free_text_list (l);

    /* Separator */
    gtk_html_write (GTK_HTML (priv->html), html_stream, HTML_SEP, strlen (HTML_SEP));

    /* Options */
    if (options != NULL)
        gtk_html_write (GTK_HTML (priv->html), html_stream, options, strlen (options));

    const_html = "</td></tr></table>";
    gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen(const_html));

    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_BODY_END, strlen(HTML_BODY_END));
    gtk_html_write (GTK_HTML (priv->html), html_stream,
            HTML_FOOTER, strlen(HTML_FOOTER));

    gtk_html_end (GTK_HTML (priv->html), html_stream, GTK_HTML_STREAM_OK);
}


static char*
get_publish_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"U\">%s</option>"
                "</select>&nbsp &nbsp "
                "<input TYPE=Submit name=\"ok\" value=\"%s\">"
                "</form>",
                U_("Choose an action:"),
                U_("Update"),
                U_("OK"));
}

static char*
get_request_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"A\">%s</option> "
                "<option VALUE=\"T\">%s</option> "
                "<option VALUE=\"D\">%s</option></select>&nbsp "
                "<input TYPE=\"checkbox\" name=\"rsvp\" value=\"1\" checked>%s&nbsp&nbsp"
                "<input TYPE=\"submit\" name=\"ok\" value=\"%s\"><br> "
                "</form>",
                U_("Choose an action:"),
                U_("Accept"),
                U_("Tentatively accept"),
                U_("Decline"),
                U_("RSVP"),
                U_("OK"));
}

static char*
get_request_fb_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"F\">%s</option></select>&nbsp &nbsp "
                "<input TYPE=Submit name=\"ok\" value=\"%s\">"
                "</form>",
                U_("Choose an action:"),
                U_("Send Free/Busy Information"),
                U_("OK"));
}

static char*
get_reply_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"R\">%s</option></select>&nbsp &nbsp "
                "<input TYPE=Submit name=\"ok\" value=\"%s\">"
                "</form>",
                U_("Choose an action:"),
                U_("Update respondent status"),
                U_("OK"));
}

#if 0
static char*
get_refresh_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"S\">%s</option></select>&nbsp &nbsp "
                "<input TYPE=Submit name=\"ok\" value=\"%s\">"
                "</form>",
                U_("Choose an action:"),
                U_("Send Latest Information"),
                U_("OK"));
}
#endif

static char*
get_cancel_options ()
{
    return g_strdup_printf ("<form><b>%s</b>&nbsp"
                "<select NAME=\"action\" SIZE=\"1\"> "
                "<option VALUE=\"C\">%s</option></select>&nbsp &nbsp "
                "<input TYPE=Submit name=\"ok\" value=\"%s\">"
                "</form>",
                U_("Choose an action:"),
                U_("Cancel"),
                U_("OK"));
}

static void
show_current_event (EItipControl *itip)
{
    EItipControlPrivate *priv;
    const gchar *itip_title, *itip_desc;
    char *options;

    priv = itip->priv;

    switch (priv->method) {
    case ICAL_METHOD_PUBLISH:
        itip_desc = U_("<b>%s</b> has published meeting information.");
        itip_title = U_("Meeting Information");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REQUEST:
        itip_desc = U_("<b>%s</b> requests your presence at a meeting.");
        itip_title = U_("Meeting Proposal");
        options = get_request_options ();
        break;
    case ICAL_METHOD_ADD:
        itip_desc = U_("<b>%s</b> wishes to add to an existing meeting.");
        itip_title = U_("Meeting Update");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REFRESH:
        itip_desc = U_("<b>%s</b> wishes to receive the latest meeting information.");
        itip_title = U_("Meeting Update Request");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = U_("<b>%s</b> has replied to a meeting request.");
        itip_title = U_("Meeting Reply");
        options = get_reply_options ();
        break;
    case ICAL_METHOD_CANCEL:
        itip_desc = U_("<b>%s</b> has cancelled a meeting.");
        itip_title = U_("Meeting Cancellation");
        options = get_cancel_options ();
        break;
    default:
        itip_desc = U_("<b>%s</b> has sent an unintelligible message.");
        itip_title = U_("Bad Meeting Message");
        options = NULL;
    }

    write_html (itip, itip_desc, itip_title, options);
    g_free (options);
}

static void
show_current_todo (EItipControl *itip)
{
    EItipControlPrivate *priv;
    const gchar *itip_title, *itip_desc;
    char *options;

    priv = itip->priv;

    switch (priv->method) {
    case ICAL_METHOD_PUBLISH:
        itip_desc = U_("<b>%s</b> has published task information.");
        itip_title = U_("Task Information");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REQUEST:
        itip_desc = U_("<b>%s</b> requests you perform a task.");
        itip_title = U_("Task Proposal");
        options = get_request_options ();
        break;
    case ICAL_METHOD_ADD:
        itip_desc = U_("<b>%s</b> wishes to add to an existing task.");
        itip_title = U_("Task Update");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REFRESH:
        itip_desc = U_("<b>%s</b> wishes to receive the latest task information.");
        itip_title = U_("Task Update Request");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = U_("<b>%s</b> has replied to a task assignment.");
        itip_title = U_("Task Reply");
        options = get_reply_options ();
        break;
    case ICAL_METHOD_CANCEL:
        itip_desc = U_("<b>%s</b> has cancelled a task.");
        itip_title = U_("Task Cancellation");
        options = get_cancel_options ();
        break;
    default:
        itip_desc = U_("<b>%s</b> has sent an unintelligible message.");
        itip_title = U_("Bad Task Message");
        options = NULL;
    }

    write_html (itip, itip_desc, itip_title, options);
    g_free (options);
}

static void
show_current_freebusy (EItipControl *itip)
{
    EItipControlPrivate *priv;
    const gchar *itip_title, *itip_desc;
    char *options;

    priv = itip->priv;

    switch (priv->method) {
    case ICAL_METHOD_PUBLISH:
        itip_desc = U_("<b>%s</b> has published free/busy information.");
        itip_title = U_("Free/Busy Information");
        options = NULL;
        break;
    case ICAL_METHOD_REQUEST:
        itip_desc = U_("<b>%s</b> requests your free/busy information.");
        itip_title = U_("Free/Busy Request");
        options = get_request_fb_options ();
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = U_("<b>%s</b> has replied to a free/busy request.");
        itip_title = U_("Free/Busy Reply");
        options = NULL;
        break;
    default:
        itip_desc = U_("<b>%s</b> has sent an unintelligible message.");
        itip_title = U_("Bad Free/Busy Message");
        options = NULL;
    }

    write_html (itip, itip_desc, itip_title, options);
    g_free (options);
}

static icalcomponent *
get_next (icalcompiter *iter)
{
    icalcomponent *ret = NULL;
    icalcomponent_kind kind;

    do {
        icalcompiter_next (iter);
        ret = icalcompiter_deref (iter);
        kind = icalcomponent_isa (ret);
    } while (ret != NULL 
         && kind != ICAL_VEVENT_COMPONENT
         && kind != ICAL_VTODO_COMPONENT
         && kind != ICAL_VFREEBUSY_COMPONENT);

    return ret;
}

static icalcomponent *
get_prev (icalcompiter *iter)
{
    icalcomponent *ret = NULL;
    icalcomponent_kind kind = ICAL_NO_COMPONENT;

    while (kind != ICAL_VEVENT_COMPONENT
           && kind != ICAL_VTODO_COMPONENT
           && kind != ICAL_VFREEBUSY_COMPONENT) {
        icalcompiter_prior (iter);
        ret = icalcompiter_deref (iter);
        kind = icalcomponent_isa (ret);
    }

    return ret;
}

static void
show_current (EItipControl *itip)
{
    EItipControlPrivate *priv;
    CalComponentVType type;
    icalcomponent *alarm_comp;
    icalcompiter alarm_iter;

    priv = itip->priv;

    set_label (itip);
    set_button_status (itip);

    if (priv->comp)
        gtk_object_unref (GTK_OBJECT (priv->comp));

    /* Strip out alarms for security purposes */
    alarm_iter = icalcomponent_begin_component (priv->ical_comp, ICAL_VALARM_COMPONENT);
    while ((alarm_comp = icalcompiter_deref (&alarm_iter)) != NULL) {
        icalcomponent_remove_component (priv->ical_comp, alarm_comp);

        icalcompiter_next (&alarm_iter);
    }

    priv->comp = cal_component_new ();
    if (!cal_component_set_icalcomponent (priv->comp, priv->ical_comp)) {
        write_error_html (itip, U_("The message does not appear to be properly formed"));
        gtk_object_unref (GTK_OBJECT (priv->comp));
        priv->comp = NULL;
        return;
    };

    type = cal_component_get_vtype (priv->comp);

    switch (type) {
    case CAL_COMPONENT_EVENT:
        show_current_event (itip);
        break;
    case CAL_COMPONENT_TODO:
        show_current_todo (itip);
        break;
    case CAL_COMPONENT_FREEBUSY:
        show_current_freebusy (itip);
        break;
    default:
        write_error_html (itip, U_("The message contains only unsupported requests."));
    }

    find_my_address (itip, priv->ical_comp);
}

void
e_itip_control_set_data (EItipControl *itip, const gchar *text)
{
    EItipControlPrivate *priv;
    icalproperty *prop;
    icalcomponent_kind kind = ICAL_NO_COMPONENT;
    icalcomponent *tz_comp;
    icalcompiter tz_iter;

    priv = itip->priv;

    clean_up (itip);

    priv->comp = NULL;
    priv->total = 0;
    priv->current = 0;

    priv->vcalendar = g_strdup (text);
    priv->top_level = cal_util_new_top_level ();

    priv->main_comp = icalparser_parse_string (priv->vcalendar);
    if (priv->main_comp == NULL) {
        write_error_html (itip, U_("The attachment does not contain a valid calendar message"));
        return;
    }

    prop = icalcomponent_get_first_property (priv->main_comp, ICAL_METHOD_PROPERTY);
    if (prop == NULL) {
        write_error_html (itip, U_("The attachment does not contain a valid calendar message"));
        return;
    }

    priv->method = icalproperty_get_method (prop);

    tz_iter = icalcomponent_begin_component (priv->main_comp, ICAL_VTIMEZONE_COMPONENT);
    while ((tz_comp = icalcompiter_deref (&tz_iter)) != NULL) {
        icalcomponent *clone;

        clone = icalcomponent_new_clone (tz_comp);
        icalcomponent_add_component (priv->top_level, clone);

        icalcompiter_next (&tz_iter);
    }

    priv->iter = icalcomponent_begin_component (priv->main_comp, ICAL_ANY_COMPONENT);
    priv->ical_comp = icalcompiter_deref (&priv->iter);
    kind = icalcomponent_isa (priv->ical_comp);
    if (kind != ICAL_VEVENT_COMPONENT
        && kind != ICAL_VTODO_COMPONENT
        && kind != ICAL_VFREEBUSY_COMPONENT)
        priv->ical_comp = get_next (&priv->iter);

    if (priv->ical_comp == NULL) {
        write_error_html (itip, U_("The attachment has no viewable calendar items"));       
        return;
    }

    priv->total = icalcomponent_count_components (priv->main_comp, ICAL_VEVENT_COMPONENT);
    priv->total += icalcomponent_count_components (priv->main_comp, ICAL_VTODO_COMPONENT);
    priv->total += icalcomponent_count_components (priv->main_comp, ICAL_VFREEBUSY_COMPONENT);

    if (priv->total > 0)
        priv->current = 1;
    else
        priv->current = 0;

    show_current (itip);
}

gchar *
e_itip_control_get_data (EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    return g_strdup (priv->vcalendar);
}

gint
e_itip_control_get_data_size (EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    if (priv->vcalendar == NULL)
        return 0;

    return strlen (priv->vcalendar);
}

void
e_itip_control_set_from_address (EItipControl *itip, const gchar *address)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    if (priv->from_address)
        g_free (priv->from_address);

    priv->from_address = g_strdup (address);
}

const gchar *
e_itip_control_get_from_address (EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    return priv->from_address;
}


static void
change_status (icalcomponent *ical_comp, const char *address, icalparameter_partstat status)
{
    icalproperty *prop;

    prop = find_attendee (ical_comp, address);
    if (prop) {
        icalparameter *param;

        icalproperty_remove_parameter (prop, ICAL_PARTSTAT_PARAMETER);
        param = icalparameter_new_partstat (status);
        icalproperty_add_parameter (prop, param);
    }
}

static void
update_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    icalcomponent *clone;
    CalClient *client;
    CalComponentVType type;
    GtkWidget *dialog;

    priv = itip->priv;

    type = cal_component_get_vtype (priv->comp);
    if (type == CAL_COMPONENT_TODO)
        client = priv->task_client;
    else
        client = priv->event_client;

    clone = icalcomponent_new_clone (priv->ical_comp);
    icalcomponent_add_component (priv->top_level, clone);

    if (!cal_client_update_objects (client, priv->top_level))
        dialog = gnome_warning_dialog (_("Calendar file could not be updated!\n"));
    else
        dialog = gnome_ok_dialog (_("Update complete\n"));
    gnome_dialog_run_and_close (GNOME_DIALOG (dialog));

    icalcomponent_remove_component (priv->top_level, clone);
}

static void
update_attendee_status (EItipControl *itip)
{
    EItipControlPrivate *priv;
    CalClient *client;
    CalClientGetStatus status;
    CalComponent *comp;
    CalComponentVType type;
    const char *uid;
    GtkWidget *dialog;

    priv = itip->priv;

    type = cal_component_get_vtype (priv->comp);
    if (type == CAL_COMPONENT_TODO)
        client = priv->task_client;
    else
        client = priv->event_client;

    /* Obtain our version */
    cal_component_get_uid (priv->comp, &uid);
    status = cal_client_get_object (client, uid, &comp);

    if (status == CAL_CLIENT_GET_SUCCESS) {
        GSList *attendees;

        cal_component_get_attendee_list (priv->comp, &attendees);
        if (attendees != NULL) {
            CalComponentAttendee *a = attendees->data;
            icalparameter_partstat partstat;

            partstat = find_attendee_partstat (priv->ical_comp, itip_strip_mailto (a->value));

            if (partstat != ICAL_PARTSTAT_NONE) {
                change_status (cal_component_get_icalcomponent (comp),
                           itip_strip_mailto (a->value),
                           partstat);
                cal_component_rescan (comp);
            } else {
                dialog = gnome_warning_dialog (_("Attendee status could "
                                 "not be updated because "
                                 "of an invalid status!\n"));
                goto cleanup;
            }
        }

        if (!cal_client_update_object (client, comp))
            dialog = gnome_warning_dialog (_("Attendee status could not be updated!\n"));
        else
            dialog = gnome_ok_dialog (_("Attendee status updated\n"));
    } else {
        dialog = gnome_warning_dialog (_("Attendee status can not be updated "
                         "because the item no longer exists"));
    }

 cleanup:
    gtk_object_unref (GTK_OBJECT (comp));
    gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
}

static void
remove_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    CalClient *client;
    CalComponentVType type;
    const char *uid;
    GtkWidget *dialog;

    priv = itip->priv;

    type = cal_component_get_vtype (priv->comp);
    if (type == CAL_COMPONENT_TODO)
        client = priv->task_client;
    else
        client = priv->event_client;

    cal_component_get_uid (priv->comp, &uid);
    if (cal_client_remove_object (client, uid))
        dialog = gnome_ok_dialog (_("Removal Complete"));
    gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
}

static void
send_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    CalComponent *comp;
    CalComponentVType type;
    const char *uid;
    CalClientGetStatus status;
    GtkWidget *dialog;

    priv = itip->priv;

    type = cal_component_get_vtype (priv->comp);
    cal_component_get_uid (priv->comp, &uid);

    switch (type) {
    case CAL_COMPONENT_EVENT:
        status = cal_client_get_object (priv->event_client, uid, &comp);
        break;
    case CAL_COMPONENT_TODO:
        status = cal_client_get_object (priv->task_client, uid, &comp);
        break;
    default:
        status = CAL_CLIENT_GET_NOT_FOUND;
    }

    if (status == CAL_CLIENT_GET_SUCCESS) {
        itip_send_comp (CAL_COMPONENT_METHOD_PUBLISH, comp);
        dialog = gnome_ok_dialog (_("Item sent!\n"));
    } else {
        dialog = gnome_warning_dialog (_("The item could not be sent!\n"));
    }
    gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
}

static void
send_freebusy (EItipControl *itip)
{
    EItipControlPrivate *priv;
    CalComponentDateTime datetime;
    time_t start, end;
    GtkWidget *dialog;
    GList *comp_list;
    icaltimezone *zone;

    priv = itip->priv;

    cal_component_get_dtstart (priv->comp, &datetime);
    if (datetime.tzid) {
        zone = icalcomponent_get_timezone (priv->top_level,
                           datetime.tzid);
    } else {
        zone = NULL;
    }
    start = icaltime_as_timet_with_zone (*datetime.value, zone);
    cal_component_free_datetime (&datetime);

    cal_component_get_dtend (priv->comp, &datetime);
    if (datetime.tzid) {
        zone = icalcomponent_get_timezone (priv->top_level,
                           datetime.tzid);
    } else {
        zone = NULL;
    }
    end = icaltime_as_timet_with_zone (*datetime.value, zone);
    cal_component_free_datetime (&datetime);

    comp_list = cal_client_get_free_busy (priv->event_client, NULL, start, end);

    if (comp_list) {
        GList *l;

        for (l = comp_list; l; l = l->next) {
            CalComponent *comp = CAL_COMPONENT (l->data);
            itip_send_comp (CAL_COMPONENT_METHOD_REPLY, comp);

            gtk_object_unref (GTK_OBJECT (comp));
        }
        dialog = gnome_ok_dialog (_("Item sent!\n"));

        g_list_free (comp_list);
    } else {
        dialog = gnome_warning_dialog (_("The item could not be sent!\n"));
    }
    gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
}

static void
prev_clicked_cb (GtkWidget *widget, gpointer data)
{
    EItipControl *itip = E_ITIP_CONTROL (data);
    EItipControlPrivate *priv;

    priv = itip->priv;

    priv->current--;
    priv->ical_comp = get_prev (&priv->iter);

    show_current (itip);
}

static void
next_clicked_cb (GtkWidget *widget, gpointer data)
{
    EItipControl *itip = E_ITIP_CONTROL (data);
    EItipControlPrivate *priv;

    priv = itip->priv;

    priv->current++;
    priv->ical_comp = get_next (&priv->iter);

    show_current (itip);
}

static void
url_requested_cb (GtkHTML *html, const gchar *url, GtkHTMLStream *handle, gpointer data)
{   unsigned char buffer[4096];
    int len, fd;
        char *path;

    path = g_strdup_printf ("%s/%s", EVOLUTION_ICONSDIR, url);

    if ((fd = open (path, O_RDONLY)) == -1) {
        g_warning ("%s", g_strerror (errno));
        goto cleanup;
    }

        while ((len = read (fd, buffer, 4096)) > 0) {
        gtk_html_write (html, handle, buffer, len);
    }

    if (len < 0) {
        /* check to see if we stopped because of an error */
        gtk_html_end (html, handle, GTK_HTML_STREAM_ERROR);
        g_warning ("%s", g_strerror (errno));
        goto cleanup;
    }
    /* done with no errors */
    gtk_html_end (html, handle, GTK_HTML_STREAM_OK);
    close (fd);

 cleanup:
    g_free (path);
}

static void
ok_clicked_cb (GtkHTML *html, const gchar *method, const gchar *url, const gchar *encoding, gpointer data)
{
    EItipControl *itip = E_ITIP_CONTROL (data);
    EItipControlPrivate *priv;
    gchar **fields;
    gboolean rsvp = FALSE;
    int i;

    priv = itip->priv;

    fields = g_strsplit (encoding, "&", -1);
    for (i = 0; fields[i] != NULL; i++) {
        gchar **key_value;

        key_value = g_strsplit (fields[i], "=", 2);

        if (key_value[0] != NULL && !strcmp (key_value[0], "action")) {
            if (key_value[1] == NULL)
                break;

            switch (key_value[1][0]) {
            case 'U':
                update_item (itip);
                break;
            case 'A':
                change_status (priv->ical_comp, priv->my_address, ICAL_PARTSTAT_ACCEPTED);
                cal_component_rescan (priv->comp);
                update_item (itip);
                break;
            case 'T':
                change_status (priv->ical_comp, priv->my_address, ICAL_PARTSTAT_TENTATIVE);
                cal_component_rescan (priv->comp);
                update_item (itip);
                break;
            case 'D':
                change_status (priv->ical_comp, priv->my_address, ICAL_PARTSTAT_DECLINED);
                cal_component_rescan (priv->comp);
                remove_item (itip);
                break;
            case 'F':
                send_freebusy (itip);
                break;
            case 'R':
                update_attendee_status (itip);
                break;
            case 'S':
                send_item (itip);
                break;
            case 'C':
                remove_item (itip);
                break;
            }
        }

        if (key_value[0] != NULL && !strcmp (key_value[0], "rsvp"))
            if (*key_value[1] == '1')
                rsvp = TRUE;

        g_strfreev (key_value);

    }
    g_strfreev (fields);

    if (rsvp) {
        CalComponent *comp = NULL;

        comp = cal_component_clone (priv->comp);
        if (comp == NULL)
            return;

        if (priv->my_address != NULL) {
            icalcomponent *ical_comp;
            icalproperty *prop;
            const char *attendee, *text;
            icalvalue *value;

            ical_comp = cal_component_get_icalcomponent (comp);

            for (prop = icalcomponent_get_first_property (ical_comp, ICAL_ATTENDEE_PROPERTY);
                 prop != NULL;
                 prop = icalcomponent_get_next_property (ical_comp, ICAL_ATTENDEE_PROPERTY))
            {
                value = icalproperty_get_value (prop);
                if (!value)
                    continue;

                attendee = icalvalue_get_string (value);
                text = itip_strip_mailto (attendee);

                if (!strstr (text, priv->my_address)) {
                    icalcomponent_remove_property (ical_comp, prop);
                    icalproperty_free (prop);
                }
            }
            cal_component_rescan (comp);
            itip_send_comp (CAL_COMPONENT_METHOD_REPLY, comp);
        } else {
            GtkWidget *dialog;

            dialog = gnome_warning_dialog (_("Unable to find any of your identities "
                             "in the attendees list!\n"));
            gnome_dialog_run_and_close (GNOME_DIALOG (dialog));
        }
        gtk_object_unref (GTK_OBJECT (comp));
    }
}