File: event-db.c

package info (click to toggle)
libeventdb 0.90-4
  • links: PTS
  • area: main
  • in suites: lenny, squeeze
  • size: 2,176 kB
  • ctags: 920
  • sloc: sh: 8,859; ansic: 4,897; xml: 2,729; makefile: 76
file content (802 lines) | stat: -rw-r--r-- 18,743 bytes parent folder | download | duplicates (2)
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
/* event-db.c: Event DB implementation.
 * Copyright (C) 2002, 2006 Philip Blundell <philb@gnu.org>
 *               2006, Florian Boor <florian@kernelconcepts.de>
 * Copyright (C) 2006, 2007 Neal H. Walfield <neal@walfield.org>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

#include <glib-object.h>
#include <glib.h>
#include <stdlib.h>
#include <stdio.h>
#include <libintl.h>
#include <string.h>

#define _(x) gettext(x)

#include "gpe/event-db.h"

#include "event-db.h"
#include "event.h"
#include "event-cal.h"

static void event_db_class_init (gpointer klass, gpointer klass_data);
static void event_db_init (GTypeInstance *instance, gpointer klass);
static void event_db_dispose (GObject *obj);
static void event_db_finalize (GObject *object);

static GObjectClass *event_db_parent_class;

GType
event_db_get_type (void)
{
  static GType type;

  if (! type)
    {
      static const GTypeInfo info =
      {
	sizeof (EventDBClass),
	NULL,
	NULL,
	event_db_class_init,
	NULL,
	NULL,
	sizeof (struct _EventDB),
	0,
	event_db_init
      };

      type = g_type_register_static (G_TYPE_OBJECT, "EventDB", &info, 0);
    }

  return type;
}

static void
event_db_class_init (gpointer klass, gpointer klass_data)
{
  GObjectClass *object_class;
  EventDBClass *edb_class;

  event_db_parent_class = g_type_class_peek_parent (klass);

  object_class = G_OBJECT_CLASS (klass);
  object_class->finalize = event_db_finalize;
  object_class->dispose = event_db_dispose;

  edb_class = (EventDBClass *) klass;
  edb_class->error_signal
    = g_signal_new ("error",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, error),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
  edb_class->calendar_new_signal
    = g_signal_new ("calendar-new",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, calendar_new),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
  edb_class->calendar_deleted_signal
    = g_signal_new ("calendar-deleted",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, calendar_deleted),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
  edb_class->calendar_reparented_signal
    = g_signal_new ("calendar-reparented",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, calendar_reparented),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
  edb_class->calendar_changed_signal
    = g_signal_new ("calendar-changed",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, calendar_changed),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
  edb_class->calendar_modified_signal
    = g_signal_new ("calendar-modified",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, calendar_modified),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);

  edb_class->event_new_signal
    = g_signal_new ("event-new",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, event_new_func),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);

  edb_class->event_removed_signal
    = g_signal_new ("event-removed",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, event_removed),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);

  edb_class->event_modified_signal
    = g_signal_new ("event-modified",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, event_removed),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);

  edb_class->alarm_fired_signal
    = g_signal_new ("alarm-fired",
		    G_OBJECT_CLASS_TYPE (object_class),
		    G_SIGNAL_RUN_LAST,
		    G_STRUCT_OFFSET (EventDBClass, alarm_fired),
		    NULL,
		    NULL,
		    g_cclosure_marshal_VOID__POINTER,
		    G_TYPE_NONE,
		    1,
		    G_TYPE_POINTER);
}

static void
event_db_init (GTypeInstance *instance, gpointer klass)
{
  EventDB *edb = EVENT_DB (instance);

  edb->events = g_hash_table_new (NULL, NULL);
}

static void
event_db_dispose (GObject *obj)
{
  /* Chain up to the parent class */
  G_OBJECT_CLASS (event_db_parent_class)->dispose (obj);
}

