aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/e-itip-control.c
blob: 4285c14c2cae7bc36a21600a746f682203c91885 (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
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-itip-control.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, 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 <unistd.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <libgnome/gnome-util.h>
#include <libgnomeui/gnome-stock-icons.h>
#include <libgnomeui/gnome-uidefs.h>
#include <libgnomeui/gnome-dialog.h>
#include <libgnomeui/gnome-dialog-util.h>
#include <bonobo/bonobo-object.h>
#include <bonobo/bonobo-exception.h>
#include <gtkhtml/gtkhtml.h>
#include <gtkhtml/gtkhtml-embedded.h>
#include <gtkhtml/gtkhtml-stream.h>
#include <libedataserver/e-source-list.h>
#include <libedataserverui/e-source-combo-box.h>
#include <libical/ical.h>
#include <libecal/e-cal-component.h>
#include <libecal/e-cal-time-util.h>
#include <libecal/e-cal.h>
#include <libedataserver/e-time-utils.h>
#include <e-util/e-dialog-widgets.h>
#include <e-util/e-html-utils.h>
#include <e-util/e-icon-factory.h>
#include <e-util/e-util-private.h>
#include "dialogs/delete-error.h"
#include "calendar-config.h"
#include "itip-utils.h"
#include "e-itip-control.h"
#include "common/authentication.h"

struct _EItipControlPrivate {
    GtkWidget *html;

    ESourceList *source_lists[E_CAL_SOURCE_TYPE_LAST];
    GHashTable *ecals[E_CAL_SOURCE_TYPE_LAST];

    ECal *current_ecal;
    ECalSourceType type;

    char action;
    gboolean rsvp;

    /* Use the gpointer variants for weak pointers. */
    union {
        GtkWidget *widget;
        gpointer pointer;
    } ok;
    union {
        GtkWidget *widget;
        gpointer pointer;
    } hbox;
    union {
        GtkWidget *widget;
        gpointer pointer;
    } vbox;

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

    int current;
    int total;

    gchar *calendar_uid;

    EAccountList *accounts;

    gchar *from_address;
    gchar *delegator_address;
    gchar *delegator_name;
    gchar *my_address;
    gint   view_only;
};

#define ACTION_DATA "EItipControl:Action"

/* 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>"

static void e_itip_control_destroy  (GtkObject               *obj);

static void find_my_address (EItipControl *itip, icalcomponent *ical_comp, icalparameter_partstat *status);
static void url_requested_cb (GtkHTML *html, const gchar *url, GtkHTMLStream *handle, gpointer data);
static gboolean object_requested_cb (GtkHTML *html, GtkHTMLEmbedded *eb, gpointer data);
static void ok_clicked_cb (GtkWidget *widget, gpointer data);

G_DEFINE_TYPE (EItipControl, e_itip_control, GTK_TYPE_VBOX)

static void
e_itip_control_class_init (EItipControlClass *klass)
{
    GtkObjectClass *gtkobject_class;

    gtkobject_class = GTK_OBJECT_CLASS (klass);

    gtkobject_class->destroy = e_itip_control_destroy;
}

static void
set_ok_sens (EItipControl *itip)
{
    EItipControlPrivate *priv;
    gboolean read_only = TRUE;

    priv = itip->priv;

    if (!priv->ok.widget)
        return;

    if (priv->current_ecal)
        e_cal_is_read_only (priv->current_ecal, &read_only, NULL);

    gtk_widget_set_sensitive (priv->ok.widget, priv->current_ecal != NULL && !read_only);
}

static void
cal_opened_cb (ECal *ecal, ECalendarStatus status, gpointer data)
{
    EItipControl *itip = data;
    EItipControlPrivate *priv;
    ESource *source;
    ECalSourceType source_type;

    priv = itip->priv;

    source_type = e_cal_get_source_type (ecal);
    source = e_cal_get_source (ecal);

    g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, cal_opened_cb, NULL);

    if (status != E_CALENDAR_STATUS_OK) {
        g_hash_table_remove (priv->ecals[source_type], e_source_peek_uid (source));

        return;
    }

    priv->current_ecal = ecal;
    set_ok_sens (itip);
}

typedef void (* EItipControlOpenFunc) (ECal *ecal, ECalendarStatus status, gpointer data);

static ECal *
start_calendar_server (EItipControl *itip, ESource *source, ECalSourceType type, EItipControlOpenFunc func, gpointer data)
{
    EItipControlPrivate *priv;
    ECal *ecal;
    icaltimezone *zone;

    priv = itip->priv;

    ecal = g_hash_table_lookup (priv->ecals[type], e_source_peek_uid (source));
    if (ecal) {
        priv->current_ecal = ecal;
        set_ok_sens (itip);
        return ecal;
    }

    ecal = auth_new_cal_from_source (source, type);

    zone = calendar_config_get_icaltimezone ();
    e_cal_set_default_timezone (ecal, zone, NULL);

    g_signal_connect (G_OBJECT (ecal), "cal_opened", G_CALLBACK (func), data);

    g_hash_table_insert (priv->ecals[type], g_strdup (e_source_peek_uid (source)), ecal);

    e_cal_open_async (ecal, TRUE);

    return ecal;
}

static ECal *
start_calendar_server_by_uid (EItipControl *itip, const char *uid, ECalSourceType type)
{
    EItipControlPrivate *priv;
    int i;

    priv = itip->priv;

    for (i = 0; i < E_CAL_SOURCE_TYPE_LAST; i++) {
        ESource *source;

        source = e_source_list_peek_source_by_uid (priv->source_lists[i], uid);
        if (source)
            return start_calendar_server (itip, source, type, cal_opened_cb, itip);
    }

    return NULL;
}

typedef struct {
    EItipControl *itip;
    char *uid;
    int count;
    gboolean show_selector;
} EItipControlFindData;

static void
source_changed_cb (ESourceComboBox *escb, EItipControl *itip)
{
    EItipControlPrivate *priv = itip->priv;
    ESource *source;

    source = e_source_combo_box_get_active (escb);

    if (priv->ok.widget)
        gtk_widget_set_sensitive (priv->ok.widget, FALSE);

    start_calendar_server (itip, source, priv->type, cal_opened_cb, itip);
}

static void
find_cal_opened_cb (ECal *ecal, ECalendarStatus status, gpointer data)
{
    EItipControlFindData *fd = data;
    EItipControlPrivate *priv;
    ESource *source;
    ECalSourceType source_type;
    icalcomponent *icalcomp;

    source_type = e_cal_get_source_type (ecal);
    source = e_cal_get_source (ecal);

    priv = fd->itip->priv;

    fd->count--;

    g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, find_cal_opened_cb, NULL);

    if (status != E_CALENDAR_STATUS_OK) {
        g_hash_table_remove (priv->ecals[source_type], e_source_peek_uid (source));

        goto cleanup;
    }

    if (e_cal_get_object (ecal, fd->uid, NULL, &icalcomp, NULL)) {
        icalcomponent_free (icalcomp);

        priv->current_ecal = ecal;
        set_ok_sens (fd->itip);
    }

 cleanup:
    if (fd->count == 0) {
        if (fd->show_selector && !priv->current_ecal && priv->vbox.widget) {
            GtkWidget *escb;
            char *uid;

            switch (priv->type) {
            case E_CAL_SOURCE_TYPE_EVENT:
                uid = calendar_config_get_primary_calendar ();
                break;
            case E_CAL_SOURCE_TYPE_TODO:
                uid = calendar_config_get_primary_tasks ();
                break;
            default:
                uid = NULL;
                g_return_if_reached ();
            }

            if (uid) {
                source = e_source_list_peek_source_by_uid (priv->source_lists[priv->type], uid);
                g_free (uid);
            }

            /* Try to create a default if there isn't one */
            if (!source)
                source = e_source_list_peek_source_any (priv->source_lists[priv->type]);

            escb = e_source_combo_box_new (priv->source_lists[priv->type]);
            g_signal_connect_object (
                escb, "changed",
                G_CALLBACK (source_changed_cb), fd->itip, 0);

            gtk_box_pack_start (GTK_BOX (priv->vbox.widget), escb, FALSE, TRUE, 0);
            gtk_widget_show (escb);

            /* FIXME What if there is no source? */
            if (source)
                e_source_combo_box_set_active (E_SOURCE_COMBO_BOX (escb), source);
        } else {
            /* FIXME Display error message to user */
        }

        g_free (fd->uid);
        g_free (fd);
    }
}

