aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/calendar.c
blob: dd0a5cca3f8acce670fc74cd8bc40db3b992239d (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
/*
 * Calendar manager object
 *
 * This keeps track of a given calendar.  Eventually this will abtract everything
 * related to getting calendars/saving calendars locally or to a remote Calendar Service
 *
 * Copyright (C) 1998 the Free Software Foundation
 *
 * Authors:
 *   Miguel de Icaza (miguel@gnu.org)
 *   Federico Mena (quartic@gimp.org)
 *
 */

#include <config.h>

#include "calendar.h"
#include "timeutil.h"
#include "versit/vcc.h"

Calendar *
calendar_new (char *title)
{
    Calendar *cal;

    cal = g_new0 (Calendar, 1);
    cal->title = g_strdup (title);

    return cal;
}

void
calendar_add_object (Calendar *cal, iCalObject *obj)
{
    printf ("Adding object\n");
    switch (obj->type){
    case ICAL_EVENT:
        cal->events = g_list_prepend (cal->events, obj);
        if (obj->recur)
            cal->recur = g_list_prepend (cal->recur, obj);
        break;

    case ICAL_TODO:
        cal->todo = g_list_prepend (cal->todo, obj);
        break;

    case ICAL_JOURNAL:
        cal->journal = g_list_prepend (cal->journal, obj);
        break;
    default:
        g_assert_not_reached ();
    }

    cal->modified = TRUE;

    /* FIXME: do we have to set the last_mod field in the object? */
}

void
calendar_remove_object (Calendar *cal, iCalObject *obj)
{
    switch (obj->type){
    case ICAL_EVENT:
        cal->events = g_list_remove (cal->events, obj);
        if (obj->recur)
            cal->recur = g_list_remove (cal->recur, obj);
        break;

    case ICAL_TODO:
        cal->todo = g_list_remove (cal->todo, obj);
        break;

    case ICAL_JOURNAL:
        cal->journal = g_list_remove (cal->journal, obj);
        break;
    default:
        g_assert_not_reached ();
    }

    cal->modified = TRUE;
}

void
calendar_destroy (Calendar *cal)
{
    g_list_foreach (cal->events, (GFunc) ical_object_destroy, NULL);
    g_list_free (cal->events);
    
    g_list_foreach (cal->todo, (GFunc) ical_object_destroy, NULL);
    g_list_free (cal->todo);
    
    g_list_foreach (cal->journal, (GFunc) ical_object_destroy, NULL);
    g_list_free (cal->journal);

    if (cal->title)
        g_free (cal->title);
    if (cal->filename)
        g_free (cal->filename);
    
    g_free (cal);
}

char *
ice (time_t t)
{
    static char buffer [100];
    struct tm *tm;

    tm = localtime (&t);
    sprintf (buffer, "%d/%d/%d", tm->tm_mday, tm->tm_mon, tm->tm_year);
    return buffer;
}

GList *
calendar_get_objects_in_range (GList *objects, time_t start, time_t end, GCompareFunc sort_func)
{
    GList *new_events = 0;

    for (; objects; objects = objects->next){
        iCalObject *object = objects->data;

        if ((start <= object->dtstart) && (object->dtend <= end)){
            if (sort_func)
                new_events = g_list_insert_sorted (new_events, object, sort_func);
            else
                new_events = g_list_prepend (new_events, object);
        }
    }

    return new_events;
}

GList *
calendar_get_events_in_range (Calendar *cal, time_t start, time_t end, GCompareFunc sort_func)
{
    return calendar_get_objects_in_range (cal->events, start, end, sort_func);
}

GList *
calendar_get_todo_in_range (Calendar *cal, time_t start, time_t end, GCompareFunc sort_func)
{
    return calendar_get_objects_in_range (cal->todo, start, end, sort_func);
}
GList *
calendar_get_journal_in_range (Calendar *cal, time_t start, time_t end, GCompareFunc sort_func)
{
    return calendar_get_objects_in_range (cal->journal, start, end, sort_func);
}

gint
calendar_compare_by_dtstart (gpointer a, gpointer b)
{
    iCalObject *obj1, *obj2;
    time_t diff;

    obj1 = a;
    obj2 = b;

    diff = obj1->dtstart - obj2->dtstart;

    return (diff < 0) ? -1 : (diff > 0) ? 1 : 0;
}

#define str_val(obj) (char *) vObjectUStringZValue (obj)

/* Loads our calendar contents from a vObject */
void
calendar_load_from_vobject (Calendar *cal, VObject *vcal)
{
    VObjectIterator i;;
    
    initPropIterator (&i, vcal);

    while (moreIteration (&i)){
        VObject *this = nextVObject (&i);
        iCalObject *ical;
        const char *object_name = vObjectName (this);

        if (strcmp (object_name, VCDCreatedProp) == 0){
            cal->created = time_from_isodate (str_val (this));
            continue;
        }
        
        if (strcmp (object_name, VCLocationProp) == 0)
            continue; /* FIXME: imlement */

        if (strcmp (object_name, VCProdIdProp) == 0)
            continue; /* FIXME: implement */

        if (strcmp (object_name, VCVersionProp) == 0)
            continue; /* FIXME: implement */
        
        ical = ical_object_create_from_vobject (this, object_name);

        if (ical)
            calendar_add_object (cal, ical);
    }
}

/* Loads a calendar from a file */
void
calendar_load (Calendar *cal, char *fname)
{
    VObject *vcal;
    
    if (cal->filename){
        g_warning ("Calendar load called again\n");
        return;
    }

    cal->filename = g_strdup (fname);
    vcal = Parse_MIME_FromFileName (fname);
    calendar_load_from_vobject (cal, vcal);
    cleanVObject (vcal);
    cleanStrTbl ();
}

void
calendar_save (Calendar *cal, char *fname)
{
    VObject *vcal;
    GList   *l;

    if (fname == NULL)
        fname = cal->filename;
    
    vcal = newVObject (VCCalProp);
    addPropValue (vcal, VCProdIdProp, "-//GNOME//NONSGML GnomeCalendar//EN");
    addPropValue (vcal, VCTimeZoneProp, "NONE");
    addPropValue (vcal, VCVersionProp, VERSION);
    cal->temp = vcal;

    for (l = cal->events; l; l = l->next){
        VObject *obj;
            
        obj = ical_object_to_vobject ((iCalObject *) l->data);
        addVObjectProp (vcal, obj);
    }
    writeVObjectToFile (fname, vcal);
    cleanVObject (vcal);
    cleanStrTbl ();
}