[devhelp] assistant-view: don't reference DhApplication



commit 3e943e2d09e99246570904f9fde16e4f89f3a8eb
Author: Carl-Anton Ingmarsson <ca ingmarsson gmail com>
Date:   Mon Jan 28 21:07:46 2013 +0100

    assistant-view: don't reference DhApplication
    
    Add a new "open-uri" signal to signal that a URI should be opened. This way
    DhAssistantView doesn't have to reference DhApplication which makes it
    possible to use DhAssistantView in other applications.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=692732

 src/dh-assistant-view.c |   49 +++++++++++++++++++++++++---------------------
 src/dh-assistant-view.h |    7 ++---
 src/dh-assistant.c      |   29 ++++++++++++++++++++++++---
 3 files changed, 55 insertions(+), 30 deletions(-)
---
diff --git a/src/dh-assistant-view.c b/src/dh-assistant-view.c
index a3f710f..ce2aa49 100644
--- a/src/dh-assistant-view.c
+++ b/src/dh-assistant-view.c
@@ -27,15 +27,20 @@
 #include "dh-util.h"
 #include "dh-book-manager.h"
 #include "dh-book.h"
-#include "dh-window.h"
 
 typedef struct {
-        DhApp    *application;
-        DhLink   *link;
-        gchar    *current_search;
-        gboolean  snippet_loaded;
+        DhBookManager *book_manager;
+        DhLink        *link;
+        gchar         *current_search;
+        gboolean       snippet_loaded;
 } DhAssistantViewPriv;
 
+enum {
+        SIGNAL_OPEN_URI,
+        SIGNAL_LAST
+};
+static guint signals[SIGNAL_LAST] = { 0 };
+
 G_DEFINE_TYPE (DhAssistantView, dh_assistant_view, WEBKIT_TYPE_WEB_VIEW);
 
 #define GET_PRIVATE(instance) G_TYPE_INSTANCE_GET_PRIVATE \
@@ -50,8 +55,8 @@ view_finalize (GObject *object)
                 g_object_unref (priv->link);
         }
 
