aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/pcs/calobj.c
blob: 6dd7fa98db81064a37678b6fa6574a8328e5260f (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
/*
 * Calendar objects implementations.
 * Copyright (C) 1998 the Free Software Foundation
 *
 * Authors:
 *   Miguel de Icaza (miguel@gnu.org)
 *   Federico Mena (federico@gimp.org)
 */
#include <string.h>
#include <glib.h>
#include "calobj.h"
#include "versit/vcc.h"

iCalObject *
ical_object_new (void)
{
    iCalObject *ico;

    ico = g_new0 (iCalObject, 1);
    
    ico->seq = -1;
    ico->dtstamp = time (NULL);

    return ico;
}

static void
default_alarm (iCalObject *ical, CalendarAlarm *alarm, char *def_mail, enum AlarmType type)
{
    alarm->enabled = 0;
    alarm->type    = type;

    if (type != ALARM_MAIL){
        alarm->count   = 15;
        alarm->units   = ALARM_MINUTES;
    } else {
        printf ("uno!\n");
        alarm->count   = 1;
        alarm->units   = ALARM_DAYS;
    }

    if (type == ALARM_MAIL)
        alarm->data = g_strdup (def_mail);
    else
        alarm->data = g_strdup ("");
}

iCalObject *
ical_new (char *comment, char *organizer, char *summary)
{
    iCalObject *ico;

    ico = ical_object_new ();

    ico->comment   = g_strdup (comment);
    ico->organizer = g_strdup (organizer);
    ico->summary   = g_strdup (summary);
    ico->class     = g_strdup ("PUBLIC");

    default_alarm  (ico, &ico->dalarm, organizer, ALARM_DISPLAY);
    default_alarm  (ico, &ico->palarm, organizer, ALARM_PROGRAM);
    default_alarm  (ico, &ico->malarm, organizer, ALARM_MAIL);
    default_alarm  (ico, &ico->aalarm, organizer, ALARM_AUDIO);
    
    return ico;
}

static void
list_free (GList *list)
{
    g_list_foreach (list, g_free, 0);
    g_list_free (list);
}

#define free_if_defined(x) if (x){ g_free (x); x = 0; }
#define lfree_if_defined(x) if (x){ list_free (x); x = 0; }
void
ical_object_destroy (iCalObject *ico)
{
    /* Regular strings */
    free_if_defined  (ico->comment);
    free_if_defined  (ico->organizer);
    free_if_defined  (ico->summary);
    free_if_defined  (ico->uid);
    free_if_defined  (ico->status);
    free_if_defined  (ico->class);
    free_if_defined  (ico->url);

    /* Lists */
    lfree_if_defined (ico->exdate);
    lfree_if_defined (ico->categories);
    lfree_if_defined (ico->resources);
    lfree_if_defined (ico->related);
    lfree_if_defined (ico->attach);
    
    g_free (ico);
}

GList *
set_list (char *str, char *sc)
{
    GList *list = 0;
    char *s;
    
    for (s = strtok (str, sc); s; s = strtok (NULL, sc))
        list = g_list_prepend (list, g_strdup (s));
    
    return list;
}

#define is_a_prop_of(obj,prop) isAPropertyOf (obj,prop)
#define str_val(obj) fakeCString (vObjectUStringZValue (obj))
#define has(obj,prop) (vo = isAPropertyOf (obj, prop))

/* FIXME: we need to load the recurrence properties */
iCalObject *
ical_object_create_from_vobject (VObject *o, const char *object_name)
{
    time_t  now = time (NULL);
    iCalObject *ical;
    VObject *vo;
    VObjectIterator i;

    ical = g_new0 (iCalObject, 1);
    
    if (strcmp (object_name, VCEventProp) == 0)
        ical->type = ICAL_EVENT;
    else if (strcmp (object_name, VCTodoProp) == 0)
        ical->type = ICAL_TODO;
    else
        return 0;

    /* uid */
    if (has (o, VCUniqueStringProp))
        ical->uid = g_strdup (str_val (vo));

    /* seq */
    if (has (o, VCSequenceProp))
        ical->seq = atoi (str_val (vo));
    else
        ical->seq = 0;
    
    /* dtstart */
    if (has (o, VCDTstartProp))
        ical->dtstart = time_from_isodate (str_val (vo));
    else
        ical->dtstart = 0;

    /* dtend */
    if (has (o, VCDTendProp))
        ical->dtend = time_from_isodate (str_val (vo));
    else
        ical->dtend = 0;

    /* dcreated */
    if (has (o, VCDCreatedProp))
        ical->created = time_from_isodate (str_val (vo));
    
    /* completed */
    if (has (o, VCCompletedProp))
        ical->completed = time_from_isodate (str_val (vo));
    
    /* last_mod */
    if (has (o, VCLastModifiedProp))
        ical->last_mod = time_from_isodate (str_val (vo));
    else
        ical->last_mod = now;

    /* exdate */
    if (has (o, VCExpDateProp))
        ical->exdate = set_list (str_val (vo), ",");

    /* description/comment */
    if (has (o, VCDescriptionProp))
        ical->comment = g_strdup (str_val (vo));
    
    /* summary */
    if (has (o, VCSummaryProp))
        ical->summary = g_strdup (str_val (vo));
    else
        ical->summary = g_strdup ("");

    /* status */
    if (has (o, VCStatusProp))
        ical->status = g_strdup (str_val (vo));
    else
        ical->status = g_strdup ("NEEDS ACTION");

    if (has (o, VCClassProp))
        ical->class = g_strdup (str_val (vo));
    else
        ical->class = "PUBLIC";

    /* categories */
    if (has (o, VCCategoriesProp))
        ical->categories = set_list (str_val (vo), ",");

    /* resources */
    if (has (o, VCResourcesProp))
        ical->resources = set_list (str_val (vo), ";");
    
    /* priority */
    if (has (o, VCPriorityProp))
        ical->priority = atoi (str_val (vo));

    /* tranparency */
    if (has (o, VCTranspProp))
        ical->transp = atoi (str_val (vo)) ? ICAL_TRANSPARENT : ICAL_OPAQUE;

    /* related */
    if (has (o, VCRelatedToProp))
        ical->related = set_list (str_val (vo), ";");

    /* attach */
    initPropIterator (&i, o);
    while (moreIteration (&i)){
        vo = nextVObject (&i);
        if (strcmp (vObjectName (vo), VCAttachProp) == 0)
            ical->attach = g_list_prepend (ical->attach, g_strdup (str_val (vo)));
    }

    /* url */
    if (has (o, VCURLProp))
        ical->url = g_strdup (str_val (vo));
    
    /* FIXME: dalarm */
    if (has (o, VCDAlarmProp))
        ;
    
    /* FIXME: aalarm */
    if (has (o, VCAAlarmProp))
        ;

    /* FIXME: palarm */
    if (has (o, VCPAlarmProp))
        ;

    /* FIXME: malarm */
    if (has (o, VCMAlarmProp))
        ;

    /* FIXME: rdate */
    if (has (o, VCRDateProp))
        ;

    /* FIXME: rrule */
    if (has (o, VCRRuleProp))
        ;
        
    return ical;
}

static char *
to_str (int num)
{
    static char buf [40];

    sprintf (buf, "%d", num);
    return buf;
}

VObject *
ical_object_to_vobject (iCalObject *ical)
{
    VObject *o;

    if (ical->type == ICAL_EVENT)
        o = newVObject (VCEventProp);
    else
        o = newVObject (VCTodoProp);

    /* uid */
    if (ical->uid)
        addPropValue (o, VCUniqueStringProp, ical->uid);

    /* seq */
    addPropValue (o, VCSequenceProp, to_str (ical->seq));

    /* dtstart */
    addPropValue (o, VCDTstartProp, isodate_from_time_t (ical->dtstart));

    /* dtend */
    addPropValue (o, VCDTendProp, isodate_from_time_t (ical->dtend));

    /* dcreated */
    addPropValue (o, VCDTendProp, isodate_from_time_t (ical->created));

    /* completed */
    if (ical->completed)
        addPropValue (o, VCDTendProp, isodate_from_time_t (ical->completed));
        
    return o;
}