b934ba6bcbbfe19817425091d96ae203e2c0da0c
[tweakr] / libmaemo-tweaks-section / maemo-tweaks-module.c
1 /*
2  * vim:ts=4:sw=4:et:cindent:cino=(0
3  */ 
4
5 #include <config.h>
6 #include <gmodule.h>
7
8 #include "maemo-tweaks-module.h"
9
10
11 enum
12 {
13     PROP_0,
14     PROP_FILENAME
15 };
16
17
18 static void      maemo_tweaks_module_finalize      (GObject      *object);
19 static void      maemo_tweaks_module_get_property  (GObject      *object,
20                                                     guint         param_id,
21                                                     GValue       *value,
22                                                     GParamSpec   *pspec);
23 static void      maemo_tweaks_module_set_property  (GObject      *object,
24                                                     guint         param_id,
25                                                     const GValue *value,
26                                                     GParamSpec   *pspec);
27 static gboolean  maemo_tweaks_module_load_module   (GTypeModule  *gmodule);
28 static void      maemo_tweaks_module_unload_module (GTypeModule  *gmodule);
29
30
31 G_DEFINE_TYPE (MaemoTweaksModule, maemo_tweaks_module, G_TYPE_TYPE_MODULE);
32
33
34 static void
35 maemo_tweaks_module_class_init (MaemoTweaksModuleClass *class)
36 {
37     GObjectClass     *object_class      = G_OBJECT_CLASS (class);
38     GTypeModuleClass *type_module_class = G_TYPE_MODULE_CLASS (class);
39
40     object_class->finalize     = maemo_tweaks_module_finalize;
41     object_class->get_property = maemo_tweaks_module_get_property;
42     object_class->set_property = maemo_tweaks_module_set_property;
43
44     type_module_class->load    = maemo_tweaks_module_load_module;
45     type_module_class->unload  = maemo_tweaks_module_unload_module;
46
47     g_object_class_install_property
48         (object_class, PROP_FILENAME,
49          g_param_spec_string ("filename",
50                               "Filename",
51                               "The filaname of the module",
52                               NULL,
53                               G_PARAM_READWRITE |
54                               G_PARAM_CONSTRUCT_ONLY));
55 }
56
57 static void
58 maemo_tweaks_module_init (MaemoTweaksModule *module)
59 {
60     module->filename = NULL;
61     module->library  = NULL;
62     module->load     = NULL;
63     module->unload   = NULL;
64 }
65
66 static void
67 maemo_tweaks_module_finalize (GObject *object)
68 {
69     MaemoTweaksModule *module = MAEMO_TWEAKS_MODULE (object);
70
71     g_free (module->filename);
72
73     G_OBJECT_CLASS (maemo_tweaks_module_parent_class)->finalize (object);
74 }
75
76 static void
77 maemo_tweaks_module_get_property (GObject    *object,
78                                   guint       param_id,
79                                   GValue     *value,
80                                   GParamSpec *pspec)
81 {
82     MaemoTweaksModule *module = MAEMO_TWEAKS_MODULE (object);
83
84     switch (param_id)
85     {
86         case PROP_FILENAME:
87             g_value_set_string (value, module->filename);
88             break;
89         default:
90             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
91             break;
92     }
93 }
94
95 static void
96 maemo_tweaks_module_set_property (GObject      *object,
97                                   guint         param_id,
98                                   const GValue *value,
99                                   GParamSpec   *pspec)
100 {
101     MaemoTweaksModule *module = MAEMO_TWEAKS_MODULE (object);
102
103     switch (param_id)
104     {
105         case PROP_FILENAME:
106             g_free (module->filename);
107             module->filename = g_value_dup_string (value);
108             break;
109         default:
110             G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
111             break;
112     }
113 }
114
115 static gboolean
116 maemo_tweaks_module_load_module (GTypeModule *gmodule)
117 {
118     MaemoTweaksModule *module = MAEMO_TWEAKS_MODULE (gmodule);
119
120     if (!module->filename)
121     {
122         g_warning ("Module path not set");
123         return FALSE;
124     }
125
126     module->library = g_module_open (module->filename, 0);
127
128     if (!module->library)
129     {
130         g_printerr ("%s\n", g_module_error ());
131         return FALSE;
132     }
133
134     /* Make sure that the loaded library contains the required methods */
135     if (! g_module_symbol (module->library,
136                            "maemo_tweaks_module_load",
137                            (gpointer) &module->load) ||
138         ! g_module_symbol (module->library,
139                            "maemo_tweaks_module_unload",
140                            (gpointer) &module->unload))
141     {
142         g_printerr ("%s\n", g_module_error ());
143         g_module_close (module->library);
144
145         return FALSE;
146     }
147
148     /* Initialize the loaded module */
149     module->load (module);
150
151     return TRUE;
152 }
153
154 static void
155 maemo_tweaks_module_unload_module (GTypeModule *gmodule)
156 {
157     MaemoTweaksModule *module = MAEMO_TWEAKS_MODULE (gmodule);
158
159     module->unload (module);
160
161     g_module_close (module->library);
162     module->library = NULL;
163
164     module->load   = NULL;
165     module->unload = NULL;
166 }
167
168 MaemoTweaksModule *
169 maemo_tweaks_module_new (const gchar *filename)
170 {
171     MaemoTweaksModule *module;
172
173     g_return_val_if_fail (filename != NULL, NULL);
174
175     module = g_object_new (MAEMO_TWEAKS_TYPE_MODULE,
176                            "filename", filename,
177                            NULL);
178
179     return module;
180 }
181