aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-module.c
blob: 6e44101cb34d5cc7c52128e6a98609fcc919dd65 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-shell-module.c
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "e-shell-module.h"

#include <gmodule.h>
#include <glib/gi18n.h>

/* This is the symbol we look for when loading a module. */
#define INIT_SYMBOL "e_shell_module_init"

#define E_SHELL_MODULE_GET_PRIVATE(obj) \
    (G_TYPE_INSTANCE_GET_PRIVATE \
    ((obj), E_TYPE_SHELL_MODULE, EShellModulePrivate))

struct _EShellModulePrivate {

    /* Set during module initialization.  This must come
     * first in the struct so the registry can read it. */
    EShellModuleInfo info;

    GModule *module;
    gchar *filename;
    EShell *shell;

    /* Initializes the loaded module. */
    void (*init) (GTypeModule *module);
};

enum {
    PROP_0,
    PROP_FILENAME,
    PROP_SHELL
};

static gpointer parent_class;

static void
shell_module_set_filename (EShellModule *shell_module,
                           const gchar *filename)
{
    g_return_if_fail (shell_module->priv->filename == NULL);
    shell_module->priv->filename = g_strdup (filename);
}

static void
shell_module_set_shell (EShellModule *shell_module,
                        EShell *shell)
{
    g_return_if_fail (shell_module->priv->shell == NULL);
    shell_module->priv->shell = g_object_ref (shell);
}