static void
find_server (EItipControl *itip, ECalComponent *comp, gboolean show_selector)
{
    EItipControlPrivate *priv;
    EItipControlFindData *fd = NULL;
    GSList *groups, *l;
    const char *uid;

    priv = itip->priv;

    e_cal_component_get_uid (comp, &uid);

    groups = e_source_list_peek_groups (priv->source_lists[priv->type]);
    for (l = groups; l; l = l->next) {
        ESourceGroup *group;
        GSList *sources, *m;

        group = l->data;

        sources = e_source_group_peek_sources (group);
        for (m = sources; m; m = m->next) {
            ESource *source;
            ECal *ecal;

            source = m->data;

            if (!fd) {
                fd = g_new0 (EItipControlFindData, 1);
                fd->itip = itip;
                fd->uid = g_strdup (uid);
                fd->show_selector = show_selector;
            }
            fd->count++;
            /* Check this return too? */
            ecal = start_calendar_server (itip, source, priv->type, find_cal_opened_cb, fd);
        }
    }
}

static void
cleanup_ecal (void *data)
{
    ECal *ecal = data;

    /* Clean up any signals */
    g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, cal_opened_cb, NULL);
    g_signal_handlers_disconnect_matched (ecal, G_SIGNAL_MATCH_FUNC, 0, 0, NULL, find_cal_opened_cb, NULL);

    g_object_unref (ecal);
}

static void
html_destroyed (gpointer data)
{
    EItipControl *itip = data;
    EItipControlPrivate *priv;

    priv = itip->priv;

    priv->html = NULL;
}

static void
e_itip_control_init (EItipControl *itip)
{
    EItipControlPrivate *priv;
    GtkWidget *scrolled_window;
    int i;

    priv = g_new0 (EItipControlPrivate, 1);
    itip->priv = priv;

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

    /* Source Lists */
    for (i = 0; i < E_CAL_SOURCE_TYPE_LAST; i++)
        priv->source_lists[i] = NULL;

    priv->source_lists[E_CAL_SOURCE_TYPE_EVENT] = e_source_list_new_for_gconf_default ("/apps/evolution/calendar/sources");
    priv->source_lists[E_CAL_SOURCE_TYPE_TODO] = e_source_list_new_for_gconf_default ("/apps/evolution/tasks/sources");

    /* Initialize the ecal hashes */
    for (i = 0; i < E_CAL_SOURCE_TYPE_LAST; i++)
        priv->ecals[i] = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, cleanup_ecal);
    priv->current_ecal = NULL;

    /* Other fields to init */
    priv->calendar_uid = NULL;
    priv->from_address = NULL;
    priv->delegator_address = NULL;
    priv->delegator_name = NULL;
    priv->my_address = NULL;
    priv->view_only = 0;

    /* Html Widget */
    priv->html = gtk_html_new ();
    gtk_html_set_default_content_type (GTK_HTML (priv->html),
                       "text/html; charset=utf-8");
    gtk_html_load_from_string (GTK_HTML (priv->html), " ", 1);
    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);
    g_object_weak_ref (G_OBJECT (priv->html), (GWeakNotify)html_destroyed, itip);
    gtk_widget_set_size_request (scrolled_window, 600, 400);
    gtk_box_pack_start (GTK_BOX (itip), scrolled_window, FALSE, FALSE, 6);

    g_signal_connect (priv->html, "url_requested", G_CALLBACK (url_requested_cb), itip);
    g_signal_connect (priv->html, "object_requested", G_CALLBACK (object_requested_cb), itip);
    g_signal_connect (priv->html, "submit", G_CALLBACK (ok_clicked_cb), itip);
}

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

    priv = itip->priv;
    if (!priv)
        return;

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

    if (priv->comp) {
        g_object_unref (priv->comp);
        priv->comp = NULL;
    }

    if (priv->top_level) {
        icalcomponent_free (priv->top_level);
        priv->top_level = NULL;
    }

    if (priv->main_comp) {
        icalcomponent_free (priv->main_comp);
        priv->main_comp = NULL;
    }
    priv->ical_comp = NULL;

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

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

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

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

    priv = itip->priv;

    if (priv) {
        int i;

        clean_up (itip);

        priv->accounts = NULL;

        for (i = 0; i < E_CAL_SOURCE_TYPE_LAST; i++) {
            if (priv->ecals[i]) {
                g_hash_table_destroy (priv->ecals[i]);
                priv->ecals[i] = NULL;
            }
        }

        if (priv->html) {
            g_signal_handlers_disconnect_matched (priv->html, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, itip);
            g_object_weak_unref (G_OBJECT (priv->html), (GWeakNotify)html_destroyed, itip);
        }

        g_free (priv);
        itip->priv = NULL;
    }

    (* GTK_OBJECT_CLASS (e_itip_control_parent_class)->destroy) (obj);
}

GtkWidget *
e_itip_control_new (void)
{
    return g_object_new (E_TYPE_ITIP_CONTROL, NULL);
}

static void
find_my_address (EItipControl *itip, icalcomponent *ical_comp, icalparameter_partstat *status)
{
    EItipControlPrivate *priv;
    icalproperty *prop;
    char *my_alt_address = NULL;

    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)) {
        icalvalue *value;
        icalparameter *param;
        const char *attendee, *name;
        char *attendee_clean, *name_clean;
        EIterator *it;

        value = icalproperty_get_value (prop);
        if (value != NULL) {
            attendee = icalvalue_get_string (value);
            attendee_clean = g_strdup (itip_strip_mailto (attendee));
            attendee_clean = g_strstrip (attendee_clean);
        } else {
            attendee = NULL;
            attendee_clean = NULL;
        }

        param = icalproperty_get_first_parameter (prop, ICAL_CN_PARAMETER);
        if (param != NULL) {
            name = icalparameter_get_cn (param);
            name_clean = g_strdup (name);
            name_clean = g_strstrip (name_clean);
        } else {
            name = NULL;
            name_clean = NULL;
        }

        if (priv->delegator_address) {
            char *delegator_clean;

            delegator_clean = g_strdup (itip_strip_mailto (attendee));
            delegator_clean = g_strstrip (delegator_clean);

            /* If the mailer told us the address to use, use that */
            if (delegator_clean != NULL
                && !g_ascii_strcasecmp (attendee_clean, delegator_clean)) {
                priv->my_address = g_strdup (itip_strip_mailto (priv->delegator_address));
                priv->my_address = g_strstrip (priv->my_address);

                if (status) {
                    param = icalproperty_get_first_parameter (prop, ICAL_PARTSTAT_PARAMETER);
                    *status = param ? icalparameter_get_partstat (param) : ICAL_PARTSTAT_NEEDSACTION;
                }
            }

            g_free (delegator_clean);
        } else {
            it = e_list_get_iterator((EList *)priv->accounts);
            while (e_iterator_is_valid(it)) {
                const EAccount *account = e_iterator_get(it);

                /* Check for a matching address */
                if (attendee_clean != NULL
                    && !g_ascii_strcasecmp (account->id->address, attendee_clean)) {
                    priv->my_address = g_strdup (account->id->address);
                    if (status) {
                        param = icalproperty_get_first_parameter (prop, ICAL_PARTSTAT_PARAMETER);
                        *status = param ? icalparameter_get_partstat (param) : ICAL_PARTSTAT_NEEDSACTION;
                    }
                    g_free (attendee_clean);
                    g_free (name_clean);
                    g_free (my_alt_address);
                    g_object_unref(it);
                    return;
                }

                /* Check for a matching cname to fall back on */
                if (name_clean != NULL
                    && !g_ascii_strcasecmp (account->id->name, name_clean))
                    my_alt_address = g_strdup (attendee_clean);

                e_iterator_next(it);
            }
            g_object_unref(it);
        }

        g_free (attendee_clean);
        g_free (name_clean);
    }

    priv->my_address = my_alt_address;
    if (status)
        *status = ICAL_PARTSTAT_NEEDSACTION;
}

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

    if (address == NULL)
        return NULL;

    for (prop = icalcomponent_get_first_property (ical_comp, ICAL_ATTENDEE_PROPERTY);
         prop != NULL;
         prop = icalcomponent_get_next_property (ical_comp, ICAL_ATTENDEE_PROPERTY)) {
        icalvalue *value;
        const char *attendee;
        char *text;

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

        attendee = icalvalue_get_string (value);

        text = g_strdup (itip_strip_mailto (attendee));
        text = g_strstrip (text);
        if (!g_ascii_strcasecmp (address, text)) {
            g_free (text);
            break;
        }
        g_free (text);
    }

    return prop;
}

