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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-summary-calendar.c
*
* Copyright (C) 2001 Ximian, Inc.
*
* 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.
*
* 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.
*
* Author: Iain Holmes <iain@ximian.com>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <gnome.h>
#include <gal/widgets/e-unicode.h>
#include "e-summary-calendar.h"
#include "e-summary.h"
#include <cal-client/cal-client.h>
#include <cal-util/timeutil.h>
#include <bonobo/bonobo-exception.h>
#include <bonobo/bonobo-moniker-util.h>
#include <bonobo-conf/bonobo-config-database.h>
#include <liboaf/liboaf.h>
#include <ical.h>
struct _ESummaryCalendar {
CalClient *client;
char *html;
gboolean wants24hr;
};
const char *
e_summary_calendar_get_html (ESummary *summary)
{
if (summary->calendar == NULL) {
return NULL;
}
return summary->calendar->html;
}
typedef struct {
char *uid;
CalComponent *comp;
CalComponentDateTime dt;
icaltimezone *zone;
} ESummaryCalEvent;
/* Returns TRUE if the TZIDs are equivalent, i.e. both NULL or the same. */
static gboolean
cal_component_compare_tzid (const char *tzid1, const char *tzid2)
{
gboolean retval = TRUE;
if (tzid1) {
if (!tzid2 || strcmp (tzid1, tzid2))
retval = FALSE;
} else {
if (tzid2)
retval = FALSE;
}
return retval;
}
gboolean
e_cal_comp_util_compare_event_timezones (CalComponent *comp,
CalClient *client,
icaltimezone *zone)
{
CalClientGetStatus status;
CalComponentDateTime start_datetime, end_datetime;
const char *tzid;
gboolean retval = FALSE;
icaltimezone *start_zone, *end_zone;
int offset1, offset2;
tzid = icaltimezone_get_tzid (zone);
cal_component_get_dtstart (comp, &start_datetime);
cal_component_get_dtend (comp, &end_datetime);
/* FIXME: DURATION may be used instead. */
if (cal_component_compare_tzid (tzid, start_datetime.tzid)
&& cal_component_compare_tzid (tzid, end_datetime.tzid)) {
/* If both TZIDs are the same as the given zone's TZID, then
we know the timezones are the same so we return TRUE. */
retval = TRUE;
} else {
/* If the TZIDs differ, we have to compare the UTC offsets
of the start and end times, using their own timezones and
the given timezone. */
status = cal_client_get_timezone (client,
start_datetime.tzid,
&start_zone);
if (status != CAL_CLIENT_GET_SUCCESS)
goto out;
offset1 = icaltimezone_get_utc_offset (start_zone,
start_datetime.value,
NULL);
offset2 = icaltimezone_get_utc_offset (zone,
start_datetime.value,
NULL);
if (offset1 == offset2) {
status = cal_client_get_timezone (client,
end_datetime.tzid,
&end_zone);
if (status != CAL_CLIENT_GET_SUCCESS)
goto out;
offset1 = icaltimezone_get_utc_offset (end_zone,
end_datetime.value,
NULL);
offset2 = icaltimezone_get_utc_offset (zone,
end_datetime.value,
NULL);
if (offset1 == offset2)
retval = TRUE;
}
}
out:
cal_component_free_datetime (&start_datetime);
cal_component_free_datetime (&end_datetime);
return retval;
}
static int
e_summary_calendar_event_sort_func (const void *e1,
const void *e2)
{
ESummaryCalEvent *event1, *event2;
event1 = *(ESummaryCalEvent **) e1;
event2 = *(ESummaryCalEvent **) e2;
if (event1->dt.value == NULL || event2->dt.value == NULL) {
return 0;
}
return icaltime_compare (*(event1->dt.value), *(event2->dt.value));
}
struct _RecurData {
ESummary *summary;
GPtrArray *array;
ESummaryCalEvent *event;
};
static gboolean
add_recurrances (CalComponent *comp,
time_t start,
time_t end,
gpointer data)
{
struct _RecurData *recur = data;
struct icaltimetype v, *p;
ESummaryCalEvent *event;
event = g_new (ESummaryCalEvent, 1);
v = icaltime_from_timet_with_zone (start, FALSE, recur->summary->tz);
p = g_new (struct icaltimetype, 1);
event->dt.value = p;
p->year = v.year;
p->month = v.month;
p->day = v.day;
p->hour = v.hour;
p->minute = v.minute;
p->second = v.second;
p->is_utc = v.is_utc;
p->is_date = v.is_date;
p->is_daylight = v.is_daylight;
p->zone = v.zone;
event->dt.tzid = recur->summary->timezone;
event->comp = comp;
event->uid = g_strdup (recur->event->uid);
event->zone = recur->summary->tz;
gtk_object_ref (GTK_OBJECT (comp));
g_ptr_array_add (recur->array, event);
return TRUE;
}
static GPtrArray *
uids_to_array (ESummary *summary,
CalClient *client,
GList *uids,
time_t begin,
time_t end)
{
GList *p;
GPtrArray *array;
g_return_val_if_fail (IS_E_SUMMARY (summary), NULL);
g_return_val_if_fail (client != NULL, NULL);
g_return_val_if_fail (uids != NULL, NULL);
array = g_ptr_array_new ();
for (p = uids; p; p = p->next) {
ESummaryCalEvent *event;
CalClientGetStatus status;
event = g_new (ESummaryCalEvent, 1);
event->uid = g_strdup (p->data);
status = cal_client_get_object (client, p->data, &event->comp);
if (status != CAL_CLIENT_GET_SUCCESS) {
g_free (event);
continue;
}
if (cal_component_has_recurrences (event->comp) == TRUE) {
struct _RecurData *recur;
recur = g_new (struct _RecurData, 1);
recur->event = event;
recur->array = array;
recur->summary = summary;
cal_recur_generate_instances (event->comp, begin, end,
add_recurrances, recur,
cal_client_resolve_tzid_cb, client);
g_free (recur);
g_free (event->uid);
g_free (event);
} else {
cal_component_get_dtstart (event->comp, &event->dt);
status = cal_client_get_timezone (client, event->dt.tzid, &event->zone);
if (status != CAL_CLIENT_GET_SUCCESS) {
gtk_object_unref (GTK_OBJECT (event->comp));
g_free (event);
continue;
}
icaltimezone_convert_time (event->dt.value, event->zone, summary->tz);
g_ptr_array_add (array, event);
}
}
qsort (array->pdata, array->len, sizeof (ESummaryCalEvent *), e_summary_calendar_event_sort_func);
return array;
}
static void
free_event_array (GPtrArray *array)
{
int i;
for (i = 0; i < array->len; i++) {
ESummaryCalEvent *event;
event = array->pdata[i];
g_free (event->uid);
gtk_object_unref (GTK_OBJECT (event->comp));
}
g_ptr_array_free (array, TRUE);
}
static gboolean
generate_html (gpointer data)
{
ESummary *summary = data;
ESummaryCalendar *calendar = summary->calendar;
GList *uids;
GString *string;
char *tmp;
time_t t, begin, end, f;
t = time (NULL);
begin = time_day_begin_with_zone (t, summary->tz);
switch (summary->preferences->days) {
case E_SUMMARY_CALENDAR_ONE_DAY:
end = time_day_end_with_zone (t, summary->tz);
break;
case E_SUMMARY_CALENDAR_FIVE_DAYS:
f = time_add_day_with_zone (t, 5, summary->tz);
end = time_day_end_with_zone (f, summary->tz);
break;
case E_SUMMARY_CALENDAR_ONE_WEEK:
f = time_add_week_with_zone (t, 1, summary->tz);
end = time_day_end_with_zone (f, summary->tz);
break;
case E_SUMMARY_CALENDAR_ONE_MONTH:
default:
f = time_add_month_with_zone (t, 1, summary->tz);
end = time_day_end_with_zone (f, summary->tz);
break;
}
uids = cal_client_get_objects_in_range (calendar->client,
CALOBJ_TYPE_EVENT, begin, end);
if (uids == NULL) {
char *s1, *s2;
s1 = e_utf8_from_locale_string (_("Appointments"));
s2 = e_utf8_from_locale_string (_("No appointments"));
g_free (calendar->html);
calendar->html = g_strconcat ("<dl><dt><img src=\"myevo-appointments.png\" align=\"middle\" "
"alt=\"\" width=\"48\" height=\"48\"> <b><a href=\"evolution:/local/Calendar\">",
s1, "</a></b></dt><dd><b>", s2, "</b></dd></dl>", NULL);
g_free (s1);
g_free (s2);
e_summary_draw (summary);
return FALSE;
} else {
GPtrArray *uidarray;
int i;
char *s;
string = g_string_new ("<dl><dt><img src=\"myevo-appointments.png\" align=\"middle\" "
"alt=\"\" width=\"48\" height=\"48\"> <b><a href=\"evolution:/local/Calendar\">");
s = e_utf8_from_locale_string (_("Appointments"));
g_string_append (string, s);
g_free (s);
g_string_append (string, "</a></b></dt><dd>");
uidarray = uids_to_array (summary, calendar->client, uids, begin, end);
for (i = 0; i < uidarray->len; i++) {
ESummaryCalEvent *event;
CalComponentText text;
time_t start_t;
struct tm *start_tm;
char *start_str, *img;
event = uidarray->pdata[i];
cal_component_get_summary (event->comp, &text);
start_t = icaltime_as_timet (*event->dt.value);
start_str = g_new (char, 20);
start_tm = localtime (&start_t);
if (calendar->wants24hr == TRUE) {
strftime (start_str, 19, _("%k%M %d %B"), start_tm);
} else {
strftime (start_str, 19, _("%l:%M %d %B"), start_tm);
}
if (cal_component_has_alarms (event->comp)) {
img = "es-appointments.png";
} else if (e_cal_comp_util_compare_event_timezones (event->comp,
calendar->client,
summary->tz) == FALSE) {
img = "timezone-16.xpm";
} else {
img = "new_appointment.xpm";
}
tmp = g_strdup_printf ("<img align=\"middle\" src=\"%s\" "
"alt=\"\" width=\"16\" height=\"16\">   "
"<font size=\"-1\"><a href=\"calendar:/%s\">%s, %s</a></font><br>",
img,
event->uid, start_str, text.value);
g_free (start_str);
g_string_append (string, tmp);
g_free (tmp);
}
free_event_array (uidarray);
g_string_append (string, "</dd></dl>");
}
if (calendar->html) {
g_free (calendar->html);
}
calendar->html = string->str;
g_string_free (string, FALSE);
e_summary_draw (summary);
return FALSE;
}
static void
cal_opened_cb (CalClient *client,
CalClientOpenStatus status,
ESummary *summary)
{
if (status == CAL_CLIENT_OPEN_SUCCESS) {
g_idle_add (generate_html, summary);
} else {
/* Need to work out what to do if there's an error */
}
}
static void
obj_changed_cb (CalClient *client,
const char *uid,
gpointer data)
{
g_idle_add (generate_html, data);
}
static void
e_summary_calendar_protocol (ESummary *summary,
const char *uri,
void *closure)
{
ESummaryCalendar *calendar;
CORBA_Environment ev;
const char *comp_uri;
GNOME_Evolution_Calendar_CompEditorFactory factory;
calendar = (ESummaryCalendar *) closure;
comp_uri = cal_client_get_uri (calendar->client);
/* Get the factory */
CORBA_exception_init (&ev);
factory = oaf_activate_from_id ("OAFIID:GNOME_Evolution_Calendar_CompEditorFactory", 0, NULL, &ev);
if (BONOBO_EX (&ev)) {
g_message ("%s: Could not activate the component editor factory (%s)", __FUNCTION__,
CORBA_exception_id (&ev));
CORBA_exception_free (&ev);
return;
}
GNOME_Evolution_Calendar_CompEditorFactory_editExisting (factory, comp_uri, (char *)uri + 10, &ev);
if (BONOBO_EX (&ev)) {
g_message ("%s: Execption while editing the component (%s)", __FUNCTION__,
CORBA_exception_id (&ev));
}
CORBA_exception_free (&ev);
bonobo_object_release_unref (factory, NULL);
}
static gboolean
locale_uses_24h_time_format (void)
{
char s[16];
time_t t = 0;
strftime (s, sizeof s, "%p", gmtime (&t));
return s[0] == '\0';
}
void
e_summary_calendar_init (ESummary *summary)
{
Bonobo_ConfigDatabase db;
CORBA_Environment ev;
ESummaryCalendar *calendar;
gboolean result;
char *uri;
g_return_if_fail (summary != NULL);
calendar = g_new (ESummaryCalendar, 1);
summary->calendar = calendar;
calendar->html = NULL;
calendar->client = cal_client_new ();
if (calendar->client == NULL) {
g_warning ("Error making the client");
return;
}
gtk_signal_connect (GTK_OBJECT (calendar->client), "cal-opened",
GTK_SIGNAL_FUNC (cal_opened_cb), summary);
gtk_signal_connect (GTK_OBJECT (calendar->client), "obj-updated",
GTK_SIGNAL_FUNC (obj_changed_cb), summary);
gtk_signal_connect (GTK_OBJECT (calendar->client), "obj-removed",
GTK_SIGNAL_FUNC (obj_changed_cb), summary);
uri = gnome_util_prepend_user_home ("evolution/local/Calendar/calendar.ics");
result = cal_client_open_calendar (calendar->client, uri, FALSE);
g_free (uri);
if (result == FALSE) {
g_message ("Open calendar failed");
}
e_summary_add_protocol_listener (summary, "calendar", e_summary_calendar_protocol, calendar);
CORBA_exception_init (&ev);
db = bonobo_get_object ("wombat:", "Bonobo/ConfigDatabase", &ev);
if (BONOBO_EX (&ev) || db == CORBA_OBJECT_NIL) {
CORBA_exception_free (&ev);
g_warning ("Error getting Wombat. Using defaults");
return;
}
calendar->wants24hr = bonobo_config_get_boolean_with_default (db, "/Calendar/Display/Use24HourFormat", locale_uses_24h_time_format (), NULL);
bonobo_object_release_unref (db, NULL);
CORBA_exception_free (&ev);
}
void
e_summary_calendar_reconfigure (ESummary *summary)
{
generate_html (summary);
}
void
e_summary_calendar_free (ESummary *summary)
{
ESummaryCalendar *calendar;
g_return_if_fail (summary != NULL);
g_return_if_fail (IS_E_SUMMARY (summary));
calendar = summary->calendar;
gtk_object_unref (GTK_OBJECT (calendar->client));
g_free (calendar->html);
g_free (calendar);
summary->calendar = NULL;
}
|