File: vevent.c

package info (click to toggle)
libgpevtype 0.16-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,520 kB
  • ctags: 81
  • sloc: sh: 8,733; ansic: 769; xml: 226; makefile: 55
file content (344 lines) | stat: -rw-r--r-- 8,724 bytes parent folder | download
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
/*
 * Copyright (C) 2004 Philip Blundell <philb@gnu.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 */

#define _GNU_SOURCE /* needed for strptime */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include <libintl.h>
#include <assert.h>

#include <time.h>

#include <gpe/vevent.h>
#include <gpe/event-db.h>

struct tag_map
{
  GType type;
  gchar *tag;
  gchar *vc;
};

static struct tag_map map[] = {
  {G_TYPE_STRING, "summary", NULL},
  {G_TYPE_STRING, "description", NULL},
  {G_TYPE_STRING, "eventid", "uid"},
  {G_TYPE_INT, "sequence", NULL},
  {G_TYPE_INT, "duration", NULL},
  {G_TYPE_INVALID, NULL, NULL}
};

static struct tag_map rec_map[] = {
  {G_TYPE_INT, "recur", "frequency"},
  {G_TYPE_INT, "rcount", "count"},
  {G_TYPE_INT, "rincrement", "unit"},
  {G_TYPE_INVALID, NULL, NULL}
};

static gint freq_map[] = { RECUR_NONE, RECUR_NONE, RECUR_NONE, RECUR_NONE, 
                           RECUR_DAILY, RECUR_WEEKLY, RECUR_MONTHLY, 
                           RECUR_YEARLY };
  
static gboolean
parse_date (const char *s, struct tm *tm, gboolean * date_only)
{
  char *p;

  memset (tm, 0, sizeof (*tm));

  p = strptime (s, "%Y-%m-%d", tm);
  if (p == NULL)
    return FALSE;

  p = strptime (p, " %H:%M", tm);

  if (date_only)
    *date_only = (p == NULL) ? TRUE : FALSE;

  return TRUE;
}

static gboolean
vevent_interpret_tag (MIMEDirVEvent * event, const char *tag,
		      const char *value)
{
  struct tag_map *t = &map[0];
  while (t->tag)
    {
      if (!strcasecmp (t->tag, tag))
        {
          if (t->type == G_TYPE_STRING)
            g_object_set (G_OBJECT (event), t->vc ? t->vc : t->tag, value,
                  NULL);
          else if (t->type == G_TYPE_INT)
            g_object_set (G_OBJECT (event), t->vc ? t->vc : t->tag,
                  atoi (value), NULL);
          else
            abort ();
          return TRUE;
        }
      t++;
    }

  if (!strcasecmp (tag, "start"))
    {
      struct tm tm;
      gboolean date_only;

      if (parse_date (value, &tm, &date_only))
        {
          MIMEDirDateTime *date;
    
          date =
            mimedir_datetime_new_from_date (tm.tm_year + 1900, tm.tm_mon + 1,
                            tm.tm_mday);
          if (!date_only)
            {
              mimedir_datetime_set_time (date, tm.tm_hour, tm.tm_min,
                         tm.tm_sec);
              date->timezone = MIMEDIR_DATETIME_UTC;
            }
    
          g_object_set (G_OBJECT (event), "dtstart", date, NULL);
        }
      else
        fprintf (stderr, "couldn't parse date '%s'\n", value);

      return TRUE;
    }
    
  if (!strcasecmp (tag, "modified"))
    {
      MIMEDirDateTime *date;

      date = mimedir_datetime_new_from_time_t (atoi (value));
      g_object_set (G_OBJECT (event), "last-modified", date, NULL);

      return TRUE;
    }
    
  /* handle recurring events */
  t = &rec_map[0];
  while (t->tag)
    {
      if (!strcasecmp (t->tag, tag))
        {
          MIMEDirRecurrence *rec = mimedir_vcomponent_get_recurrence((MIMEDirVComponent*)event);
         
          if (rec == NULL)
            {
              rec = mimedir_recurrence_new();
              mimedir_vcomponent_set_recurrence((MIMEDirVComponent*)event, rec);
            } 
          if (t->type == G_TYPE_STRING)
            g_object_set (G_OBJECT (rec), t->vc ? t->vc : t->tag, value,
                  NULL);
          else if (t->type == G_TYPE_INT)
            g_object_set (G_OBJECT (rec), t->vc ? t->vc : t->tag,
                  atoi (value), NULL);
          else
            abort ();
          return TRUE;
        }
      t++;
    }
    
  return FALSE;
}