static void
write_label_piece (EItipControl *itip, ECalComponentDateTime *dt,
                   GString *buffer,
           const char *stext, const char *etext,
           gboolean just_date)
{
    EItipControlPrivate *priv;
    struct tm tmp_tm;
    char time_buf[64];
    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) {
        zone = calendar_config_get_icaltimezone ();
        icaltimezone_convert_time (dt->value, icaltimezone_get_utc_timezone (), zone);
    }

    tmp_tm = icaltimetype_to_tm (dt->value);
    if (just_date)
        tmp_tm.tm_hour = tmp_tm.tm_min = tmp_tm.tm_sec = 0;

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

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

    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 && !just_date) {
        /* Note that this returns UTF-8, since all iCalendar data is
           UTF-8. But it probably is not translated. */
        display_name = icaltimezone_get_display_name (zone);
        if (display_name && *display_name) {
            g_string_append_len (buffer, " <font size=-1>[", 16);

            /* We check if it is one of our builtin timezone names,
               in which case we call gettext to translate it. */
            if (icaltimezone_get_builtin_timezone (display_name)) {
                g_string_append_printf (buffer, "%s", _(display_name));
            } else {
                g_string_append_printf (buffer, "%s", display_name);
            }
            g_string_append_len (buffer, "]</font>", 8);
        }
    }

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

static const char *
nth (int n)
{
    if (n == -1)
        return "last";
    else if (n < 1 || n > 31)
        return "?";
    else
        return e_cal_recur_nth[n];
}

static const char *dayname[] = {
    N_("Sunday"),
    N_("Monday"),
    N_("Tuesday"),
    N_("Wednesday"),
    N_("Thursday"),
    N_("Friday"),
    N_("Saturday")
};

static inline char *
get_dayname (struct icalrecurrencetype *r, int i)
{
    enum icalrecurrencetype_weekday day;

    day = icalrecurrencetype_day_day_of_week (r->by_day[i]);
    g_return_val_if_fail (day > 0 && day < 8, "?");

    return _(dayname[day - 1]);
}

static void
write_recurrence_piece (EItipControl *itip, ECalComponent *comp,
                        GString *buffer)
{
    GSList *rrules;
    struct icalrecurrencetype *r;
    int i;

    g_string_append_len (buffer, "<b>Recurring:</b> ", 18);

    if (!e_cal_component_has_simple_recurrence (comp)) {
        g_string_append_printf (
            buffer, "%s", _("Yes. (Complex Recurrence)"));
        return;
    }

    e_cal_component_get_rrule_list (comp, &rrules);
    g_return_if_fail (rrules && !rrules->next);

    r = rrules->data;

    switch (r->freq) {
    case ICAL_DAILY_RECURRENCE:
                /* For Translators: In this can also be translated as "With the period of %d
                 day/days", where %d is a number. The entire sentence is of the form "Recurring:
                 Every %d day/days" */
        /* For Translators : 'Every day' is event Recurring every day */
        /* For Translators : 'Every %d days' is event Recurring every %d days. %d is a digit */
        g_string_append_printf (
            buffer, ngettext ("Every day",
            "Every %d days", r->interval),
            r->interval);
        break;

    case ICAL_WEEKLY_RECURRENCE:
        if (r->by_day[0] == ICAL_RECURRENCE_ARRAY_MAX) {
                        /* For Translators: In this can also be translated as "With the period of %d
                         week/weeks", where %d is a number. The entire sentence is of the form "Recurring:
                         Every %d week/weeks" */
            /* For Translators : 'Every week' is event Recurring every week */
            /* For Translators : 'Every %d weeks' is event Recurring every %d weeks. %d is a digit */
            g_string_append_printf (
                buffer, ngettext ("Every week",
                "Every %d weeks", r->interval),
                r->interval);
        } else {
            /* For Translators : 'Every week on' is event Recurring every week on (dayname) and (dayname) and (dayname) */
            /* For Translators : 'Every %d weeks on' is event Recurring: every %d weeks on (dayname) and (dayname). %d is a digit */
            g_string_append_printf (
                buffer, ngettext ("Every week on ",
                "Every %d weeks on ", r->interval),
                r->interval);

            for (i = 1; i < 8 && r->by_day[i] != ICAL_RECURRENCE_ARRAY_MAX; i++) {
                if (i > 1)
                    g_string_append_len (buffer, ", ", 2);
                g_string_append (buffer, get_dayname (r, i - 1));
            }
            if (i > 1)
                /* For Translators : 'and' is part of the sentence 'event recurring every week on (dayname) and (dayname)' */
                g_string_append_printf (buffer, "%s", _(" and "));
            g_string_append (buffer, get_dayname (r, i - 1));
        }
        break;

    case ICAL_MONTHLY_RECURRENCE:
        if (r->by_month_day[0] != ICAL_RECURRENCE_ARRAY_MAX) {
            /* For Translators : 'The %s day of' is part of the sentence 'event recurring on the (nth) day of every month.' */
            g_string_append_printf (
                buffer, _("The %s day of "),
                nth (r->by_month_day[0]));
        } else {
            int pos;

            /* Outlook 2000 uses BYDAY=TU;BYSETPOS=2, and will not
               accept BYDAY=2TU. So we now use the same as Outlook
               by default. */

            pos = icalrecurrencetype_day_position (r->by_day[0]);
            if (pos == 0)
                pos = r->by_set_pos[0];

            /* For Translators : 'The %s %s of' is part of the sentence 'event recurring on the (nth) (dayname) of every month.'
               eg,third monday of every month */
            g_string_append_printf (
                buffer, _("The %s %s of "),
                nth (pos), get_dayname (r, 0));
        }

              /* For Translators: In this can also be translated as "With the period of %d
                 month/months", where %d is a number. The entire sentence is of the form "Recurring:
                 Every %d month/months" */
        /* For Translators : 'every month' is part of the sentence 'event recurring on the (nth) day of every month.' */
        /* For Translators : 'every %d months' is part of the sentence 'event recurring on the (nth) day of every %d months.'
         %d is a digit */
        g_string_append_printf (
            buffer, ngettext ("every month",
            "every %d months", r->interval),
            r->interval);
        break;

    case ICAL_YEARLY_RECURRENCE:
              /* For Translators: In this can also be translated as "With the period of %d
                 year/years", where %d is a number. The entire sentence is of the form "Recurring:
                 Every %d year/years" */
        /* For Translators : 'Every year' is event Recurring every year */
        /* For Translators : 'Every %d years' is event Recurring every %d years. %d is a digit */
        g_string_append_printf (
            buffer, ngettext ("Every year",
            "Every %d years", r->interval),
            r->interval);
        break;

    default:
        g_return_if_reached ();
    }

    if (r->count) {
          /* For Translators:'a total of %d time' is part of the sentence of the form 'event recurring every day,a total of % time.' %d is a digit*/
          /* For Translators:'a total of %d times' is part of the sentence of the form 'event recurring every day,a total of % times.' %d is a digit*/
        g_string_append_printf (
            buffer, ngettext ("a total of %d time",
            " a total of %d times", r->count), r->count);
    } else if (!icaltime_is_null_time (r->until)) {
        ECalComponentDateTime dt;

        /* FIXME This should get the tzid id, not the whole zone */
        dt.value = &r->until;
        dt.tzid = icaltimezone_get_tzid ((icaltimezone *)r->until.zone);

        write_label_piece (itip, &dt, buffer, 
                   /* For Translators : ', ending on' is part of the sentence of the form 'event recurring every day, ending on (date).'*/
                   _(", ending on "), NULL, TRUE);
    }

    g_string_append_len (buffer, "<br>", 4);
}

