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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Author :
* Damon Chaplin <damon@ximian.com>
*
* Copyright 2000, Ximian, Inc.
*
* 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
*/
/*
* This tests the recurrence rule expansion functions.
*
* NOTE: currently it starts from the event start date and continues
* until all recurrence rules/dates end or we reach MAX_OCCURRENCES
* occurrences. So it does not test generating occurrences for a specific
* interval. A nice addition might be to do this automatically and compare
* the results from the complete set to ensure they match.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtkmain.h>
#include <cal-util/cal-recur.h>
#include <cal-util/cal-util.h>
/* Since events can recur infinitely, we set a limit to the number of
occurrences we output. */
#define MAX_OCCURRENCES 1000
static void usage (void);
static void generate_occurrences (icalcomponent *comp);
static gboolean occurrence_cb (CalComponent *comp,
time_t instance_start,
time_t instance_end,
gpointer data);
int
main (int argc,
char *argv[])
{
gchar *filename;
icalcomponent *icalcomp;
gtk_init (&argc, &argv);
if (argc != 2)
usage ();
filename = argv[1];
icalcomp = cal_util_parse_ics_file (filename);
if (icalcomp)
generate_occurrences (icalcomp);
return 0;
}
static void
usage (void)
{
g_print ("Usage: test-recur <filename>\n");
exit (1);
}
/* This resolves any TZIDs in the components. The VTIMEZONEs must be in the
file we are reading. */
static icaltimezone*
resolve_tzid_cb (const char *tzid,
gpointer user_data)
{
icalcomponent *vcalendar_comp = user_data;
if (!tzid || !tzid[0])
return NULL;
else if (!strcmp (tzid, "UTC"))
return icaltimezone_get_utc_timezone ();
return icalcomponent_get_timezone (vcalendar_comp, tzid);
}
static void
generate_occurrences (icalcomponent *icalcomp)
{
icalcompiter iter;
icaltimezone *default_timezone;
/* This is the timezone we will use for DATE and floating values. */
default_timezone = icaltimezone_get_utc_timezone ();
for (iter = icalcomponent_begin_component (icalcomp, ICAL_ANY_COMPONENT);
icalcompiter_deref (&iter) != NULL;
icalcompiter_next (&iter)) {
icalcomponent *tmp_icalcomp;
CalComponent *comp;
icalcomponent_kind kind;
gint occurrences;
tmp_icalcomp = icalcompiter_deref (&iter);
kind = icalcomponent_isa (tmp_icalcomp);
if (!(kind == ICAL_VEVENT_COMPONENT
|| kind == ICAL_VTODO_COMPONENT
|| kind == ICAL_VJOURNAL_COMPONENT))
continue;
comp = cal_component_new ();
if (!cal_component_set_icalcomponent (comp, tmp_icalcomp))
continue;
g_print ("#############################################################################\n");
g_print ("%s\n\n", icalcomponent_as_ical_string (tmp_icalcomp));
g_print ("Instances:\n");
occurrences = 0;
/* I use specific times when I am trying to pin down a bug seen
in one of the calendar views. */
#if 0
cal_recur_generate_instances (comp, 982022400, 982108800,
occurrence_cb, &occurrences,
resolve_tzid_cb, icalcomp,
default_timezone);
#else
cal_recur_generate_instances (comp, -1, -1,
occurrence_cb, &occurrences,
resolve_tzid_cb, icalcomp,
default_timezone);
#endif
/* Print the component again so we can see the
X-EVOLUTION-ENDDATE parameter (only set if COUNT is used).
*/
g_print ("#############################################################################\n");
#if 0
g_print ("%s\n\n", icalcomponent_as_ical_string (tmp_icalcomp));
#endif
}
}
static gboolean
occurrence_cb (CalComponent *comp,
time_t instance_start,
time_t instance_end,
gpointer data)
{
char start[32], finish[32];
gint *occurrences;
occurrences = (gint*) data;
strcpy (start, ctime (&instance_start));
start[24] = '\0';
strcpy (finish, ctime (&instance_end));
finish[24] = '\0';
g_print ("%s - %s\n", start, finish);
(*occurrences)++;
return (*occurrences == MAX_OCCURRENCES) ? FALSE : TRUE;
}
|