aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/calendar-component.c
blob: 8dd78658979f6b48da128081789339a474d8ebc5 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* calendar-component.c
 *
 * Copyright (C) 2003  Ettore Perazzoli
 *
 * 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.
 *
 * Authors: Ettore Perazzoli <ettore@ximian.com>
 *          Rodrigo Moya <rodrigo@ximian.com>
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>

#include "calendar-component.h"
#include "calendar-commands.h"
#include "gnome-cal.h"
#include "migration.h"
#include "dialogs/new-calendar.h"

#include "widgets/misc/e-source-selector.h"

#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-i18n.h>
#include <gtk/gtkimage.h>
#include <gtk/gtkimagemenuitem.h>
#include <gtk/gtkmessagedialog.h>
#include <gtk/gtkstock.h>
#include <gal/util/e-util.h>

#include <errno.h>


#define PARENT_TYPE bonobo_object_get_type ()
static BonoboObjectClass *parent_class = NULL;


struct _CalendarComponentPrivate {
    char *config_directory;

    GConfClient *gconf_client;
    ESourceList *source_list;
    GSList *source_selection;
    
    GnomeCalendar *calendar;
    GtkWidget *source_selector;
};


/* Utility functions.  */

static void
add_uri_for_source (ESource *source, GnomeCalendar *calendar)
{
    char *uri = e_source_get_uri (source);

    gnome_calendar_add_event_uri (calendar, uri);
    g_free (uri);
}

static void
remove_uri_for_source (ESource *source, GnomeCalendar *calendar)
{
    char *uri = e_source_get_uri (source);

    gnome_calendar_remove_event_uri (calendar, uri);
    g_free (uri);
}

static gboolean
is_in_selection (GSList *selection, ESource *source)
{
    GSList *l;
    
    for (l = selection; l; l = l->next) {
        ESource *selected_source = l->data;
        
        if (!strcmp (e_source_peek_uid (selected_source), e_source_peek_uid (source)))
            return TRUE;
    }

    return FALSE;
}

static void
update_uris_for_selection (ESourceSelector *selector, CalendarComponent *calendar_component)
{
    CalendarComponentPrivate *priv;
    GSList *selection, *l;
    
    selection = e_source_selector_get_selection (selector);

    priv = calendar_component->priv;
    
    for (l = priv->source_selection; l; l = l->next) {
        ESource *old_selected_source = l->data;

        if (!is_in_selection (selection, old_selected_source))
            remove_uri_for_source (old_selected_source, priv->calendar);
    }   
    
    for (l = selection; l; l = l->next) {
        ESource *selected_source = l->data;
        
        add_uri_for_source (selected_source, priv->calendar);
    }

    e_source_selector_free_selection (priv->source_selection);
    priv->source_selection = selection;
}

/* Callbacks.  */
static void
add_popup_menu_item (GtkMenu *menu, const char *label, const char *pixmap,
             GCallback callback, gpointer user_data)
{
    GtkWidget *item, *image;

    if (pixmap) {
        item = gtk_image_menu_item_new_with_label (label);

        /* load the image */
        image = gtk_image_new_from_file (pixmap);
        if (!image)
            image = gtk_image_new_from_stock (pixmap, GTK_ICON_SIZE_MENU);

        if (image)
            gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), image);
    } else {
        item = gtk_menu_item_new_with_label (label);
    }

    if (callback)
        g_signal_connect (G_OBJECT (item), "activate", callback, user_data);

    gtk_menu_append (menu, item);
    gtk_widget_show (item);
}

static void
delete_calendar_cb (GtkWidget *widget, CalendarComponent *comp)
{
    GSList *selection, *l;
    CalendarComponentPrivate *priv;

    priv = comp->priv;
    
    selection = e_source_selector_get_selection (E_SOURCE_SELECTOR (priv->source_selector));
    if (!selection)
        return;

    for (l = selection; l; l = l->next) {
        GtkWidget *dialog;
        ESource *selected_source = l->data;

        /* create the confirmation dialog */
        dialog = gtk_message_dialog_new (
            GTK_WINDOW (gtk_widget_get_toplevel (widget)),
            GTK_DIALOG_MODAL,
            GTK_MESSAGE_QUESTION,
            GTK_BUTTONS_YES_NO,
            _("Calendar '%s' will be removed. Are you sure you want to continue?"),
            e_source_peek_name (selected_source));
        if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_YES) {
            if (e_source_selector_source_is_selected (E_SOURCE_SELECTOR (priv->source_selector),
                                  selected_source))
                e_source_selector_unselect_source (E_SOURCE_SELECTOR (priv->source_selector),
                                   selected_source);

            e_source_group_remove_source (e_source_peek_group (selected_source), selected_source);

            /* FIXME: remove the calendar.ics file and the directory */
        }

        gtk_widget_destroy (dialog);
    }

    e_source_selector_free_selection (selection);
}