static void
set_date_label (EItipControl *itip, GtkHTML *html, GtkHTMLStream *html_stream,
        ECalComponent *comp)
{
    ECalComponentDateTime datetime;
    GString *buffer;
    gchar *str;
    gboolean wrote = FALSE, task_completed = FALSE;
    ECalComponentVType type;

    buffer = g_string_sized_new (1024);
    type = e_cal_component_get_vtype (comp);

    e_cal_component_get_dtstart (comp, &datetime);
    if (datetime.value) {
        /* For Translators : 'starts' is starts:date implying a task starts on what date */
        str = g_strdup_printf ("<b>%s:</b>", _("Starts"));
        write_label_piece (itip, &datetime, buffer, str, "<br>", FALSE);
        gtk_html_write (html, html_stream, buffer->str, buffer->len);
        wrote = TRUE;
        g_free (str);
    }
    e_cal_component_free_datetime (&datetime);

    /* Reset the buffer. */
    g_string_truncate (buffer, 0);

    e_cal_component_get_dtend (comp, &datetime);
    if (datetime.value){
        /* For Translators : 'ends' is ends:date implying a task ends on what date */
        str = g_strdup_printf ("<b>%s:</b>", _("Ends"));
        write_label_piece (itip, &datetime, buffer, str, "<br>", FALSE);
        gtk_html_write (html, html_stream, buffer->str, buffer->len);
        wrote = TRUE;
        g_free (str);
    }
    e_cal_component_free_datetime (&datetime);

    /* Reset the buffer. */
    g_string_truncate (buffer, 0);

    if (e_cal_component_has_recurrences (comp)) {
        write_recurrence_piece (itip, comp, buffer);
        gtk_html_write (html, html_stream, buffer->str, buffer->len);
        wrote = TRUE;
    }

    /* Reset the buffer. */
    g_string_truncate (buffer, 0);

    datetime.tzid = NULL;
    e_cal_component_get_completed (comp, &datetime.value);
    if (type == E_CAL_COMPONENT_TODO && datetime.value) {
        /* Pass TRUE as is_utc, so it gets converted to the current
           timezone. */
        str = g_strdup_printf ("<b>%s:</b>", _("Completed"));
        datetime.value->is_utc = TRUE;
        write_label_piece (itip, &datetime, buffer, str, "<br>", FALSE);
        gtk_html_write (html, html_stream, buffer->str, buffer->len);
        wrote = TRUE;
        task_completed = TRUE;
        g_free (str);
    }
    e_cal_component_free_datetime (&datetime);

    /* Reset the buffer. */
    g_string_truncate (buffer, 0);

    e_cal_component_get_due (comp, &datetime);
    if (type == E_CAL_COMPONENT_TODO && !task_completed && datetime.value) {
        str = g_strdup_printf ("<b>%s:</b>", _("Due"));
        write_label_piece (itip, &datetime, buffer, str, "<br>", FALSE);
        gtk_html_write (html, html_stream, buffer->str, buffer->len);
        wrote = TRUE;
        g_free (str);
    }

    e_cal_component_free_datetime (&datetime);

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

    g_string_free (buffer, TRUE);
}

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;
    gchar *filename;

    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>",
                _("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 */
    filename = e_icon_factory_get_icon_filename ("stock_new-meeting", E_ICON_SIZE_DIALOG);
    gtk_html_stream_printf (html_stream, "<img src=\"%s\"></td>", filename);
    g_free (filename);

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

    /* Title */
    set_message (GTK_HTML (priv->html), html_stream, _("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;
    ECalComponentText text;
    ECalComponentOrganizer organizer;
    ECalComponentAttendee *attendee;
    GSList *attendees, *l = NULL;
    const char *string;
    gchar *html;
    const gchar *const_html;
    gchar *filename;
    gchar *str;

    priv = itip->priv;

    if (priv->html == NULL)
        return;

    /* Html widget */
    html_stream = gtk_html_begin (GTK_HTML (priv->html));
    gtk_html_stream_printf (html_stream,
                "<html><head><title>%s</title></head>",
                _("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 */
    filename = e_icon_factory_get_icon_filename ("stock_new-meeting", E_ICON_SIZE_DIALOG);
    gtk_html_stream_printf (html_stream, "<img src=\"%s\"></td>", filename);
    g_free (filename);

    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 */
        e_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, _("An unknown person"));
        }
        break;
    case ICAL_METHOD_REQUEST:
        /* The organizer sent this */
        e_cal_component_get_organizer (priv->comp, &organizer);
        if (priv->delegator_address != NULL) {
            if (organizer.value != NULL)
                html = g_strdup_printf (itip_desc,
                            organizer.cn ?
                            organizer.cn :
                            itip_strip_mailto (organizer.value),
                            priv->delegator_name ?
                            priv->delegator_name :
                                    priv->delegator_address);
            else
                html = g_strdup_printf (itip_desc, _("An unknown person"),
                            priv->delegator_name ?
                            priv->delegator_name :
                                    priv->delegator_address);
        } else {
            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, _("An unknown person"));
        }

        break;

    case ICAL_METHOD_PUBLISH:
    case ICAL_METHOD_ADD:
    case ICAL_METHOD_CANCEL:
    default:
        /* The organizer sent this */
        e_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, _("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 = _("<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 */
    e_cal_component_get_summary (priv->comp, &text);
    str = g_strdup_printf ("<i>%s:</i>", _("None"));

    html = text.value ? e_text_to_html_full (text.value, E_TEXT_TO_HTML_CONVERT_NL, 0) : str;
    gtk_html_stream_printf (html_stream, "<b>%s</b><br>%s<br><br>",
                _("Summary:"), html);
    g_free (str);
    if (text.value)
        g_free (html);

    /* Location */
    e_cal_component_get_location (priv->comp, &string);
    if (string != NULL) {
        html = e_text_to_html_full (string, E_TEXT_TO_HTML_CONVERT_NL, 0);
        gtk_html_stream_printf (html_stream, "<b>%s</b><br>%s<br><br>",
                    _("Location:"), html);
        g_free (html);
    }

    /* Status */
    if (priv->method == ICAL_METHOD_REPLY) {
        GSList *alist;

        e_cal_component_get_attendee_list (priv->comp, &alist);

        if (alist != NULL) {
            ECalComponentAttendee *a = alist->data;

            gtk_html_stream_printf (html_stream, "<b>%s</b><br>",
                        _("Status:"));

            switch (a->status) {
            case ICAL_PARTSTAT_ACCEPTED:
                gtk_html_stream_printf (html_stream, "%s<br><br>",
                            _("Accepted"));
                break;
            case ICAL_PARTSTAT_TENTATIVE:
                gtk_html_stream_printf (html_stream, "%s<br><br>",
                            _("Tentatively Accepted"));
                break;
            case ICAL_PARTSTAT_DECLINED:
                gtk_html_stream_printf (html_stream, "%s<br><br>",
                            _("Declined"));
                break;
            default:
                gtk_html_stream_printf (html_stream, "%s<br><br>",
                            _("Unknown"));
            }
        }

        e_cal_component_free_attendee_list (alist);
    }

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

    if (l && text.value) {
        html = e_text_to_html_full (text.value, E_TEXT_TO_HTML_CONVERT_NL, 0);
        gtk_html_stream_printf (html_stream, "<b>%s</b><br>%s",
                    _("Description:"), html);
        g_free (html);
    }
    e_cal_component_free_text_list (l);

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

    /* Options */
    if (!e_itip_control_get_view_only (itip)) {
        if (options != NULL) {
            const_html = "</td></tr><tr><td valign=\"center\">";
            gtk_html_write (GTK_HTML (priv->html), html_stream, const_html, strlen (const_html));
            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 (void)
{
    return g_strdup_printf ("<object classid=\"itip:publish_options\"></object>");
}

static char*
get_request_options (void)
{
    return g_strdup_printf ("<object classid=\"itip:request_options\"></object>");
}

static char*
get_request_fb_options (void)
{
    return g_strdup_printf ("<object classid=\"itip:freebusy_options\"></object>");
}

static char*
get_reply_options (void)
{
    return g_strdup_printf ("<object classid=\"itip:reply_options\"></object>");
}

static char*
get_refresh_options (void)
{
    return g_strdup_printf ("<object classid=\"itip:refresh_options\"></object>");
}

static char*
get_cancel_options (gboolean found, icalcomponent_kind kind)
{
    if (!found) {
        switch (kind) {
        case ICAL_VEVENT_COMPONENT:
            return g_strdup_printf ("<i>%s</i>", _("The meeting has been canceled, however it could not be found in your calendars"));
        case ICAL_VTODO_COMPONENT:
            return g_strdup_printf ("<i>%s</i>", _("The task has been canceled, however it could not be found in your task lists"));
        default:
            g_return_val_if_reached (NULL);
        }
    }

    return g_strdup_printf ("<object classid=\"itip:cancel_options\"></object>");
}


static ECalComponent *
get_real_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    ECalComponent *comp;
    icalcomponent *icalcomp;
    gboolean found = FALSE;
    const char *uid;

    priv = itip->priv;

    e_cal_component_get_uid (priv->comp, &uid);

    found = e_cal_get_object (priv->current_ecal, uid, NULL, &icalcomp, NULL);
    if (!found)
        return NULL;

    comp = e_cal_component_new ();
    if (!e_cal_component_set_icalcomponent (comp, icalcomp)) {
        g_object_unref (comp);
        icalcomponent_free (icalcomp);
        return NULL;
    }

    return comp;
}

static void
adjust_item (EItipControl *itip, ECalComponent *comp)
{
    ECalComponent *real_comp;

    real_comp = get_real_item (itip);
    if (real_comp != NULL) {
        ECalComponentText text;
        const char *string;
        GSList *l;

        e_cal_component_get_summary (real_comp, &text);
        e_cal_component_set_summary (comp, &text);
        e_cal_component_get_location (real_comp, &string);
        e_cal_component_set_location (comp, string);
        e_cal_component_get_description_list (real_comp, &l);
        e_cal_component_set_description_list (comp, l);
        e_cal_component_free_text_list (l);

        g_object_unref (real_comp);
    } else {
        ECalComponentText text = {_("Unknown"), NULL};

        e_cal_component_set_summary (comp, &text);
    }
}

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

    priv = itip->priv;

    priv->type = E_CAL_SOURCE_TYPE_EVENT;

    switch (priv->method) {
    case ICAL_METHOD_PUBLISH:
        itip_desc = _("<b>%s</b> has published meeting information.");
        itip_title = _("Meeting Information");
        options = get_publish_options ();
        show_selector = TRUE;
        break;
    case ICAL_METHOD_REQUEST:
        if (priv->delegator_address != NULL)
            itip_desc = _("<b>%s</b> requests the presence of %s at a meeting.");
        else
            itip_desc = _("<b>%s</b> requests your presence at a meeting.");
        itip_title = _("Meeting Proposal");
        options = get_request_options ();
        show_selector = TRUE;
        break;
    case ICAL_METHOD_ADD:
        /* FIXME Whats going on here? */
        itip_desc = _("<b>%s</b> wishes to be added to an existing meeting.");
        itip_title = _("Meeting Update");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REFRESH:
        itip_desc = _("<b>%s</b> wishes to receive the latest meeting information.");
        itip_title = _("Meeting Update Request");
        options = get_refresh_options ();

        /* Provide extra info, since its not in the component */
        adjust_item (itip, priv->comp);
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = _("<b>%s</b> has replied to a meeting request.");
        itip_title = _("Meeting Reply");
        options = get_reply_options ();

        /* Provide extra info, since might not be in the component */
        adjust_item (itip, priv->comp);
        break;
    case ICAL_METHOD_CANCEL:
        itip_desc = _("<b>%s</b> has canceled a meeting.");
        itip_title = _("Meeting Cancelation");
        /* FIXME priv->current_ecal will always be NULL so the
         * user won't see an error message, the OK button will
         * just be de-sensitized */
        options = get_cancel_options (TRUE, ICAL_VEVENT_COMPONENT);

        /* Provide extra info, since might not be in the component */
        adjust_item (itip, priv->comp);
        break;
    default:
        itip_desc = _("<b>%s</b> has sent an unintelligible message.");
        itip_title = _("Bad Meeting Message");
        options = NULL;
    }

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

    if (priv->calendar_uid)
        priv->current_ecal = start_calendar_server_by_uid (itip, priv->calendar_uid, priv->type);
    else
        find_server (itip, priv->comp, show_selector);
}

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

    priv = itip->priv;

    priv->type = E_CAL_SOURCE_TYPE_TODO;

    switch (priv->method) {
    case ICAL_METHOD_PUBLISH:
        itip_desc = _("<b>%s</b> has published task information.");
        itip_title = _("Task Information");
        options = get_publish_options ();
        show_selector = TRUE;
        break;
    case ICAL_METHOD_REQUEST:
        /* FIXME Does this need to handle like events above? */
        if (priv->delegator_address != NULL)
            itip_desc = _("<b>%s</b> requests %s to perform a task.");
        else
            itip_desc = _("<b>%s</b> requests you perform a task.");
        itip_title = _("Task Proposal");
        options = get_request_options ();
        show_selector = TRUE;
        break;
    case ICAL_METHOD_ADD:
        /* FIXME Whats going on here? */
        itip_desc = _("<b>%s</b> wishes to be added to an existing task.");
        itip_title = _("Task Update");
        options = get_publish_options ();
        break;
    case ICAL_METHOD_REFRESH:
        itip_desc = _("<b>%s</b> wishes to receive the latest task information.");
        itip_title = _("Task Update Request");
        options = get_refresh_options ();

        /* Provide extra info, since its not in the component */
        adjust_item (itip, priv->comp);
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = _("<b>%s</b> has replied to a task assignment.");
        itip_title = _("Task Reply");
        options = get_reply_options ();

        /* Provide extra info, since might not be in the component */
        adjust_item (itip, priv->comp);
        break;
    case ICAL_METHOD_CANCEL:
        itip_desc = _("<b>%s</b> has canceled a task.");
        itip_title = _("Task Cancelation");
        /* FIXME priv->current_ecal will always be NULL so the
         * user won't see an error message, the OK button will
         * just be de-sensitized */
        options = get_cancel_options (TRUE, ICAL_VTODO_COMPONENT);

        /* Provide extra info, since might not be in the component */
        adjust_item (itip, priv->comp);
        break;
    default:
        itip_desc = _("<b>%s</b> has sent an unintelligible message.");
        itip_title = _("Bad Task Message");
        options = NULL;
    }

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

    if (priv->calendar_uid)
        priv->current_ecal = start_calendar_server_by_uid (itip, priv->calendar_uid, priv->type);
    else
        find_server (itip, priv->comp, show_selector);
}

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 = _("<b>%s</b> has published free/busy information.");
        itip_title = _("Free/Busy Information");
        options = NULL;
        break;
    case ICAL_METHOD_REQUEST:
        itip_desc = _("<b>%s</b> requests your free/busy information.");
        itip_title = _("Free/Busy Request");
        options = get_request_fb_options ();
        break;
    case ICAL_METHOD_REPLY:
        itip_desc = _("<b>%s</b> has replied to a free/busy request.");
        itip_title = _("Free/Busy Reply");
        options = NULL;
        break;
    default:
        itip_desc = _("<b>%s</b> has sent an unintelligible message.");
        itip_title = _("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);
        if (ret == NULL)
            break;
        kind = icalcomponent_isa (ret);
    } while (ret != NULL
         && kind != ICAL_VEVENT_COMPONENT
         && kind != ICAL_VTODO_COMPONENT
         && kind != ICAL_VFREEBUSY_COMPONENT);

    return ret;
}

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

    priv = itip->priv;

    g_object_ref (itip);

    if (priv->comp)
        g_object_unref (priv->comp);
    priv->current_ecal = NULL;

    /* Determine any delegate sections */
    prop = icalcomponent_get_first_property (priv->ical_comp, ICAL_X_PROPERTY);
    while (prop) {
        const char *x_name, *x_val;

        x_name = icalproperty_get_x_name (prop);
        x_val = icalproperty_get_x (prop);

        if (!strcmp (x_name, "X-EVOLUTION-DELEGATOR-CALENDAR-UID"))
            e_itip_control_set_calendar_uid (itip, x_val);
        else if (!strcmp (x_name, "X-EVOLUTION-DELEGATOR-CALENDAR-URI"))
            g_warning (G_STRLOC ": X-EVOLUTION-DELEGATOR-CALENDAR-URI used");
        else if (!strcmp (x_name, "X-EVOLUTION-DELEGATOR-ADDRESS"))
            e_itip_control_set_delegator_address (itip, x_val);
        else if (!strcmp (x_name, "X-EVOLUTION-DELEGATOR-NAME"))
            e_itip_control_set_delegator_name (itip, x_val);

        prop = icalcomponent_get_next_property (priv->ical_comp, ICAL_X_PROPERTY);
    }

    /* 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) {
        icalcompiter_next (&alarm_iter);

        icalcomponent_remove_component (priv->ical_comp, alarm_comp);
        icalcomponent_free (alarm_comp);
    }

    priv->comp = e_cal_component_new ();
    if (!e_cal_component_set_icalcomponent (priv->comp, priv->ical_comp)) {
        write_error_html (itip, _("The message does not appear to be properly formed"));
        g_object_unref (priv->comp);
        priv->comp = NULL;
        g_object_unref (itip);
        return;
    };

    /* Add default reminder if the config says so */
    if (calendar_config_get_use_default_reminder ()) {
        ECalComponentAlarm *acomp;
        int interval;
        CalUnits units;
        ECalComponentAlarmTrigger trigger;

        interval = calendar_config_get_default_reminder_interval ();
        units = calendar_config_get_default_reminder_units ();

        acomp = e_cal_component_alarm_new ();

        e_cal_component_alarm_set_action (acomp, E_CAL_COMPONENT_ALARM_DISPLAY);

        trigger.type = E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
        memset (&trigger.u.rel_duration, 0, sizeof (trigger.u.rel_duration));

        trigger.u.rel_duration.is_neg = TRUE;

        switch (units) {
        case CAL_MINUTES:
            trigger.u.rel_duration.minutes = interval;
            break;
        case CAL_HOURS:
            trigger.u.rel_duration.hours = interval;
            break;
        case CAL_DAYS:
            trigger.u.rel_duration.days = interval;
            break;
        default:
            g_return_if_reached ();
        }

        e_cal_component_alarm_set_trigger (acomp, trigger);
        e_cal_component_add_alarm (priv->comp, acomp);

        e_cal_component_alarm_free (acomp);
    }

    type = e_cal_component_get_vtype (priv->comp);

    switch (type) {
    case E_CAL_COMPONENT_EVENT:
        show_current_event (itip);
        break;
    case E_CAL_COMPONENT_TODO:
        show_current_todo (itip);
        break;
    case E_CAL_COMPONENT_FREEBUSY:
        show_current_freebusy (itip);
        break;
    default:
        write_error_html (itip, _("The message contains only unsupported requests."));
    }

    find_my_address (itip, priv->ical_comp, NULL);

    g_object_unref (itip);
}

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;
    if (priv == NULL)
        return;

    clean_up (itip);

    if (text == NULL || *text == '\0') {
        gtk_html_load_from_string (GTK_HTML (priv->html), " ", 1);
        return;
    }

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

    priv->main_comp = icalparser_parse_string (priv->vcalendar);
    if (priv->main_comp == NULL || !is_icalcomp_valid (priv->main_comp)) {
        write_error_html (itip, _("The attachment does not contain a valid calendar message"));

        if (priv->main_comp) {
            icalcomponent_free (priv->main_comp);
            priv->main_comp = NULL;
        }

        return;
    }

    prop = icalcomponent_get_first_property (priv->main_comp, ICAL_METHOD_PROPERTY);
    if (prop == NULL) {
        priv->method = ICAL_METHOD_PUBLISH;
    } else {
        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);
    if (priv->ical_comp != NULL) {
        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, _("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;
}

void
e_itip_control_set_view_only (EItipControl *itip, gint view_only)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    priv->view_only = view_only;
}

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

    priv = itip->priv;

    return priv->view_only;
}


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

    priv = itip->priv;

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

    priv->delegator_address = g_strdup (address);
}

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

    priv = itip->priv;

    return priv->delegator_address;
}


