aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/pcs/cal-backend-file.c
blob: c906b09ffd7965ebaf8ad02af9d0a7f9aa1faedf (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
/* Evolution calendar - iCalendar file backend
 *
 * Copyright (C) 2000 Ximian, Inc.
 * Copyright (C) 2000 Ximian, Inc.
 *
 * Author: Federico Mena-Quintero <federico@ximian.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.
 */

#include <config.h>
#include <gtk/gtksignal.h>
#include "e-util/e-dbhash.h"
#include "cal-util/cal-recur.h"
#include "cal-backend-file.h"



/* Private part of the CalBackendFile structure */
struct _CalBackendFilePrivate {
    /* URI where the calendar data is stored */
    GnomeVFSURI *uri;

    /* List of Cal objects with their listeners */
    GList *clients;

    /* Toplevel VCALENDAR component */
    icalcomponent *icalcomp;

    /* All the CalComponent objects in the calendar, hashed by UID.  The
     * hash key *is* the uid returned by cal_component_get_uid(); it is not
     * copied, so don't free it when you remove an object from the hash
     * table.
     */
    GHashTable *comp_uid_hash;

    /* All event, to-do, and journal components in the calendar; they are
     * here just for easy access (i.e. so that you don't have to iterate
     * over the comp_uid_hash).  If you need *all* the components in the
     * calendar, iterate over the hash instead.
     */
    GList *events;
    GList *todos;
    GList *journals;

    /* Idle handler for saving the calendar when it is dirty */
    guint idle_id;
};



static void cal_backend_file_class_init (CalBackendFileClass *class);
static void cal_backend_file_init (CalBackendFile *cbfile);
static void cal_backend_file_destroy (GtkObject *object);

static GnomeVFSURI *cal_backend_file_get_uri (CalBackend *backend);
static void cal_backend_file_add_cal (CalBackend *backend, Cal *cal);
static CalBackendOpenStatus cal_backend_file_open (CalBackend *backend, GnomeVFSURI *uri,
                           gboolean only_if_exists);
static gboolean cal_backend_file_is_loaded (CalBackend *backend);

static int cal_backend_file_get_n_objects (CalBackend *backend, CalObjType type);
static char *cal_backend_file_get_object (CalBackend *backend, const char *uid);
static char *cal_backend_file_get_timezone_object (CalBackend *backend, const char *tzid);
static CalObjType cal_backend_file_get_type_by_uid (CalBackend *backend, const char *uid);
static GList *cal_backend_file_get_uids (CalBackend *backend, CalObjType type);
static GList *cal_backend_file_get_objects_in_range (CalBackend *backend, CalObjType type,
                             time_t start, time_t end);
static char *cal_backend_file_get_free_busy (CalBackend *backend, time_t start, time_t end);
static GNOME_Evolution_Calendar_CalObjChangeSeq *cal_backend_file_get_changes (
    CalBackend *backend, CalObjType type, const char *change_id);

static GNOME_Evolution_Calendar_CalComponentAlarmsSeq *cal_backend_file_get_alarms_in_range (
    CalBackend *backend, time_t start, time_t end);

static GNOME_Evolution_Calendar_CalComponentAlarms *cal_backend_file_get_alarms_for_object (
    CalBackend *backend, const char *uid,
    time_t start, time_t end, gboolean *object_found);

static gboolean cal_backend_file_update_objects (CalBackend *backend, const char *calobj);
static gboolean cal_backend_file_remove_object (CalBackend *backend, const char *uid);

static icaltimezone* cal_backend_file_get_timezone (CalBackend *backend, const char *tzid);

static CalBackendClass *parent_class;



/**
 * cal_backend_file_get_type:
 * @void: 
 * 
 * Registers the #CalBackendFile class if necessary, and returns the type ID
 * associated to it.
 * 
 * Return value: The type ID of the #CalBackendFile class.
 **/
GtkType
cal_backend_file_get_type (void)
{
    static GtkType cal_backend_file_type = 0;

    if (!cal_backend_file_type) {
        static const GtkTypeInfo cal_backend_file_info = {
            "CalBackendFile",
            sizeof (CalBackendFile),
            sizeof (CalBackendFileClass),
            (GtkClassInitFunc) cal_backend_file_class_init,
            (GtkObjectInitFunc) cal_backend_file_init,
            NULL, /* reserved_1 */
            NULL, /* reserved_2 */
            (GtkClassInitFunc) NULL
        };

        cal_backend_file_type = gtk_type_unique (CAL_BACKEND_TYPE, &cal_backend_file_info);
    }

    return cal_backend_file_type;
}

/* Class initialization function for the file backend */
static void
cal_backend_file_class_init (CalBackendFileClass *class)
{
    GtkObjectClass *object_class;
    CalBackendClass *backend_class;

    object_class = (GtkObjectClass *) class;
    backend_class = (CalBackendClass *) class;

    parent_class = gtk_type_class (CAL_BACKEND_TYPE);

    object_class->destroy = cal_backend_file_destroy;

    backend_class->get_uri = cal_backend_file_get_uri;
    backend_class->add_cal = cal_backend_file_add_cal;
    backend_class->open = cal_backend_file_open;
    backend_class->is_loaded = cal_backend_file_is_loaded;
    backend_class->get_n_objects = cal_backend_file_get_n_objects;
    backend_class->get_object = cal_backend_file_get_object;
    backend_class->get_timezone_object = cal_backend_file_get_timezone_object;
    backend_class->get_type_by_uid = cal_backend_file_get_type_by_uid;
    backend_class->get_uids = cal_backend_file_get_uids;
    backend_class->get_objects_in_range = cal_backend_file_get_objects_in_range;
    backend_class->get_free_busy = cal_backend_file_get_free_busy;
    backend_class->get_changes = cal_backend_file_get_changes;
    backend_class->get_alarms_in_range = cal_backend_file_get_alarms_in_range;
    backend_class->get_alarms_for_object = cal_backend_file_get_alarms_for_object;
    backend_class->update_objects = cal_backend_file_update_objects;
    backend_class->remove_object = cal_backend_file_remove_object;

    backend_class->get_timezone = cal_backend_file_get_timezone;
}

/* Object initialization function for the file backend */
static void
cal_backend_file_init (CalBackendFile *cbfile)
{
    CalBackendFilePrivate *priv;

    priv = g_new0 (CalBackendFilePrivate, 1);
    cbfile->priv = priv;

    priv->uri = NULL;
    priv->clients = NULL;
    priv->icalcomp = NULL;
    priv->comp_uid_hash = NULL;
    priv->events = NULL;
    priv->todos = NULL;
    priv->journals = NULL;
}

/* g_hash_table_foreach() callback to destroy a CalComponent */
static void
free_cal_component (gpointer key, gpointer value, gpointer data)
{
    CalComponent *comp;

    comp = CAL_COMPONENT (value);
    gtk_object_unref (GTK_OBJECT (comp));
}

/* Saves the calendar data */
static void
save (CalBackendFile *cbfile)
{
    CalBackendFilePrivate *priv;
    GnomeVFSHandle *handle = NULL;
    GnomeVFSResult result;
    GnomeVFSFileSize out;
    gchar *tmp;
    char *buf;
    
    priv = cbfile->priv;
    g_assert (priv->uri != NULL);
    g_assert (priv->icalcomp != NULL);

    /* Make a backup copy of the file if it exists */
    tmp = gnome_vfs_uri_to_string (priv->uri, GNOME_VFS_URI_HIDE_NONE);
    if (tmp) {
        GnomeVFSURI *backup_uri;
        gchar *backup_uristr;
        
        backup_uristr = g_strconcat (tmp, "~", NULL);
        backup_uri = gnome_vfs_uri_new (backup_uristr);
        
        result = gnome_vfs_move_uri (priv->uri, backup_uri, TRUE);
        gnome_vfs_uri_unref (backup_uri);
        
        g_free (tmp);
        g_free (backup_uristr);
    }
    
    /* Now write the new file out */
    result = gnome_vfs_create_uri (&handle, priv->uri, 
                       GNOME_VFS_OPEN_WRITE,
                       FALSE, 0666);
    
    if (result != GNOME_VFS_OK)
        goto error;
    
    buf = icalcomponent_as_ical_string (priv->icalcomp);
    result = gnome_vfs_write (handle, buf, strlen (buf) * sizeof (char), &out);

    if (result != GNOME_VFS_OK)
        goto error;

    gnome_vfs_close (handle);

    return;
    
 error:
    g_warning ("Error writing calendar file.");
    return;
}

/* Destroy handler for the file backend */
static void
cal_backend_file_destroy (GtkObject *object)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    g_return_if_fail (object != NULL);
    g_return_if_fail (IS_CAL_BACKEND_FILE (object));

    cbfile = CAL_BACKEND_FILE (object);
    priv = cbfile->priv;

    g_assert (priv->clients == NULL);

    /* Save if necessary */

    if (priv->idle_id != 0) {
        save (cbfile);
        g_source_remove (priv->idle_id);
        priv->idle_id = 0;
    }

    /* Clean up */

    if (priv->uri) {
        gnome_vfs_uri_unref (priv->uri);
        priv->uri = NULL;
    }

    if (priv->comp_uid_hash) {
        g_hash_table_foreach (priv->comp_uid_hash, 
                      free_cal_component, NULL);
        g_hash_table_destroy (priv->comp_uid_hash);
        priv->comp_uid_hash = NULL;
    }

    g_list_free (priv->events);
    g_list_free (priv->todos);
    g_list_free (priv->journals);

    priv->events = NULL;
    priv->todos = NULL;
    priv->journals = NULL;

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

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

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



/* Looks up a component by its UID on the backend's component hash table */
static CalComponent *
lookup_component (CalBackendFile *cbfile, const char *uid)
{
    CalBackendFilePrivate *priv;
    CalComponent *comp;

    priv = cbfile->priv;

    comp = g_hash_table_lookup (priv->comp_uid_hash, uid);

    return comp;
}



/* Calendar backend methods */

/* Get_uri handler for the file backend */
static GnomeVFSURI *
cal_backend_file_get_uri (CalBackend *backend)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);
    g_assert (priv->uri != NULL);

    return priv->uri;
}

