aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/modules/e-cal-shell-view-private.c
blob: d5b15a370fc23f5bb5b580a282e26d68de6ece17 (plain) (blame)
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
/*
 * e-cal-shell-view-private.c
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with the program; if not, see <http://www.gnu.org/licenses/>  
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#include "e-cal-shell-view-private.h"

#include "calendar/gui/calendar-view-factory.h"
#include "widgets/menus/gal-view-factory-etable.h"

static void
cal_shell_view_update_timezone (ECalShellView *cal_shell_view)
{
#if 0
    ECalShellContent *cal_shell_content;
    ECalShellSidebar *cal_shell_sidebar;
    GnomeCalendarViewType view_type;
    ECalendarView *calendar_view;
    icaltimezone *timezone;

    cal_shell_content = cal_shell_view->priv->cal_shell_content;
    cal_shell_sidebar = cal_shell_view->priv->cal_shell_sidebar;

    e_cal_shell_sidebar_update_timezone (cal_shell_sidebar);

    view_type = e_cal_shell_content_get_current_view (cal_shell_content);
    calendar_view = e_cal_shell_content_get_calendar_view (
        cal_shell_content, view_type);

    timezone = calendar_config_get_icaltimezone ();
    e_calendar_view_get_icaltimezone (calendar_view, timezone);
#endif
}

static void
cal_shell_view_timezone_changed_cb (GConfClient *client,
                                    guint id,
                                    GConfEntry *entry,
                                    gpointer user_data)
{
    ECalShellView *cal_shell_view = user_data;

    cal_shell_view_update_timezone (cal_shell_view);
}

static struct tm
cal_shell_view_get_current_time (ECalendarItem *calitem,
                                 ECalShellView *cal_shell_view)
{
    ECalShellContent *cal_shell_content;
    struct icaltimetype tt;
    icaltimezone *timezone;

    cal_shell_content = cal_shell_view->priv->cal_shell_content;
    timezone = e_cal_shell_content_get_timezone (cal_shell_content);

    tt = icaltime_from_timet_with_zone (time (NULL), FALSE, timezone);

    return icaltimetype_to_tm (&tt);
}

static void
cal_shell_view_mini_calendar_date_range_changed_cb (ECalShellView *cal_shell_view,
                                                    ECalendarItem *calitem)
{
    /* FIXME gnome-calendar.c calls update_query() here. */
}
                                                    
static void
cal_shell_view_mini_calendar_selection_changed_cb (ECalShellView *cal_shell_view,
                                                   ECalendarItem *calitem)
{
    /* FIXME */
}

static void
cal_shell_view_mini_calendar_scroll_event_cb (ECalShellView *cal_shell_view,
                                              GdkEventScroll *event,
                                              ECalendar *mini_calendar)
{
    ECalendarItem *calitem;
    GDate start_date, end_date;

    calitem = mini_calendar->calitem;
    if (!e_calendar_item_get_selection (calitem, &start_date, &end_date))
        return;

    switch (event->direction) {
        case GDK_SCROLL_UP:
            g_date_subtract_months (&start_date, 1);
            g_date_subtract_months (&end_date, 1);
            break;

        case GDK_SCROLL_DOWN:
            g_date_add_months (&start_date, 1);
            g_date_add_months (&end_date, 1);
            break;

        default:
            g_return_if_reached ();
    }

    /* XXX Does ECalendarItem emit a signal for this?  If so, maybe
     *     we could move this handler into ECalShellSidebar. */
    e_calendar_item_set_selection (calitem, &start_date, &end_date);

    cal_shell_view_mini_calendar_date_range_changed_cb (
        cal_shell_view, calitem);
}

