]> Pileus Git - grits/blob - src/gis-plugin.c
Miscellaneous bug fixes
[grits] / src / gis-plugin.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 /**
19  * SECTION:gis-plugin
20  * @short_description: Plugin support
21  *
22  * A plugin in libgis is a GObject which implements the GisPlugin interface. Additionally, each
23  * plugin is compiled to a separate shared object and loaded conditionally at runtime when the
24  * plugin is enabled. Each such shared object should define a GisPluginConstructor() function named
25  * gis_plugin_NAME_new which will be called when loading the plugin.
26  *
27  * Almost all libgis functionality is provided by a set of plugins. Each plugin can how however much
28  * it likes. The interface between plugins and the rest of libgis is intentionally very thin. Since
29  * libgis is the library, plugins must manually do everything. For instance, to draw something in
30  * the world, the plugin must add an object to the viewer. Likewise, plugins need to register
31  * callbacks on the viewer in order to receive updates, very little happens automagically.
32  *
33  * That being said, one thing that plugins do do automagically, is provide a configuration area.
34  * Since the plugin doesn't know what application is is being loaded form, it is better for the
35  * application to ask the plugin for it's confirmation area, not the other way around.
36  */
37
38 #include <glib.h>
39 #include <gmodule.h>
40
41 #include "gis-plugin.h"
42
43 /********************
44  * Plugin interface *
45  ********************/
46 static void gis_plugin_base_init(gpointer g_class)
47 {
48         static gboolean is_initialized = FALSE;
49         if (!is_initialized) {
50                 /* add properties and signals to the interface here */
51                 is_initialized = TRUE;
52         }
53 }
54
55 GType gis_plugin_get_type()
56 {
57         static GType type = 0;
58         if (type == 0) {
59                 static const GTypeInfo info = {
60                         sizeof(GisPluginInterface),
61                         gis_plugin_base_init,
62                         NULL,
63                 };
64                 type = g_type_register_static(G_TYPE_INTERFACE,
65                                 "GisPlugin", &info, 0);
66         }
67         return type;
68 }
69
70 /**
71  * gis_plugin_get_name:
72  * @plugin: the plugin
73  *
74  * Get a short human readable name for a plugin, this is not necessarily the
75  * same as the name of the shared object.
76  *
77  * Returns: a short name for the plugin
78  */
79 const gchar *gis_plugin_get_name(GisPlugin *plugin)
80 {
81         if (!GIS_IS_PLUGIN(plugin))
82                 return NULL;
83         return GIS_PLUGIN_GET_INTERFACE(plugin)->name;
84 }
85
86 /**
87  * gis_plugin_get_description:
88  * @plugin: the plugin
89  *
90  * Get a description of a plugin
91  *
92  * Returns: a description of the plugin
93  */
94 const gchar *gis_plugin_get_description(GisPlugin *plugin)
95 {
96         if (!GIS_IS_PLUGIN(plugin))
97                 return NULL;
98         return GIS_PLUGIN_GET_INTERFACE(plugin)->description;
99 }
100
101 /**
102  * gis_plugin_get_config:
103  * @plugin: the plugin
104  *
105  * Each plugin can provide a configuration area. Applications using libgis
106  * should display this configuration area to the user so they can modify the
107  * behavior of the plugin.
108  *
109  * Returns: a configuration widget for the plugin
110  */
111 GtkWidget *gis_plugin_get_config(GisPlugin *plugin)
112 {
113         if (!GIS_IS_PLUGIN(plugin))
114                 return NULL;
115         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(plugin);
116         return iface->get_config ? iface->get_config(plugin) : NULL;
117 }
118
119
120 /***************
121  * Plugins API *
122  ***************/
123 typedef struct {
124         gchar *name;
125         GisPlugin *plugin;
126 } GisPluginStore;
127
128 /**
129  * gis_plugins_new:
130  * @dir:   the directory to search for plugins in
131  * @prefs: a #GisPrefs to save the state of plugins, or NULL
132  *
133  * Create a new plugin source. If @prefs is not %NULL, the state of the plugins
134  * will be saved when they are either enabled or disabled.
135  *
136  * Returns: the new plugin source
137  */
138 GisPlugins *gis_plugins_new(const gchar *dir, GisPrefs *prefs)
139 {
140         g_debug("GisPlugins: new - dir=%s", dir);
141         GisPlugins *plugins = g_new0(GisPlugins, 1);
142         plugins->prefs = prefs;
143         if (dir)
144                 plugins->dir = g_strdup(dir);
145         return plugins;
146 }
147
148 /**
149  * gis_plugins_free:
150  * @plugins: the #GisPlugins to free
151  *
152  * Free data used by a plugin source
153  */
154 void gis_plugins_free(GisPlugins *plugins)
155 {
156         g_debug("GisPlugins: free");
157         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
158                 GisPluginStore *store = cur->data;
159                 g_debug("GisPlugin: freeing %s refs=%d->%d", store->name,
160                         G_OBJECT(store->plugin)->ref_count,
161                         G_OBJECT(store->plugin)->ref_count-1);
162                 g_object_unref(store->plugin);
163                 g_free(store->name);
164                 g_free(store);
165         }
166         g_list_free(plugins->plugins);
167         if (plugins->dir)
168                 g_free(plugins->dir);
169         g_free(plugins);
170 }
171
172 /**
173  * gis_plugins_available:
174  * @plugins: the plugin source
175  *
176  * Search the plugin directory for shared objects which can be loaded as
177  * plugins.
178  *
179  * Returns: the list of available plugins
180  */
181 GList *gis_plugins_available(GisPlugins *plugins)
182 {
183         g_debug("GisPlugins: available");
184         GList *list = NULL;
185         gchar *dirs[] = {plugins->dir, PLUGINSDIR};
186         g_debug("pluginsdir=%s", PLUGINSDIR);
187         for (int i = 0; i<2; i++) {
188                 if (dirs[i] == NULL)
189                         continue;
190                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
191                 if (dir == NULL)
192                         continue;
193                 g_debug("            checking %s", dirs[i]);
194                 const gchar *name;
195                 while ((name = g_dir_read_name(dir))) {
196                         if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
197                                 gchar **parts = g_strsplit(name, ".", 2);
198                                 list = g_list_prepend(list, g_strdup(parts[0]));
199                                 g_strfreev(parts);
200                         }
201                 }
202                 g_dir_close(dir);
203         }
204         list = g_list_sort(list, (GCompareFunc)g_strcmp0);
205         for (GList *cur = list; cur; cur = cur->next)
206                 while (cur->next && g_str_equal(cur->data,cur->next->data)) {
207                         GList *tmp = cur->next;
208                         list = g_list_remove_link(list, cur);
209                         cur = tmp;
210                 }
211         return list;
212 }
213
214 /**
215  * gis_plugins_load:
216  * @plugins: the plugins source
217  * @name:    the name of the plugin to load
218  * @viewer:  a #GisViewer to pass to the plugins constructor
219  * @prefs:   a #GisPrefs to pass to the plugins constructor
220  *
221  * @name should be the name of the shared object without the file extension.
222  * This is the same as what is returned by gis_plugins_available().
223  *
224  * When loading plugins, the @prefs argument is used, not the #GisPrefs stored
225  * in @plugins.
226  *
227  * Returns: the new plugin
228  */
229 GisPlugin *gis_plugins_load(GisPlugins *plugins, const char *name,
230                 GisViewer *viewer, GisPrefs *prefs)
231 {
232         g_debug("GisPlugins: load %s", name);
233         gchar *path = g_strdup_printf("%s/%s.%s", plugins->dir, name, G_MODULE_SUFFIX);
234         g_debug("GisPlugins: load - trying %s", path);
235         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
236                 g_free(path);
237                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
238         }
239         g_debug("GisPlugins: load - trying %s", path);
240         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
241                 g_warning("Module %s not found", name);
242                 g_free(path);
243                 return NULL;
244         }
245         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
246         g_free(path);
247         if (module == NULL) {
248                 g_warning("Unable to load module %s: %s", name, g_module_error());
249                 return NULL;
250         }
251
252         gpointer constructor_ptr; // GCC 4.1 fix?
253         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
254         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
255                 g_warning("Unable to load symbol %s from %s: %s",
256                                 constructor_str, name, g_module_error());
257                 g_module_close(module);
258                 g_free(constructor_str);
259                 return NULL;
260         }
261         g_free(constructor_str);
262         GisPluginConstructor constructor = constructor_ptr;
263
264         GisPluginStore *store = g_new0(GisPluginStore, 1);
265         store->name = g_strdup(name);
266         store->plugin = constructor(viewer, prefs);
267         plugins->plugins = g_list_prepend(plugins->plugins, store);
268         return store->plugin;
269 }
270
271 /**
272  * gis_plugins_enable:
273  * @plugins: the plugins source
274  * @name:    the name of the plugin to load
275  * @viewer:  a #GisViewer to pass to the plugins constructor
276  * @prefs:   a #GisPrefs to pass to the plugins constructor
277  *
278  * Load a plugin and save it's loaded/unloaded state in the #GisPrefs stored in
279  * #plugins.
280  *
281  * See also: gis_plugins_load()
282  *
283  * Returns: the new plugin
284  */
285 GisPlugin *gis_plugins_enable(GisPlugins *plugins, const char *name,
286                 GisViewer *viewer, GisPrefs *prefs)
287 {
288         GisPlugin *plugin = gis_plugins_load(plugins, name, viewer, prefs);
289         gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, TRUE);
290         return plugin;
291 }
292
293 /**
294  * gis_plugins_load_enabled:
295  * @plugins: the plugins source
296  * @viewer:  a #GisViewer to pass to the plugins constructor
297  * @prefs:   a #GisPrefs to pass to the plugins constructor
298  *
299  * Load all enabled which have previously been enabled.
300  *
301  * See also: gis_plugins_load()
302  *
303  * Returns: a list of all loaded plugins
304  */
305 GList *gis_plugins_load_enabled(GisPlugins *plugins,
306                 GisViewer *viewer, GisPrefs *prefs)
307 {
308         GList *loaded = NULL;
309         for (GList *cur = gis_plugins_available(plugins); cur; cur = cur->next) {
310                 gchar *name = cur->data;
311                 if (gis_prefs_get_boolean_v(plugins->prefs, "plugins", name, NULL)) {
312                         GisPlugin *plugin = gis_plugins_load(plugins, name, viewer, prefs);
313                         loaded = g_list_prepend(loaded, plugin);
314                 }
315         }
316         return loaded;
317 }
318
319 /**
320  * gis_plugins_unload:
321  * @plugins: the plugins source
322  * @name:    the name of the plugin to unload
323  *
324  * Unload a plugin and free any associated data.
325  *
326  * Returns: %FALSE
327  */
328 gboolean gis_plugins_unload(GisPlugins *plugins, const char *name)
329 {
330         g_debug("GisPlugins: unload %s", name);
331         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
332                 GisPluginStore *store = cur->data;
333                 if (g_str_equal(store->name, name)) {
334                         g_object_unref(store->plugin);
335                         g_free(store->name);
336                         g_free(store);
337                         plugins->plugins = g_list_delete_link(plugins->plugins, cur);
338                 }
339         }
340         return FALSE;
341 }
342
343 /**
344  * gis_plugins_disable:
345  * @plugins: the plugins source
346  * @name:    the name of the plugin to unload
347  *
348  * Unload a plugin and save it's loaded/unloaded state in the #GisPrefs stored
349  * in #plugins.
350  *
351  * See also: gis_plugins_unload()
352  *
353  * Returns: %FALSE
354  */
355 gboolean gis_plugins_disable(GisPlugins *plugins, const char *name)
356 {
357         gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, FALSE);
358         gis_plugins_unload(plugins, name);
359         return FALSE;
360 }
361
362 /**
363  * gis_plugins_foreach:
364  * @plugins:   the plugins source
365  * @callback:  a function to call on each plugin
366  * @user_data: user data to pass to the function
367  *
368  * Iterate over all plugins loaded by the plugins source
369  */
370 void gis_plugins_foreach(GisPlugins *plugins, GCallback _callback, gpointer user_data)
371 {
372         g_debug("GisPlugins: foreach");
373         if (plugins == NULL)
374                 return;
375         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
376         CBFunc callback = (CBFunc)_callback;
377         for (GList *cur = plugins->plugins; cur; cur = cur->next) {
378                 GisPluginStore *store = cur->data;
379                 callback(store->plugin, store->name, user_data);
380         }
381 }