/* Callback used when a Cal is destroyed */
static void
cal_destroy_cb (GtkObject *object, gpointer data)
{
    Cal *cal;
    Cal *lcal;
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    GList *l;

    cal = CAL (object);

    cbfile = CAL_BACKEND_FILE (data);
    priv = cbfile->priv;

    /* Find the cal in the list of clients */

    for (l = priv->clients; l; l = l->next) {
        lcal = CAL (l->data);

        if (lcal == cal)
            break;
    }

    g_assert (l != NULL);

    /* Disconnect */

    priv->clients = g_list_remove_link (priv->clients, l);
    g_list_free_1 (l);

    /* When all clients go away, notify the parent factory about it so that
     * it may decide whether to kill the backend or not.
     */
    if (!priv->clients)
        cal_backend_last_client_gone (CAL_BACKEND (cbfile));
}

/* Add_cal handler for the file backend */
static void
cal_backend_file_add_cal (CalBackend *backend, Cal *cal)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_if_fail (priv->icalcomp != NULL);
    g_return_if_fail (cal != NULL);
    g_return_if_fail (IS_CAL (cal));

    /* We do not keep a reference to the Cal since the calendar user agent
     * owns it.
     */

    gtk_signal_connect (GTK_OBJECT (cal), "destroy",
                GTK_SIGNAL_FUNC (cal_destroy_cb),
                backend);

    priv->clients = g_list_prepend (priv->clients, cal);
}

/* Idle handler; we save the calendar since it is dirty */
static gboolean
save_idle (gpointer data)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    cbfile = CAL_BACKEND_FILE (data);
    priv = cbfile->priv;

    g_assert (priv->icalcomp != NULL);

    save (cbfile);

    priv->idle_id = 0;
    return FALSE;
}

/* Marks the file backend as dirty and queues a save operation */
static void
mark_dirty (CalBackendFile *cbfile)
{
    CalBackendFilePrivate *priv;

    priv = cbfile->priv;

    if (priv->idle_id != 0)
        return;

    priv->idle_id = g_idle_add (save_idle, cbfile);
}

/* Checks if the specified component has a duplicated UID and if so changes it */
static void
check_dup_uid (CalBackendFile *cbfile, CalComponent *comp)
{
    CalBackendFilePrivate *priv;
    CalComponent *old_comp;
    const char *uid;
    char *new_uid;

    priv = cbfile->priv;

    cal_component_get_uid (comp, &uid);

    old_comp = g_hash_table_lookup (priv->comp_uid_hash, uid);
    if (!old_comp)
        return; /* Everything is fine */

    g_message ("check_dup_uid(): Got object with duplicated UID `%s', changing it...", uid);

    new_uid = cal_component_gen_uid ();
    cal_component_set_uid (comp, new_uid);
    g_free (new_uid);

    /* FIXME: I think we need to reset the SEQUENCE property and reset the
     * CREATED/DTSTAMP/LAST-MODIFIED.
     */

    mark_dirty (cbfile);
}

