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
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-summary-calendar.c
*
* Copyright (C) 2001 Ximian, Inc.
* Copyright (C) 2002 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.
*
* 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 "e-util/e-config-listener.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>
#define MAX_RELOAD_TRIES 10
struct _ESummaryCalendar {
CalClient *client;
char *html;
gboolean wants24hr;
char *default_uri;
EConfigListener *config_listener;
int cal_open_reload_timeout_id;
int reload_count;
};
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;
}
static gboolean
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);
/* If either the DTSTART or the DTEND is a DATE value, we return TRUE.
Maybe if one was a DATE-TIME we should check that, but that should
not happen often. */
if ((start_datetime.value && start_datetime.value->is_date)
|| (end_datetime.value && end_datetime.value->is_date)) {
retval = TRUE;
goto out;
}
/* If the event uses UTC for DTSTART & DTEND, return TRUE. Outlook
will send single events as UTC, so we don't want to mark all of
these. */
if ((!start_datetime.value || start_datetime.value->is_utc)
&& (!end_datetime.value || end_datetime.value->is_utc)) {
retval = TRUE;
goto out;
}
/* If the event uses floating time for DTSTART & DTEND, return TRUE.
Imported vCalendar files will use floating times, so we don't want
to mark all of these. */
if (!start_datetime.tzid && !end_datetime.tzid) {
retval = TRUE;
goto out;
}
/* 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;
if (start_datetime.value) {
offset1 = icaltimezone_get_utc_offset (start_zone,
start_datetime.value,
NULL);
offset2 = icaltimezone_get_utc_offset (zone,
start_datetime.value,
NULL);
if (offset1 != offset2)
goto out;
}
status = cal_client_get_timezone (client,
end_datetime.tzid,
&end_zone);
if (status != CAL_CLIENT_GET_SUCCESS)
goto out;
if (end_datetime.value) {
offset1 = icaltimezone_get_utc_offset (end_zone,
end_datetime.value,
NULL);
offset2 = icaltimezone_get_utc_offset (zone,
end_datetime.value,
NULL);
if (offset1 != offset2)
goto out;
}
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,
recur->summary->tz);
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;
if (cal_client_get_load_state (calendar->client) != CAL_CLIENT_LOAD_LOADED)
return FALSE;
/* Set the default timezone on the server. */
if (summary->tz) {
cal_client_set_default_timezone (calendar->client,
summary->tz);
}
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=\"", calendar->default_uri, "\">",
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 (NULL);
g_string_sprintf (string, "<dl><dt><img src=\"myevo-appointments.png\" align=\"middle\" "
"alt=\"\" width=\"48\" height=\"48\"> <b><a href=\"%s\">",
calendar->default_uri);
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;
struct tm start_tm;
char start_str[64], *start_str_utf, *img;
event = uidarray->pdata[i];
cal_component_get_summary (event->comp, &text);
start_tm = icaltimetype_to_tm (event->dt.value);
if (calendar->wants24hr == TRUE) {
strftime (start_str, sizeof start_str, _("%k:%M %d %B"), &start_tm);
} else {
strftime (start_str, sizeof start_str, _("%l:%M %d %B"), &start_tm);
}
if (cal_component_has_alarms (event->comp)) {
img = "es-appointments.png";
} else if (compare_event_timezones (event->comp,
calendar->client,
summary->tz) == FALSE) {
img = "timezone-16.xpm";
} else {
img = "new_appointment.xpm";
}
start_str_utf = e_utf8_from_locale_string (start_str);
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_utf, text.value ? text.value : _("No description"));
g_free (start_str_utf);
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 gboolean
cal_open_reload_timeout (void *data)
{
ESummary *summary = (ESummary *) data;
summary->calendar->cal_open_reload_timeout_id = 0;
if (++ summary->calendar->reload_count >= MAX_RELOAD_TRIES) {
summary->calendar->reload_count = 0;
return FALSE;
}
cal_client_open_default_calendar (summary->calendar->client, FALSE);
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 {
summary->calendar->cal_open_reload_timeout_id = g_timeout_add (1000,
cal_open_reload_timeout,
summary);
}
}
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';
}
static void
setup_calendar (ESummary *summary)
{
ESummaryCalendar *calendar;
calendar = summary->calendar;
g_assert (calendar != NULL);
if (calendar->cal_open_reload_timeout_id != 0) {
g_source_remove (calendar->cal_open_reload_timeout_id);
calendar->cal_open_reload_timeout_id = 0;
calendar->reload_count = 0;
}
if (calendar->client != NULL)
gtk_object_unref (GTK_OBJECT (calendar->client));
calendar->client = cal_client_new ();
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);
if (! cal_client_open_default_calendar (calendar->client, FALSE))
g_message ("Open calendar failed");
calendar->wants24hr = e_config_listener_get_boolean_with_default (calendar->config_listener,
"/Calendar/Display/Use24HourFormat",
locale_uses_24h_time_format (), NULL);
calendar->default_uri = e_config_listener_get_string_with_default (calendar->config_listener,
"/DefaultFolders/calendar_path",
"evolution:/local/Calendar", NULL);
}
static void
config_listener_key_changed_cb (EConfigListener *listener,
const char *key,
void *user_data)
{
setup_calendar (E_SUMMARY (user_data));
generate_html (user_data);
}
static void
setup_config_listener (ESummary *summary)
{
ESummaryCalendar *calendar;
calendar = summary->calendar;
g_assert (calendar != NULL);
calendar->config_listener = e_config_listener_new ();
gtk_signal_connect (GTK_OBJECT (calendar->config_listener), "key_changed",
GTK_SIGNAL_FUNC (config_listener_key_changed_cb), summary);
setup_calendar (summary);
}
void
e_summary_calendar_init (ESummary *summary)
{
ESummaryCalendar *calendar;
g_return_if_fail (summary != NULL);
calendar = g_new0 (ESummaryCalendar, 1);
summary->calendar = calendar;
calendar->html = NULL;
setup_config_listener (summary);
setup_calendar (summary);
e_summary_add_protocol_listener (summary, "calendar", e_summary_calendar_protocol, calendar);
}
void
e_summary_calendar_reconfigure (ESummary *summary)
{
setup_calendar (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;
if (calendar->cal_open_reload_timeout_id != 0)
g_source_remove (calendar->cal_open_reload_timeout_id);
gtk_object_unref (GTK_OBJECT (calendar->client));
g_free (calendar->html);
g_free (calendar->default_uri);
g_free (calendar);
summary->calendar = NULL;
}
|