/* Timer callback.  Checks for expired events on EDB->CACHE_LIST.  */
static gboolean
flush_cache (gpointer data)
{
  EventDB *edb = EVENT_DB (data);
  GSList *list = edb->cache_list;
  edb->cache_list = NULL;

  time_t now = time (NULL);
  GSList *i;
  for (i = list; i; i = i->next)
    {
      EventSource *ev = EVENT_SOURCE (i->data);
      if (ev->dead_time + CACHE_EXPIRE <= now)
	/* No new reference in at least the last 60 seconds.  Kill
	   it.  */
	g_object_remove_toggle_ref (G_OBJECT (ev),
				    event_source_toggle_ref_notify, NULL);
      else
	edb->cache_list = g_slist_prepend (edb->cache_list, ev);
    }
  g_slist_free (list);

  if (! edb->cache_list)
    /* Nothing more to do.  */
    {
      edb->cache_buzzer = 0;
      return FALSE;
    }
  else
    /* Still some live items.  Call back in a little while.  */
    return TRUE;
}

void
event_source_toggle_ref_notify (gpointer data,
				GObject *object,
				gboolean is_last_ref)
{
  EventSource *ev = EVENT_SOURCE (object);

  if (is_last_ref)
    /* Last user reference just went away.  */
    {
      if (ev->modified)
	/* XXX: How to propagate any error correctly?  */
	event_flush (EVENT (ev), NULL);

      time_t now = time (NULL);
      ev->dead_time = now;
      ev->edb->cache_list = g_slist_prepend (ev->edb->cache_list, ev);

      if (! ev->edb->cache_buzzer)
	/* In two minutes.  */
	ev->edb->cache_buzzer = g_timeout_add (CACHE_EXPIRE / 2 * 1000,
					       flush_cache, ev->edb);
    }
  else
    /* Someone just grabbed a reference to EV.  Remove it from the
       kill list.  */
    ev->edb->cache_list = g_slist_remove (ev->edb->cache_list, ev);
}

/* This function is called when the system is idle.  It takes the
   EventSources and EventCalendars which have been added to
   EDB->LAUNDRY_LIST in add_to_laundry_pile and writes them to backing
   store and sends a modification signal for each.  */
static gboolean
do_laundry (gpointer data)
{
  EventDB *edb = EVENT_DB (data);
  GSList *l;

  /* Don't run again.  */
  if (edb->laundry_buzzer)
    /* We don't simply return FALSE to say that we won't be run again
       as event_db_finalize calls us directly.  */
    g_source_remove (edb->laundry_buzzer);
  edb->laundry_buzzer = 0;

  GSList *list = edb->laundry_list;
  edb->laundry_list = NULL;

  for (l = list; l; l = g_slist_next (l))
    {
      if (IS_EVENT_SOURCE (l->data))
	{
	  EventSource *e = EVENT_SOURCE (l->data);
	  if (e->modified)
	    /* XXX: How to propagate any error correctly?  */
	    event_flush (EVENT (e), NULL);
	  g_object_unref (e);
	}
      else
	{
	  EventCalendar *e = EVENT_CALENDAR (l->data);

	  if (e->modified)
	    {
	      g_signal_emit
		(edb, EVENT_DB_GET_CLASS (edb)->calendar_modified_signal,
		 0, e);
	      e->modified = FALSE;
	    }
	  if (e->changed)
	    {
	      /* XXX: How to propagate any error correctly?  */
	      event_calendar_flush (e, NULL);
	      g_signal_emit
		(edb, EVENT_DB_GET_CLASS (edb)->calendar_changed_signal,
		 0, e);
	      e->changed = FALSE;
	    }
	  g_object_unref (e);
	}
    }

  /* Destroy the list.  */
  g_slist_free (list);

  return FALSE;
}

void
add_to_laundry_pile (GObject *e)
{
  EventDB *edb;
  if (IS_EVENT_SOURCE (e))
    edb = EVENT_SOURCE (e)->edb;
  else
    edb = EVENT_CALENDAR (e)->edb;

  g_object_ref (e);
  edb->laundry_list = g_slist_prepend (edb->laundry_list, e);
  if (! edb->laundry_buzzer)
    edb->laundry_buzzer = g_idle_add (do_laundry, edb);
}