/* Tries to add an icalcomponent to the file backend.  We only store the objects
 * of the types we support; all others just remain in the toplevel component so
 * that we don't lose them.
 */
static void
add_component (CalBackendFile *cbfile, CalComponent *comp, gboolean add_to_toplevel)
{
    CalBackendFilePrivate *priv;
    GList **list;
    const char *uid;
    
    priv = cbfile->priv;

    switch (cal_component_get_vtype (comp)) {
    case CAL_COMPONENT_EVENT:
        list = &priv->events;
        break;

    case CAL_COMPONENT_TODO:
        list = &priv->todos;
        break;

    case CAL_COMPONENT_JOURNAL:
        list = &priv->journals;
        break;

    default:
        g_assert_not_reached ();
        return;
    }

    /* Ensure that the UID is unique; some broken implementations spit
     * components with duplicated UIDs.
     */
    check_dup_uid (cbfile, comp);
    cal_component_get_uid (comp, &uid);
    g_hash_table_insert (priv->comp_uid_hash, (char *)uid, comp);

    *list = g_list_prepend (*list, comp);

    /* Put the object in the toplevel component if required */

    if (add_to_toplevel) {
        icalcomponent *icalcomp;

        icalcomp = cal_component_get_icalcomponent (comp);
        g_assert (icalcomp != NULL);

        icalcomponent_add_component (priv->icalcomp, icalcomp);
    }
}

/* Removes a component from the backend's hash and lists.  Does not perform
 * notification on the clients.  Also removes the component from the toplevel
 * icalcomponent.
 */
static void
remove_component (CalBackendFile *cbfile, CalComponent *comp)
{
    CalBackendFilePrivate *priv;
    icalcomponent *icalcomp;
    const char *uid;
    GList **list, *l;

    priv = cbfile->priv;

    /* Remove the icalcomp from the toplevel */

    icalcomp = cal_component_get_icalcomponent (comp);
    g_assert (icalcomp != NULL);

    icalcomponent_remove_component (priv->icalcomp, icalcomp);

    /* Remove it from our mapping */

    cal_component_get_uid (comp, &uid);
    g_hash_table_remove (priv->comp_uid_hash, uid);
    
    switch (cal_component_get_vtype (comp)) {
    case CAL_COMPONENT_EVENT:
        list = &priv->events;
        break;

    case CAL_COMPONENT_TODO:
        list = &priv->todos;
        break;

    case CAL_COMPONENT_JOURNAL:
        list = &priv->journals;
        break;

    default:
                /* Make the compiler shut up. */
            list = NULL;
        g_assert_not_reached ();
    }

    l = g_list_find (*list, comp);
    g_assert (l != NULL);

    *list = g_list_remove_link (*list, l);
    g_list_free_1 (l);

    gtk_object_unref (GTK_OBJECT (comp));
}

/* Scans the toplevel VCALENDAR component and stores the objects it finds */
static void
scan_vcalendar (CalBackendFile *cbfile)
{
    CalBackendFilePrivate *priv;
    icalcompiter iter;

    priv = cbfile->priv;
    g_assert (priv->icalcomp != NULL);
    g_assert (priv->comp_uid_hash != NULL);

    for (iter = icalcomponent_begin_component (priv->icalcomp, ICAL_ANY_COMPONENT);
         icalcompiter_deref (&iter) != NULL;
         icalcompiter_next (&iter)) {
        icalcomponent *icalcomp;
        icalcomponent_kind kind;
        CalComponent *comp;

        icalcomp = icalcompiter_deref (&iter);
        
        kind = icalcomponent_isa (icalcomp);

        if (!(kind == ICAL_VEVENT_COMPONENT
              || kind == ICAL_VTODO_COMPONENT
              || kind == ICAL_VJOURNAL_COMPONENT))
            continue;

        comp = cal_component_new ();

        if (!cal_component_set_icalcomponent (comp, icalcomp))
            continue;

        add_component (cbfile, comp, FALSE);
    }
}

/* Callback used from icalparser_parse() */
static char *
get_line_fn (char *s, size_t size, void *data)
{
    FILE *file;

    file = data;
    return fgets (s, size, file);
}

/* Parses an open iCalendar file and returns a toplevel component with the contents */
static icalcomponent *
parse_file (FILE *file)
{
    icalparser *parser;
    icalcomponent *icalcomp;

    parser = icalparser_new ();
    icalparser_set_gen_data (parser, file);

    icalcomp = icalparser_parse (parser, get_line_fn);
    icalparser_free (parser);

    return icalcomp;
}

/* Parses an open iCalendar file and loads it into the backend */
static CalBackendOpenStatus
open_cal (CalBackendFile *cbfile, GnomeVFSURI *uri, FILE *file)
{
    CalBackendFilePrivate *priv;
    icalcomponent *icalcomp;

    priv = cbfile->priv;

    icalcomp = parse_file (file);

    if (fclose (file) != 0) {
        if (icalcomp)
            icalcomponent_free (icalcomp);

        return CAL_BACKEND_OPEN_ERROR;
    }

    if (!icalcomp)
        return CAL_BACKEND_OPEN_ERROR;
        
    /* FIXME: should we try to demangle XROOT components and
     * individual components as well?
     */

    if (icalcomponent_isa (icalcomp) != ICAL_VCALENDAR_COMPONENT) {
        icalcomponent_free (icalcomp);
        return CAL_BACKEND_OPEN_ERROR;
    }

    priv->icalcomp = icalcomp;

    priv->comp_uid_hash = g_hash_table_new (g_str_hash, g_str_equal);
    scan_vcalendar (cbfile);

    gnome_vfs_uri_ref (uri);
    priv->uri = uri;

    return CAL_BACKEND_OPEN_SUCCESS;
}