-        if (priv->application) {
-                g_object_unref (priv->application);
+        if (priv->book_manager) {
+                g_object_unref (priv->book_manager);
         }
 
         g_free (priv->current_search);
@@ -70,7 +75,6 @@ assistant_decide_policy (WebKitWebView           *web_view,
         WebKitNavigationPolicyDecision *navigation_decision;
         WebKitNavigationType            navigation_type;
         WebKitURIRequest               *request;
-        GtkWidget                      *window;
 
         if (decision_type != WEBKIT_POLICY_DECISION_TYPE_NAVIGATION_ACTION) {
                 webkit_policy_decision_ignore (decision);
@@ -100,8 +104,7 @@ assistant_decide_policy (WebKitWebView           *web_view,
                 return TRUE;
         }
 
-        window = GTK_WIDGET (dh_app_peek_first_window (priv->application));
-        _dh_window_display_uri (DH_WINDOW (window), uri);
+        g_signal_emit (web_view, signals[SIGNAL_OPEN_URI], 0, uri);
         webkit_policy_decision_ignore (decision);
 
         return TRUE;
@@ -126,10 +129,7 @@ assistant_navigation_requested (WebKitWebView        *web_view,
                 return WEBKIT_NAVIGATION_RESPONSE_ACCEPT;
         }
         else if (g_str_has_prefix (uri, "file://")) {
-                GtkWidget *window;
-
-                window = GTK_WIDGET (dh_app_peek_first_window (priv->application));
-                _dh_window_display_uri (DH_WINDOW (window), uri);
+                g_signal_emit (web_view, signals[SIGNAL_OPEN_URI], 0, uri);
         }
 
         return WEBKIT_NAVIGATION_RESPONSE_IGNORE;
@@ -165,6 +165,14 @@ dh_assistant_view_class_init (DhAssistantViewClass* klass)
 #endif
 
         g_type_class_add_private (klass, sizeof (DhAssistantViewPriv));
+
+        signals[SIGNAL_OPEN_URI] = g_signal_new ("open-uri",
+                                                 G_TYPE_FROM_CLASS (object_class),
+                                                 0, 0,
+                                                 NULL, NULL,
+                                                 NULL,
+                                                 G_TYPE_NONE, 1,
+                                                 G_TYPE_STRING);
 }
 
 static void
@@ -440,7 +448,6 @@ dh_assistant_view_search (DhAssistantView *view,
         DhLink              *link;
         DhLink              *exact_link;
         DhLink              *prefix_link;
-        DhBookManager       *book_manager;
         GList               *books;
 
         g_return_val_if_fail (DH_IS_ASSISTANT_VIEW (view), FALSE);
@@ -459,12 +466,10 @@ dh_assistant_view_search (DhAssistantView *view,
         g_free (priv->current_search);
         priv->current_search = g_strdup (str);
 
-        book_manager = dh_app_peek_book_manager (priv->application);
-
         prefix_link = NULL;
         exact_link = NULL;
 
-        for (books = dh_book_manager_get_books (book_manager);
+        for (books = dh_book_manager_get_books (priv->book_manager);
              !exact_link && books;
              books = g_list_next (books)) {
                 GList *l;
@@ -517,15 +522,15 @@ dh_assistant_view_search (DhAssistantView *view,
 }
 
 void
-dh_assistant_view_set_app (DhAssistantView *view,
-                           DhApp           *application)
+dh_assistant_view_set_book_manager (DhAssistantView *view,
+                                    DhBookManager   *book_manager)
 {
         DhAssistantViewPriv *priv;
 
         g_return_if_fail (DH_IS_ASSISTANT_VIEW (view));
-        g_return_if_fail (DH_IS_APP (application));
+        g_return_if_fail (DH_IS_BOOK_MANAGER (book_manager));
 
         priv = GET_PRIVATE (view);
 
-        priv->application = g_object_ref (application);
+        priv->book_manager = g_object_ref (book_manager);
 }
diff --git a/src/dh-assistant-view.h b/src/dh-assistant-view.h
index 0d1c4ee..0676806 100644
--- a/src/dh-assistant-view.h
+++ b/src/dh-assistant-view.h
@@ -26,7 +26,7 @@
 #else
 #include <webkit/webkit.h>
 #endif
-#include "dh-app.h"
+#include "dh-book-manager.h"
 #include "dh-link.h"
 
 G_BEGIN_DECLS
@@ -53,9 +53,8 @@ GType      dh_assistant_view_get_type (void) G_GNUC_CONST;
 GtkWidget* dh_assistant_view_new      (void);
 gboolean   dh_assistant_view_search   (DhAssistantView *view,
                                        const gchar     *str);
-DhApp*     dh_assistant_view_peek_app (DhAssistantView *view);
-void       dh_assistant_view_set_app  (DhAssistantView *view,
-                                       DhApp           *application);
+void       dh_assistant_view_set_book_manager  (DhAssistantView *view,
+                                                DhBookManager   *book_manager);
 gboolean   dh_assistant_view_set_link (DhAssistantView *view,
                                        DhLink          *link);
 G_END_DECLS
diff --git a/src/dh-assistant.c b/src/dh-assistant.c
index 2a7ed96..593ec92 100644
--- a/src/dh-assistant.c
+++ b/src/dh-assistant.c
@@ -30,9 +30,10 @@
 #include "dh-settings.h"
 
 typedef struct {
-        GtkWidget *main_box;
-        GtkWidget *view;
-        DhSettings *settings;
+        DhApp         *application;
+        GtkWidget     *main_box;
+        GtkWidget     *view;
+        DhSettings    *settings;
 } DhAssistantPriv;
 
 static void dh_assistant_class_init (DhAssistantClass *klass);
@@ -56,6 +57,19 @@ assistant_key_press_event_cb (GtkWidget   *widget,
         return FALSE;
 }
 
+static void
+assistant_view_open_uri_cb (DhAssistantView *view,
+                            const char      *uri,
+                            DhAssistant     *assistant)
+{
+        DhAssistantPriv  *priv;
+        GtkWindow* window;
+
+        priv = GET_PRIVATE (assistant);
+        window = dh_app_peek_first_window (priv->application);
+        _dh_window_display_uri (DH_WINDOW (window), uri);
+}
+
 static gboolean
 window_configure_event_cb (GtkWidget *window,
                            GdkEventConfigure *event,
@@ -77,6 +91,7 @@ dispose (GObject *object)
 {
         DhAssistant *assistant = DH_ASSISTANT (object);
         DhAssistantPriv *priv = GET_PRIVATE (assistant);
+        g_clear_object (&priv->application);
         g_clear_object (&priv->settings);
 
         G_OBJECT_CLASS (dh_assistant_parent_class)->dispose (object);
@@ -108,6 +123,10 @@ dh_assistant_init (DhAssistant *assistant)
 
         priv->view = dh_assistant_view_new ();
 
+        g_signal_connect (priv->view, "open-uri",
+                          G_CALLBACK (assistant_view_open_uri_cb),
+                          assistant);
+
         g_signal_connect (assistant, "key-press-event",
                           G_CALLBACK (assistant_key_press_event_cb),
                           assistant);
@@ -146,8 +165,10 @@ dh_assistant_new (DhApp *application)
         assistant = g_object_new (DH_TYPE_ASSISTANT, NULL);
 
         priv = GET_PRIVATE (assistant);
+        priv->application = g_object_ref (application);
 
-        dh_assistant_view_set_app (DH_ASSISTANT_VIEW (priv->view), application);
+        dh_assistant_view_set_book_manager (DH_ASSISTANT_VIEW (priv->view),
+                                            dh_app_peek_book_manager (application));
 
         return assistant;
 }



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