static void
cal_shell_view_load_view_collection (EShellViewClass *shell_view_class)
{
    GalViewCollection *collection;
    GalViewFactory *factory;
    ETableSpecification *spec;
    const gchar *base_dir;
    gchar *filename;

    collection = shell_view_class->view_collection;

    base_dir = EVOLUTION_ETSPECDIR;
    spec = e_table_specification_new ();
    filename = g_build_filename (base_dir, ETSPEC_FILENAME, NULL);
    if (!e_table_specification_load_from_file (spec, filename))
        g_critical ("Unable to load ETable specification file "
                "for calendars");
    g_free (filename);

    factory = calendar_view_factory_new (GNOME_CAL_DAY_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_WORK_WEEK_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_WEEK_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = calendar_view_factory_new (GNOME_CAL_MONTH_VIEW);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);

    factory = gal_view_factory_etable_new (spec);
    gal_view_collection_add_factory (collection, factory);
    g_object_unref (factory);
    g_object_unref (spec);

    gal_view_collection_load (collection);
}

static void
cal_shell_view_notify_view_id_cb (ECalShellView *cal_shell_view)
{
    ECalShellContent *cal_shell_content;
    GalViewInstance *view_instance;
    const gchar *view_id;

    cal_shell_content = cal_shell_view->priv->cal_shell_content;
    view_instance =
        e_cal_shell_content_get_view_instance (cal_shell_content);
    view_id = e_shell_view_get_view_id (E_SHELL_VIEW (cal_shell_view));

    /* A NULL view ID implies we're in a custom view.  But you can
     * only get to a custom view via the "Define Views" dialog, which
     * would have already modified the view instance appropriately.
     * Furthermore, there's no way to refer to a custom view by ID
     * anyway, since custom views have no IDs. */
    if (view_id == NULL)
        return;

    gal_view_instance_set_current_view_id (view_instance, view_id);
}

void
e_cal_shell_view_private_init (ECalShellView *cal_shell_view,
                               EShellViewClass *shell_view_class)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;
    ESourceList *source_list;
    GObject *object;

    object = G_OBJECT (shell_view_class->type_module);
    source_list = g_object_get_data (object, "source-list");
    g_return_if_fail (E_IS_SOURCE_LIST (source_list));

    priv->source_list = g_object_ref (source_list);
    priv->calendar_actions = gtk_action_group_new ("calendars");
    priv->filter_actions = gtk_action_group_new ("calendars-filter");

    if (!gal_view_collection_loaded (shell_view_class->view_collection))
        cal_shell_view_load_view_collection (shell_view_class);

    g_signal_connect (
        cal_shell_view, "notify::view-id",
        G_CALLBACK (cal_shell_view_notify_view_id_cb), NULL);
}

void
e_cal_shell_view_private_constructed (ECalShellView *cal_shell_view)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;
    ECalShellContent *cal_shell_content;
    ECalShellSidebar *cal_shell_sidebar;
    EShellContent *shell_content;
    EShellSidebar *shell_sidebar;
    EShellView *shell_view;
    GnomeCalendar *calendar;
    ECalendar *mini_calendar;
    guint notification;

    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_content = e_shell_view_get_shell_content (shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

    /* Cache these to avoid lots of awkward casting. */
    priv->cal_shell_content = g_object_ref (shell_content);
    priv->cal_shell_sidebar = g_object_ref (shell_sidebar);

    cal_shell_content = E_CAL_SHELL_CONTENT (shell_content);
    calendar = e_cal_shell_content_get_calendar (cal_shell_content);

    cal_shell_sidebar = E_CAL_SHELL_SIDEBAR (shell_sidebar);
    mini_calendar = e_cal_shell_sidebar_get_mini_calendar (cal_shell_sidebar);

    e_calendar_item_set_get_time_callback (
        mini_calendar->calitem, (ECalendarItemGetTimeCallback)
        cal_shell_view_get_current_time, cal_shell_view, NULL);

    g_signal_connect_swapped (
        calendar, "dates-shown-changed",
        G_CALLBACK (e_cal_shell_view_update_sidebar),
        cal_shell_view);

    g_signal_connect_swapped (
        mini_calendar, "scroll-event",
        G_CALLBACK (cal_shell_view_mini_calendar_scroll_event_cb),
        cal_shell_view);

    g_signal_connect_swapped (
        mini_calendar->calitem, "date-range-changed",
        G_CALLBACK (cal_shell_view_mini_calendar_date_range_changed_cb),
        cal_shell_view);

    g_signal_connect_swapped (
        mini_calendar->calitem, "selection-changed",
        G_CALLBACK (cal_shell_view_mini_calendar_selection_changed_cb),
        cal_shell_view);

    /* Listen for configuration changes. */

#if 0
    notification = calendar_config_add_notification_timezone (
        cal_shell_view_timezone_changed_cb, cal_shell_view);
    priv->notifications = g_list_prepend (
        priv->notifications, GUINT_TO_POINTER (notification));
    cal_shell_view_update_timezone (cal_shell_view);
#endif

    e_cal_shell_view_actions_init (shell_view);
    e_cal_shell_view_update_sidebar (cal_shell_view);
}

