[libgepub/wip/cosimoc/api-rework: 4/13] widget: make the GepubDoc a property



commit a001a75ee510a44e543d152e5e99e8ad52094d78
Author: Cosimo Cecchi <cosimoc gnome org>
Date:   Sat Jun 18 12:10:45 2016 -0700

    widget: make the GepubDoc a property
    
    Instead of having a mismatched get_doc/load_epub. This changes the API
    slightly so that clients will need to create the GepubDoc before passing
    it to the view.

 libgepub/gepub-widget.c |   73 +++++++++++++++++++++++++++++++++++++++++-----
 libgepub/gepub-widget.h |    5 ++-
 tests/test-gepub.c      |    7 +++-
 3 files changed, 74 insertions(+), 11 deletions(-)
---
diff --git a/libgepub/gepub-widget.c b/libgepub/gepub-widget.c
index 2090ecf..02292d7 100644
--- a/libgepub/gepub-widget.c
+++ b/libgepub/gepub-widget.c
@@ -32,6 +32,14 @@ struct _GepubWidgetClass {
     WebKitWebViewClass parent_class;
 };
 
+enum {
+    PROP_0,
+    PROP_DOC,
+    NUM_PROPS
+};
+
+static GParamSpec *properties[NUM_PROPS] = { NULL, };
+
 G_DEFINE_TYPE (GepubWidget, gepub_widget, WEBKIT_TYPE_WEB_VIEW)
 
 static void
@@ -68,6 +76,42 @@ resource_callback (WebKitURISchemeRequest *request, gpointer user_data)
 }
 
 static void
+gepub_widget_set_property (GObject      *object,
+                           guint         prop_id,
+                           const GValue *value,
+                           GParamSpec   *pspec)
+{
+    GepubWidget *widget = GEPUB_WIDGET (object);
+
+    switch (prop_id) {
+    case PROP_DOC:
+        gepub_widget_set_doc (widget, g_value_get_object (value));
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
+gepub_widget_get_property (GObject    *object,
+                           guint       prop_id,
+                           GValue     *value,
+                           GParamSpec *pspec)
+{
+    GepubWidget *widget = GEPUB_WIDGET (object);
+
+    switch (prop_id) {
+    case PROP_DOC:
+        g_value_set_object (value, gepub_widget_get_doc (widget));
+        break;
+    default:
+        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+        break;
+    }
+}
+
+static void
 gepub_widget_finalize (GObject *object)
 {
     GepubWidget *widget = GEPUB_WIDGET (object);
@@ -101,6 +145,18 @@ gepub_widget_class_init (GepubWidgetClass *klass)
 
     object_class->constructed = gepub_widget_constructed;
     object_class->finalize = gepub_widget_finalize;
+    object_class->set_property = gepub_widget_set_property;
+    object_class->get_property = gepub_widget_get_property;
+
+    properties[PROP_DOC] =
+        g_param_spec_object ("doc",
+                             "The GepubDoc",
+                             "The GepubDoc for this widget",
+                             GEPUB_TYPE_DOC,
+                             G_PARAM_READWRITE |
+                             G_PARAM_STATIC_STRINGS);
+
+    g_object_class_install_properties (object_class, NUM_PROPS, properties);
 }
 
 /**
@@ -128,19 +184,20 @@ gepub_widget_get_doc (GepubWidget *widget)
 }
 
 /**
- * gepub_widget_load_epub:
+ * gepub_widget_set_doc:
  * @widget: a #GepubWidget
- * @path: The epub doc path
+ * @doc: (nullable): a #GepubDoc
  *
- * This method reloads the widget with the new document
+ * Sets @doc as the document displayed by the widget.
  */
 void
-gepub_widget_load_epub (GepubWidget *widget, const gchar *path)
+gepub_widget_set_doc (GepubWidget *widget,
+                      GepubDoc    *doc)
 {
-    g_clear_object (&widget->doc);
-
-    widget->doc = gepub_doc_new (path);
-    gepub_widget_reload (widget);
+    if (g_set_object (&widget->doc, doc)) {
+        gepub_widget_reload (widget);
+        g_object_notify_by_pspec (G_OBJECT (widget), properties[PROP_DOC]);
+    }
 }
 
 /**
diff --git a/libgepub/gepub-widget.h b/libgepub/gepub-widget.h
index 93aaad1..4be7585 100644
--- a/libgepub/gepub-widget.h
+++ b/libgepub/gepub-widget.h
@@ -41,8 +41,11 @@ typedef struct _GepubWidgetClass GepubWidgetClass;
 GType             gepub_widget_get_type                        (void) G_GNUC_CONST;
 
 GtkWidget        *gepub_widget_new                             (void);
+
 GepubDoc         *gepub_widget_get_doc                         (GepubWidget *widget);
-void              gepub_widget_load_epub                       (GepubWidget *widget, const gchar *path);
+void              gepub_widget_set_doc                         (GepubWidget *widget,
+                                                                GepubDoc    *doc);
+
 void              gepub_widget_reload                          (GepubWidget *widget);
 
 G_END_DECLS
diff --git a/tests/test-gepub.c b/tests/test-gepub.c
index 4e0021b..be893a0 100644
--- a/tests/test-gepub.c
+++ b/tests/test-gepub.c
@@ -261,13 +261,14 @@ main (int argc, char **argv)
     gtk_container_add (GTK_CONTAINER (window), vpaned);
 
     // gepub widget
-    gepub_widget_load_epub (GEPUB_WIDGET (widget), argv[1]);
-    doc = gepub_widget_get_doc (GEPUB_WIDGET (widget));
+    doc = gepub_doc_new (argv[1]);
     if (!doc) {
         perror ("BAD epub FILE");
         return -1;
     }
 
+    gepub_widget_set_doc (GEPUB_WIDGET (widget), doc);
+
     scrolled = gtk_scrolled_window_new (NULL, NULL);
     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled), GTK_POLICY_AUTOMATIC, 
GTK_POLICY_AUTOMATIC);
     textview2 = gtk_text_view_new ();
@@ -321,5 +322,7 @@ main (int argc, char **argv)
 
     gtk_main ();
 
+    g_object_unref (doc);
+
     return 0;
 }


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