[devhelp/wip/tintou/language-gobject] Transform DhLanguage into a GObject



commit 04214a96e99c35888ba55618a8de87edd191e179
Author: Corentin Noël <corentin elementary io>
Date:   Tue Nov 22 10:21:21 2016 +0100

    Transform DhLanguage into a GObject
    
    https://bugzilla.gnome.org/show_bug.cgi?id=775261

 src/dh-book-manager.c |    4 +-
 src/dh-language.c     |   63 ++++++++++++++++++++++++++++++++----------------
 src/dh-language.h     |   28 ++++++++++++++++++++-
 3 files changed, 70 insertions(+), 25 deletions(-)
---
diff --git a/src/dh-book-manager.c b/src/dh-book-manager.c
index b10cc6b..02bb72b 100644
--- a/src/dh-book-manager.c
+++ b/src/dh-book-manager.c
@@ -118,7 +118,7 @@ dh_book_manager_finalize (GObject *object)
 
         priv = dh_book_manager_get_instance_private (DH_BOOK_MANAGER (object));
 
-        g_list_free_full (priv->languages, (GDestroyNotify)dh_language_free);
+        g_list_free_full (priv->languages, (GDestroyNotify)g_object_unref);
 
         g_slist_free_full (priv->books_disabled, g_free);
 