static void
new_calendar_cb (GtkWidget *widget, ESourceSelector *selector)
{
    new_calendar_dialog (GTK_WINDOW (gtk_widget_get_toplevel (widget)));
}

static void
fill_popup_menu_callback (ESourceSelector *selector, GtkMenu *menu, CalendarComponent *comp)
{
    add_popup_menu_item (menu, _("New Calendar"), NULL, G_CALLBACK (new_calendar_cb), comp);
    add_popup_menu_item (menu, _("Delete"), GTK_STOCK_DELETE, G_CALLBACK (delete_calendar_cb), comp);
    add_popup_menu_item (menu, _("Rename"), NULL, NULL, NULL);
}

static void
source_selection_changed_callback (ESourceSelector *selector, 
                   CalendarComponent *calendar_component)
{
    update_uris_for_selection (selector, calendar_component);
}

static void
primary_source_selection_changed_callback (ESourceSelector *selector,
                       CalendarComponent *calendar_component)
{
    CalendarComponentPrivate *priv;
    ESource *source;
    char *uri;

    priv = calendar_component->priv;
    
    source = e_source_selector_peek_primary_selection (selector);
    if (!source)
        return;

    /* Set the default */
    uri = e_source_get_uri (source);
    gnome_calendar_set_default_uri (priv->calendar, uri);
    g_free (uri);

}

/* GObject methods.  */

static void
impl_dispose (GObject *object)
{
    CalendarComponentPrivate *priv = CALENDAR_COMPONENT (object)->priv;

    if (priv->source_list != NULL) {
        g_object_unref (priv->source_list);
        priv->source_list = NULL;
    }

    if (priv->source_selection != NULL) {
        e_source_selector_free_selection (priv->source_selection);
        priv->source_selection = NULL;
    }
    
    if (priv->gconf_client != NULL) {
        g_object_unref (priv->gconf_client);
        priv->gconf_client = NULL;
    }

    (* G_OBJECT_CLASS (parent_class)->dispose) (object);
}

static void
impl_finalize (GObject *object)
{
    CalendarComponentPrivate *priv = CALENDAR_COMPONENT (object)->priv;

    g_free (priv->config_directory);

    g_free (priv);

    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}


/* Evolution::Component CORBA methods.  */

static void
control_activate_cb (BonoboControl *control, gboolean activate, gpointer data)
{
    GnomeCalendar *gcal;

    gcal = GNOME_CALENDAR (data);

    if (activate)
        calendar_control_activate (control, gcal);
    else
        calendar_control_deactivate (control, gcal);
}

static void
impl_createControls (PortableServer_Servant servant,
             Bonobo_Control *corba_sidebar_control,
             Bonobo_Control *corba_view_control,
             CORBA_Environment *ev)
{
    CalendarComponent *calendar_component = CALENDAR_COMPONENT (bonobo_object_from_servant (servant));
    CalendarComponentPrivate *priv;
    GtkWidget *selector_scrolled_window;
    BonoboControl *sidebar_control;
    BonoboControl *view_control;

    priv = calendar_component->priv;
    
    /* Create sidebar selector */
    priv->source_selector = e_source_selector_new (calendar_component->priv->source_list);
    gtk_widget_show (priv->source_selector);

    selector_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_container_add (GTK_CONTAINER (selector_scrolled_window), priv->source_selector);
    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (selector_scrolled_window),
                    GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (selector_scrolled_window),
                         GTK_SHADOW_IN);
    gtk_widget_show (selector_scrolled_window);

    sidebar_control = bonobo_control_new (selector_scrolled_window);

    /* Create main calendar view */
    /* FIXME Instead of returning, we should make a control with a
     * label describing the problem */
    priv->calendar = GNOME_CALENDAR (gnome_calendar_new ());
    if (!priv->calendar) {
        g_warning (G_STRLOC ": could not create the calendar widget!");
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_Component_Failed,
                     NULL);
        return;
    }
    
    gtk_widget_show (GTK_WIDGET (priv->calendar));

    view_control = bonobo_control_new (GTK_WIDGET (priv->calendar));
    if (!view_control) {
        g_warning (G_STRLOC ": could not create the control!");
        CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
                     ex_GNOME_Evolution_Component_Failed,
                     NULL);
        return;
    }
    g_object_set_data (G_OBJECT (priv->calendar), "control", view_control);

    g_signal_connect (view_control, "activate", G_CALLBACK (control_activate_cb), priv->calendar);

    g_signal_connect_object (priv->source_selector, "selection_changed",
                 G_CALLBACK (source_selection_changed_callback), 
                 G_OBJECT (calendar_component), 0);
    g_signal_connect_object (priv->source_selector, "primary_selection_changed",
                 G_CALLBACK (primary_source_selection_changed_callback), 
                 G_OBJECT (calendar_component), 0);
    g_signal_connect_object (priv->source_selector, "fill_popup_menu",
                 G_CALLBACK (fill_popup_menu_callback),
                 G_OBJECT (calendar_component), 0);

    update_uris_for_selection (E_SOURCE_SELECTOR (priv->source_selector), calendar_component);

    *corba_sidebar_control = CORBA_Object_duplicate (BONOBO_OBJREF (sidebar_control), ev);
    *corba_view_control = CORBA_Object_duplicate (BONOBO_OBJREF (view_control), ev);
}