static CalBackendOpenStatus
create_cal (CalBackendFile *cbfile, GnomeVFSURI *uri)
{
    CalBackendFilePrivate *priv;
    icalproperty *prop;

    priv = cbfile->priv;

    /* Create the new calendar information */

    priv->icalcomp = icalcomponent_new (ICAL_VCALENDAR_COMPONENT);

    /* RFC 2445, section 4.7.1 */
    prop = icalproperty_new_calscale ("GREGORIAN");
    icalcomponent_add_property (priv->icalcomp, prop);

    /* RFC 2445, section 4.7.3 */
    prop = icalproperty_new_prodid ("-//Ximian//NONSGML Evolution Calendar//EN");
    icalcomponent_add_property (priv->icalcomp, prop);

    /* RFC 2445, section 4.7.4.  This is the iCalendar spec version, *NOT*
     * the product version!  Do not change this!
     */
    prop = icalproperty_new_version ("2.0");
    icalcomponent_add_property (priv->icalcomp, prop);

    /* Create our internal data */

    priv->comp_uid_hash = g_hash_table_new (g_str_hash, g_str_equal);

    gnome_vfs_uri_ref (uri);
    priv->uri = uri;

    mark_dirty (cbfile);

    return CAL_BACKEND_OPEN_SUCCESS;
}

/* Open handler for the file backend */
static CalBackendOpenStatus
cal_backend_file_open (CalBackend *backend, GnomeVFSURI *uri, gboolean only_if_exists)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    char *str_uri;
    FILE *file;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp == NULL, CAL_BACKEND_OPEN_ERROR);
    g_return_val_if_fail (uri != NULL, CAL_BACKEND_OPEN_ERROR);

    g_assert (priv->uri == NULL);
    g_assert (priv->comp_uid_hash == NULL);

    if (!gnome_vfs_uri_is_local (uri))
        return CAL_BACKEND_OPEN_ERROR;

    str_uri = gnome_vfs_uri_to_string (uri,
                       (GNOME_VFS_URI_HIDE_USER_NAME
                        | GNOME_VFS_URI_HIDE_PASSWORD
                        | GNOME_VFS_URI_HIDE_HOST_NAME
                        | GNOME_VFS_URI_HIDE_HOST_PORT
                        | GNOME_VFS_URI_HIDE_TOPLEVEL_METHOD));

    /* Load! */
    file = fopen (str_uri, "r");
    g_free (str_uri);

    if (file)
        return open_cal (cbfile, uri, file);
    else {
        if (only_if_exists)
            return CAL_BACKEND_OPEN_NOT_FOUND;

        return create_cal (cbfile, uri);
    }
}

/* is_loaded handler for the file backend */
static gboolean
cal_backend_file_is_loaded (CalBackend *backend)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    return (priv->icalcomp != NULL);
}

/* Get_n_objects handler for the file backend */
static int
cal_backend_file_get_n_objects (CalBackend *backend, CalObjType type)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    int n;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, -1);

    n = 0;

    if (type & CALOBJ_TYPE_EVENT)
        n += g_list_length (priv->events);

    if (type & CALOBJ_TYPE_TODO)
        n += g_list_length (priv->todos);

    if (type & CALOBJ_TYPE_JOURNAL)
        n += g_list_length (priv->journals);

    return n;
}

/* Get_object handler for the file backend */
static char *
cal_backend_file_get_object (CalBackend *backend, const char *uid)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    CalComponent *comp;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (uid != NULL, NULL);

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);
    g_assert (priv->comp_uid_hash != NULL);

    comp = lookup_component (cbfile, uid);

    if (!comp)
        return NULL;

    return cal_component_get_as_string (comp);
}

/* Get_object handler for the file backend */
static char *
cal_backend_file_get_timezone_object (CalBackend *backend, const char *tzid)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    icaltimezone *icaltz;
    icalcomponent *icalcomp;
    char *ical_string;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_print ("In cal_backend_file_get_timezone_object: %s\n", tzid);

    g_return_val_if_fail (tzid != NULL, NULL);

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);
    g_assert (priv->comp_uid_hash != NULL);

    g_print ("  getting icaltz\n");
    icaltz = icalcomponent_get_timezone (priv->icalcomp, tzid);
    if (!icaltz)
        return NULL;

    g_print ("  getting icalcomp\n");
    icalcomp = icaltimezone_get_component (icaltz);
    if (!icalcomp)
        return NULL;

    g_print ("  getting ical_string\n");
    ical_string = icalcomponent_as_ical_string (icalcomp);
    /* We dup the string; libical owns that memory. */
    if (ical_string)
      return g_strdup (ical_string);
    else
      return NULL;
}

static CalObjType
cal_backend_file_get_type_by_uid (CalBackend *backend, const char *uid)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    CalComponent *comp;
    CalComponentVType type;
    
    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    comp = lookup_component (cbfile, uid);
    if (!comp)
        return CAL_COMPONENT_NO_TYPE;
    
    type = cal_component_get_vtype (comp);
    switch (type) {
    case CAL_COMPONENT_EVENT:
        return CALOBJ_TYPE_EVENT;
    case CAL_COMPONENT_TODO:
        return CALOBJ_TYPE_TODO;
    case CAL_COMPONENT_JOURNAL:
        return CALOBJ_TYPE_JOURNAL;
    default:
        return CAL_COMPONENT_NO_TYPE;
    }
}

/* Builds a list of UIDs from a list of CalComponent objects */
static void
build_uids_list (GList **list, GList *components)
{
    GList *l;

    for (l = components; l; l = l->next) {
        CalComponent *comp;
        const char *uid;

        comp = CAL_COMPONENT (l->data);
        cal_component_get_uid (comp, &uid);
        *list = g_list_prepend (*list, g_strdup (uid));
    }
}

/* Get_uids handler for the file backend */
static GList *
cal_backend_file_get_uids (CalBackend *backend, CalObjType type)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    GList *list;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);

    list = NULL;

    if (type & CALOBJ_TYPE_EVENT)
        build_uids_list (&list, priv->events);

    if (type & CALOBJ_TYPE_TODO)
        build_uids_list (&list, priv->todos);

    if (type & CALOBJ_TYPE_JOURNAL)
        build_uids_list (&list, priv->journals);

    return list;
}

/* Callback used from cal_recur_generate_instances(); adds the component's UID
 * to our hash table.
 */
static gboolean
add_instance (CalComponent *comp, time_t start, time_t end, gpointer data)
{
    GHashTable *uid_hash;
    const char *uid;
    const char *old_uid;

    uid_hash = data;

    /* We only care that the component's UID is listed in the hash table;
     * that's why we only allow generation of one instance (i.e. return
     * FALSE every time).
     */

    cal_component_get_uid (comp, &uid);

    old_uid = g_hash_table_lookup (uid_hash, uid);
    if (old_uid)
        return FALSE;

    g_hash_table_insert (uid_hash, (char *) uid, NULL);
    return FALSE;
}