void
e_itip_control_set_delegator_name (EItipControl *itip, const gchar *name)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

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

    priv->delegator_name = g_strdup (name);
}

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

    priv = itip->priv;

    return priv->delegator_name;
}

void
e_itip_control_set_calendar_uid (EItipControl *itip, const gchar *uri)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

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

    priv->calendar_uid = g_strdup (uri);
}

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

    priv = itip->priv;

    return priv->calendar_uid;
}


static gboolean
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);
    } else {
        icalparameter *param;

        if (address != NULL) {
            prop = icalproperty_new_attendee (address);
            icalcomponent_add_property (ical_comp, prop);

            param = icalparameter_new_role (ICAL_ROLE_OPTPARTICIPANT);
            icalproperty_add_parameter (prop, param);

            param = icalparameter_new_partstat (status);
            icalproperty_add_parameter (prop, param);
        } else {
            EAccount *a;

            a = itip_addresses_get_default ();

            prop = icalproperty_new_attendee (a->id->address);
            icalcomponent_add_property (ical_comp, prop);

            param = icalparameter_new_cn (a->id->name);
            icalproperty_add_parameter (prop, param);

            param = icalparameter_new_role (ICAL_ROLE_REQPARTICIPANT);
            icalproperty_add_parameter (prop, param);

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

    return TRUE;
}

static void
update_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    struct icaltimetype stamp;
    char *str;
    icalproperty *prop;
    icalcomponent *clone;
    GtkWidget *dialog;
    GError *error = NULL;

    priv = itip->priv;

    /* Set X-MICROSOFT-CDO-REPLYTIME to record the time at which
     * the user accepted/declined the request. (Outlook ignores
     * SEQUENCE in REPLY reponses and instead requires that each
     * updated response have a later REPLYTIME than the previous
     * one.) This also ends up getting saved in our own copy of
     * the meeting, though there's currently no way to see that
     * information (unless it's being saved to an Exchange folder
     * and you then look at it in Outlook).
     */
    stamp = icaltime_current_time_with_zone (icaltimezone_get_utc_timezone ());
    str = icaltime_as_ical_string (stamp);
    prop = icalproperty_new_x (str);
    g_free (str);
    icalproperty_set_x_name (prop, "X-MICROSOFT-CDO-REPLYTIME");
    icalcomponent_add_property (priv->ical_comp, prop);

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

    if (!e_cal_receive_objects (priv->current_ecal, priv->top_level, &error)) {
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_ERROR,
            GTK_BUTTONS_OK,
            "%s", error->message);
        g_error_free (error);
    } else {
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "%s", _("Update complete\n"));
    }
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);

    icalcomponent_remove_component (priv->top_level, clone);
    icalcomponent_free (clone);
}