@@ -876,7 +876,7 @@ book_manager_dec_language (DhBookManager *book_manager,
 
         /* If language count reaches zero, remove from list */
         if (dh_language_dec_n_books_enabled (li->data)) {
-                dh_language_free (li->data);
+                g_object_unref (li->data);
                 priv->languages = g_list_delete_link (priv->languages, li);
 
                 /* Emit signal to notify others */
diff --git a/src/dh-language.c b/src/dh-language.c
index b2e0f8d..cfa9d50 100644
--- a/src/dh-language.c
+++ b/src/dh-language.c
@@ -20,24 +20,36 @@
 #include "dh-language.h"
 #include <string.h>
 
-struct _DhLanguage {
+G_DEFINE_TYPE (DhLanguage, dh_language, G_TYPE_OBJECT)
+#define DH_LANGUAGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), DH_TYPE_LANGUAGE, DhLanguagePrivate))
+
+struct _DhLanguagePrivate {
         /* Name of the language */
         gchar *name;
         /* Number of books enabled in the language */
         gint   n_books_enabled;
 };
 
-/**
- * dh_language_free:
- * @language: a #DhLanguage object
- *
- * Free memory associated with the language.
- */
-void
-dh_language_free (DhLanguage *language)
+static void
+dh_language_finalize (GObject *gobject)
+{
+        DhLanguagePrivate *priv = DH_LANGUAGE_GET_PRIVATE (gobject);
+        g_free (priv->name);
+        G_OBJECT_CLASS (dh_language_parent_class)->finalize (gobject);
+}
+
+static void
+dh_language_class_init (DhLanguageClass *klass)
+{
+        GObjectClass *object_class = G_OBJECT_CLASS (klass);
+        g_type_class_add_private (object_class, sizeof (DhLanguagePrivate));
+        object_class->finalize = dh_language_finalize;
+}
+
+static void
+dh_language_init (DhLanguage *self)
 {
-        g_free (language->name);
-        g_slice_free (DhLanguage, language);
+        
 }
 
 /**
@@ -52,11 +64,13 @@ DhLanguage *
 dh_language_new (const gchar *name)
 {
         DhLanguage *language;
+        DhLanguagePrivate *priv;
 
         g_return_val_if_fail (name != NULL, NULL);
 
-        language = g_slice_new0 (DhLanguage);
-        language->name = g_strdup (name);
+        language = g_object_new (DH_TYPE_LANGUAGE, NULL);
+        priv = DH_LANGUAGE_GET_PRIVATE (language);
+        priv->name = g_strdup (name);
 
         return language;
 }
@@ -78,7 +92,7 @@ dh_language_compare (const DhLanguage *language_a,
         g_return_val_if_fail (language_a != NULL, -1);
         g_return_val_if_fail (language_b != NULL, -1);
 
-        return strcmp (language_a->name, language_b->name);
+        return strcmp (dh_language_get_name (language_a), dh_language_get_name (language_b));
 }
 
 /**
@@ -98,7 +112,7 @@ dh_language_compare_by_name (const DhLanguage *language_a,
         g_return_val_if_fail (language_a != NULL, -1);
         g_return_val_if_fail (language_name_b != NULL, -1);
 
-        return strcmp (language_a->name, language_name_b);
+        return strcmp (dh_language_get_name (language_a), language_name_b);
 }
 
 /**
@@ -110,11 +124,12 @@ dh_language_compare_by_name (const DhLanguage *language_a,
  * Returns: The name of the language
  */
 const gchar *
-dh_language_get_name (DhLanguage *language)
+dh_language_get_name (const DhLanguage *language)
 {
         g_return_val_if_fail (language != NULL, NULL);
+        g_return_val_if_fail (DH_LANGUAGE_GET_PRIVATE (language) != NULL, NULL);
 
-        return language->name;
+        return DH_LANGUAGE_GET_PRIVATE (language)->name;
 }
 
 /**
@@ -128,9 +143,11 @@ dh_language_get_name (DhLanguage *language)
 gint
 dh_language_get_n_books_enabled (DhLanguage *language)
 {
+        DhLanguagePrivate *priv;
         g_return_val_if_fail (language != NULL, -1);
+        priv = DH_LANGUAGE_GET_PRIVATE (language);
 
-        return language->n_books_enabled;
+        return priv->n_books_enabled;
 }
 
 /**
@@ -142,9 +159,11 @@ dh_language_get_n_books_enabled (DhLanguage *language)
 void
 dh_language_inc_n_books_enabled (DhLanguage *language)
 {
+        DhLanguagePrivate *priv;
         g_return_if_fail (language != NULL);
+        priv = DH_LANGUAGE_GET_PRIVATE (language);
 
-        language->n_books_enabled++;
+        priv->n_books_enabled++;
 }
 
 /**
@@ -158,8 +177,10 @@ dh_language_inc_n_books_enabled (DhLanguage *language)
 gboolean
 dh_language_dec_n_books_enabled (DhLanguage *language)
 {
+        DhLanguagePrivate *priv;
         g_return_val_if_fail (language != NULL, FALSE);
+        priv = DH_LANGUAGE_GET_PRIVATE (language);
 
-        language->n_books_enabled--;
-        return (language->n_books_enabled <= 0) ? TRUE : FALSE;
+        priv->n_books_enabled--;
+        return (priv->n_books_enabled <= 0) ? TRUE : FALSE;
 }
diff --git a/src/dh-language.h b/src/dh-language.h
index 2c65400..474593a 100644
--- a/src/dh-language.h
+++ b/src/dh-language.h
@@ -21,11 +21,33 @@
 
 #include <glib-object.h>
 
+G_BEGIN_DECLS
+
+#define DH_TYPE_LANGUAGE                (dh_language_get_type ())
+#define DH_LANGUAGE(obj)                (G_TYPE_CHECK_INSTANCE_CAST ((obj), DH_TYPE_LANGUAGE, DhLanguage))
+#define DH_LANGUAGE_CLASS(klass)        (G_TYPE_CHECK_CLASS_CAST ((klass), DH_TYPE_LANGUAGE, 
DhLanguageClass))
+#define DH_IS_LANGUAGE(obj)             (G_TYPE_CHECK_INSTANCE_TYPE ((obj), DH_TYPE_LANGUAGE))
+#define DH_IS_LANGUAGE_CLASS(klass)     (G_TYPE_CHECK_CLASS_TYPE ((klass), DH_TYPE_LANGUAGE))
+#define DH_LANGUAGE_GET_CLASS(obj)      (G_TYPE_INSTANCE_GET_CLASS ((obj), DH_TYPE_LANGUAGE, 
DhLanguageClass))
+
 typedef struct _DhLanguage DhLanguage;
+typedef struct _DhLanguageClass DhLanguageClass;
+typedef struct _DhLanguagePrivate DhLanguagePrivate;
+
+struct _DhLanguage {
+        GObject parent_instance;
+        /*<private>*/
+        DhLanguagePrivate *priv;
+};
 
+struct _DhLanguageClass
+{
+        GObjectClass parent_class;
+};
+
+GType        dh_language_get_type            (void) G_GNUC_CONST;
 DhLanguage  *dh_language_new                 (const gchar      *name);
-void         dh_language_free                (DhLanguage       *language);
-const gchar *dh_language_get_name            (DhLanguage       *language);
+const gchar *dh_language_get_name            (const DhLanguage *language);
 gint         dh_language_compare             (const DhLanguage *language_a,
                                               const DhLanguage *language_b);
 gint         dh_language_compare_by_name     (const DhLanguage *language_a,
@@ -34,4 +56,6 @@ gint         dh_language_get_n_books_enabled (DhLanguage       *language);
 void         dh_language_inc_n_books_enabled (DhLanguage       *language);
 gboolean     dh_language_dec_n_books_enabled (DhLanguage       *language);
 
+G_END_DECLS
+
 #endif /* __DH_LANGUAGE_H__ */


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