[gupnp/wip/phako/initable: 2/2] XMLDoc: Implement GInitable




commit 2945e2959a6d3eaafdf46a537f072beba9346b9d
Author: Jens Georg <mail jensge org>
Date:   Sun Oct 24 17:48:42 2021 +0200

    XMLDoc: Implement GInitable
    
    Fixes #52

 libgupnp/gupnp-xml-doc.c | 170 +++++++++++++++++++++++++++++++++++++----------
 1 file changed, 135 insertions(+), 35 deletions(-)
---
diff --git a/libgupnp/gupnp-xml-doc.c b/libgupnp/gupnp-xml-doc.c
index 2c60a5f..2871a9c 100644
--- a/libgupnp/gupnp-xml-doc.c
+++ b/libgupnp/gupnp-xml-doc.c
@@ -22,6 +22,7 @@
 
 #include <config.h>
 #include <string.h>
+#include <gio/gio.h>
 #include "gupnp-xml-doc.h"
 #include "gupnp-error.h"
 
@@ -34,11 +35,79 @@
 struct _GUPnPXMLDoc {
         GObject parent;
         xmlDoc *doc;
+        gboolean initialized;
+        char *path;
 };
 
-G_DEFINE_TYPE (GUPnPXMLDoc,
-               gupnp_xml_doc,
-               G_TYPE_OBJECT)
+static GInitableIface *initable_parent_iface = NULL;
+static void
+gupnp_xml_doc_initable_iface_init (gpointer g_iface, gpointer iface_data);
+
+G_DEFINE_TYPE_EXTENDED (
+        GUPnPXMLDoc,
+        gupnp_xml_doc,
+        G_TYPE_OBJECT,
+        0,
+        G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
+                               gupnp_xml_doc_initable_iface_init))
+
+enum
+{
+        PROP_0,
+        PROP_DOC,
+        PROP_PATH
+};
+
+static gboolean
+gupnp_xml_doc_initable_init (GInitable *initable,
+                             GCancellable *cancellable,
+                             GError **error)
+{
+        GUPnPXMLDoc *self = GUPNP_XML_DOC (initable);
+        if (self->initialized == TRUE)
+                return TRUE;
+
+        if (self->doc != NULL)
+                return TRUE;
+
+        if (self->path == NULL) {
+                g_set_error_literal (error,
+                                     GUPNP_XML_ERROR,
+                                     GUPNP_XML_ERROR_OTHER,
+                                     "Neither path nor document passed when "
+                                     "creating GUPnPXMLDoc");
+                return FALSE;
+        }
+
+        int flags = XML_PARSE_PEDANTIC;
+        if (!g_getenv ("GUPNP_DEBUG")) {
+                flags |= XML_PARSE_NOWARNING | XML_PARSE_NOERROR;
+        }
+
+        self->doc = xmlReadFile (self->path, NULL, flags);
+        if (self->doc == NULL) {
+                g_set_error (error,
+                             GUPNP_XML_ERROR,
+                             GUPNP_XML_ERROR_PARSE,
+                             "Failed to parse %s\n",
+                             self->path);
+
+                return FALSE;
+        }
+
+        return TRUE;
+}
+
+
+static void
+gupnp_xml_doc_initable_iface_init (gpointer g_iface, gpointer iface_data)
+{
+        (void) iface_data;
+
+        GInitableIface *iface = (GInitableIface *)g_iface;
+        initable_parent_iface = g_type_interface_peek_parent (iface);
+        iface->init = gupnp_xml_doc_initable_init;
+}
 
 static void
 gupnp_xml_doc_init (G_GNUC_UNUSED GUPnPXMLDoc *doc)
@@ -58,6 +127,36 @@ gupnp_xml_doc_finalize (GObject *object)
         G_OBJECT_CLASS (gupnp_xml_doc_parent_class)->finalize (object);
 }
 
+static void
+gupnp_xml_doc_set_property (GObject *object,
+                            guint property_id,
+                            const GValue *value,
+                            GParamSpec *pspec)
+{
+        GUPnPXMLDoc *self = GUPNP_XML_DOC (object);
+        switch (property_id) {
+        case PROP_DOC:
+                self->doc = g_value_get_pointer (value);
+                break;
+        case PROP_PATH:
+                self->path = g_value_dup_string (value);
+                break;
+        default:
+                G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+                break;
+        }
+}
+
+static void
+gupnp_xml_doc_get_property (GObject *object,
+                            guint property_id,
+                            GValue *value,
+                            GParamSpec *pspec)
+{
+        (void) value;
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+}
+
 static void
 gupnp_xml_doc_class_init (GUPnPXMLDocClass *klass)
 {
@@ -65,7 +164,28 @@ gupnp_xml_doc_class_init (GUPnPXMLDocClass *klass)
 
         object_class = G_OBJECT_CLASS (klass);
 
+        object_class->set_property = gupnp_xml_doc_set_property;
+        object_class->get_property = gupnp_xml_doc_get_property;
         object_class->finalize = gupnp_xml_doc_finalize;
+
+        g_object_class_install_property (
+                object_class,
+                PROP_DOC,
+                g_param_spec_pointer ("doc",
+                                      "doc",
+                                      "doc",
+                                      G_PARAM_CONSTRUCT_ONLY |
+                                              G_PARAM_WRITABLE |
+                                              G_PARAM_STATIC_STRINGS));
+        g_object_class_install_property (
+                object_class,
+                PROP_PATH,
+                g_param_spec_string ("path",
+                                     "path",
+                                     "path",
+                                     NULL,
+                                     G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE |
+                                             G_PARAM_STATIC_STRINGS));
 }
 
 /**
@@ -81,16 +201,12 @@ gupnp_xml_doc_class_init (GUPnPXMLDocClass *klass)
 GUPnPXMLDoc *
 gupnp_xml_doc_new (xmlDoc *xml_doc)
 {
-        GUPnPXMLDoc *doc;
-
-        g_return_val_if_fail (xml_doc != NULL, NULL);
-
-        doc = g_object_new (GUPNP_TYPE_XML_DOC, NULL);
-
-        doc->doc = xml_doc;
-
-        return doc;
-}
+        return g_initable_new (GUPNP_TYPE_XML_DOC,
+                               NULL,
+                               NULL,
+                               "doc",
+                               xml_doc,
+                               NULL);}
 
 /**
  * gupnp_xml_doc_new_from_path:
@@ -107,28 +223,12 @@ GUPnPXMLDoc *
 gupnp_xml_doc_new_from_path (const char *path,
                              GError    **error)
 {
-        xmlDoc *doc;
-        int flags;
-
-        flags = XML_PARSE_PEDANTIC;
-
-        if (!g_getenv ("GUPNP_DEBUG")) {
-                flags |= XML_PARSE_NOWARNING | XML_PARSE_NOERROR;
-        }
-
-        g_return_val_if_fail (path != NULL, NULL);
-        doc = xmlReadFile (path, NULL, flags);
-        if (doc == NULL) {
-                g_set_error (error,
-                             GUPNP_XML_ERROR,
-                             GUPNP_XML_ERROR_PARSE,
-                             "Failed to parse %s\n",
-                             path);
-
-                return NULL;
-        }
-
-        return gupnp_xml_doc_new (doc);
+        return g_initable_new (GUPNP_TYPE_XML_DOC,
+                               NULL,
+                               error,
+                               "path",
+                               path,
+                               NULL);
 }
 
 /**


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