static void
update_attendee_status (EItipControl *itip)
{
    EItipControlPrivate *priv;
    ECalComponent *comp = NULL;
    icalcomponent *icalcomp = NULL;
    const char *uid;
    GtkWidget *dialog;
    GError *error = NULL;

    priv = itip->priv;

    /* Obtain our version */
    e_cal_component_get_uid (priv->comp, &uid);
    if (e_cal_get_object (priv->current_ecal, uid, NULL, &icalcomp, NULL)) {
        GSList *attendees;

        comp = e_cal_component_new ();
        if (!e_cal_component_set_icalcomponent (comp, icalcomp)) {
            icalcomponent_free (icalcomp);

            dialog = gtk_message_dialog_new (
                NULL, 0,
                GTK_MESSAGE_WARNING,
                GTK_BUTTONS_OK,
                "%s", _("Object is invalid and "
                "cannot be updated\n"));
        } else {
            e_cal_component_get_attendee_list (priv->comp, &attendees);
            if (attendees != NULL) {
                ECalComponentAttendee *a = attendees->data;
                icalproperty *prop;

                prop = find_attendee (icalcomp, itip_strip_mailto (a->value));

                if (prop == NULL) {
                    gint response;

                    dialog = gtk_message_dialog_new (
                        NULL, GTK_DIALOG_MODAL,
                        GTK_MESSAGE_QUESTION,
                        GTK_BUTTONS_YES_NO,
                        "%s", _("This response is not from a "
                        "current attendee.  Add as an attendee?"));
                    response = gtk_dialog_run (GTK_DIALOG (dialog));
                    gtk_widget_destroy (dialog);

                    if (response == GTK_RESPONSE_YES) {
                        change_status (icalcomp,
                                   itip_strip_mailto (a->value),
                                   a->status);
                        e_cal_component_rescan (comp);
                    } else {
                        goto cleanup;
                    }
                } else if (a->status == ICAL_PARTSTAT_NONE || a->status == ICAL_PARTSTAT_X) {
                    dialog = gtk_message_dialog_new (
                        NULL, 0,
                        GTK_MESSAGE_WARNING,
                        GTK_BUTTONS_OK,
                        "%s", _("Attendee status could not be "
                        "updated because of an invalid status!\n"));
                    goto run;
                } else {
                    change_status (icalcomp,
                               itip_strip_mailto (a->value),
                               a->status);
                    e_cal_component_rescan (comp);
                }
            }
        }

        if (!e_cal_modify_object (priv->current_ecal, icalcomp, CALOBJ_MOD_ALL, &error)) {
            dialog = gtk_message_dialog_new (
                NULL, 0,
                GTK_MESSAGE_WARNING,
                GTK_BUTTONS_OK,
                "%s", error->message);
            g_error_free (error);
        } else {
            dialog = gtk_message_dialog_new (
                NULL, 0,
                GTK_MESSAGE_INFO,
                GTK_BUTTONS_OK,
                "%s", _("Attendee status updated\n"));
        }
    } else {
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK,
            "%s", _("Attendee status can not be updated "
            "because the item no longer exists"));
    }

 run:
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);

 cleanup:
    if (comp != NULL)
        g_object_unref (comp);
}

