Add module manager.
authorSalvatore Iovene <salvatore@iovene.com>
Sun, 6 Dec 2009 11:26:45 +0000 (13:26 +0200)
committerSalvatore Iovene <salvatore@iovene.com>
Sun, 6 Dec 2009 11:26:45 +0000 (13:26 +0200)
maemo-tweaks-module-manager.c [new file with mode: 0644]
maemo-tweaks-module-manager.h [new file with mode: 0644]
maemo-tweaks-types.h [new file with mode: 0644]

diff --git a/maemo-tweaks-module-manager.c b/maemo-tweaks-module-manager.c
new file mode 100644 (file)
index 0000000..90a7f50
--- /dev/null
@@ -0,0 +1,204 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#include <config.h>
+
+#include <string.h>
+#include <unistd.h>
+
+#include <gmodule.h>
+
+#include "maemo-tweaks-types.h"
+#include "libmaemo-tweaks-section/maemo-tweaks-module.h"
+#include "maemo-tweaks-module-manager.h"
+
+
+enum
+{
+    PROP_0,
+    PROP_MODULE_PATH
+};
+
+
+static GObject *
+maemo_tweaks_module_manager_constructor (GType type,
+                                         guint n_params,
+                                         GObjectConstructParam *params);
+static void
+maemo_tweaks_module_manager_finalize (GObject *object);
+
+static void
+maemo_tweaks_module_manager_get_property (GObject *object,
+                                          guint param_id,
+                                          GValue *value,
+                                          GParamSpec *pspec);
+
+static void
+maemo_tweaks_module_manager_set_property (GObject *object,
+                                          guint param_id,
+                                          const GValue *value,
+                                          GParamSpec *pspec);
+
+static gboolean
+maemo_tweaks_module_manager_query_modules
+    (MaemoTweaksModuleManager *manager);
+
+
+G_DEFINE_TYPE (MaemoTweaksModuleManager, maemo_tweaks_module_manager,
+               G_TYPE_OBJECT);
+
+
+static void
+maemo_tweaks_module_manager_class_init (MaemoTweaksModuleManagerClass *klass)
+{
+    GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+    object_class->constructor  = maemo_tweaks_module_manager_constructor;
+    object_class->finalize     = maemo_tweaks_module_manager_finalize;
+    object_class->get_property = maemo_tweaks_module_manager_get_property;
+    object_class->set_property = maemo_tweaks_module_manager_set_property;
+
+    g_object_class_install_property
+        (object_class, PROP_MODULE_PATH,
+         g_param_spec_string ("module-path",
+                              "Module Path",
+                              "The path where to look for modules",
+                              NULL,
+                              G_PARAM_READWRITE |
+                              G_PARAM_CONSTRUCT_ONLY));
+}
+
+static void
+maemo_tweaks_module_manager_init (MaemoTweaksModuleManager *manager)
+{
+    manager->module_path = NULL;
+    manager->modules     = NULL;
+}
+
+static GObject *
+maemo_tweaks_module_manager_constructor (GType type,
+                                         guint n_params,
+                                         GObjectConstructParam *params)
+{
+    GObject *object;
+    MaemoTweaksModuleManager *manager;
+
+    object = G_OBJECT_CLASS
+        (maemo_tweaks_module_manager_parent_class)->constructor (type,
+                                                                 n_params,
+                                                                 params);
+
+    manager = MAEMO_TWEAKS_MODULE_MANAGER (object);
+
+    if (manager->module_path)
+        maemo_tweaks_module_manager_query_modules (manager);
+
+    return object;
+}
+
+static void
+maemo_tweaks_module_manager_finalize (GObject *object)
+{
+    MaemoTweaksModuleManager *manager = MAEMO_TWEAKS_MODULE_MANAGER (object);
+
+    g_free (manager->module_path);
+
+    /* GTypeModules most not be finalized, don't unref them */
+    g_list_free (manager->modules);
+
+    G_OBJECT_CLASS (maemo_tweaks_module_manager_parent_class)->
+        finalize (object);
+}
+
+static void
+maemo_tweaks_module_manager_get_property (GObject *object,
+                                          guint param_id,
+                                          GValue *value,
+                                          GParamSpec *pspec)
+{
+    MaemoTweaksModuleManager *manager = MAEMO_TWEAKS_MODULE_MANAGER (object);
+
+    switch (param_id)
+    {
+        case PROP_MODULE_PATH:
+            g_value_set_string (value, manager->module_path);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+            break;
+    }
+}
+
+static void
+maemo_tweaks_module_manager_set_property (GObject *object,
+                                          guint param_id,
+                                          const GValue *value,
+                                          GParamSpec *pspec)
+{
+    MaemoTweaksModuleManager *manager = MAEMO_TWEAKS_MODULE_MANAGER (object);
+
+    switch (param_id)
+    {
+        case PROP_MODULE_PATH:
+            manager->module_path = g_value_dup_string (value);
+            break;
+        default:
+            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
+            break;
+    }
+}
+
+static gboolean
+maemo_tweaks_module_manager_is_valid_module_name (const gchar *name)
+{
+    return g_str_has_suffix (name, ".so");
+}
+
+static gboolean
+maemo_tweaks_module_manager_query_modules (MaemoTweaksModuleManager *manager)
+{
+    const gchar *name;
+    GDir        *dir;
+    GError      *error = NULL;
+
+    dir = g_dir_open (manager->module_path, 0, &error);
+
+    if (!dir)
+    {
+        g_printerr ("Error while opening module dir: %s\n", error->message);
+        g_clear_error (&error);
+        return FALSE;
+    }
+
+    while ((name = g_dir_read_name (dir)))
+    {
+        if (maemo_tweaks_module_manager_is_valid_module_name (name))
+        {
+            MaemoTweaksModule *module;
+            gchar     *path;
+
+            path = g_build_filename (manager->module_path, name, NULL);
+            module = maemo_tweaks_module_new (path);
+
+            if (! g_type_module_use (G_TYPE_MODULE (module)))
+            {
+                g_printerr ("Failed to load module: %s\n", path);
+                g_object_unref (module);
+                g_free (path);
+                continue;
+            }
+
+            g_free (path);
+
+            g_type_module_unuse (G_TYPE_MODULE (module));
+
+            manager->modules = g_list_prepend (manager->modules, module);
+        }
+    }
+
+    g_dir_close (dir);
+
+    return TRUE;
+}
+
diff --git a/maemo-tweaks-module-manager.h b/maemo-tweaks-module-manager.h
new file mode 100644 (file)
index 0000000..3dbafef
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#ifndef __MAEMO_TWEAKS_MODULE_MANAGER_H__
+#define __MAEMO_TWEAKS_MODULE_MANAGER_H__
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define MAEMO_TWEAKS_TYPE_MODULE_MANAGER \
+        (maemo_tweaks_module_manager_get_type ())
+#define MAEMO_TWEAKS_MODULE_MANAGER(obj) \
+        (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+        MAEMO_TWEAKS_TYPE_MODULE_MANAGER, \
+        MaemoTweaksModuleManager))
+#define MAEMO_TWEAKS_MODULE_MANAGER_CLASS(k) \
+        (G_TYPE_CHECK_CLASS_CAST((k), \
+        MAEMO_TWEAKS_TYPE_MODULE_MANAGER, \
+        MaemoTweaksModuleManagerClass))
+#define MAEMO_TWEAKS_IS_MODULE_MANAGER(obj) \
+        (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+        MAEMO_TWEAKS_TYPE_MODULE_MANAGER))
+#define MAEMO_TWEAKS_IS_MODULE_MANAGER_CLASS(k) \
+        (G_TYPE_CHECK_CLASS_TYPE((k), \
+        MAEMO_TWEAKS_TYPE_MODULE_MANAGER))
+#define MAEMO_TWEAKS_MODULE_MANAGER_GET_CLASS(o) \
+        (G_TYPE_INSTANCE_GET_CLASS ((o), \
+        MAEMO_TWEAKS_TYPE_MODULE_MANAGER, \
+        MaemoTweaksModuleManagerClass))
+
+
+typedef struct _MaemoTweaksModuleManagerClass MaemoTweaksModuleManagerClass;
+
+struct _MaemoTweaksModuleManager
+{
+    GObject  parent_instance;
+
+    gchar   *module_path;
+    GList   *modules;
+};
+
+struct _MaemoTweaksModuleManagerClass
+{
+    GObjectClass  parent_class;
+};
+
+
+GType maemo_tweaks_module_manager_get_type (void) G_GNUC_CONST;
+
+
+G_END_DECLS
+
+#endif /* __MAEMO_TWEAKS_MODULE_MANAGER_H__ */
+
diff --git a/maemo-tweaks-types.h b/maemo-tweaks-types.h
new file mode 100644 (file)
index 0000000..7548de1
--- /dev/null
@@ -0,0 +1,11 @@
+/*
+ * vim:ts=4:sw=4:et:cindent:cino=(0
+ */ 
+
+#ifndef __MAEMO_TWEAKS_TYPES_H__
+#define __MAEMO_TWEAKS_TYPES_H__
+
+typedef struct _MaemoTweaksModuleManager MaemoTweaksModuleManager;
+
+#endif /* __MAEMO_TWEAKS_TYPES_H__ */
+