2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
21 #include "gis-plugin.h"
26 static void gis_plugin_base_init(gpointer g_class)
28 static gboolean is_initialized = FALSE;
29 if (!is_initialized) {
30 /* add properties and signals to the interface here */
31 is_initialized = TRUE;
35 GType gis_plugin_get_type()
37 static GType type = 0;
39 static const GTypeInfo info = {
40 sizeof(GisPluginInterface),
44 type = g_type_register_static(G_TYPE_INTERFACE,
45 "GisPlugin", &info, 0);
50 const gchar *gis_plugin_get_name(GisPlugin *self)
52 if (!GIS_IS_PLUGIN(self))
54 return GIS_PLUGIN_GET_INTERFACE(self)->name;
57 const gchar *gis_plugin_get_description(GisPlugin *self)
59 if (!GIS_IS_PLUGIN(self))
61 return GIS_PLUGIN_GET_INTERFACE(self)->description;
64 GtkWidget *gis_plugin_get_config(GisPlugin *self)
66 if (!GIS_IS_PLUGIN(self))
68 GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
69 return iface->get_config ? iface->get_config(self) : NULL;
81 GisPlugins *gis_plugins_new(const gchar *dir, GisPrefs *prefs)
83 g_debug("GisPlugins: new - dir=%s", dir);
84 GisPlugins *self = g_new0(GisPlugins, 1);
87 self->dir = g_strdup(dir);
91 void gis_plugins_free(GisPlugins *self)
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);
103 g_list_free(self->plugins);
109 GList *gis_plugins_available(GisPlugins *self)
111 g_debug("GisPlugins: available");
113 gchar *dirs[] = {self->dir, PLUGINSDIR};
114 g_debug("pluginsdir=%s", PLUGINSDIR);
115 for (int i = 0; i<2; i++) {
118 GDir *dir = g_dir_open(dirs[i], 0, NULL);
121 g_debug(" checking %s", dirs[i]);
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]));
134 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
135 GisViewer *viewer, GisPrefs *prefs)
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)) {
142 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
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);
150 GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
152 if (module == NULL) {
153 g_warning("Unable to load module %s: %s", name, g_module_error());
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);
166 g_free(constructor_str);
167 GisPluginConstructor constructor = constructor_ptr;
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;
176 GisPlugin *gis_plugins_enable(GisPlugins *self, const char *name,
177 GisViewer *viewer, GisPrefs *prefs)
179 GisPlugin *plugin = gis_plugins_load(self, name, viewer, prefs);
180 gis_prefs_set_boolean_v(self->prefs, "plugins", name, TRUE);
184 GList *gis_plugins_load_enabled(GisPlugins *self,
185 GisViewer *viewer, GisPrefs *prefs)
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);
198 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
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);
207 self->plugins = g_list_delete_link(self->plugins, cur);
213 gboolean gis_plugins_disable(GisPlugins *self, const char *name)
215 gis_prefs_set_boolean_v(self->prefs, "plugins", name, FALSE);
216 gis_plugins_unload(self, name);
220 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
222 g_debug("GisPlugins: foreach");
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);