aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/cal-util/icalendar-test.c
blob: 921e109a2c1d9775ea8f3cca999d3cb6b70d3d2a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>

#include <config.h>
#include <gtk/gtksignal.h>
#include <cal-util/calobj.h>
#include "libversit/vcc.h"

#include "icalendar-save.h"
#include "icalendar.h"


static icalcomponent* 
icalendar_parse_file (char* fname)
{
    FILE* fp;
    icalcomponent* comp = NULL;
    gchar* str;
    struct stat st;
    int n;

    fp = fopen (fname, "r");
    if (!fp) {
        g_warning ("Cannot open open calendar file.");
        return NULL;
    }
    
    stat (fname, &st);
    
    str = g_malloc (st.st_size + 2);
    
    n = fread ((gchar*) str, 1, st.st_size, fp);
    if (n != st.st_size) {
        g_warning ("Read error.");
    }
    str[n] = '\0';

    fclose (fp);
    
    comp = icalparser_parse_string (str);
    g_free (str);

    return comp;
}


static GList *
icalendar_calendar_load (GList *icals, char *fname)
{
    icalcomponent *comp;
    icalcomponent *subcomp;
    iCalObject    *ical;

    comp = icalendar_parse_file (fname);
    subcomp = icalcomponent_get_first_component (comp,
                             ICAL_ANY_COMPONENT);
    while (subcomp) {
        ical = ical_object_create_from_icalcomponent (subcomp);
        if (ical->type != ICAL_EVENT && 
            ical->type != ICAL_TODO  &&
            ical->type != ICAL_JOURNAL) {
            g_warning ("Skipping unsupported iCalendar component");
        } else {
            printf ("prepending %p\n", ical);
            icals = g_list_prepend (icals, ical);
        }
        subcomp = icalcomponent_get_next_component (comp,
                               ICAL_ANY_COMPONENT);
    }

    return icals;
}




static void
icalendar_calendar_save (GList *icals, char *fname)
{
    GList *cur;
    icalcomponent *top = icalcomponent_new (ICAL_VCALENDAR_COMPONENT);
    char *out_cal_string;

    for (cur=icals; cur; cur=cur->next) {
        iCalObject *ical = (iCalObject *) cur->data;
        icalcomponent *comp;
        comp = icalcomponent_create_from_ical_object (ical);
        icalcomponent_add_component (top, comp);
    }

    out_cal_string = icalcomponent_as_ical_string (top);

    printf ("---------------------------------------------------------\n");
    printf ("%s", out_cal_string);
}



int main (int argc, char *argv[])
{
    GList *icals = NULL;
    int i;
    long int n0, n1;
        struct icaldurationtype dt;


    /* test icaldurationtype_from_timet */
    srandom (time (0));

    for (i=0; i<10; i++) {
        n0 = random () % ((60 * 60 * 24 * 7) * 4);
        dt = icaldurationtype_from_timet (n0);
        n1 = icaldurationtype_as_timet (dt);

        printf ("%ld -> (%d %d %d %d %d) -> %ld\n",
            n0,
            dt.weeks, dt.days, dt.hours, dt.minutes, dt.seconds,
            n1);
        if (n0 != n1) abort ();
    }

    /*****************/
    /* test conversion of icalcomponents to and from iCalObjects */
    /*****************/

    /* load an ical file */

    if (argc < 2) {
        printf ("give ical file as argument.\n");
        return 1;
    }

    icals = icalendar_calendar_load (icals, argv[ 1 ]);

    printf ("loaded %d ical components\n", g_list_length (icals));


    /* save it back out */

    icalendar_calendar_save (icals, "out.ical");

    return 0;
}