static icaltimezone*
resolve_tzid (const char *tzid, gpointer data)
{
    icalcomponent *vcalendar_comp = data;

    if (!tzid || !tzid[0])
        return NULL;
    else if (!strcmp (tzid, "UTC"))
        return icaltimezone_get_utc_timezone ();
    else
        return icalcomponent_get_timezone (vcalendar_comp, tzid);
}


/* Populates a hash table with the UIDs of the components that occur or recur
 * within a specific time range.
 */
static void
get_instances_in_range (GHashTable *uid_hash, GList *components, time_t start, time_t end)
{
    GList *l;

    for (l = components; l; l = l->next) {
        CalComponent *comp;
        icalcomponent *icalcomp, *vcalendar_comp;

        comp = CAL_COMPONENT (l->data);

        /* Get the parent VCALENDAR component, so we can resolve
           TZIDs. */
        icalcomp = cal_component_get_icalcomponent (comp);
        vcalendar_comp = icalcomponent_get_parent (icalcomp);
        g_assert (vcalendar_comp != NULL);

        cal_recur_generate_instances (comp, start, end, add_instance, uid_hash, resolve_tzid, vcalendar_comp);
    }
}

/* Used from g_hash_table_foreach(), adds a UID from the hash table to our list */
static void
add_uid_to_list (gpointer key, gpointer value, gpointer data)
{
    GList **list;
    const char *uid;
    char *uid_copy;

    list = data;

    uid = key;
    uid_copy = g_strdup (uid);

    *list = g_list_prepend (*list, uid_copy);
}

/* Get_objects_in_range handler for the file backend */
static GList *
cal_backend_file_get_objects_in_range (CalBackend *backend, CalObjType type,
                       time_t start, time_t end)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    GList *event_list;
    GHashTable *uid_hash;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);

    g_return_val_if_fail (start != -1 && end != -1, NULL);
    g_return_val_if_fail (start <= end, NULL);

    uid_hash = g_hash_table_new (g_str_hash, g_str_equal);

    if (type & CALOBJ_TYPE_EVENT)
        get_instances_in_range (uid_hash, priv->events, start, end);

    if (type & CALOBJ_TYPE_TODO)
        get_instances_in_range (uid_hash, priv->todos, start, end);

    if (type & CALOBJ_TYPE_JOURNAL)
        get_instances_in_range (uid_hash, priv->journals, start, end);

    event_list = NULL;
    g_hash_table_foreach (uid_hash, add_uid_to_list, &event_list);
    g_hash_table_destroy (uid_hash);

    return event_list;
}

/* Get_free_busy handler for the file backend */
static char *
cal_backend_file_get_free_busy (CalBackend *backend, time_t start, time_t end)
{
    return NULL;
}

typedef struct 
{
    CalBackend *backend;
    GList *changes;
    GList *change_ids;
} CalBackendFileComputeChangesData;

static void
cal_backend_file_compute_changes_foreach_key (const char *key, gpointer data)
{
    CalBackendFileComputeChangesData *be_data = data;
    char *calobj = cal_backend_get_object (be_data->backend, key);
    
    if (calobj == NULL) {
        CalComponent *comp;
        GNOME_Evolution_Calendar_CalObjChange *coc;

        comp = cal_component_new ();
        cal_component_set_new_vtype (comp, CAL_COMPONENT_TODO);
        cal_component_set_uid (comp, key);

        coc = GNOME_Evolution_Calendar_CalObjChange__alloc ();
        coc->calobj =  CORBA_string_dup (cal_component_get_as_string (comp));
        coc->type = GNOME_Evolution_Calendar_DELETED;
        be_data->changes = g_list_prepend (be_data->changes, coc);
        be_data->change_ids = g_list_prepend (be_data->change_ids, (gpointer) key);
    }
}

static GNOME_Evolution_Calendar_CalObjChangeSeq *
cal_backend_file_compute_changes (CalBackend *backend, CalObjType type, const char *change_id)
{
    char    *filename;
    EDbHash *ehash;
    CalBackendFileComputeChangesData be_data;
    GNOME_Evolution_Calendar_CalObjChangeSeq *seq;
    GList *uids, *changes = NULL, *change_ids = NULL;
    GList *i, *j;
    int n;
    
    /* Find the changed ids - FIX ME, path should not be hard coded */
    if (type == GNOME_Evolution_Calendar_TYPE_TODO)
        filename = g_strdup_printf ("%s/evolution/local/Tasks/%s.db", g_get_home_dir (), change_id);
    else 
        filename = g_strdup_printf ("%s/evolution/local/Calendar/%s.db", g_get_home_dir (), change_id);
    ehash = e_dbhash_new (filename);
    g_free (filename);
    
    uids = cal_backend_get_uids (backend, type);
    
    /* Calculate adds and modifies */
    for (i = uids; i != NULL; i = i->next) {
        GNOME_Evolution_Calendar_CalObjChange *coc;
        char *uid = i->data;
        char *calobj = cal_backend_get_object (backend, uid);

        g_assert (calobj != NULL);

        /* check what type of change has occurred, if any */
        switch (e_dbhash_compare (ehash, uid, calobj)) {
        case E_DBHASH_STATUS_SAME:
            break;
        case E_DBHASH_STATUS_NOT_FOUND:
            coc = GNOME_Evolution_Calendar_CalObjChange__alloc ();
            coc->calobj =  CORBA_string_dup (calobj);
            coc->type = GNOME_Evolution_Calendar_ADDED;
            changes = g_list_prepend (changes, coc);
            change_ids = g_list_prepend (change_ids, uid);
            break;
        case E_DBHASH_STATUS_DIFFERENT:
            coc = GNOME_Evolution_Calendar_CalObjChange__alloc ();
            coc->calobj =  CORBA_string_dup (calobj);
            coc->type = GNOME_Evolution_Calendar_MODIFIED;
            changes = g_list_prepend (changes, coc);
            change_ids = g_list_prepend (change_ids, uid);
            break;
        }
    }

    /* Calculate deletions */
    be_data.backend = backend;
    be_data.changes = changes;
    be_data.change_ids = change_ids;
    e_dbhash_foreach_key (ehash, (EDbHashFunc)cal_backend_file_compute_changes_foreach_key, &be_data);
    changes = be_data.changes;
    change_ids = be_data.change_ids;
    
    /* Build the sequence and update the hash */
    n = g_list_length (changes);

    seq = GNOME_Evolution_Calendar_CalObjChangeSeq__alloc ();
    seq->_length = n;
    seq->_buffer = CORBA_sequence_GNOME_Evolution_Calendar_CalObjChange_allocbuf (n);
    CORBA_sequence_set_release (seq, TRUE);

    for (i = changes, j = change_ids, n = 0; i != NULL; i = i->next, j = j->next, n++) {
        GNOME_Evolution_Calendar_CalObjChange *coc = i->data;
        GNOME_Evolution_Calendar_CalObjChange *seq_coc;
        char *uid = j->data;

        /* sequence building */
        seq_coc = &seq->_buffer[n];
        seq_coc->calobj = CORBA_string_dup (coc->calobj);
        seq_coc->type = coc->type;

        /* hash updating */
        if (coc->type == GNOME_Evolution_Calendar_ADDED 
            || coc->type == GNOME_Evolution_Calendar_MODIFIED) {
            e_dbhash_add (ehash, uid, coc->calobj);
        } else {
            e_dbhash_remove (ehash, uid);
        }       

        CORBA_free (coc);
    }   
    e_dbhash_write (ehash);
    e_dbhash_destroy (ehash);

    cal_obj_uid_list_free (uids);
    g_list_free (change_ids);
    g_list_free (changes);
    
    return seq;
}