static void
event_db_finalize (GObject *object)
{
  EventDB *edb = EVENT_DB (object);

  /* Cancel any outstanding timeouts.  */
  if (edb->alarm)
    g_source_remove (edb->alarm);
  if (edb->laundry_buzzer)
    do_laundry (edb);
  if (edb->cache_buzzer)
    g_source_remove (edb->cache_buzzer);

  GSList *i;
  GSList *next = edb->upcoming_alarms;
  while (next)
    {
      i = next;
      next = i->next;

      Event *ev = EVENT (i->data);
      g_object_unref (ev);
    }
  g_slist_free (edb->upcoming_alarms);

  next = edb->cache_list;
  while (next)
    {
      i = next;
      next = i->next;

      EventSource *ev = EVENT_SOURCE (i->data);
      /* XXX: How to propagate any error correctly?  */
      event_flush (EVENT (ev), NULL);
      g_object_remove_toggle_ref (G_OBJECT (ev),
				  event_source_toggle_ref_notify, NULL);
      ev->edb = NULL;
    }
  g_slist_free (edb->cache_list);

  g_hash_table_destroy (edb->events);

  next = edb->calendars;
  while (next)
    {
      i = next;
      next = i->next;

      EventCalendar *ec = EVENT_CALENDAR (i->data);
      g_object_unref (ec);
    }
  g_slist_free (edb->calendars);

  G_OBJECT_CLASS (event_db_parent_class)->finalize (object);
}

void
event_db_error_punt (EventDB *edb, GError *error)
{
  SIGNAL_ERROR_GERROR (edb, NULL, error);
}

static void
event_db_set_alarms_fired_through (EventDB *edb, time_t t, GError **error)
{
  GError *e = NULL;
  EVENT_DB_GET_CLASS (edb)->acknowledge_alarms_through (edb, t, &e);
  if (e)
    {
      SIGNAL_ERROR_GERROR (edb, error, e);
      return;
    }

  edb->alarms_fired_through = t;
}

gboolean
buzzer (gpointer data)
{
  /* XXX: How to propagate any error correctly?  */

  EventDB *edb = EVENT_DB (data);

  time_t now = time (NULL);
  if (edb->period_end < now)
    {
      g_assert (! edb->upcoming_alarms);
#define PERIOD_LENGTH 24 * 60 * 60
      edb->period_end = now + PERIOD_LENGTH;
    }

  /* When the next alarm fires (or when we need to refresh the
     upcoming event list).  */
  time_t next_alarm = edb->period_end;

  if (! edb->upcoming_alarms)
    {
      /* Get the alarms since EDB->ALARMS_FIRED_THROUGH and
	 until EDB_PERIOD_END.  */
      GError *e = NULL;
      edb->upcoming_alarms
	= event_db_list_alarms_for_period (edb,
					   edb->alarms_fired_through + 1,
					   edb->period_end, &e);
      if (e)
	SIGNAL_ERROR_GERROR (edb, NULL, e);
      else
	{
	  edb->period_end = now + PERIOD_LENGTH;

	  /* And advance alarms_fired_through to NOW.  */
	  event_db_set_alarms_fired_through (edb, now, NULL);
	}
    }

  GSList *next = edb->upcoming_alarms;
  GSList *i;
  while (next)
    {
      i = next;
      next = i->next;

      EventSource *ev = EVENT_SOURCE (i->data);

      /* Has this event gone off?  */
      time_t start = event_get_start (EVENT (ev));
      int alarm = event_get_alarm (EVENT (ev));
      if (start - alarm <= now)
	{
	  /* Mark it as unacknowledged.  */
	  EVENT_DB_GET_CLASS (ev->edb)->event_mark_unacknowledged (i->data,
								   NULL);

	  /* And signal the user a signal.  */
	  GValue args[2];
	  GValue rv;

	  args[0].g_type = 0;
	  g_value_init (&args[0], G_TYPE_FROM_INSTANCE (G_OBJECT (edb)));
	  g_value_set_instance (&args[0], edb);
        
	  args[1].g_type = 0;
	  g_value_init (&args[1], G_TYPE_POINTER);
	  g_value_set_pointer (&args[1], ev);

	  g_signal_emitv (args,
			  EVENT_DB_GET_CLASS (edb)->alarm_fired_signal,
			  0, &rv);

	  /* Remove from the upcoming alarms list.  */
	  edb->upcoming_alarms = g_slist_delete_link (edb->upcoming_alarms, i);
	  /* And drop our reference.  */
	  g_object_unref (ev);
	}
      else
	/* No, in which case will this be the next alarm to go
	   fire?  */
	next_alarm = MIN (next_alarm, start - alarm);
    }

  edb->alarm = g_timeout_add ((next_alarm - now) * 1000, buzzer, edb);

  /* Don't trigger this timeout again.  */
  return FALSE;
}