/* Initialization.  */

static void
calendar_component_class_init (CalendarComponentClass *class)
{
    POA_GNOME_Evolution_Component__epv *epv = &class->epv;
    GObjectClass *object_class = G_OBJECT_CLASS (class);

    parent_class = g_type_class_peek_parent (class);

    epv->createControls = impl_createControls;

    object_class->dispose = impl_dispose;
    object_class->finalize = impl_finalize;
}

static void
calendar_component_init (CalendarComponent *component)
{
    CalendarComponentPrivate *priv;
    GSList *groups;

    priv = g_new0 (CalendarComponentPrivate, 1);

    priv->config_directory = g_build_filename (g_get_home_dir (),
                           ".evolution", "calendar", "config",
                           NULL);

    /* EPFIXME: Should use a custom one instead?  Also we should add
     * calendar_component_peek_gconf_client().  */
    priv->gconf_client = gconf_client_get_default ();

    priv->source_list = e_source_list_new_for_gconf (priv->gconf_client,
                             "/apps/evolution/calendar/sources");

    /* create default calendars if there are no groups */
    groups = e_source_list_peek_groups (priv->source_list);
    if (!groups) {
        ESourceGroup *group;
        ESource *source;
        char *base_uri, *new_dir;

        /* create the local source group */
        base_uri = g_build_filename (g_get_home_dir (),
                         "/.evolution/calendar/local/OnThisComputer/",
                         NULL);
        group = e_source_group_new (_("On This Computer"), base_uri);
        e_source_list_add_group (priv->source_list, group, -1);

        /* migrate calendars from older setup */
        if (!migrate_old_calendars (group)) {
            /* create default calendars */
            new_dir = g_build_filename (base_uri, "Personal/", NULL);
            if (!e_mkdir_hier (new_dir, 0700)) {
                source = e_source_new (_("Personal"), "Personal");
                e_source_group_add_source (group, source, -1);
            }
            g_free (new_dir);

            new_dir = g_build_filename (base_uri, "Work/", NULL);
            if (!e_mkdir_hier (new_dir, 0700)) {
                source = e_source_new (_("Work"), "Work");
                e_source_group_add_source (group, source, -1);
            }
            g_free (new_dir);
        }

        g_free (base_uri);

        /* create the remote source group */
        group = e_source_group_new (_("On The Web"), "webcal://");
        e_source_list_add_group (priv->source_list, group, -1);
    }

    component->priv = priv;
}


/* Public API.  */

CalendarComponent *
calendar_component_peek (void)
{
    static CalendarComponent *component = NULL;

    if (component == NULL) {
        component = g_object_new (calendar_component_get_type (), NULL);

        if (e_mkdir_hier (calendar_component_peek_config_directory (component), 0777) != 0) {
            g_warning (G_STRLOC ": Cannot create directory %s: %s",
                   calendar_component_peek_config_directory (component),
                   g_strerror (errno));
            g_object_unref (component);
            component = NULL;
        }
    }

    return component;
}

const char *
calendar_component_peek_config_directory (CalendarComponent *component)
{
    return component->priv->config_directory;
}


BONOBO_TYPE_FUNC_FULL (CalendarComponent, GNOME_Evolution_Component, PARENT_TYPE, calendar_component)