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 *plugin)
52 if (!GIS_IS_PLUGIN(plugin))
54 return GIS_PLUGIN_GET_INTERFACE(plugin)->name;
57 const gchar *gis_plugin_get_description(GisPlugin *plugin)
59 if (!GIS_IS_PLUGIN(plugin))
61 return GIS_PLUGIN_GET_INTERFACE(plugin)->description;
64 GtkWidget *gis_plugin_get_config(GisPlugin *plugin)
66 if (!GIS_IS_PLUGIN(plugin))
68 GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(plugin);
69 return iface->get_config ? iface->get_config(plugin) : NULL;
81 GisPlugins *gis_plugins_new(const gchar *dir, GisPrefs *prefs)
83 g_debug("GisPlugins: new - dir=%s", dir);
84 GisPlugins *plugins = g_new0(GisPlugins, 1);
85 plugins->prefs = prefs;
87 plugins->dir = g_strdup(dir);
91 void gis_plugins_free(GisPlugins *plugins)
93 g_debug("GisPlugins: free");
94 for (GList *cur = plugins->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(plugins->plugins);
105 g_free(plugins->dir);
109 GList *gis_plugins_available(GisPlugins *plugins)
111 g_debug("GisPlugins: available");
113 gchar *dirs[] = {plugins->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]));
135 GisPlugin *gis_plugins_load(GisPlugins *plugins, const char *name,
136 GisViewer *viewer, GisPrefs *prefs)
138 g_debug("GisPlugins: load %s", name);
139 gchar *path = g_strdup_printf("%s/%s.%s", plugins->dir, name, G_MODULE_SUFFIX);
140 g_debug("GisPlugins: load - trying %s", path);
141 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
143 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
145 g_debug("GisPlugins: load - trying %s", path);
146 if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
147 g_warning("Module %s not found", name);
151 GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
153 if (module == NULL) {
154 g_warning("Unable to load module %s: %s", name, g_module_error());
158 gpointer constructor_ptr; // GCC 4.1 fix?
159 gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
160 if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
161 g_warning("Unable to load symbol %s from %s: %s",
162 constructor_str, name, g_module_error());
163 g_module_close(module);
164 g_free(constructor_str);
167 g_free(constructor_str);
168 GisPluginConstructor constructor = constructor_ptr;
170 GisPluginStore *store = g_new0(GisPluginStore, 1);
171 store->name = g_strdup(name);
172 store->plugin = constructor(viewer, prefs);
173 plugins->plugins = g_list_prepend(plugins->plugins, store);
174 return store->plugin;
177 GisPlugin *gis_plugins_enable(GisPlugins *plugins, const char *name,
178 GisViewer *viewer, GisPrefs *prefs)
180 GisPlugin *plugin = gis_plugins_load(plugins, name, viewer, prefs);
181 gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, TRUE);
185 GList *gis_plugins_load_enabled(GisPlugins *plugins,
186 GisViewer *viewer, GisPrefs *prefs)
188 GList *loaded = NULL;
189 for (GList *cur = gis_plugins_available(plugins); cur; cur = cur->next) {
190 gchar *name = cur->data;
191 if (gis_prefs_get_boolean_v(plugins->prefs, "plugins", name, NULL)) {
192 GisPlugin *plugin = gis_plugins_load(plugins, name, viewer, prefs);
193 loaded = g_list_prepend(loaded, plugin);
199 gboolean gis_plugins_unload(GisPlugins *plugins, const char *name)
201 g_debug("GisPlugins: unload %s", name);
202 for (GList *cur = plugins->plugins; cur; cur = cur->next) {
203 GisPluginStore *store = cur->data;
204 if (g_str_equal(store->name, name)) {
205 g_object_unref(store->plugin);
208 plugins->plugins = g_list_delete_link(plugins->plugins, cur);
214 gboolean gis_plugins_disable(GisPlugins *plugins, const char *name)
216 gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, FALSE);
217 gis_plugins_unload(plugins, name);
221 void gis_plugins_foreach(GisPlugins *plugins, GCallback _callback, gpointer user_data)
223 g_debug("GisPlugins: foreach");
226 typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
227 CBFunc callback = (CBFunc)_callback;
228 for (GList *cur = plugins->plugins; cur; cur = cur->next) {
229 GisPluginStore *store = cur->data;
230 callback(store->plugin, store->name, user_data);