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