aboutsummaryrefslogtreecommitdiffstats
path: root/modules/plugin-lib/e-plugin-lib.c
blob: 46ddae3859e81b3f787f9c36ba5f78a912ff3078 (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
/*
 * e-plugin-lib.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.
 *
 * 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 Lesser General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

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

#include "e-plugin-lib.h"

#ifdef G_OS_WIN32
#include <libedataserver/e-data-server-util.h> /* for e_util_replace_prefix() */
#endif

#include <string.h>

static gpointer parent_class;
static GType plugin_lib_type;

/* TODO:
 * We need some way to manage lifecycle.
 * We need some way to manage state.
 *
 * Maybe just the g module init method will do, or we could add
 * another which returns context.
 *
 * There is also the question of per-instance context, e.g. for config
 * pages.
 */

static gint
plugin_lib_loadmodule (EPlugin *plugin)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (plugin);
    EPluginLibEnableFunc enable;
    gboolean found_symbol;

    if (plugin_lib->module != NULL)
        return 0;

    if (plugin_lib->location == NULL) {
        plugin->enabled = FALSE;
        g_warning ("Location not set in plugin '%s'", plugin->name);
        return -1;
    }

    plugin_lib->module = g_module_open (plugin_lib->location, 0);

    if (plugin_lib->module == NULL) {
        plugin->enabled = FALSE;
        g_warning (
            "can't load plugin '%s': %s",
            plugin_lib->location, g_module_error ());
        return -1;
    }

    /* If the plugin is disabled, we're done. */
    if (!plugin->enabled)
        return 0;

    found_symbol = g_module_symbol (
        plugin_lib->module,
        "e_plugin_lib_enable",
        (gpointer) &enable);

    if (found_symbol) {
        if (enable (plugin, TRUE) != 0) {
            plugin->enabled = FALSE;
            g_module_close (plugin_lib->module);
            plugin_lib->module = NULL;
            return -1;
        }
    }

    return 0;
}

static gpointer
plugin_lib_get_symbol (EPlugin *plugin,
                       const gchar *name)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (plugin);
    gpointer symbol;

    if (plugin_lib_loadmodule (plugin) != 0)
        return NULL;

    if (!g_module_symbol (plugin_lib->module, name, &symbol))
        return NULL;

    return symbol;
}

static gpointer
plugin_lib_invoke (EPlugin *plugin,
                   const gchar *name,
                   gpointer data)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (plugin);
    EPluginLibFunc func;

    if (!plugin->enabled) {
        g_warning (
            "Trying to invoke '%s' on disabled plugin '%s'",
            name, plugin->id);
        return NULL;
    }

    func = plugin_lib_get_symbol (plugin, name);

    if (func == NULL) {
        g_warning (
            "Cannot resolve symbol '%s' in plugin '%s' "
            "(not exported?)", name, plugin_lib->location);
        return NULL;
    }

    return func (plugin, data);
}

static gint
plugin_lib_construct (EPlugin *plugin,
                      xmlNodePtr root)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (plugin);

    /* Set the location before chaining up, as some EPluginHooks
     * will cause the module to load during hook construction. */

    plugin_lib->location = e_plugin_xml_prop (root, "location");

    if (plugin_lib->location == NULL) {
        g_warning ("Library plugin '%s' has no location", plugin->id);
        return -1;
    }
#ifdef G_OS_WIN32
    {
        gchar *mapped_location =
            e_util_replace_prefix (
                EVOLUTION_PREFIX,
                e_util_get_prefix (),
                plugin_lib->location);
        g_free (plugin_lib->location);
        plugin_lib->location = mapped_location;
    }
#endif

    /* Chain up to parent's construct() method. */
    if (E_PLUGIN_CLASS (parent_class)->construct (plugin, root) == -1)
        return -1;

    /* If we're enabled, check for the load-on-startup property */
    if (plugin->enabled) {
        xmlChar *tmp;

        tmp = xmlGetProp (root, (const guchar *)"load-on-startup");
        if (tmp) {
            if (plugin_lib_loadmodule (plugin) != 0) {
                xmlFree (tmp);
                return -1;
            }
            xmlFree (tmp);
        }
    }

    return 0;
}

static GtkWidget *
plugin_lib_get_configure_widget (EPlugin *plugin)
{
    EPluginLibGetConfigureWidgetFunc get_configure_widget;

    get_configure_widget = plugin_lib_get_symbol (
        plugin, "e_plugin_lib_get_configure_widget");

    if (get_configure_widget != NULL)
        return get_configure_widget (plugin);

    return NULL;
}

static void
plugin_lib_enable (EPlugin *plugin,
                   gint state)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (plugin);
    EPluginLibEnableFunc enable;

    E_PLUGIN_CLASS (parent_class)->enable (plugin, state);

    /* if we're disabling and it isn't loaded, nothing to do */
    if (!state && plugin_lib->module == NULL)
        return;

    enable = plugin_lib_get_symbol (plugin, "e_plugin_lib_enable");

    if (enable != NULL)
        enable (plugin, state);
}

static void
plugin_lib_finalize (GObject *object)
{
    EPluginLib *plugin_lib = E_PLUGIN_LIB (object);

    g_free (plugin_lib->location);

    if (plugin_lib->module)
        g_module_close (plugin_lib->module);

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

static void
plugin_lib_class_init (EPluginClass *class)
{
    GObjectClass *object_class;

    parent_class = g_type_class_peek_parent (class);

    object_class = G_OBJECT_CLASS (class);
    object_class->finalize = plugin_lib_finalize;

    class->construct = plugin_lib_construct;
    class->invoke = plugin_lib_invoke;
    class->get_symbol = plugin_lib_get_symbol;
    class->enable = plugin_lib_enable;
    class->get_configure_widget = plugin_lib_get_configure_widget;
    class->type = "shlib";
}

GType
e_plugin_lib_get_type (void)
{
    return plugin_lib_type;
}

void
e_plugin_lib_register_type (GTypeModule *type_module)
{
    static const GTypeInfo type_info = {
        sizeof (EPluginLibClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) plugin_lib_class_init,
        (GClassFinalizeFunc) NULL,
        NULL,  /* class_data */
        sizeof (EPluginLib),
        0,     /* n_preallocs */
        (GInstanceInitFunc) NULL,
        NULL   /* value_table */
    };

    plugin_lib_type = g_type_module_register_type (
        type_module, E_TYPE_PLUGIN,
        "EPluginLib", &type_info, 0);
}