/* Get_changes handler for the file backend */
static GNOME_Evolution_Calendar_CalObjChangeSeq *
cal_backend_file_get_changes (CalBackend *backend, CalObjType type, const char *change_id)
{
    g_return_val_if_fail (backend != NULL, NULL);
    g_return_val_if_fail (IS_CAL_BACKEND (backend), NULL);

    return cal_backend_file_compute_changes (backend, type, change_id);
}

/* Computes the range of time in which recurrences should be generated for a
 * component in order to compute alarm trigger times.
 */
static void
compute_alarm_range (CalComponent *comp, GList *alarm_uids, time_t start, time_t end,
             time_t *alarm_start, time_t *alarm_end)
{
    GList *l;

    *alarm_start = start;
    *alarm_end = end;

    for (l = alarm_uids; l; l = l->next) {
        const char *auid;
        CalComponentAlarm *alarm;
        CalAlarmTrigger trigger;
        struct icaldurationtype *dur;
        time_t dur_time;

        auid = l->data;
        alarm = cal_component_get_alarm (comp, auid);
        g_assert (alarm != NULL);

        cal_component_alarm_get_trigger (alarm, &trigger);
        cal_component_alarm_free (alarm);

        switch (trigger.type) {
        case CAL_ALARM_TRIGGER_NONE:
        case CAL_ALARM_TRIGGER_ABSOLUTE:
            continue;

        case CAL_ALARM_TRIGGER_RELATIVE_START:
        case CAL_ALARM_TRIGGER_RELATIVE_END:
            dur = &trigger.u.rel_duration;
            dur_time = icaldurationtype_as_int (*dur);

            if (dur->is_neg)
                /* If the duration is negative then dur_time
                 * will be negative as well; that is why we
                 * subtract to expand the range.
                 */
                *alarm_end = MAX (*alarm_end, end - dur_time);
            else
                *alarm_start = MIN (*alarm_start, start - dur_time);

            break;

        default:
            g_assert_not_reached ();
        }
    }

    g_assert (*alarm_start <= *alarm_end);
}

/* Closure data to generate alarm occurrences */
struct alarm_occurrence_data {
    /* These are the info we have */
    GList *alarm_uids;
    time_t start;
    time_t end;

    /* This is what we compute */
    GSList *triggers;
    int n_triggers;
};

/* Callback used from cal_recur_generate_instances(); generates triggers for all
 * of a component's RELATIVE alarms.
 */
static gboolean
add_alarm_occurrences_cb (CalComponent *comp, time_t start, time_t end, gpointer data)
{
    struct alarm_occurrence_data *aod;
    GList *l;

    aod = data;

    for (l = aod->alarm_uids; l; l = l->next) {
        const char *auid;
        CalComponentAlarm *alarm;
        CalAlarmTrigger trigger;
        struct icaldurationtype *dur;
        time_t dur_time;
        time_t occur_time, trigger_time;
        CalAlarmInstance *instance;

        auid = l->data;
        alarm = cal_component_get_alarm (comp, auid);
        g_assert (alarm != NULL);

        cal_component_alarm_get_trigger (alarm, &trigger);
        cal_component_alarm_free (alarm);

        if (trigger.type != CAL_ALARM_TRIGGER_RELATIVE_START
            && trigger.type != CAL_ALARM_TRIGGER_RELATIVE_END)
            continue;

        dur = &trigger.u.rel_duration;
        dur_time = icaldurationtype_as_int (*dur);

        if (trigger.type == CAL_ALARM_TRIGGER_RELATIVE_START)
            occur_time = start;
        else
            occur_time = end;

        /* If dur->is_neg is true then dur_time will already be
         * negative.  So we do not need to test for dur->is_neg here; we
         * can simply add the dur_time value to the occur_time and get
         * the correct result.
         */

        trigger_time = occur_time + dur_time;

        if (trigger_time < aod->start || trigger_time >= aod->end)
            continue;

        instance = g_new (CalAlarmInstance, 1);
        instance->auid = auid;
        instance->trigger = trigger_time;
        instance->occur_start = start;
        instance->occur_end = end;

        aod->triggers = g_slist_prepend (aod->triggers, instance);
        aod->n_triggers++;
    }

    return TRUE;
}

/* Generates the absolute triggers for a component */
static void
generate_absolute_triggers (CalComponent *comp, struct alarm_occurrence_data *aod)
{
    GList *l;
    CalComponentDateTime dt_start, dt_end;

    cal_component_get_dtstart (comp, &dt_start);
    cal_component_get_dtend (comp, &dt_end);

    for (l = aod->alarm_uids; l; l = l->next) {
        const char *auid;
        CalComponentAlarm *alarm;
        CalAlarmTrigger trigger;
        time_t abs_time;
        CalAlarmInstance *instance;

        auid = l->data;
        alarm = cal_component_get_alarm (comp, auid);
        g_assert (alarm != NULL);

        cal_component_alarm_get_trigger (alarm, &trigger);
        cal_component_alarm_free (alarm);

        if (trigger.type != CAL_ALARM_TRIGGER_ABSOLUTE)
            continue;

        abs_time = icaltime_as_timet (trigger.u.abs_time);

        if (abs_time < aod->start || abs_time >= aod->end)
            continue;

        instance = g_new (CalAlarmInstance, 1);
        instance->auid = auid;
        instance->trigger = abs_time;

        /* No particular occurrence, so just use the times from the component */

        if (dt_start.value)
            instance->occur_start = icaltime_as_timet (*dt_start.value);
        else
            instance->occur_start = -1;

        if (dt_end.value)
            instance->occur_end = icaltime_as_timet (*dt_end.value);
        else
            instance->occur_end = -1;

        aod->triggers = g_slist_prepend (aod->triggers, instance);
        aod->n_triggers++;
    }

    cal_component_free_datetime (&dt_start);
    cal_component_free_datetime (&dt_end);
}

