Removing Daniel Baumann from uploaders (Closes: #704335).
[debian/lxappearance.git] / src / plugin.c
1 // plugin.c
2 //
3 // Copyright 2010 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
4 //
5 // This program is free software; you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation; either version 2 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 // MA 02110-1301, USA.
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include "plugin.h"
25 #include "lxappearance.h"
26 #include <gmodule.h>
27
28 #define PLUGIN_DIR PACKAGE_LIB_DIR"/lxappearance/plugins"
29
30 typedef gboolean (*PluginLoadFunc)(LXAppearance*, GtkBuilder*);
31 typedef void (*PluginUnloadFunc)(LXAppearance*);
32
33 typedef struct _Plugin Plugin;
34 struct _Plugin
35 {
36 GModule* module;
37 PluginLoadFunc load;
38 PluginUnloadFunc unload;
39 };
40
41 static GSList* plugins = NULL;
42
43 void plugins_init(GtkBuilder* builder)
44 {
45 GDir* dir = g_dir_open(PLUGIN_DIR, 0, NULL);
46 const char* name = NULL;
47 if(!dir)
48 return;
49 while(name = g_dir_read_name(dir))
50 {
51 if(g_str_has_suffix(name, ".so"))
52 {
53 char* file = g_build_filename(PLUGIN_DIR, name, NULL);
54 GModule* mod = g_module_open(file, 0);
55 g_free(file);
56 if(mod)
57 {
58 PluginLoadFunc load;
59 gboolean loaded = FALSE;
60 g_debug("module: %s", g_module_name(mod));
61 if(g_module_symbol(mod, "plugin_load", (gpointer*)&load))
62 loaded = load(&app, builder);
63 if(loaded)
64 {
65 Plugin* plugin = g_slice_new0(Plugin);
66 plugin->module = mod;
67 plugin->load = load;
68 g_module_symbol(mod, "plugin_unload", (gpointer*)&plugin->unload);
69 plugins = g_slist_prepend(plugins, plugin);
70 }
71 else
72 g_module_close(mod);
73 }
74 else
75 g_debug("open failed: %s\n%s", name, g_module_error());
76 }
77 }
78 g_dir_close(dir);
79 }
80
81 void plugins_finalize()
82 {
83 GSList* l;
84 for(l = plugins; l; l=l->next)
85 {
86 Plugin* plugin = (Plugin*)l->data;
87 if(plugin->unload)
88 plugin->unload(&app);
89 g_module_close(plugin->module);
90 g_slice_free(Plugin, plugin);
91 }
92 g_slist_free(plugins);
93 }