]> Pileus Git - grits/blob - src/gis-plugin.c
Update copyright and email address
[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 #include <glib.h>
19 #include <gmodule.h>
20
21 #include "gis-plugin.h"
22
23 /********************
24  * Plugin interface *
25  ********************/
26 static void gis_plugin_base_init(gpointer g_class)
27 {
28         static gboolean is_initialized = FALSE;
29         if (!is_initialized) {
30                 /* add properties and signals to the interface here */
31                 is_initialized = TRUE;
32         }
33 }
34
35 GType gis_plugin_get_type()
36 {
37         static GType type = 0;
38         if (type == 0) {
39                 static const GTypeInfo info = {
40                         sizeof(GisPluginInterface),
41                         gis_plugin_base_init,
42                         NULL,
43                 };
44                 type = g_type_register_static(G_TYPE_INTERFACE,
45                                 "GisPlugin", &info, 0);
46         }
47         return type;
48 }
49
50 GtkWidget *gis_plugin_get_config(GisPlugin *self)
51 {
52         if (!GIS_IS_PLUGIN(self))
53                 return NULL;
54         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
55         return iface->get_config ? iface->get_config (self) : NULL;
56 }
57
58
59 /***************
60  * Plugins API *
61  ***************/
62 typedef struct {
63         gchar *name;
64         GisPlugin *plugin;
65 } GisPluginStore;
66
67 GisPlugins *gis_plugins_new(gchar *dir)
68 {
69         g_debug("GisPlugins: new - dir=%s", dir);
70         GisPlugins *plugins = g_new0(GisPlugins, 1);
71         if (dir)
72                 plugins->dir = g_strdup(dir);
73         return plugins;
74 }
75
76 void gis_plugins_free(GisPlugins *self)
77 {
78         g_debug("GisPlugins: free");
79         for (GList *cur = self->plugins; cur; cur = cur->next) {
80                 GisPluginStore *store = cur->data;
81                 g_debug("GisPlugin: freeing %s refs=%d->%d", store->name,
82                         G_OBJECT(store->plugin)->ref_count,
83                         G_OBJECT(store->plugin)->ref_count-1);
84                 g_object_unref(store->plugin);
85                 g_free(store->name);
86                 g_free(store);
87         }
88         g_list_free(self->plugins);
89         if (self->dir)
90                 g_free(self->dir);
91         g_free(self);
92 }
93
94 GList *gis_plugins_available(GisPlugins *self)
95 {
96         g_debug("GisPlugins: available");
97         GList *list = NULL;
98         gchar *dirs[] = {self->dir, PLUGINSDIR};
99         g_debug("pluginsdir=%s", PLUGINSDIR);
100         for (int i = 0; i<2; i++) {
101                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
102                 if (dir == NULL)
103                         continue;
104                 g_debug("            checking %s", dirs[i]);
105                 const gchar *name;
106                 while ((name = g_dir_read_name(dir))) {
107                         if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
108                                 gchar **parts = g_strsplit(name, ".", 2);
109                                 list = g_list_prepend(list, g_strdup(parts[0]));
110                                 g_strfreev(parts);
111                         }
112                 }
113         }
114         return list;
115 }
116
117 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
118                 GisViewer *viewer, GisPrefs *prefs)
119 {
120         g_debug("GisPlugins: load %s", name);
121         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
122         g_debug("GisPlugins: load - trying %s", path);
123         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
124                 g_free(path);
125                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
126         }
127         g_debug("GisPlugins: load - trying %s", path);
128         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
129                 g_warning("Module %s not found", name);
130                 g_free(path);
131                 return NULL;
132         }
133         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
134         g_free(path);
135         if (module == NULL) {
136                 g_warning("Unable to load module %s: %s", name, g_module_error());
137                 return NULL;
138         }
139
140         gpointer constructor_ptr; // GCC 4.1 fix?
141         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
142         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
143                 g_warning("Unable to load symbol %s from %s: %s",
144                                 constructor_str, name, g_module_error());
145                 g_module_close(module);
146                 g_free(constructor_str);
147                 return NULL;
148         }
149         g_free(constructor_str);
150         GisPluginConstructor constructor = constructor_ptr;
151
152         GisPluginStore *store = g_new0(GisPluginStore, 1);
153         store->name = g_strdup(name);
154         store->plugin = constructor(viewer, prefs);
155         self->plugins = g_list_prepend(self->plugins, store);
156         return store->plugin;
157 }
158
159 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
160 {
161         g_debug("GisPlugins: unload %s", name);
162         for (GList *cur = self->plugins; cur; cur = cur->next) {
163                 GisPluginStore *store = cur->data;
164                 if (g_str_equal(store->name, name)) {
165                         g_object_unref(store->plugin);
166                         g_free(store->name);
167                         g_free(store);
168                         self->plugins = g_list_delete_link(self->plugins, cur);
169                 }
170         }
171         return FALSE;
172 }
173 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
174 {
175         g_debug("GisPlugins: foreach");
176         if (self == NULL)
177                 return;
178         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
179         CBFunc callback = (CBFunc)_callback;
180         for (GList *cur = self->plugins; cur; cur = cur->next) {
181                 GisPluginStore *store = cur->data;
182                 callback(store->plugin, store->name, user_data);
183         }
184 }