/* Compares two alarm instances; called from g_slist_sort() */
static gint
compare_alarm_instance (gconstpointer a, gconstpointer b)
{
    const CalAlarmInstance *aia, *aib;

    aia = a;
    aib = b;

    if (aia->trigger < aib->trigger)
        return -1;
    else if (aia->trigger > aib->trigger)
        return 1;
    else
        return 0;
}

/* Generates alarm instances for a calendar component.  Returns the instances
 * structure, or NULL if no alarm instances occurred in the specified time
 * range.
 */
static CalComponentAlarms *
generate_alarms_for_comp (CalComponent *comp, time_t start, time_t end)
{
    GList *alarm_uids;
    time_t alarm_start, alarm_end;
    struct alarm_occurrence_data aod;
    CalComponentAlarms *alarms;
    icalcomponent *icalcomp, *vcalendar_comp;

    if (!cal_component_has_alarms (comp))
        return NULL;

    alarm_uids = cal_component_get_alarm_uids (comp);
    compute_alarm_range (comp, alarm_uids, start, end, &alarm_start, &alarm_end);

    aod.alarm_uids = alarm_uids;
    aod.start = start;
    aod.end = end;
    aod.triggers = NULL;
    aod.n_triggers = 0;

    /* Get the parent VCALENDAR component, so we can resolve TZIDs.  */
    icalcomp = cal_component_get_icalcomponent (comp);
    vcalendar_comp = icalcomponent_get_parent (icalcomp);
    g_assert (vcalendar_comp != NULL);

    cal_recur_generate_instances (comp, alarm_start, alarm_end,
                      add_alarm_occurrences_cb, &aod,
                      resolve_tzid, vcalendar_comp);

    /* We add the ABSOLUTE triggers separately */
    generate_absolute_triggers (comp, &aod);

    if (aod.n_triggers == 0)
        return NULL;

    /* Create the component alarm instances structure */

    alarms = g_new (CalComponentAlarms, 1);
    alarms->comp = comp;
    gtk_object_ref (GTK_OBJECT (alarms->comp));
    alarms->alarms = g_slist_sort (aod.triggers, compare_alarm_instance);

    return alarms;
}

/* Iterates through all the components in the comps list and generates alarm
 * instances for them; putting them in the comp_alarms list.  Returns the number
 * of elements it added to that list.
 */
static int
generate_alarms_for_list (GList *comps, time_t start, time_t end, GSList **comp_alarms)
{
    GList *l;
    int n;

    n = 0;

    for (l = comps; l; l = l->next) {
        CalComponent *comp;
        CalComponentAlarms *alarms;

        comp = CAL_COMPONENT (l->data);
        alarms = generate_alarms_for_comp (comp, start, end);

        if (alarms) {
            *comp_alarms = g_slist_prepend (*comp_alarms, alarms);
            n++;
        }
    }

    return n;
}

/* Fills a CORBA sequence of alarm instances */
static void
fill_alarm_instances_seq (GNOME_Evolution_Calendar_CalAlarmInstanceSeq *seq, GSList *alarms)
{
    int n_alarms;
    GSList *l;
    int i;

    n_alarms = g_slist_length (alarms);

    CORBA_sequence_set_release (seq, TRUE);
    seq->_length = n_alarms;
    seq->_buffer = CORBA_sequence_GNOME_Evolution_Calendar_CalAlarmInstance_allocbuf (n_alarms);

    for (l = alarms, i = 0; l; l = l->next, i++) {
        CalAlarmInstance *instance;
        GNOME_Evolution_Calendar_CalAlarmInstance *corba_instance;

        instance = l->data;
        corba_instance = seq->_buffer + i;

        corba_instance->auid = CORBA_string_dup (instance->auid);
        corba_instance->trigger = (long) instance->trigger;
        corba_instance->occur_start = (long) instance->occur_start;
        corba_instance->occur_end = (long) instance->occur_end;
    }
}

/* Get_alarms_in_range handler for the file backend */
static GNOME_Evolution_Calendar_CalComponentAlarmsSeq *
cal_backend_file_get_alarms_in_range (CalBackend *backend, time_t start, time_t end)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    int n_comp_alarms;
    GSList *comp_alarms;
    GSList *l;
    int i;
    GNOME_Evolution_Calendar_CalComponentAlarmsSeq *seq;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);

    g_return_val_if_fail (start != -1 && end != -1, NULL);
    g_return_val_if_fail (start <= end, NULL);

    /* Per RFC 2445, only VEVENTs and VTODOs can have alarms */

    n_comp_alarms = 0;
    comp_alarms = NULL;

    n_comp_alarms += generate_alarms_for_list (priv->events, start, end, &comp_alarms);
    n_comp_alarms += generate_alarms_for_list (priv->todos, start, end, &comp_alarms);

    seq = GNOME_Evolution_Calendar_CalComponentAlarmsSeq__alloc ();
    CORBA_sequence_set_release (seq, TRUE);
    seq->_length = n_comp_alarms;
    seq->_buffer = CORBA_sequence_GNOME_Evolution_Calendar_CalComponentAlarms_allocbuf (
        n_comp_alarms);

    for (l = comp_alarms, i = 0; l; l = l->next, i++) {
        CalComponentAlarms *alarms;
        char *comp_str;

        alarms = l->data;

        comp_str = cal_component_get_as_string (alarms->comp);
        seq->_buffer[i].calobj = CORBA_string_dup (comp_str);
        g_free (comp_str);

        fill_alarm_instances_seq (&seq->_buffer[i].alarms, alarms->alarms);

        cal_component_alarms_free (alarms);
    }

    g_slist_free (comp_alarms);

    return seq;
}

