aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/cal-util/cal-util.c
blob: 9730bfdbc28a46844715c4b2cb2663d46c4ece6c (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
/* Evolution calendar utilities and types
 *
 * 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 version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <config.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
#include "cal-util.h"



/**
 * cal_obj_instance_list_free:
 * @list: List of #CalObjInstance structures.
 *
 * Frees a list of #CalObjInstance structures.
 **/
void
cal_obj_instance_list_free (GList *list)
{
    CalObjInstance *i;
    GList *l;

    for (l = list; l; l = l->next) {
        i = l->data;

        g_assert (i != NULL);
        g_assert (i->uid != NULL);

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

    g_list_free (list);
}

/**
 * cal_obj_uid_list_free:
 * @list: List of strings with unique identifiers.
 *
 * Frees a list of unique identifiers for calendar objects.
 **/
void
cal_obj_uid_list_free (GList *list)
{
    GList *l;

    for (l = list; l; l = l->next) {
        char *uid;

        uid = l->data;

        g_assert (uid != NULL);
        g_free (uid);
    }

    g_list_free (list);
}

icalcomponent *
cal_util_new_top_level (void)
{
    icalcomponent *icalcomp;
    icalproperty *prop;

    icalcomp = icalcomponent_new (ICAL_VCALENDAR_COMPONENT);

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

       /* RFC 2445, section 4.7.3 */
    prop = icalproperty_new_prodid ("-//Ximian//NONSGML Evolution Calendar//EN");
    icalcomponent_add_property (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 (icalcomp, prop);

    return icalcomp;
}

/* 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;
    time_t repeat_time;

    *alarm_start = start;
    *alarm_end = end;

    repeat_time = 0;

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

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

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

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

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

            if (repeat.repetitions != 0) {
                int rdur;

                rdur = repeat.repetitions * icaldurationtype_as_int (repeat.duration);
                repeat_time = MAX (repeat_time, rdur);
            }

            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 ();
        }
    }

    *alarm_start -= repeat_time;

    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;
};

static void
add_trigger (struct alarm_occurrence_data *aod, const char *auid, time_t trigger,
         time_t occur_start, time_t occur_end)
{
    CalAlarmInstance *instance;

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

    aod->triggers = g_slist_prepend (aod->triggers, instance);
    aod->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;
        CalAlarmRepeat repeat;
        struct icaldurationtype *dur;
        time_t dur_time;
        time_t occur_time, trigger_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_get_repeat (alarm, &repeat);
        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;

        /* Add repeating alarms */

        if (repeat.repetitions != 0) {
            int i;
            time_t repeat_time;

            repeat_time = icaldurationtype_as_int (repeat.duration);

            for (i = 0; i < repeat.repetitions; i++) {
                time_t t;

                t = trigger_time + (i + 1) * repeat_time;

                if (t >= aod->start && t < aod->end)
                    add_trigger (aod, auid, t, start, end);
            }
        }

        /* Add the trigger itself */

        if (trigger_time >= aod->start && trigger_time < aod->end)
            add_trigger (aod, auid, trigger_time, start, end);
    }

    return TRUE;
}

/* Generates the absolute triggers for a component */
static void
generate_absolute_triggers (CalComponent *comp, struct alarm_occurrence_data *aod,
                CalRecurResolveTimezoneFn resolve_tzid,
                gpointer user_data,
                icaltimezone *default_timezone)
{
    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;
        CalAlarmRepeat repeat;
        CalAlarmTrigger trigger;
        time_t abs_time;
        time_t occur_start, occur_end;
        icaltimezone *zone;

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

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

        if (trigger.type != CAL_ALARM_TRIGGER_ABSOLUTE)
            continue;

        /* Absolute triggers are always in UTC; see RFC 2445 section 4.8.6.3 */
        zone = icaltimezone_get_utc_timezone ();

        abs_time = icaltime_as_timet_with_zone (trigger.u.abs_time, zone);

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

        if (dt_start.value) {
            if (dt_start.tzid && !dt_start.value->is_date)
                zone = (* resolve_tzid) (dt_start.tzid, user_data);
            else
                zone = default_timezone;

            occur_start = icaltime_as_timet_with_zone (*dt_start.value, zone);
        } else
            occur_start = -1;

        if (dt_end.value) {
            if (dt_end.tzid && !dt_end.value->is_date)
                zone = (* resolve_tzid) (dt_end.tzid, user_data);
            else
                zone = default_timezone;

            occur_end = icaltime_as_timet_with_zone (*dt_end.value, zone);
        } else
            occur_end = -1;

        /* Add repeating alarms */

        if (repeat.repetitions != 0) {
            int i;
            time_t repeat_time;

            repeat_time = icaldurationtype_as_int (repeat.duration);

            for (i = 0; i < repeat.repetitions; i++) {
                time_t t;

                t = abs_time + (i + 1) * repeat_time;

                if (t >= aod->start && t < aod->end)
                    add_trigger (aod, auid, t, occur_start, occur_end);
            }
        }

        /* Add the trigger itself */

        if (abs_time >= aod->start && abs_time < aod->end)
            add_trigger (aod, auid, abs_time, occur_start, occur_end);
    }

    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;
}

