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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
* Authors:
* Hans Petter Jansson <hpj@ximian.com>
*
* Copyright 2003, 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
*/
/*
* ECalListView - display calendar events in an ETable.
*/
#include <config.h>
#include "e-cal-list-view.h"
#include "ea-calendar.h"
#include <math.h>
#include <time.h>
#include <sys/stat.h>
#include <gdk/gdkkeysyms.h>
#include <gdk/gdkx.h>
#include <gtk/gtkdnd.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <gtk/gtkvscrollbar.h>
#include <gtk/gtkwindow.h>
#include <gal/widgets/e-gui-utils.h>
#include <gal/util/e-util.h>
#include <gal/e-table/e-table-memory-store.h>
#include <gal/e-table/e-cell-checkbox.h>
#include <gal/e-table/e-cell-toggle.h>
#include <gal/e-table/e-cell-text.h>
#include <gal/e-table/e-cell-combo.h>
#include <gal/widgets/e-popup-menu.h>
#include <widgets/misc/e-cell-date-edit.h>
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-exec.h>
#include <libgnome/gnome-util.h>
#include <e-util/e-categories-config.h>
#include <e-util/e-dialog-utils.h>
#include <libecal/e-cal-time-util.h>
#include "e-cal-model-calendar.h"
#include "e-cell-date-edit-text.h"
#include "dialogs/delete-comp.h"
#include "dialogs/delete-error.h"
#include "dialogs/send-comp.h"
#include "dialogs/cancel-comp.h"
#include "dialogs/recur-comp.h"
#include "comp-util.h"
#include "itip-utils.h"
#include "calendar-commands.h"
#include "calendar-config.h"
#include "goto.h"
#include "misc.h"
static void e_cal_list_view_class_init (ECalListViewClass *class);
static void e_cal_list_view_init (ECalListView *cal_list_view);
static void e_cal_list_view_destroy (GtkObject *object);
static void e_cal_list_view_update_query (ECalendarView *cal_view);
static GList *e_cal_list_view_get_selected_events (ECalendarView *cal_view);
static gboolean e_cal_list_view_get_selected_time_range (ECalendarView *cal_view, time_t *start_time, time_t *end_time);
static gboolean e_cal_list_view_get_visible_time_range (ECalendarView *cal_view, time_t *start_time,
time_t *end_time);
static gboolean e_cal_list_view_popup_menu (GtkWidget *widget);
static void e_cal_list_view_show_popup_menu (ECalListView *cal_list_view, gint row,
GdkEvent *gdk_event);
static gboolean e_cal_list_view_on_table_right_click (GtkWidget *table, gint row, gint col,
GdkEvent *event, gpointer data);
static GtkTableClass *parent_class; /* Should be ECalendarViewClass? */
E_MAKE_TYPE (e_cal_list_view, "ECalListView", ECalListView, e_cal_list_view_class_init,
e_cal_list_view_init, e_calendar_view_get_type ());
static void
e_cal_list_view_class_init (ECalListViewClass *class)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
ECalendarViewClass *view_class;
parent_class = g_type_class_peek_parent (class);
object_class = (GtkObjectClass *) class;
widget_class = (GtkWidgetClass *) class;
view_class = (ECalendarViewClass *) class;
/* Method override */
object_class->destroy = e_cal_list_view_destroy;
widget_class->popup_menu = e_cal_list_view_popup_menu;
view_class->get_selected_events = e_cal_list_view_get_selected_events;
view_class->get_selected_time_range = e_cal_list_view_get_selected_time_range;
view_class->get_visible_time_range = e_cal_list_view_get_visible_time_range;
view_class->update_query = e_cal_list_view_update_query;
}
static gint
date_compare_cb (gconstpointer a, gconstpointer b)
{
ECellDateEditValue *dv1 = (ECellDateEditValue *) a;
ECellDateEditValue *dv2 = (ECellDateEditValue *) b;
struct icaltimetype tt;
/* First check if either is NULL. NULL dates sort last. */
if (!dv1 || !dv2) {
if (dv1 == dv2)
return 0;
else if (dv1)
return -1;
else
return 1;
}
/* Copy the 2nd value and convert it to the same timezone as the
first. */
tt = dv2->tt;
icaltimezone_convert_time (&tt, dv2->zone, dv1->zone);
/* Now we can compare them. */
return icaltime_compare (dv1->tt, tt);
}
static void
e_cal_list_view_init (ECalListView *cal_list_view)
{
cal_list_view->query = NULL;
cal_list_view->table_scrolled = NULL;
cal_list_view->cursor_event = NULL;
cal_list_view->set_table_id = 0;
}
/* Returns the current time, for the ECellDateEdit items. */
static struct tm
get_current_time_cb (ECellDateEdit *ecde, gpointer data)
{
ECalListView *cal_list_view = data;
icaltimezone *zone;
struct tm tmp_tm = { 0 };
struct icaltimetype tt;
zone = e_calendar_view_get_timezone (E_CALENDAR_VIEW (cal_list_view));
tt = icaltime_from_timet_with_zone (time (NULL), FALSE, zone);
/* Now copy it to the struct tm and return it. */
tmp_tm.tm_year = tt.year - 1900;
tmp_tm.tm_mon = tt.month - 1;
tmp_tm.tm_mday = tt.day;
tmp_tm.tm_hour = tt.hour;
tmp_tm.tm_min = tt.minute;
tmp_tm.tm_sec = tt.second;
tmp_tm.tm_isdst = -1;
return tmp_tm;
}
static void
load_table_state (ECalListView *cal_list_view)
{
struct stat st;
if (!cal_list_view->table_state_path)
return;
if (stat (cal_list_view->table_state_path, &st) == 0 && st.st_size > 0 && S_ISREG (st.st_mode)) {
e_table_load_state (e_table_scrolled_get_table (cal_list_view->table_scrolled),
cal_list_view->table_state_path);
}
}
static void
save_table_state (ECalListView *cal_list_view)
{
if (!cal_list_view->table_state_path)
return;
e_table_save_state (e_table_scrolled_get_table (cal_list_view->table_scrolled),
cal_list_view->table_state_path);
}
static void
setup_e_table (ECalListView *cal_list_view)
{
ECalModelCalendar *model;
ETableExtras *extras;
GList *strings;
ECell *cell, *popup_cell;
GnomeCanvas *canvas;
GtkStyle *style;
model = E_CAL_MODEL_CALENDAR (e_calendar_view_get_model (E_CALENDAR_VIEW (cal_list_view)));
if (cal_list_view->table_scrolled) {
save_table_state (cal_list_view);
gtk_widget_destroy (GTK_WIDGET (cal_list_view->table_scrolled));
}
/* Create the header columns */
extras = e_table_extras_new();
/* Normal string fields */
cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
g_object_set (G_OBJECT (cell),
"bg_color_column", E_CAL_MODEL_FIELD_COLOR,
NULL);
e_table_extras_add_cell (extras, "calstring", cell);
/* Date fields */
cell = e_cell_date_edit_text_new (NULL, GTK_JUSTIFY_LEFT);
g_object_set (G_OBJECT (cell),
"bg_color_column", E_CAL_MODEL_FIELD_COLOR,
NULL);
popup_cell = e_cell_date_edit_new ();
e_cell_popup_set_child (E_CELL_POPUP (popup_cell), cell);
g_object_unref (cell);
e_table_extras_add_cell (extras, "dateedit", popup_cell);
cal_list_view->dates_cell = E_CELL_DATE_EDIT (popup_cell);
e_cell_date_edit_set_get_time_callback (E_CELL_DATE_EDIT (popup_cell),
get_current_time_cb,
cal_list_view, NULL);
/* Combo fields */
cell = e_cell_text_new (NULL, GTK_JUSTIFY_LEFT);
g_object_set (G_OBJECT (cell),
"bg_color_column", E_CAL_MODEL_FIELD_COLOR,
"editable", FALSE,
NULL);
popup_cell = e_cell_combo_new ();
e_cell_popup_set_child (E_CELL_POPUP (popup_cell), cell);
g_object_unref (cell);
strings = NULL;
strings = g_list_append (strings, (char*) _("Public"));
strings = g_list_append (strings, (char*) _("Private"));
strings = g_list_append (strings, (char*) _("Confidential"));
e_cell_combo_set_popdown_strings (E_CELL_COMBO (popup_cell),
strings);
e_table_extras_add_cell (extras, "classification", popup_cell);
/* Sorting */
e_table_extras_add_compare (extras, "date-compare",
date_compare_cb);
/* Create table view */
cal_list_view->table_scrolled = E_TABLE_SCROLLED (
e_table_scrolled_new_from_spec_file (E_TABLE_MODEL (model),
extras,
EVOLUTION_ETSPECDIR "/e-cal-list-view.etspec",
NULL));
/* Make sure text is readable on top of our color coding */
canvas = GNOME_CANVAS (e_table_scrolled_get_table (cal_list_view->table_scrolled)->table_canvas);
style = gtk_widget_get_style (GTK_WIDGET (canvas));
style->fg [GTK_STATE_SELECTED] = style->text [GTK_STATE_NORMAL];
style->fg [GTK_STATE_ACTIVE] = style->text [GTK_STATE_NORMAL];
gtk_widget_set_style (GTK_WIDGET (canvas), style);
/* Load state, if possible */
load_table_state (cal_list_view);
/* Connect signals */
g_signal_connect (e_table_scrolled_get_table (cal_list_view->table_scrolled),
"right-click", G_CALLBACK (e_cal_list_view_on_table_right_click), cal_list_view);
/* Attach and show widget */
gtk_table_attach (GTK_TABLE (cal_list_view), GTK_WIDGET (cal_list_view->table_scrolled),
0, 2, 0, 2, GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 1, 1);
gtk_widget_show (GTK_WIDGET (cal_list_view->table_scrolled));
}
GtkWidget *
e_cal_list_view_construct (ECalListView *cal_list_view, const gchar *table_state_path)
{
if (table_state_path)
cal_list_view->table_state_path = g_strdup (table_state_path);
else
cal_list_view->table_state_path = NULL;
setup_e_table (cal_list_view);
return GTK_WIDGET (cal_list_view);
}
/**
* e_cal_list_view_new:
* @Returns: a new #ECalListView.
*
* Creates a new #ECalListView.
**/
GtkWidget *
e_cal_list_view_new (const gchar *table_state_path)
{
ECalListView *cal_list_view;
ECalModel *model;
model = E_CAL_MODEL (e_cal_model_calendar_new ());
cal_list_view = g_object_new (e_cal_list_view_get_type (), "model", model, NULL);
if (!e_cal_list_view_construct (cal_list_view, table_state_path)) {
g_message (G_STRLOC ": Could not construct the calendar list GUI");
g_object_unref (cal_list_view);
return NULL;
}
g_object_unref (model);
return GTK_WIDGET (cal_list_view);
}
static void
e_cal_list_view_destroy (GtkObject *object)
{
ECalListView *cal_list_view;
cal_list_view = E_CAL_LIST_VIEW (object);
if (cal_list_view->query) {
g_signal_handlers_disconnect_matched (cal_list_view->query, G_SIGNAL_MATCH_DATA,
0, 0, NULL, NULL, cal_list_view);
g_object_unref (cal_list_view->query);
cal_list_view->query = NULL;
}
if (cal_list_view->set_table_id) {
g_source_remove (cal_list_view->set_table_id);
cal_list_view->set_table_id = 0;
}
if (cal_list_view->table_state_path) {
save_table_state (cal_list_view);
g_free (cal_list_view->table_state_path);
cal_list_view->table_state_path = NULL;
}
if (cal_list_view->cursor_event) {
g_free (cal_list_view->cursor_event);
cal_list_view->cursor_event = NULL;
}
if (cal_list_view->table_scrolled) {
gtk_widget_destroy (GTK_WIDGET (cal_list_view->table_scrolled));
cal_list_view->table_scrolled = NULL;
}
GTK_OBJECT_CLASS (parent_class)->destroy (object);
}
static gboolean
setup_e_table_cb (gpointer data)
{
setup_e_table (E_CAL_LIST_VIEW (data));
E_CAL_LIST_VIEW (data)->set_table_id = 0;
return FALSE;
}
static void
e_cal_list_view_update_query (ECalendarView *cal_list_view)
{
if (!E_CAL_LIST_VIEW (cal_list_view)->set_table_id)
E_CAL_LIST_VIEW (cal_list_view)->set_table_id =
g_idle_add (setup_e_table_cb, cal_list_view);
}
static void
e_cal_list_view_show_popup_menu (ECalListView *cal_list_view, gint row, GdkEvent *gdk_event)
{
GtkMenu *popup;
popup = e_calendar_view_create_popup_menu (E_CALENDAR_VIEW (cal_list_view));
e_popup_menu (popup, gdk_event);
}
static gboolean
e_cal_list_view_popup_menu (GtkWidget *widget)
{
ECalListView *cal_list_view = E_CAL_LIST_VIEW (widget);
e_cal_list_view_show_popup_menu (cal_list_view, -1, NULL);
return TRUE;
}
static gboolean
e_cal_list_view_on_table_right_click (GtkWidget *table, gint row, gint col, GdkEvent *event,
gpointer data)
{
ECalListView *cal_list_view = E_CAL_LIST_VIEW (data);
e_cal_list_view_show_popup_menu (cal_list_view, row, event);
return TRUE;
}
static gboolean
e_cal_list_view_get_selected_time_range (ECalendarView *cal_view, time_t *start_time, time_t *end_time)
{
GList *selected;
icaltimezone *zone;
selected = e_calendar_view_get_selected_events (cal_view);
if (selected) {
ECalendarViewEvent *event = (ECalendarViewEvent *) selected->data;
ECalComponentDateTime dtstart, dtend;
ECalComponent *comp;
comp = e_cal_component_new ();
e_cal_component_set_icalcomponent (comp, icalcomponent_new_clone (event->comp_data->icalcomp));
if (start_time) {
e_cal_component_get_dtstart (comp, &dtstart);
if (dtstart.tzid) {
zone = icalcomponent_get_timezone (e_cal_component_get_icalcomponent (comp), dtstart.tzid);
} else {
zone = NULL;
}
*start_time = icaltime_as_timet_with_zone (*dtstart.value, zone);
e_cal_component_free_datetime (&dtstart);
}
if (end_time) {
e_cal_component_get_dtend (comp, &dtend);
if (dtend.tzid) {
zone = icalcomponent_get_timezone (e_cal_component_get_icalcomponent (comp), dtend.tzid);
} else {
zone = NULL;
}
*end_time = icaltime_as_timet_with_zone (*dtend.value, zone);
e_cal_component_free_datetime (&dtend);
}
g_object_unref (comp);
g_list_free (selected);
return TRUE;
}
return FALSE;
}
static GList *
e_cal_list_view_get_selected_events (ECalendarView *cal_view)
{
GList *event_list = NULL;
gint cursor_row;
if (E_CAL_LIST_VIEW (cal_view)->cursor_event) {
g_free (E_CAL_LIST_VIEW (cal_view)->cursor_event);
E_CAL_LIST_VIEW (cal_view)->cursor_event = NULL;
}
cursor_row = e_table_get_cursor_row (e_table_scrolled_get_table (E_CAL_LIST_VIEW (cal_view)->table_scrolled));
if (cursor_row >= 0) {
ECalendarViewEvent *event;
event = E_CAL_LIST_VIEW (cal_view)->cursor_event = g_new0 (ECalendarViewEvent, 1);
event->comp_data =
e_cal_model_get_component_at (e_calendar_view_get_model (cal_view),
cursor_row);
event_list = g_list_prepend (event_list, event);
}
return event_list;
}
static void
adjust_range (icaltimetype icaltime, time_t *earliest, time_t *latest, gboolean *set)
{
time_t t;
if (!icaltime_is_valid_time (icaltime))
return;
t = icaltime_as_timet (icaltime);
*earliest = MIN (*earliest, t);
*latest = MAX (*latest, t);
*set = TRUE;
}
/* NOTE: Time use for this function increases linearly with number of events. This is not
* ideal, since it's used in a couple of places. We could probably be smarter about it,
* and use do it less frequently... */
static gboolean
e_cal_list_view_get_visible_time_range (ECalendarView *cal_view, time_t *start_time, time_t *end_time)
{
time_t earliest = G_MAXINT, latest = 0;
gboolean set = FALSE;
gint n_rows, i;
n_rows = e_table_model_row_count (E_TABLE_MODEL (e_calendar_view_get_model (cal_view)));
for (i = 0; i < n_rows; i++) {
ECalModelComponent *comp;
icalcomponent *icalcomp;
comp = e_cal_model_get_component_at (e_calendar_view_get_model (cal_view), i);
if (!comp)
continue;
icalcomp = comp->icalcomp;
if (!icalcomp)
continue;
adjust_range (icalcomponent_get_dtstart (icalcomp), &earliest, &latest, &set);
adjust_range (icalcomponent_get_dtend (icalcomp), &earliest, &latest, &set);
}
if (set) {
*start_time = earliest;
*end_time = latest;
return TRUE;
}
return FALSE;
}
gboolean
e_cal_list_view_get_range_shown (ECalListView *cal_list_view, GDate *start_date, gint *days_shown)
{
time_t first, last;
GDate end_date;
if (!e_cal_list_view_get_visible_time_range (E_CALENDAR_VIEW (cal_list_view), &first, &last))
return FALSE;
time_to_gdate_with_zone (start_date, first, e_calendar_view_get_timezone (E_CALENDAR_VIEW (cal_list_view)));
time_to_gdate_with_zone (&end_date, last, e_calendar_view_get_timezone (E_CALENDAR_VIEW (cal_list_view)));
*days_shown = g_date_days_between (start_date, &end_date);
return TRUE;
}
|