[monet/monet-xml] mn-config: convert to gobject



commit 9ab7f3b8c1afde6ad6b7999bb66e58cfd85a254b
Author: Thomas Wood <thos gnome org>
Date:   Sun May 9 23:31:59 2010 +0100

    mn-config: convert to gobject

 monet/mn-config.c |  124 ++++++++++++++++++++++++++++++++++++++++++++++++-----
 monet/mn-config.h |   69 +++++++++++++++++++++++++-----
 monet/mn-style.c  |   12 +++++-
 3 files changed, 182 insertions(+), 23 deletions(-)
---
diff --git a/monet/mn-config.c b/monet/mn-config.c
index a70aad7..5c5b18f 100644
--- a/monet/mn-config.c
+++ b/monet/mn-config.c
@@ -25,6 +25,112 @@
 
 #include <string.h>
 
+G_DEFINE_TYPE (MnConfig, mn_config, G_TYPE_OBJECT)
+
+#define CONFIG_PRIVATE(o) \
+  (G_TYPE_INSTANCE_GET_PRIVATE ((o), MN_TYPE_CONFIG, MnConfigPrivate))
+
+struct _MnConfigPrivate
+{
+  GHashTable *colors;
+  MnConfig *config;
+};
+
+static void
+mn_config_get_property (GObject    *object,
+                       guint       property_id,
+                       GValue     *value,
+                       GParamSpec *pspec)
+{
+  switch (property_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+mn_config_set_property (GObject      *object,
+                       guint         property_id,
+                       const GValue *value,
+                       GParamSpec   *pspec)
+{
+  switch (property_id)
+    {
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    }
+}
+
+static void
+mn_config_dispose (GObject *object)
+{
+  G_OBJECT_CLASS (mn_config_parent_class)->dispose (object);
+}
+
+static void
+mn_config_free_ops (GSList **ops)
+{
+  gint i;
+
+  for (i = 0; i < 4; i++)
+    {
+      g_slist_foreach (ops[i], (GFunc) g_free, NULL);
+      g_slist_free (ops[i]);
+      ops[i] = NULL;
+    }
+}
+
+static void
+mn_config_finalize (GObject *object)
+{
+  MnConfig *config = MN_CONFIG (object);
+  /* MnConfigPrivate *priv = config->priv; */
+
+  mn_config_free_ops (config->button_ops);
+
+  mn_config_free_ops (config->entry_ops);
+
+  mn_config_free_ops (config->check_ops);
+
+  mn_config_free_ops (config->radio_ops);
+
+  mn_config_free_ops (config->menu_ops);
+
+  mn_config_free_ops (config->menu_item_ops);
+
+  mn_config_free_ops (config->menu_bar_ops);
+
+  G_OBJECT_CLASS (mn_config_parent_class)->finalize (object);
+}
+
+static void
+mn_config_class_init (MnConfigClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  g_type_class_add_private (klass, sizeof (MnConfigPrivate));
+
+  object_class->get_property = mn_config_get_property;
+  object_class->set_property = mn_config_set_property;
+  object_class->dispose = mn_config_dispose;
+  object_class->finalize = mn_config_finalize;
+}
+
+static void
+mn_config_init (MnConfig *self)
+{
+  self->priv = CONFIG_PRIVATE (self);
+}
+
+MnConfig *
+mn_config_new (void)
+{
+  return g_object_new (MN_TYPE_CONFIG, NULL);
+}
+
+
+
 static cairo_pattern_t*
 cairo_pattern_from_paint (const gchar *string)
 {
@@ -328,13 +434,13 @@ monet_end_element (GMarkupParseContext  *context,
 }
 
 
-MnConfig*
-mn_config_load_from_file (const gchar  *filename,
+gboolean
+mn_config_load_from_file (MnConfig     *config,
+                          const gchar  *filename,
                           GError      **err)
 {
   GMarkupParser parser = { monet_start_element, monet_end_element, NULL, NULL, perror };
   GMarkupParseContext *context;
-  MnConfig *config;
   GError *error = NULL;
   gchar *markup;
 
@@ -348,13 +454,9 @@ mn_config_load_from_file (const gchar  *filename,
       g_warning ("Could not load Monet configuration file \"%s\": %s", filename,
                  error->message);
       g_propagate_error (err, error);
-      return NULL;
+      return FALSE;
     }
 
-  config = g_new0 (MnConfig, 1);
-
-
-
   context = g_markup_parse_context_new (&parser, 0, config, NULL);
 
   g_markup_parse_context_parse (context, markup, strlen (markup), &error);
@@ -366,12 +468,12 @@ mn_config_load_from_file (const gchar  *filename,
       g_propagate_error (err, error);
 
       g_free (markup);
-      g_free (config);
       g_markup_parse_context_free (context);
-      return NULL;
+      return FALSE;
     }
 
   g_free (markup);
   g_markup_parse_context_free (context);
-  return config;
+
+  return TRUE;
 }
diff --git a/monet/mn-config.h b/monet/mn-config.h
index 10e0e85..47089b8 100644
--- a/monet/mn-config.h
+++ b/monet/mn-config.h
@@ -20,18 +20,44 @@
  */
 
 #include <glib.h>
+#include <glib-object.h>
 #include <cairo/cairo.h>
 
-typedef struct
-{
-  GSList *normal;
-  GSList *hover;
-  GSList *active;
-  GSList *disabled;
-} MnWidgetOps;
+G_BEGIN_DECLS
 
-typedef struct
+#define MN_TYPE_CONFIG mn_config_get_type()
+
+#define MN_CONFIG(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+  MN_TYPE_CONFIG, MnConfig))
+
+#define MN_CONFIG_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_CAST ((klass), \
+  MN_TYPE_CONFIG, MnConfigClass))
+
+#define MN_IS_CONFIG(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+  MN_TYPE_CONFIG))
+
+#define MN_IS_CONFIG_CLASS(klass) \
+  (G_TYPE_CHECK_CLASS_TYPE ((klass), \
+  MN_TYPE_CONFIG))
+
+#define MN_CONFIG_GET_CLASS(obj) \
+  (G_TYPE_INSTANCE_GET_CLASS ((obj), \
+  MN_TYPE_CONFIG, MnConfigClass))
+
+typedef struct _MnConfig MnConfig;
+typedef struct _MnConfigClass MnConfigClass;
+typedef struct _MnConfigPrivate MnConfigPrivate;
+
+struct _MnConfig
 {
+  GObject parent;
+
+  MnConfigPrivate *priv;
+
+  /*< private >*/
   GSList *button_ops[4];
   GSList *entry_ops[4];
   GSList *check_ops[4];
@@ -39,9 +65,22 @@ typedef struct
   GSList *menu_ops[4];
   GSList *menu_item_ops[4];
   GSList *menu_bar_ops[4];
-} MnConfig;
+};
+
+struct _MnConfigClass
+{
+  GObjectClass parent_class;
+};
 
 
+typedef struct
+{
+  GSList *normal;
+  GSList *hover;
+  GSList *active;
+  GSList *disabled;
+} MnWidgetOps;
+
 typedef enum
 {
   MN_RECT,
@@ -88,5 +127,13 @@ typedef struct
   double y2;
 } MnLineOp;
 
-MnConfig* mn_config_load_from_file (const gchar  *file,
-                                    GError      **err);
+
+GType mn_config_get_type (void) G_GNUC_CONST;
+
+MnConfig* mn_config_new (void);
+
+gboolean mn_config_load_from_file (MnConfig     *config,
+                                   const gchar  *file,
+                                   GError      **err);
+
+G_END_DECLS
diff --git a/monet/mn-style.c b/monet/mn-style.c
index b335d92..be1f7b3 100644
--- a/monet/mn-style.c
+++ b/monet/mn-style.c
@@ -67,6 +67,14 @@ mn_style_set_property (GObject      *object,
 static void
 mn_style_dispose (GObject *object)
 {
+  MnStylePrivate *priv = MN_STYLE (object)->priv;
+
+  if (priv->config)
+    {
+      g_object_unref (priv->config);
+      priv->config = NULL;
+    }
+
   G_OBJECT_CLASS (mn_style_parent_class)->dispose (object);
 }
 
@@ -117,7 +125,9 @@ mn_style_load_config (MnStyle      *style,
                       const gchar  *filename,
                       GError      **error)
 {
-  style->priv->config = mn_config_load_from_file (filename, error);
+  style->priv->config = mn_config_new ();
+
+  mn_config_load_from_file (style->priv->config, filename, error);
 
   return (style->priv->config != NULL);
 }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]