GSList *
event_db_list_unacknowledged_alarms (EventDB *edb, GError **error)
{
  GSList *list = EVENT_DB_GET_CLASS (edb)->list_unacknowledged_alarms (edb,
								       error);
  buzzer (edb);
  return list;
}

static void
events_enumerate (EventDB *edb,
		  time_t period_start, time_t period_end,
		  gboolean alarms,
		  int (*cb) (EventSource *ev),
		  GError **error)
{
  /* Make sure any in memory changes are flushed to disk.  */
  do_laundry (edb);

  EVENT_DB_GET_CLASS (edb)->events_enumerate
    (edb, period_start, period_end, alarms, cb, error);
}

Event *
event_db_next_alarm (EventDB *edb, time_t now, GError **error)
{
  Event *next = NULL;

  GError *e = NULL;

  int callback (EventSource *ev)
    {
      GSList *list = event_list (ev, now, 0, 1, TRUE, &e);
      g_object_unref (ev);
      if (e)
	return 1;
      if (! list)
	return 0;

      Event *e = EVENT (list->data);
      g_slist_free (list);

      if (! next)
	next = e;
      else if (event_get_start (e) - event_get_alarm (e)
	       < event_get_start (next) - event_get_alarm (next))
	{
	  g_object_unref (next);
	  next = e;
	}
      else
	g_object_unref (e);

      return 0;
    }

  events_enumerate (edb, now, 0, TRUE, callback, &e);
  if (e)
    {
      SIGNAL_ERROR_GERROR (edb, error, e);

      g_object_unref (next);
      return NULL;
    }

  return next;
}

/* Return the event with uid UID in event database EDB from backing
   store or NULL if none exists.  */
static EventSource *
event_load (EventDB *edb, guint uid, GError **error)
{
  EventSource *ev;

  ev = EVENT_SOURCE (g_hash_table_lookup (edb->events, GINT_TO_POINTER(uid)));
  if (ev)
    /* Already loaded, just add a reference and return it.  */
    {
      g_object_ref (ev);
      return ev;
    }

  ev = EVENT_SOURCE (g_object_new (event_source_get_type (), NULL));
  ev->edb = edb;
  ev->uid = uid;

  GError *e = NULL;
  int exists = EVENT_DB_GET_CLASS (edb)->event_load (ev, &e);
  if (e)
    {
      SIGNAL_ERROR_GERROR (edb, error, e);
      g_object_unref (ev);
      return NULL;
    }
  if (exists)
    {
      g_hash_table_insert (edb->events, (gpointer) ev->uid, ev);
      return ev;
    }
  else
    {
      g_object_unref (ev);
      return NULL;
    }
}

Event *
event_db_find_by_uid (EventDB *edb, guint uid, GError **error)
{
  EventSource *ev = event_load (edb, uid, error);
  if (! ev)
    return NULL;
  else
    return EVENT (ev);
}

Event *
event_db_find_by_eventid (EventDB *edb, const char *eventid, GError **error)
{
  g_return_val_if_fail (eventid, NULL);

  int uid = EVENT_DB_GET_CLASS (edb)->eventid_to_uid (edb, eventid, error);
  if (uid == 0)
    return NULL;

  return event_db_find_by_uid (edb, uid, error);
}

