]> Pileus Git - grits/blob - src/gis-plugin.c
Convert self to real names
[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 *plugin)
51 {
52         if (!GIS_IS_PLUGIN(plugin))
53                 return NULL;
54         return GIS_PLUGIN_GET_INTERFACE(plugin)->name;
55 }
56
57 const gchar *gis_plugin_get_description(GisPlugin *plugin)
58 {
59         if (!GIS_IS_PLUGIN(plugin))
60                 return NULL;
61         return GIS_PLUGIN_GET_INTERFACE(plugin)->description;
62 }
63
64 GtkWidget *gis_plugin_get_config(GisPlugin *plugin)
65 {
66         if (!GIS_IS_PLUGIN(plugin))
67                 return NULL;
68         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(plugin);
69         return iface->get_config ? iface->get_config(plugin) : 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 *plugins = g_new0(GisPlugins, 1);
85         plugins->prefs = prefs;
86         if (dir)
87                 plugins->dir = g_strdup(dir);
88         return plugins;
89 }
90
91 void gis_plugins_free(GisPlugins *plugins)
92 {
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);
100                 g_free(store->name);
101                 g_free(store);
102         }
103         g_list_free(plugins->plugins);
104         if (plugins->dir)
105                 g_free(plugins->dir);
106         g_free(plugins);
107 }
108
109 GList *gis_plugins_available(GisPlugins *plugins)
110 {
111         g_debug("GisPlugins: available");
112         GList *list = NULL;
113         gchar *dirs[] = {plugins->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                 g_dir_close(dir);
131         }
132         return list;
133 }
134
135 GisPlugin *gis_plugins_load(GisPlugins *plugins, const char *name,
136                 GisViewer *viewer, GisPrefs *prefs)
137 {
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)) {
142                 g_free(path);
143                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
144         }
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);
148                 g_free(path);
149                 return NULL;
150         }
151         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
152         g_free(path);
153         if (module == NULL) {
154                 g_warning("Unable to load module %s: %s", name, g_module_error());
155                 return NULL;
156         }
157
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);
165                 return NULL;
166         }
167         g_free(constructor_str);
168         GisPluginConstructor constructor = constructor_ptr;
169
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;
175 }
176
177 GisPlugin *gis_plugins_enable(GisPlugins *plugins, const char *name,
178                 GisViewer *viewer, GisPrefs *prefs)
179 {
180         GisPlugin *plugin = gis_plugins_load(plugins, name, viewer, prefs);
181         gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, TRUE);
182         return plugin;
183 }
184
185 GList *gis_plugins_load_enabled(GisPlugins *plugins,
186                 GisViewer *viewer, GisPrefs *prefs)
187 {
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);
194                 }
195         }
196         return loaded;
197 }
198
199 gboolean gis_plugins_unload(GisPlugins *plugins, const char *name)
200 {
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);
206                         g_free(store->name);
207                         g_free(store);
208                         plugins->plugins = g_list_delete_link(plugins->plugins, cur);
209                 }
210         }
211         return FALSE;
212 }
213
214 gboolean gis_plugins_disable(GisPlugins *plugins, const char *name)
215 {
216         gis_prefs_set_boolean_v(plugins->prefs, "plugins", name, FALSE);
217         gis_plugins_unload(plugins, name);
218         return FALSE;
219 }
220
221 void gis_plugins_foreach(GisPlugins *plugins, GCallback _callback, gpointer user_data)
222 {
223         g_debug("GisPlugins: foreach");
224         if (plugins == NULL)
225                 return;
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);
231         }
232 }