void
e_cal_shell_view_private_dispose (ECalShellView *cal_shell_view)
{
    ECalShellViewPrivate *priv = cal_shell_view->priv;

    DISPOSE (priv->source_list);

    DISPOSE (priv->calendar_actions);
    DISPOSE (priv->filter_actions);

    DISPOSE (priv->cal_shell_content);
    DISPOSE (priv->cal_shell_sidebar);

    if (cal_shell_view->priv->activity != NULL) {
        /* XXX Activity is not cancellable. */
        e_activity_complete (cal_shell_view->priv->activity);
        g_object_unref (cal_shell_view->priv->activity);
        cal_shell_view->priv->activity = NULL;
    }
}

void
e_cal_shell_view_private_finalize (ECalShellView *cal_shell_view)
{
    /* XXX Nothing to do? */
}

void
e_cal_shell_view_open_event (ECalShellView *cal_shell_view,
                             ECalModelComponent *comp_data)
{
    CompEditor *editor;
    CompEditorFlags flags = 0;
    ECalComponent *comp;
    icalcomponent *clone;
    icalproperty *prop;
    const gchar *uid;

    g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));
    g_return_if_fail (E_IS_CAL_MODEL_COMPONENT (comp_data));

    uid = icalcomponent_get_uid (comp_data->icalcomp);
    editor = comp_editor_find_instance (uid);

    if (editor != NULL)
        goto exit;

    comp = e_cal_component_new ();
    clone = icalcomponent_new_clone (comp_data->icalcomp);
    e_cal_component_set_icalcomponent (comp, clone);

    prop = icalcomponent_get_first_property (
        comp_data->icalcomp, ICAL_ATTENDEE_PROPERTY);
    if (prop != NULL)
        flags |= COMP_EDITOR_MEETING;

    if (itip_organizer_is_user (comp, comp_data->client))
        flags |= COMP_EDITOR_USER_ORG;

    if (itip_sentby_is_user (comp))
        flags |= COMP_EDITOR_USER_ORG;

    if (!e_cal_component_has_attendees (comp))
        flags |= COMP_EDITOR_USER_ORG;

    editor = event_editor_new (comp_data->client, flags);
    comp_editor_edit_comp (editor, comp);

    g_object_ref (comp);

exit:
    gtk_window_present (GTK_WINDOW (editor));
}

void
e_cal_shell_view_set_status_message (ECalShellView *cal_shell_view,
                                     const gchar *status_message)
{
    EActivity *activity;
    EShellView *shell_view;
    EShellModule *shell_module;

    g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));

    activity = cal_shell_view->priv->activity;
    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_module = e_shell_view_get_shell_module (shell_view);

    if (status_message == NULL || *status_message == '\0') {
        if (activity != NULL) {
            e_activity_complete (activity);
            g_object_unref (activity);
            activity = NULL;
        }

    } else if (activity == NULL) {
        activity = e_activity_new (status_message);
        e_shell_module_add_activity (shell_module, activity);

    } else
        e_activity_set_primary_text (activity, status_message);

    cal_shell_view->priv->activity = activity;
}

