File: test-alarms.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 (144 lines) | stat: -rw-r--r-- 3,839 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
/* test-alarms.c - Test the event_db_list_alarms_for_period.
   Copyright (C) 2006 Neal H. Walfield <neal@walfield.org>

   This file is part of GPE.

   GPE 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, or (at your option)
   any later version.

   GPE 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, USA. */

int do_test (int argc, char *argv[]);

#include "test-skeleton.h"

#include <stdio.h>
#include <error.h>
#include "gpe/event-db.h"

static int fail;

#define TIME_TO_STRING(t, buffer) \
  ({ \
    struct tm _tm; \
    time_t __t = t; \
    localtime_r (&__t, &_tm); \
    strftime (buffer, sizeof (buffer), "%F %T", &_tm); \
   })

int
do_test (int argc, char *argv[])
{
  char *file;
  int fd;
  /* Tue Jun 20 07:00:00 UTC 2006.  */
  time_t start = 1150786800;
  setenv ("TZ", "UTC", 1);
  tzset ();

  /* Initialize the g_object system.  */
  g_type_init ();

  fd = create_temp_file (".", &file);
  /* event_db_new will open it itself.  */
  close (fd);

  GError *err = NULL;
  EventDB *edb = event_db_new (file, &err);
  if (! edb)
    error (1, 0, "evend_db_new: %s", err->message);

  void edb_error (EventDB *edb, const char *error)
    {
      puts (error);
    }
  g_signal_connect (G_OBJECT (edb),
		    "error", G_CALLBACK (edb_error), NULL);

  /* One hour.  */
#define INTERVAL 60 * 60
  /* Ten minutes.  */
#define ALARM 10 * 60

  /* One time event -- no alarm.  */
  Event *ev = event_new (edb, NULL, NULL, NULL);
  event_set_summary (ev, "no alarm", NULL);
  event_set_recurrence_start (ev, start, NULL);
  event_set_duration (ev, 60, NULL);
  g_object_unref (ev);

  /* One time event -- alarm.  */
  ev = event_new (edb, NULL, NULL, NULL);
  event_set_summary (ev, "once at 7:10", NULL);
  event_set_recurrence_start (ev, start + ALARM, NULL);
  event_set_duration (ev, 60, NULL);
  event_set_alarm (ev, ALARM, NULL);
  g_object_unref (ev);

  /* Daily event -- alarm.  */
  ev = event_new (edb, NULL, NULL, NULL);
  event_set_summary (ev, "daily at 7, twice", NULL);
  event_set_recurrence_start (ev, start, NULL);
  event_set_duration (ev, 1, NULL);
  event_set_alarm (ev, ALARM, NULL);
  event_set_recurrence_type (ev, RECUR_DAILY, NULL);
  event_set_recurrence_count (ev, 2, NULL);
  g_object_unref (ev);

  int i;
  for (i = -5; i < 3 * 24; i ++)
    {
      time_t s = start + i * 60 * 60;
      time_t e = s + 60 * 60 - 1;

      printf ("Next alarm after ");
      char buffer[200];
      TIME_TO_STRING (s, buffer);
      printf ("%s", buffer);

      Event *ev = event_db_next_alarm (edb, s, NULL);
      if (! ev)
	printf (" none\n");
      else
	{
	  printf (" %s at ", event_get_summary (ev, NULL));
	  TIME_TO_STRING (event_get_start (ev) - event_get_alarm (ev),
			  buffer);
	  printf ("%s\n", buffer);

	  g_object_unref (ev);
	}

      GSList *list
	= event_db_list_alarms_for_period (edb, s, e, NULL);

      TIME_TO_STRING (s, buffer);
      fputs (buffer, stdout);
      fputs (" until ", stdout);
      TIME_TO_STRING (e, buffer);
      puts (buffer);

      GSList *l;
      list = g_slist_sort (list, event_compare_func);
      for (l = list; l; l = l->next)
	{
	  Event *ev = EVENT (l->data);
	  TIME_TO_STRING (event_get_start (ev), buffer);
	  printf ("%s: %s\n", buffer, event_get_summary (ev, NULL));
	}

      event_list_unref (list);
    }

  return fail;
}