]> Pileus Git - grits/blob - src/gis-plugin.c
Convert for expose callback to callback objects
[grits] / src / gis-plugin.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 GtkWidget *gis_plugin_get_config(GisPlugin *self)
51 {
52         if (!GIS_IS_PLUGIN(self))
53                 return NULL;
54         GisPluginInterface *iface = GIS_PLUGIN_GET_INTERFACE(self);
55         return iface->get_config ? iface->get_config (self) : NULL;
56 }
57
58
59 /***************
60  * Plugins API *
61  ***************/
62 typedef struct {
63         gchar *name;
64         GisPlugin *plugin;
65 } GisPluginStore;
66
67 GisPlugins *gis_plugins_new(gchar *dir)
68 {
69         g_debug("GisPlugins: new - dir=%s", dir);
70         GisPlugins *plugins = g_new0(GisPlugins, 1);
71         if (dir)
72                 plugins->dir = g_strdup(dir);
73         plugins->plugins = g_ptr_array_new();
74         return plugins;
75 }
76
77 void gis_plugins_free(GisPlugins *self)
78 {
79         g_debug("GisPlugins: free");
80         for (int i = 0; i < self->plugins->len; i++) {
81                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
82                 g_object_unref(store->plugin);
83                 g_free(store->name);
84                 g_free(store);
85                 g_ptr_array_remove_index(self->plugins, i);
86         }
87         g_ptr_array_free(self->plugins, TRUE);
88         if (self->dir)
89                 g_free(self->dir);
90         g_free(self);
91 }
92
93 GList *gis_plugins_available(GisPlugins *self)
94 {
95         g_debug("GisPlugins: available");
96         GList *list = NULL;
97         gchar *dirs[] = {self->dir, PLUGINSDIR};
98         g_debug("pluginsdir=%s", PLUGINSDIR);
99         for (int i = 0; i<2; i++) {
100                 GDir *dir = g_dir_open(dirs[i], 0, NULL);
101                 if (dir == NULL)
102                         continue;
103                 g_debug("            checking %s", dirs[i]);
104                 const gchar *name;
105                 while ((name = g_dir_read_name(dir))) {
106                         if (g_pattern_match_simple("*." G_MODULE_SUFFIX, name)) {
107                                 gchar **parts = g_strsplit(name, ".", 2);
108                                 list = g_list_prepend(list, g_strdup(parts[0]));
109                                 g_strfreev(parts);
110                         }
111                 }
112         }
113         return list;
114 }
115
116 GisPlugin *gis_plugins_load(GisPlugins *self, const char *name,
117                 GisViewer *viewer, GisPrefs *prefs)
118 {
119         g_debug("GisPlugins: load %s", name);
120         gchar *path = g_strdup_printf("%s/%s.%s", self->dir, name, G_MODULE_SUFFIX);
121         g_debug("GisPlugins: load - trying %s", path);
122         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
123                 g_free(path);
124                 path = g_strdup_printf("%s/%s.%s", PLUGINSDIR, name, G_MODULE_SUFFIX);
125         }
126         g_debug("GisPlugins: load - trying %s", path);
127         if (!g_file_test(path, G_FILE_TEST_EXISTS)) {
128                 g_warning("Module %s not found", name);
129                 g_free(path);
130                 return NULL;
131         }
132         GModule *module = g_module_open(path, G_MODULE_BIND_LAZY);
133         g_free(path);
134         if (module == NULL) {
135                 g_warning("Unable to load module %s: %s", name, g_module_error());
136                 return NULL;
137         }
138
139         gpointer constructor_ptr; // GCC 4.1 fix?
140         gchar *constructor_str = g_strconcat("gis_plugin_", name, "_new", NULL);
141         if (!g_module_symbol(module, constructor_str, &constructor_ptr)) {
142                 g_warning("Unable to load symbol %s from %s: %s",
143                                 constructor_str, name, g_module_error());
144                 g_module_close(module);
145                 g_free(constructor_str);
146                 return NULL;
147         }
148         g_free(constructor_str);
149         GisPluginConstructor constructor = constructor_ptr;
150
151         GisPluginStore *store = g_new0(GisPluginStore, 1);
152         store->name = g_strdup(name);
153         store->plugin = constructor(viewer, prefs);
154         g_ptr_array_add(self->plugins, store);
155         return store->plugin;
156 }
157
158 gboolean gis_plugins_unload(GisPlugins *self, const char *name)
159 {
160         g_debug("GisPlugins: unload %s", name);
161         for (int i = 0; i < self->plugins->len; i++) {
162                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
163                 if (g_str_equal(store->name, name)) {
164                         g_object_unref(store->plugin);
165                         g_free(store->name);
166                         g_free(store);
167                         g_ptr_array_remove_index(self->plugins, i);
168                 }
169         }
170         return FALSE;
171 }
172 void gis_plugins_foreach(GisPlugins *self, GCallback _callback, gpointer user_data)
173 {
174         g_debug("GisPlugins: foreach");
175         if (self == NULL)
176                 return;
177         typedef void (*CBFunc)(GisPlugin *, const gchar *, gpointer);
178         CBFunc callback = (CBFunc)_callback;
179         for (int i = 0; i < self->plugins->len; i++) {
180                 GisPluginStore *store = g_ptr_array_index(self->plugins, i);
181                 callback(store->plugin, store->name, user_data);
182         }
183 }