void
e_cal_shell_view_update_sidebar (ECalShellView *cal_shell_view)
{
#if 0  /* KILL-BONOBO */
    EShellView *shell_view;
    EShellSidebar *shell_sidebar;
    GnomeCalendar *calendar;
    GnomeCalendarViewType view;
    time_t start_time, end_time;
    struct tm start_tm, end_tm;
    struct icaltimetype start_tt, end_tt;
    icaltimezone *timezone;
    gchar buffer[512];
    gchar end_buffer[512];

    g_return_if_fail (E_IS_CAL_SHELL_VIEW (cal_shell_view));

    shell_view = E_SHELL_VIEW (cal_shell_view);
    shell_sidebar = e_shell_view_get_shell_sidebar (shell_view);

    calendar = e_cal_shell_view_get_calendar (cal_shell_view);

    gnome_calendar_get_visible_time_range (
        calendar, &start_time, &end_time);
    timezone = gnome_calendar_get_timezone (calendar);
    view = gnome_calendar_get_view (calendar);

    start_tt = icaltime_from_timet_with_zone (start_time, FALSE, timezone);
    start_tm.tm_year = start_tt.year - 1900;
    start_tm.tm_mon = start_tt.month - 1;
    start_tm.tm_mday = start_tt.day;
    start_tm.tm_hour = start_tt.hour;
    start_tm.tm_min = start_tt.minute;
    start_tm.tm_sec = start_tt.second;
    start_tm.tm_isdst = -1;
    start_tm.tm_wday = time_day_of_week (
        start_tt.day, start_tt.month - 1, start_tt.year);

    /* Subtract one from end_time so we don't get an extra day. */
    end_tt = icaltime_from_timet_with_zone (end_time - 1, FALSE, timezone);
    end_tm.tm_year = end_tt.year - 1900;
    end_tm.tm_mon = end_tt.month - 1;
    end_tm.tm_mday = end_tt.day;
    end_tm.tm_hour = end_tt.hour;
    end_tm.tm_min = end_tt.minute;
    end_tm.tm_sec = end_tt.second;
    end_tm.tm_isdst = -1;
    end_tm.tm_wday = time_day_of_week (
        end_tt.day, end_tt.month - 1, end_tt.year);

    switch (view) {
        case GNOME_CAL_DAY_VIEW:
        case GNOME_CAL_WORK_WEEK_VIEW:
        case GNOME_CAL_WEEK_VIEW:
            if (start_tm.tm_year == end_tm.tm_year &&
                start_tm.tm_mon == end_tm.tm_mon &&
                start_tm.tm_mday == end_tm.tm_mday) {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%A %d %b %Y"), &start_tm);
            } else if (start_tm.tm_year == end_tm.tm_year) {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%a %d %b"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%a %d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            } else {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%a %d %b %Y"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%a %d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            }
            break;

        case GNOME_CAL_MONTH_VIEW:
        case GNOME_CAL_LIST_VIEW:
            if (start_tm.tm_year == end_tm.tm_year) {
                if (start_tm.tm_mon == end_tm.tm_mon) {
                    e_utf8_strftime (
                        buffer,
                        sizeof (buffer),
                        "%d", &start_tm);
                    e_utf8_strftime (
                        end_buffer,
                        sizeof (end_buffer),
                        _("%d %b %Y"), &end_tm);
                    strcat (buffer, " - ");
                    strcat (buffer, end_buffer);
                } else {
                    e_utf8_strftime (
                        buffer,
                        sizeof (buffer),
                        _("%d %b"), &start_tm);
                    e_utf8_strftime (
                        end_buffer,
                        sizeof (end_buffer),
                        _("%d %b %Y"), &end_tm);
                    strcat (buffer, " - ");
                    strcat (buffer, end_buffer);
                }
            } else {
                e_utf8_strftime (
                    buffer, sizeof (buffer),
                    _("%d %b %Y"), &start_tm);
                e_utf8_strftime (
                    end_buffer, sizeof (end_buffer),
                    _("%d %b %Y"), &end_tm);
                strcat (buffer, " - ");
                strcat (buffer, end_buffer);
            }
            break;

        default:
            g_return_if_reached ();
    }

    e_shell_sidebar_set_secondary_text (shell_sidebar, buffer);
#endif
}