/**
 * cal_util_generate_alarms_for_comp
 * @comp: the CalComponent to generate alarms from
 * @start: start time
 * @end: end time
 * @resolve_tzid: callback for resolving timezones
 * @user_data: data to be passed to the resolve_tzid callback
 * @default_timezone: the timezone used to resolve DATE and floating DATE-TIME
 * values.
 *
 * Generates alarm instances for a calendar component.  Returns the instances
 * structure, or NULL if no alarm instances occurred in the specified time
 * range.
 */
CalComponentAlarms *
cal_util_generate_alarms_for_comp (CalComponent *comp,
                   time_t start,
                   time_t end,
                   CalRecurResolveTimezoneFn resolve_tzid,
                   gpointer user_data,
                   icaltimezone *default_timezone)
{
    GList *alarm_uids;
    time_t alarm_start, alarm_end;
    struct alarm_occurrence_data aod;
    CalComponentAlarms *alarms;

    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;

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

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

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

    /* Create the component alarm instances structure */

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

    return alarms;
}

/**
 * cal_util_generate_alarms_for_list
 * @comps: list of CalComponent's
 * @start: start time
 * @end: end time
 * @comp_alarms: list to be returned
 * @resolve_tzid: callback for resolving timezones
 * @user_data: data to be passed to the resolve_tzid callback
 * @default_timezone: the timezone used to resolve DATE and floating DATE-TIME
 * values.
 *
 * 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.
 */
int
cal_util_generate_alarms_for_list (GList *comps,
                   time_t start,
                   time_t end,
                   GSList **comp_alarms,
                   CalRecurResolveTimezoneFn resolve_tzid,
                   gpointer user_data,
                   icaltimezone *default_timezone)
{
    GList *l;
    int n;

    n = 0;

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

        comp = CAL_COMPONENT (l->data);
        alarms = cal_util_generate_alarms_for_comp (comp, start, end, resolve_tzid, user_data, default_timezone);

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

    return n;
}


/* Converts an iCalendar PRIORITY value to a translated string. Any unknown
   priority value (i.e. not 0-9) will be returned as "" (undefined). */
char *
cal_util_priority_to_string (int priority)
{
    char *retval;

    if (priority <= 0)
        retval = "";
    else if (priority <= 4)
        retval = _("High");
    else if (priority == 5)
        retval = _("Normal");
    else if (priority <= 9)
        retval = _("Low");
    else
        retval = "";

    return retval;
}


/* Converts a translated priority string to an iCalendar priority value.
   Returns -1 if the priority string is not valid. */
int
cal_util_priority_from_string (const char *string)
{
    int priority;

    /* An empty string is the same as 'None'. */
    if (!string || !string[0] || !g_strcasecmp (string, _("Undefined")))
        priority = 0;
    else if (!g_strcasecmp (string, _("High")))
        priority = 3;
    else if (!g_strcasecmp (string, _("Normal")))
        priority = 5;
    else if (!g_strcasecmp (string, _("Low")))
        priority = 7;
    else
        priority = -1;

    return priority;
}

char *
cal_util_expand_uri (char *uri, gboolean tasks)
{
    char *file_uri, *file_name;

    if (!strncmp (uri, "file://", 7)) {
        file_uri = uri + 7;
        if (strlen (file_uri) > 4
            && !strcmp (file_uri + strlen (file_uri) - 4, ".ics")) {
            
            /* it's a .ics file */
            return g_strdup (uri);
        }

        /* we assume it's a dir and glom <type>.ics onto the end. */
        if (tasks)
            file_name = g_concat_dir_and_file (file_uri, "tasks.ics");
        else
            file_name = g_concat_dir_and_file (file_uri, "calendar.ics");
        file_uri = g_strdup_printf("file://%s", file_name);
        g_free(file_name);
    } else {
        file_uri = g_strdup (uri);
    }

    return file_uri;
}

/* callback for icalcomponent_foreach_tzid */
typedef struct {
    icalcomponent *vcal_comp;
    CalComponent *comp;
} ForeachTzidData;

static void
add_timezone_cb (icalparameter *param, void *data)
{
    icaltimezone *tz;
    const char *tzid;
    icalcomponent *vtz_comp;
    ForeachTzidData *f_data = (ForeachTzidData *) data;

    tzid = icalparameter_get_tzid (param);
    if (!tzid)
        return;

    tz = icalcomponent_get_timezone (f_data->vcal_comp, tzid);
    if (tz)
        return;

    tz = icalcomponent_get_timezone (cal_component_get_icalcomponent (f_data->comp),
                     tzid);
    if (!tz) {
        tz = icaltimezone_get_builtin_timezone_from_tzid (tzid);
        if (!tz)
            return;
    }

    vtz_comp = icaltimezone_get_component (tz);
    if (!vtz_comp)
        return;

    icalcomponent_add_component (f_data->vcal_comp,
                     icalcomponent_new_clone (vtz_comp));
}

/* Adds VTIMEZONE components to a VCALENDAR for all tzid's
 * in the given CalComponent. */
void
cal_util_add_timezones_from_component (icalcomponent *vcal_comp,
                       CalComponent *comp)
{
    ForeachTzidData f_data;

    g_return_if_fail (vcal_comp != NULL);
    g_return_if_fail (IS_CAL_COMPONENT (comp));

    f_data.vcal_comp = vcal_comp;
    f_data.comp = comp;
    icalcomponent_foreach_tzid (cal_component_get_icalcomponent (comp),
                    add_timezone_cb, &f_data);
}