MIMEDirVEvent *
vevent_from_event_t (event_t event)
{
  MIMEDirVEvent *vevent;
  struct tm *tm;
          
  if (!event) 
    return NULL;
  
  vevent = mimedir_vevent_new ();
  
  /* start time */
  if ((tm = localtime(&(event->start))))
    {
      MIMEDirDateTime *date;

      date =
        mimedir_datetime_new_from_date (tm->tm_year + 1900, tm->tm_mon + 1,
                        tm->tm_mday);

      g_object_set (G_OBJECT (vevent), "dtstart", date, NULL);
    }
  else
    {
      g_object_unref(vevent);
      return NULL;
    }

  /* retrieve data and fill fields */    
  if (!event->details)
    event_db_get_details(event);
    
  g_object_set (G_OBJECT (vevent), "duration", (gint)event->duration, NULL);
  g_object_set (G_OBJECT (vevent), "uid", event->eventid, NULL);
  g_object_set (G_OBJECT (vevent), "summary", event->details->summary, NULL);
  g_object_set (G_OBJECT (vevent), "description", event->details->description, NULL);
  
  /* handle recurring events */
  if ((event->recur) && (event->recur->type != RECUR_NONE))
    {
      MIMEDirRecurrence *rec = mimedir_recurrence_new();
      mimedir_vcomponent_set_recurrence((MIMEDirVComponent*)vevent, rec);
      g_object_set (G_OBJECT (rec), "frequency", event->recur->type + 3, NULL); /* hack, known offset */
      g_object_set (G_OBJECT (rec), "count", event->recur->count, NULL);
      g_object_set (G_OBJECT (rec), "unit", event->recur->increment, NULL);
      
      if (event->recur->end)     
        if ((tm = localtime(&(event->recur->end))))
          {
            MIMEDirDateTime *date;
    
            date =
              mimedir_datetime_new_from_date (tm->tm_year + 1900, tm->tm_mon + 1,
                            tm->tm_mday);
    
            g_object_set (G_OBJECT (rec), "until", date, NULL);
          }
      }
  return vevent;
}

MIMEDirVEvent *
vevent_from_tags (GSList * tags)
{
  MIMEDirVEvent *vevent = mimedir_vevent_new ();

  while (tags)
    {
      gpe_tag_pair *p = tags->data;

      vevent_interpret_tag (vevent, p->tag, p->value);

      tags = tags->next;
    }

  return vevent;
}

GSList *
vevent_to_tags (MIMEDirVEvent * vevent)
{
  GSList *data = NULL;
  struct tag_map *t = &map[0];
  MIMEDirDateTime *date;
  MIMEDirRecurrence *rec = NULL;

  while (t->tag)
    {
      if (t->type == G_TYPE_STRING)
        {
          gchar *value;
    
          g_object_get (G_OBJECT (vevent), t->vc ? t->vc : t->tag, &value,
                NULL);
    
          if (value)
            data = gpe_tag_list_prepend (data, t->tag, g_strstrip (value));
        }
      else if (t->type == G_TYPE_INT)
        {
          gint value;
    
          g_object_get (G_OBJECT (vevent), t->vc ? t->vc : t->tag, &value,
                NULL);
    
          if (value != 0)
            data =
              gpe_tag_list_prepend (data, t->tag,
                        g_strdup_printf ("%d", value));
        }
      else
        abort ();

      t++;
    }

  g_object_get (G_OBJECT (vevent), "dtstart", &date, NULL);
  if (date)
    {
      gchar buf[256];
      MIMEDirDateTime *end_date;
      time_t start_t, end_t;

      mimedir_datetime_to_utc (date);

      data =
    	gpe_tag_list_prepend (data, "start",
                              mimedir_datetime_to_string (date));

      g_object_get (G_OBJECT (vevent), "dtend", &end_date, NULL);
      if (end_date)
        {
          unsigned int duration;
    
          start_t = mimedir_datetime_get_time_t (date);
          end_t = mimedir_datetime_get_time_t (end_date);
    
          duration = end_t - start_t;
          sprintf (buf, "%d", duration);
          data = gpe_tag_list_prepend (data, "duration", g_strdup (buf));
        }
    }
    
  g_object_get (G_OBJECT (vevent), "recurrence", &rec, NULL);
  if (rec)
    {
      struct tag_map *t = &rec_map[0];
    
      while (t->tag)
        {
            if (t->type == G_TYPE_INT)
            {
              gint value;
        
              g_object_get (G_OBJECT (rec), t->vc ? t->vc : t->tag, &value,
                    NULL);
        
              if (!strcmp(t->tag, "recur") && (value <= RECUR_YEARLY) && (value > 0))
                  value = freq_map[value];
              
              if (value != 0)
                {
                  data =
                    gpe_tag_list_prepend (data, t->tag,
                                          g_strdup_printf ("%d", value));
                }
            }
          else
            abort ();
    
          t++;
        }

      g_object_get (G_OBJECT (rec), "until", &date, NULL);
      if (date)
        {
          mimedir_datetime_to_utc (date);
    
          data =
            gpe_tag_list_prepend (data, "rend",
                                  mimedir_datetime_to_string (date));
        }
        
    }

  return data;
}