static void
send_item (EItipControl *itip)
{
    EItipControlPrivate *priv;
    ECalComponent *comp;
    GtkWidget *dialog;

    priv = itip->priv;

    comp = get_real_item (itip);

    if (comp != NULL) {
        itip_send_comp (E_CAL_COMPONENT_METHOD_REQUEST, comp, priv->current_ecal, NULL, NULL, NULL);
        g_object_unref (comp);
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "%s", _("Item sent!\n"));
    } else {
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK,
            "%s", _("The item could not be sent!\n"));
    }
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

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

    priv = itip->priv;

    e_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);
    e_cal_component_free_datetime (&datetime);

    e_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);
    e_cal_component_free_datetime (&datetime);

    if (e_cal_get_free_busy (priv->current_ecal, NULL, start, end, &comp_list, NULL)) {
        GList *l;

        for (l = comp_list; l; l = l->next) {
            ECalComponent *comp = E_CAL_COMPONENT (l->data);
            itip_send_comp (E_CAL_COMPONENT_METHOD_REPLY, comp, priv->current_ecal, NULL, NULL, NULL);

            g_object_unref (comp);
        }
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_INFO,
            GTK_BUTTONS_OK,
            "%s", _("Item sent!\n"));

        g_list_free (comp_list);
    } else {
        dialog = gtk_message_dialog_new (
            NULL, 0,
            GTK_MESSAGE_WARNING,
            GTK_BUTTONS_OK,
            "%s", _("The item could not be sent!\n"));
    }
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

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

    if ((fd = g_open (url, O_RDONLY|O_BINARY, 0)) == -1) {
        g_warning ("%s", g_strerror (errno));
        return;
    }

        while ((len = read (fd, buffer, 4096)) > 0) {
        gtk_html_write (html, handle, (char *)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));
        return;
    }
    /* done with no errors */
    gtk_html_end (html, handle, GTK_HTML_STREAM_OK);
    close (fd);
}

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

    priv = itip->priv;

    priv->action = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (widget), ACTION_DATA));
}

static void
add_option (EItipControl *itip, GtkWidget *menu, const char *text, char action)
{
    GtkWidget *item;

    item = gtk_menu_item_new_with_label (text);
    g_signal_connect (item, "activate", G_CALLBACK (option_activated_cb), itip);
    g_object_set_data (G_OBJECT (item), ACTION_DATA, GINT_TO_POINTER ((int)action));
    gtk_menu_shell_append (GTK_MENU_SHELL (menu), item);
    gtk_widget_show (item);

}

