]> Pileus Git - grits/blob - src/gis-plugin.c
Add better suppressions file
[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 const gchar *gis_plugin_get_name(GisPlugin *self)
51 {
52         if (!GIS_IS_PLUGIN(self))
53                 return NULL;
54         return GIS_PLUGIN_GET_INTERFACE(self)->name;
55 }
56
57 const gchar *gis_plugin_get_description(GisPlugin *self)
58 {
59         if (!GIS_IS_PLUGIN(self))
60                 return NULL;
61         return GIS_PLUGIN_GET_INTERFACE(self)->description;
62 }
63
64 GtkWidget *gis_plugin_get_config(GisPlugin *self)
65 {
66         if (!GIS_IS_PLUGIN(self))
67                 return NULL;
68         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
69         return iface->get_config ? iface->get_config(self) : NULL;
70 }
71
72
73 /***************
74  * Plugins API *
75  ***************/
76 typedef struct {
77         gchar *name;
78         GisPlugin *plugin;
79 } GisPluginStore;
80
81 GisPlugins *gis_plugins_new(const gchar *dir, GisPrefs *prefs)
82 {
83         g_debug("GisPlugins: new - dir=%s", dir);
84         GisPlugins *self = g_new0(GisPlugins, 1);
85         self->prefs = prefs;
86         if (dir)
87                 self->dir = g_strdup(dir);
88         return self;
89 }
90
91 void gis_plugins_free(GisPlugins *self)
92 {
93         g_debug("GisPlugins: free");
94         for (GList *cur = self->plugins; cur; cur = cur->next) {
95                 GisPluginStore *store = cur->data;
96                 g_debug("GisPlugin: freeing %s refs=%d->%d", store->name,
97                         G_OBJECT(store->plugin)->ref_count,
98                         G_OBJECT(store->plugin)->ref_count-1);
99                 g_object_unref(store->plugin);
100                 g_free(store->name);
101                 g_free(store);
102         }
103         g_list_free(self->plugins);
104         if (self->dir)
105                 g_free(self->dir);
106         g_free(self);
107 }
108
109 GList *gis_plugins_available(GisPlugins *self)
110 {
111         g_debug("GisPlugins: available");
112         GList *list = NULL;
113         gchar *dirs[] = {self->dir, PLUGINSDIR};
114         g_debug("pluginsdir=%s", PLUGINSDIR);
115         for (int i = 0; i<2; i++) {
116                 if (dirs[i] == NULL)
117                         continue;
118                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
119                 if (dir == NULL)
120                         continue;
121                 g_debug("            checking %s", dirs[i]);
122                 const gchar *name;
123                 while ((name = g_dir_read_name(dir))) {
124                         if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
125                                 gchar **parts = g_strsplit(name, ".", 2);
126                                 list = g_list_prepend(list, g_strdup(parts[0]));
127                                 g_strfreev(parts);
128                         }
129                 }
130         }
131         return list;
132 }
133
134 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
135                 GisViewer *viewer, GisPrefs *prefs)
136 {
137         g_debug("GisPlugins: load %s", name);
138         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
139         g_debug("GisPlugins: load - trying %s", path);
140         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
141                 g_free(path);
142                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
143         }
144         g_debug("GisPlugins: load - trying %s", path);
145         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
146                 g_warning("Module %s not found", name);
147                 g_free(path);
148                 return NULL;
149         }
150         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
151         g_free(path);
152         if (module == NULL) {
153                 g_warning("Unable to load module %s: %s", name, g_module_error());
154                 return NULL;
155         }
156
157         gpointer constructor_ptr; // GCC 4.1 fix?
158         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
159         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
160                 g_warning("Unable to load symbol %s from %s: %s",
161                                 constructor_str, name, g_module_error());
162                 g_module_close(module);
163                 g_free(constructor_str);
164                 return NULL;
165         }
166         g_free(constructor_str);
167         GisPluginConstructor constructor = constructor_ptr;
168
169         GisPluginStore *store = g_new0(GisPluginStore, 1);
170         store->name = g_strdup(name);
171         store->plugin = constructor(viewer, prefs);
172         self->plugins = g_list_prepend(self->plugins, store);
173         return store->plugin;
174 }
175
176 GisPlugin *gis_plugins_enable(GisPlugins *self, const char *name,
177                 GisViewer *viewer, GisPrefs *prefs)
178 {
179         GisPlugin *plugin = gis_plugins_load(self, name, viewer, prefs);
180         gis_prefs_set_boolean_v(self->prefs, "plugins", name, TRUE);
181         return plugin;
182 }
183
184 GList *gis_plugins_load_enabled(GisPlugins *self,
185                 GisViewer *viewer, GisPrefs *prefs)
186 {
187         GList *loaded = NULL;
188         for (GList *cur = gis_plugins_available(self); cur; cur = cur->next) {
189                 gchar *name = cur->data;
190                 if (gis_prefs_get_boolean_v(self->prefs, "plugins", name, NULL)) {
191                         GisPlugin *plugin = gis_plugins_load(self, name, viewer, prefs);
192                         loaded = g_list_prepend(loaded, plugin);
193                 }
194         }
195         return loaded;
196 }
197
198 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
199 {
200         g_debug("GisPlugins: unload %s", name);
201         for (GList *cur = self->plugins; cur; cur = cur->next) {
202                 GisPluginStore *store = cur->data;
203                 if (g_str_equal(store->name, name)) {
204                         g_object_unref(store->plugin);
205                         g_free(store->name);
206                         g_free(store);
207                         self->plugins = g_list_delete_link(self->plugins, cur);
208                 }
209         }
210         return FALSE;
211 }
212
213 gboolean gis_plugins_disable(GisPlugins *self, const char *name)
214 {
215         gis_prefs_set_boolean_v(self->prefs, "plugins", name, FALSE);
216         gis_plugins_unload(self, name);
217         return FALSE;
218 }
219
220 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
221 {
222         g_debug("GisPlugins: foreach");
223         if (self == NULL)
224                 return;
225         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
226         CBFunc callback = (CBFunc)_callback;
227         for (GList *cur = self->plugins; cur; cur = cur->next) {
228                 GisPluginStore *store = cur->data;
229                 callback(store->plugin, store->name, user_data);
230         }
231 }