aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/calendar-component.c
blob: 96c7d8eafe976c23d9904abf843b0032464f0047 (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
/* -*- 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.
 *
 * Author: Ettore Perazzoli <ettore@ximian.com>
 */

#include <config.h>

#include "calendar-component.h"

#include "control-factory.h"

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

#include <bonobo/bonobo-control.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;
};


/* Utility functions.  */

static void
load_uri_for_selection (ESourceSelector *selector,
            BonoboControl *view_control)
{
    ESource *selected_source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (selector));

    if (selected_source != NULL) {
        char *uri = e_source_get_uri (selected_source);
        bonobo_control_set_property (view_control, NULL, "folder_uri", TC_CORBA_string, uri, NULL);
        g_free (uri);
    }
}


/* Callbacks.  */

static void
primary_source_selection_changed_callback (ESourceSelector *selector,
                       BonoboControl *view_control)
{
    load_uri_for_selection (selector, view_control);
}


/* 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->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
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));
    GtkWidget *selector;
    GtkWidget *selector_scrolled_window;
    BonoboControl *sidebar_control;
    BonoboControl *view_control;

    selector = e_source_selector_new (calendar_component->priv->source_list);
    gtk_widget_show (selector);

    selector_scrolled_window = gtk_scrolled_window_new (NULL, NULL);
    gtk_container_add (GTK_CONTAINER (selector_scrolled_window), selector);
    gtk_widget_show (selector_scrolled_window);

    sidebar_control = bonobo_control_new (selector_scrolled_window);

    view_control = control_factory_new_control ();
    g_signal_connect_object (selector, "primary_selection_changed",
                 G_CALLBACK (primary_source_selection_changed_callback), G_OBJECT (view_control), 0);
    load_uri_for_selection (E_SOURCE_SELECTOR (selector), view_control);

    *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;

    epv->createControls = impl_createControls;
}

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

    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");

    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 ("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)