static GSList *
event_db_list_for_period_internal (EventDB *edb,
				   time_t period_start, time_t period_end,
				   gboolean only_untimed, 
				   gboolean alarms,
				   GError **error)
{
  GSList *list = NULL;
  GError *e = NULL;

  int callback (EventSource *ev)
    {
      g_assert(! EVENT (ev)->dead);
      /* LIVE (ev); */
      printf("foo\n");

      if (only_untimed && ! ev->untimed)
	goto out;

      printf("bar\n");

      GSList *l = event_list (ev, period_start, period_end, 0, alarms, &e);
      if (e)
	g_assert (! l);
      else
	list = g_slist_concat (list, l);

    out:
      g_object_unref (ev);
      return e ? 1 : 0;
    }

  events_enumerate (edb, period_start, period_end, alarms, callback, &e);
  if (e)
    {
      SIGNAL_ERROR_GERROR (edb, error, e);
      g_slist_free (list);
      return NULL;
    }

  return list;
}

GSList *
event_db_list_for_period (EventDB *edb, time_t start, time_t end,
			  GError **error)
{
  return event_db_list_for_period_internal (edb, start, end, FALSE, FALSE,
					    error);
}

GSList *
event_db_list_alarms_for_period (EventDB *edb, time_t start, time_t end,
				 GError **error)
{
  return event_db_list_for_period_internal (edb, start, end, FALSE, TRUE,
					    error);
}

GSList *
event_db_untimed_list_for_period (EventDB *edb, time_t start, time_t end,
				  GError **error)
{
  return event_db_list_for_period_internal (edb, start, end, TRUE, FALSE,
					    error);
}

EventCalendar *
event_db_find_calendar_by_uid (EventDB *edb, guint uid, GError **error)
{
  GSList *i;

  for (i = edb->calendars; i; i = i->next)
    {
      if (event_calendar_get_uid (EVENT_CALENDAR (i->data)) == uid)
	{
	  g_object_ref (i->data);
	  return i->data;
	}
    }

  return NULL;
}

EventCalendar *
event_db_find_calendar_by_name (EventDB *edb, const gchar *name,
				GError **error)
{
  g_return_val_if_fail (name, NULL);
    
  GSList *iter;
  for (iter = edb->calendars; iter; iter = iter->next)
    {
      EventCalendar *ec = iter->data;
      gboolean found = FALSE;
      gchar *calendar_name = event_calendar_get_title (ec, error);
      if (! calendar_name)
	return NULL;

      if (strcmp (calendar_name, name) == 0)
	found = TRUE;
      g_free (calendar_name);
      
      if (found)
        { 
          g_object_ref (ec);
          return ec;
        }
    }

  return NULL;
}

EventCalendar *
event_db_get_default_calendar (EventDB *edb, const char *title,
			       GError **error)
{
  GError *e = NULL;
  EventCalendar *ec
    = event_db_find_calendar_by_uid (edb, edb->default_calendar, &e);
  if (e)
    {
      SIGNAL_ERROR_GERROR (edb, error, e);
      return NULL;
    }

  if (! ec)
    {
      /* There is no calendar associated with the default calendar id,
	 create it.  */
      ec = event_calendar_new_full (edb, NULL, TRUE, title ?: _("My Calendar"),
				    NULL, NULL, NULL, 0, 0, error);
      if (ec)
	event_db_set_default_calendar (edb, ec, error);
    }

  return ec;
}

void
event_db_set_default_calendar (EventDB *edb, EventCalendar *ec,
			       GError **error)
{
  if (ec->uid == edb->default_calendar)
    return;

  edb->default_calendar = ec->uid;

  EVENT_DB_GET_CLASS (ec->edb)->set_default_calendar (edb, ec, error);
}

GSList *
event_db_list_event_calendars (EventDB *edb, GError **error)
{
  GSList *l = g_slist_copy (edb->calendars);
  if (! l)
    /* Default calendar doesn't exit.  Create it.  */
    return g_slist_prepend (NULL,
			    event_db_get_default_calendar (edb, NULL, error));

  GSList *i;
  for (i = l; i; i = i->next)
    g_object_ref (i->data);

  return l;
}