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 GtkWidget *gis_plugin_get_config(GisPlugin *self)
52 if (!GIS_IS_PLUGIN(self))
54 GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
55 return iface->get_config ? iface->get_config (self) : NULL;
67 GisPlugins *gis_plugins_new(gchar *dir)
69 g_debug("GisPlugins: new - dir=%s", dir);
70 GisPlugins *plugins = g_new0(GisPlugins, 1);
72 plugins->dir = g_strdup(dir);
76 void gis_plugins_free(GisPlugins *self)
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);
88 g_list_free(self->plugins);
94 GList *gis_plugins_available(GisPlugins *self)
96 g_debug("GisPlugins: available");
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);
104 g_debug(" checking %s", dirs[i]);
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]));
117 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
118 GisViewer *viewer, GisPrefs *prefs)
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)) {
125 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
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);
133 GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
135 if (module == NULL) {
136 g_warning("Unable to load module %s: %s", name, g_module_error());
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);
149 g_free(constructor_str);
150 GisPluginConstructor constructor = constructor_ptr;
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;
159 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
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);
168 self->plugins = g_list_delete_link(self->plugins, cur);
173 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
175 g_debug("GisPlugins: foreach");
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);