static void
shell_module_set_property (GObject *object,
                           guint property_id,
                           const GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_FILENAME:
            shell_module_set_filename (
                E_SHELL_MODULE (object),
                g_value_get_string (value));
            return;

        case PROP_SHELL:
            shell_module_set_shell (
                E_SHELL_MODULE (object),
                g_value_get_object (value));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_module_get_property (GObject *object,
                           guint property_id,
                           GValue *value,
                           GParamSpec *pspec)
{
    switch (property_id) {
        case PROP_FILENAME:
            g_value_set_string (
                value, e_shell_module_get_filename (
                E_SHELL_MODULE (object)));
            return;

        case PROP_SHELL:
            g_value_set_object (
                value, e_shell_module_get_shell (
                E_SHELL_MODULE (object)));
            return;
    }

    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
}

static void
shell_module_dispose (GObject *object)
{
    EShellModulePrivate *priv;

    priv = E_SHELL_MODULE_GET_PRIVATE (object);

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

    /* Chain up to parent's dispose() method. */
    G_OBJECT_CLASS (parent_class)->dispose (object);
}

static void
shell_module_finalize (GObject *object)
{
    EShellModulePrivate *priv;

    priv = E_SHELL_MODULE_GET_PRIVATE (object);

    g_free (priv->filename);

    /* Chain up to parent's finalize() method. */
    G_OBJECT_CLASS (parent_class)->finalize (object);
}

static gboolean
shell_module_load (GTypeModule *type_module)
{
    EShellModulePrivate *priv;
    gpointer symbol;

    priv = E_SHELL_MODULE_GET_PRIVATE (type_module);

    g_return_val_if_fail (priv->filename != NULL, FALSE);
    priv->module = g_module_open (priv->filename, 0);

    if (priv->module == NULL)
        goto fail;

    if (!g_module_symbol (priv->module, INIT_SYMBOL, &symbol))
        goto fail;

    priv->init = symbol;
    priv->init (type_module);

    return TRUE;

fail:
    g_warning ("%s", g_module_error ());

    if (priv->module != NULL)
        g_module_close (priv->module);

    return FALSE;
}

static void
shell_module_unload (GTypeModule *type_module)
{
    EShellModulePrivate *priv;

    priv = E_SHELL_MODULE_GET_PRIVATE (type_module);

    g_module_close (priv->module);
    priv->module = NULL;

    priv->init = NULL;
}

static void
shell_module_class_init (EShellModuleClass *class)
{
    GObjectClass *object_class;
    GTypeModuleClass *type_module_class;

    parent_class = g_type_class_peek_parent (class);
    g_type_class_add_private (class, sizeof (EShellModulePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->set_property = shell_module_set_property;
    object_class->get_property = shell_module_get_property;
    object_class->dispose = shell_module_dispose;
    object_class->finalize = shell_module_finalize;

    type_module_class = G_TYPE_MODULE_CLASS (class);
    type_module_class->load = shell_module_load;
    type_module_class->unload = shell_module_unload;

    g_object_class_install_property (
        object_class,
        PROP_FILENAME,
        g_param_spec_string (
            "filename",
            _("Filename"),
            _("The filename of the module"),
            NULL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));

    g_object_class_install_property (
        object_class,
        PROP_SHELL,
        g_param_spec_object (
            "shell",
            _("Shell"),
            _("The EShell singleton"),
            E_TYPE_SHELL,
            G_PARAM_READWRITE |
            G_PARAM_CONSTRUCT_ONLY));
}

static void
shell_module_init (EShellModule *shell_module)
{
    shell_module->priv = E_SHELL_MODULE_GET_PRIVATE (shell_module);
}

GType
e_shell_module_get_type (void)
{
    static GType type = 0;

    if (G_UNLIKELY (type == 0)) {
        const GTypeInfo type_info = {
            sizeof (EShellModuleClass),
            (GBaseInitFunc) NULL,
            (GBaseFinalizeFunc) NULL,
            (GClassInitFunc) shell_module_class_init,
            (GClassFinalizeFunc) NULL,
            NULL,  /* class_data */
            sizeof (EShellModule),
            0,     /* n_preallocs */
            (GInstanceInitFunc) shell_module_init,
            NULL   /* value_table */
        };

        type = g_type_register_static (
            G_TYPE_TYPE_MODULE, "EShellModule", &type_info, 0);
    }

    return type;
}

EShellModule *
e_shell_module_new (EShell *shell,
                    const gchar *filename)
{
    g_return_val_if_fail (filename != NULL, NULL);

    return g_object_new (
        E_TYPE_SHELL_MODULE, "shell", shell,
        "filename", filename, NULL);
}

gint
e_shell_module_compare (EShellModule *shell_module_a,
                        EShellModule *shell_module_b)
{
    gint a = shell_module_a->priv->info.sort_order;
    gint b = shell_module_b->priv->info.sort_order;

    return (a < b) ? -1 : (a > b);
}

const gchar *
e_shell_module_get_filename (EShellModule *shell_module)
{
    g_return_val_if_fail (E_IS_SHELL_MODULE (shell_module), NULL);

    return shell_module->priv->filename;
}

EShell *
e_shell_module_get_shell (EShellModule *shell_module)
{
    g_return_val_if_fail (E_IS_SHELL_MODULE (shell_module), NULL);

    return shell_module->priv->shell;
}

gboolean
e_shell_module_is_busy (EShellModule *shell_module)
{
    EShellModuleInfo *module_info;

    g_return_val_if_fail (E_IS_SHELL_MODULE (shell_module), FALSE);

    module_info = &shell_module->priv->info;

    if (module_info->is_busy != NULL)
        return module_info->is_busy (shell_module);

    return FALSE;
}

gboolean
e_shell_module_shutdown (EShellModule *shell_module)
{
    EShellModuleInfo *module_info;

    g_return_val_if_fail (E_IS_SHELL_MODULE (shell_module), TRUE);

    module_info = &shell_module->priv->info;

    if (module_info->shutdown != NULL)
        return module_info->shutdown (shell_module);

    return TRUE;
}

void
e_shell_module_set_info (EShellModule *shell_module,
                         const EShellModuleInfo *info)
{
    GTypeModule *module;
    EShellModuleInfo *module_info;

    g_return_if_fail (E_IS_SHELL_MODULE (shell_module));
    g_return_if_fail (info != NULL);

    module = G_TYPE_MODULE (shell_module);
    module_info = &shell_module->priv->info;

    /* A module name is required. */
    g_return_if_fail (info->name != NULL);
    module_info->name = g_intern_string (info->name);
    g_type_module_set_name (module, module_info->name);

    module_info->aliases = g_intern_string (info->aliases);
    module_info->schemes = g_intern_string (info->schemes);
    module_info->sort_order = info->sort_order;

    module_info->is_busy = info->is_busy;
    module_info->shutdown = info->shutdown;
}