static void
insert_boxes (GtkHTMLEmbedded *eb, EItipControl *itip)
{
    EItipControlPrivate *priv;

    priv = itip->priv;

    priv->vbox.widget = gtk_vbox_new (FALSE, 12);
    g_object_add_weak_pointer (G_OBJECT (priv->vbox.widget), &priv->vbox.pointer);
    gtk_container_add (GTK_CONTAINER (eb), priv->vbox.widget);
    gtk_widget_show (priv->vbox.widget);

    priv->hbox.widget = gtk_hbox_new (FALSE, 6);
    g_object_add_weak_pointer (G_OBJECT (priv->hbox.widget), &priv->hbox.pointer);

    gtk_box_pack_start (GTK_BOX (priv->vbox.widget), priv->hbox.widget, FALSE, TRUE, 0);
    gtk_widget_show (priv->hbox.widget);
}

static void
insert_label (GtkWidget *hbox)
{
    GtkWidget *label;
    char *text;

    text = g_strdup_printf ("<b>%s</b>", _("Choose an action:"));
    label = gtk_label_new (NULL);
    gtk_label_set_markup (GTK_LABEL (label), text);
    g_free (text);

    gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, TRUE, 0);
    gtk_widget_show (label);
}

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

    priv = itip->priv;

    priv->rsvp = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget));
}

static void
insert_rsvp (GtkWidget *hbox, EItipControl *itip)
{
    EItipControlPrivate *priv;
    GtkWidget *btn;

    priv = itip->priv;

    /* To translators: RSVP means "please reply" */
    btn = gtk_check_button_new_with_label ("RSVP");
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (btn), TRUE);
    priv->rsvp = TRUE;

    g_signal_connect (btn, "clicked", G_CALLBACK (rsvp_clicked_cb), itip);

    gtk_box_pack_start (GTK_BOX (hbox), btn, FALSE, TRUE, 0);
    gtk_widget_show (btn);
}

static void
insert_ok (GtkWidget *hbox, EItipControl *itip)
{
    EItipControlPrivate *priv;
    priv = itip->priv;

    priv->ok.widget = gtk_button_new_from_stock (GTK_STOCK_OK);
    g_object_add_weak_pointer (G_OBJECT (priv->ok.widget), &priv->ok.pointer);

    g_signal_connect (priv->ok.widget, "clicked", G_CALLBACK (ok_clicked_cb), itip);

    set_ok_sens (itip);

    gtk_box_pack_start (GTK_BOX (hbox), priv->ok.widget, FALSE, TRUE, 0);
    gtk_widget_show (priv->ok.widget);
}

static gboolean
publish_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Update"), 'U');
    priv->action = 'U';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_box_pack_start (GTK_BOX (priv->hbox.widget), option, FALSE, TRUE, 0);
    gtk_widget_show (option);

    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
request_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Accept"), 'A');
    add_option (itip, menu, _("Tentatively accept"), 'T');
    add_option (itip, menu, _("Decline"), 'D');
    priv->action = 'A';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_box_pack_start (GTK_BOX (priv->hbox.widget), option, FALSE, TRUE, 0);
    gtk_widget_show (option);

    insert_rsvp (priv->hbox.widget, itip);
    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
freebusy_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Send Free/Busy Information"), 'F');
    priv->action = 'F';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_container_add (GTK_CONTAINER (priv->hbox.widget), option);
    gtk_widget_show (option);

    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
reply_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Update respondent status"), 'R');
    priv->action = 'R';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_container_add (GTK_CONTAINER (priv->hbox.widget), option);
    gtk_widget_show (option);

    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
refresh_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Send Latest Information"), 'S');
    priv->action = 'R';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_container_add (GTK_CONTAINER (priv->hbox.widget), option);
    gtk_widget_show (option);

    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
cancel_options_object (EItipControl *itip, GtkHTML *html, GtkHTMLEmbedded *eb)
{
    EItipControlPrivate *priv;
    GtkWidget *option, *menu;

    priv = itip->priv;

    insert_boxes (eb, itip);
    insert_label (priv->hbox.widget);

    option = gtk_option_menu_new ();

    menu = gtk_menu_new ();

    add_option (itip, menu, _("Cancel"), 'C');
    priv->action = 'C';

    gtk_option_menu_set_menu (GTK_OPTION_MENU (option), menu);

    gtk_container_add (GTK_CONTAINER (priv->hbox.widget), option);
    gtk_widget_show (option);

    insert_ok (priv->hbox.widget, itip);

    return TRUE;
}

static gboolean
object_requested_cb (GtkHTML *html, GtkHTMLEmbedded *eb, gpointer data)
{
    EItipControl *itip = E_ITIP_CONTROL (data);

    if (!strcmp (eb->classid, "itip:publish_options"))
        return publish_options_object (itip, html, eb);
    else if (!strcmp (eb->classid, "itip:request_options"))
        return request_options_object (itip, html, eb);
    else if (!strcmp (eb->classid, "itip:freebusy_options"))
        return freebusy_options_object (itip, html, eb);
    else if (!strcmp (eb->classid, "itip:reply_options"))
        return reply_options_object (itip, html, eb);
    else if (!strcmp (eb->classid, "itip:refresh_options"))
        return refresh_options_object (itip, html, eb);
    else if (!strcmp (eb->classid, "itip:cancel_options"))
        return cancel_options_object (itip, html, eb);

    return FALSE;
}

static void
ok_clicked_cb (GtkWidget *widget, gpointer data)
{
    EItipControl *itip = E_ITIP_CONTROL (data);
    EItipControlPrivate *priv;
    gboolean status = FALSE;

    priv = itip->priv;

    if (!priv->my_address && priv->current_ecal != NULL)
        e_cal_get_cal_address (priv->current_ecal, &priv->my_address, NULL);

    switch (priv->action) {
    case 'U':
        update_item (itip);
        break;
    case 'A':
        status = change_status (priv->ical_comp, priv->my_address,
                    ICAL_PARTSTAT_ACCEPTED);
        if (status) {
            e_cal_component_rescan (priv->comp);
            update_item (itip);
        }
        break;
    case 'T':
        status = change_status (priv->ical_comp, priv->my_address,
                    ICAL_PARTSTAT_TENTATIVE);
        if (status) {
            e_cal_component_rescan (priv->comp);
            update_item (itip);
        }
        break;
    case 'D':
        status = change_status (priv->ical_comp, priv->my_address,
                    ICAL_PARTSTAT_DECLINED);
        if (status) {
            e_cal_component_rescan (priv->comp);
            update_item (itip);
        }
        break;
    case 'F':
        send_freebusy (itip);
        break;
    case 'R':
        update_attendee_status (itip);
        break;
    case 'S':
        send_item (itip);
        break;
    case 'C':
        update_item (itip);
        break;
    }

    if (e_cal_get_save_schedules (priv->current_ecal))
        return;

    if (priv->rsvp && status) {
        ECalComponent *comp = NULL;
        icalcomponent *ical_comp;
        icalproperty *prop;
        icalvalue *value;
        const char *attendee;
        GSList *l, *list = NULL;

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

        if (priv->my_address == NULL)
            find_my_address (itip, priv->ical_comp, NULL);
        g_return_if_fail (priv->my_address != NULL);

        ical_comp = e_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))
        {
            char *text;

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

            attendee = icalvalue_get_string (value);

            text = g_strdup (itip_strip_mailto (attendee));
            text = g_strstrip (text);
            if (g_ascii_strcasecmp (priv->my_address, text))
                list = g_slist_prepend (list, prop);
            g_free (text);
        }

        for (l = list; l; l = l->next) {
            prop = l->data;
            icalcomponent_remove_property (ical_comp, prop);
            icalproperty_free (prop);
        }
        g_slist_free (list);

        e_cal_component_rescan (comp);
        itip_send_comp (E_CAL_COMPONENT_METHOD_REPLY, comp, priv->current_ecal, priv->top_level, NULL, NULL);

        g_object_unref (comp);
    }
}