/* Get_alarms_for_object handler for the file backend */
static GNOME_Evolution_Calendar_CalComponentAlarms *
cal_backend_file_get_alarms_for_object (CalBackend *backend, const char *uid,
                    time_t start, time_t end, gboolean *object_found)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    CalComponent *comp;
    char *comp_str;
    GNOME_Evolution_Calendar_CalComponentAlarms *corba_alarms;
    CalComponentAlarms *alarms;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);

    g_return_val_if_fail (uid != NULL, NULL);
    g_return_val_if_fail (start != -1 && end != -1, NULL);
    g_return_val_if_fail (start <= end, NULL);
    g_return_val_if_fail (object_found != NULL, NULL);

    comp = lookup_component (cbfile, uid);
    if (!comp) {
        *object_found = FALSE;
        return NULL;
    }

    *object_found = TRUE;

    comp_str = cal_component_get_as_string (comp);
    corba_alarms = GNOME_Evolution_Calendar_CalComponentAlarms__alloc ();

    corba_alarms->calobj = CORBA_string_dup (comp_str);
    g_free (comp_str);

    alarms = generate_alarms_for_comp (comp, start, end);
    if (alarms) {
        fill_alarm_instances_seq (&corba_alarms->alarms, alarms->alarms);
        cal_component_alarms_free (alarms);
    } else
        fill_alarm_instances_seq (&corba_alarms->alarms, NULL);

    return corba_alarms;
}

/* Notifies a backend's clients that an object was updated */
static void
notify_update (CalBackendFile *cbfile, const char *uid)
{
    CalBackendFilePrivate *priv;
    GList *l;

    priv = cbfile->priv;

    cal_backend_obj_updated (CAL_BACKEND (cbfile), uid);

    for (l = priv->clients; l; l = l->next) {
        Cal *cal;

        cal = CAL (l->data);
        cal_notify_update (cal, uid);
    }
}

/* Notifies a backend's clients that an object was removed */
static void
notify_remove (CalBackendFile *cbfile, const char *uid)
{
    CalBackendFilePrivate *priv;
    GList *l;

    priv = cbfile->priv;

    cal_backend_obj_removed (CAL_BACKEND (cbfile), uid);

    for (l = priv->clients; l; l = l->next) {
        Cal *cal;

        cal = CAL (l->data);
        cal_notify_remove (cal, uid);
    }
}

/* Update_objects handler for the file backend. */
static gboolean
cal_backend_file_update_objects (CalBackend *backend, const char *calobj)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    icalcomponent *icalcomp, *vcalendar_comp = NULL;
    icalcomponent_kind kind;
    CalComponent *old_comp;
    CalComponent *comp;
    const char *comp_uid;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, FALSE);

    g_return_val_if_fail (calobj != NULL, FALSE);

    /* Pull the component from the string and ensure that it is sane */

    fprintf (stderr, "cal_backend_file: Parsing string:\n%s\n", calobj);
    icalcomp = icalparser_parse_string ((char *) calobj);

    if (!icalcomp)
        return FALSE;

    fprintf (stderr, "cal_backend_file: Parsed OK.\n");

    kind = icalcomponent_isa (icalcomp);

    if (kind == ICAL_VCALENDAR_COMPONENT) {
        int num_found = 0;
        icalcomponent_kind child_kind;
        icalcomponent *subcomp;

        fprintf (stderr, "cal_backend_file: VCALENDAR found\n");

        /* We have a VCALENDAR containing the VEVENT/VTODO and the
           related timezone data, so we have to step through it to
           find the actual VEVENT/VTODO component. */
        vcalendar_comp = icalcomp;

        subcomp = icalcomponent_get_first_component (vcalendar_comp,
                                 ICAL_ANY_COMPONENT);
        while (subcomp) {
            child_kind = icalcomponent_isa (subcomp);
            if (child_kind == ICAL_VEVENT_COMPONENT
                || child_kind == ICAL_VTODO_COMPONENT
                || child_kind == ICAL_VJOURNAL_COMPONENT) {
                icalcomp = subcomp;
                num_found++;
            }
            subcomp = icalcomponent_get_next_component (vcalendar_comp,
                                    ICAL_ANY_COMPONENT);
        }

        /* If we didn't find exactly 1 VEVENT/VTODO it is an error. */
        if (num_found != 1) {
            icalcomponent_free (icalcomp);
            return FALSE;
        }

    } else if (!(kind == ICAL_VEVENT_COMPONENT
             || kind == ICAL_VTODO_COMPONENT
             || kind == ICAL_VJOURNAL_COMPONENT)) {
        /* We don't support this type of component */
        icalcomponent_free (icalcomp);
        return FALSE;
    }

    comp = cal_component_new ();
    if (!cal_component_set_icalcomponent (comp, icalcomp)) {
        gtk_object_unref (GTK_OBJECT (comp));
        icalcomponent_free (icalcomp);
        return FALSE;
    }

    /* Get the UID, and check it isn't empty. */

    cal_component_get_uid (comp, &comp_uid);

    if (!comp_uid || !comp_uid[0]) {
        gtk_object_unref (GTK_OBJECT (comp));
        return FALSE;
    }

    /* Update the component */

    old_comp = lookup_component (cbfile, comp_uid);

    if (old_comp)
        remove_component (cbfile, old_comp);

    if (kind == ICAL_VCALENDAR_COMPONENT) {
        /* If we have a VCALENDAR component with child VTIMEZONEs and
           the VEVENT/VTODO, we have to merge it into the existing
           VCALENDAR, resolving any conflicting TZIDs. */
        icalcomponent_merge_component (priv->icalcomp, vcalendar_comp);

        /* Now we add the component to our local cache, but we pass
           FALSE as the last argument, since we have already added
           the libical component when merging above.*/
        add_component (cbfile, comp, FALSE);
    } else {
        add_component (cbfile, comp, TRUE);
    }

    mark_dirty (cbfile);

    /* FIXME: do the notification asynchronously */
    notify_update (cbfile, comp_uid);

    return TRUE;
}

/* Remove_object handler for the file backend */
static gboolean
cal_backend_file_remove_object (CalBackend *backend, const char *uid)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;
    CalComponent *comp;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, FALSE);

    g_return_val_if_fail (uid != NULL, FALSE);

    comp = lookup_component (cbfile, uid);
    if (!comp)
        return FALSE;

    remove_component (cbfile, comp);
    mark_dirty (cbfile);

    /* FIXME: do the notification asynchronously */
    notify_remove (cbfile, uid);

    return TRUE;
}


static icaltimezone*
cal_backend_file_get_timezone (CalBackend *backend, const char *tzid)
{
    CalBackendFile *cbfile;
    CalBackendFilePrivate *priv;

    cbfile = CAL_BACKEND_FILE (backend);
    priv = cbfile->priv;

    g_return_val_if_fail (priv->icalcomp != NULL, NULL);

    if (!strcmp (tzid, "UTC"))
        return icaltimezone_get_utc_timezone ();
    else
        return icalcomponent_get_timezone (priv->icalcomp, tzid);
}