[evolution/wip-webkit2] PreferPlain - Make it work under WK2



commit fd6e71d4211bb85e93d51cd54cc42a347c809876
Author: Tomas Popela <tpopela redhat com>
Date:   Tue Nov 19 14:00:38 2013 +0100

    PreferPlain - Make it work under WK2

 e-util/e-dom-utils.c                               |   92 ++++++++++++++++++++
 e-util/e-dom-utils.h                               |   11 ++-
 .../e-mail-display-popup-prefer-plain.c            |   13 ++-
 modules/prefer-plain/web-extension/Makefile.am     |    5 +-
 .../module-prefer-plain-web-extension.c            |   22 ++---
 5 files changed, 123 insertions(+), 20 deletions(-)
---
diff --git a/e-util/e-dom-utils.c b/e-util/e-dom-utils.c
index dd39c55..234076e 100644
--- a/e-util/e-dom-utils.c
+++ b/e-util/e-dom-utils.c
@@ -724,6 +724,7 @@ e_dom_utils_find_element_by_id (WebKitDOMDocument *document,
 
        return NULL;
 }
+
 gboolean
 e_dom_utils_element_exists (WebKitDOMDocument *document,
                             const gchar *element_id)
@@ -889,6 +890,97 @@ e_dom_utils_element_is_hidden (WebKitDOMDocument *document,
                WEBKIT_DOM_HTML_ELEMENT (element));
 }
 
+static void
+get_total_offsets (WebKitDOMElement *element,
+                   glong *left,
+                   glong *top)
+{
+       WebKitDOMElement *offset_parent;
+
+       if (left)
+               *left = 0;
+
+       if (top)
+               *top = 0;
+
+       offset_parent = element;
+       do {
+               if (left)
+                       *left += webkit_dom_element_get_offset_left (offset_parent);
+               if (top)
+                       *top += webkit_dom_element_get_offset_top (offset_parent);
+               offset_parent = webkit_dom_element_get_offset_parent (offset_parent);
+       } while (offset_parent);
+}
+
+static WebKitDOMElement *
+find_element_from_point (WebKitDOMDocument *document,
+                         gint32 x,
+                         gint32 y,
+                         WebKitDOMElement *element_on_point)
+{
+       WebKitDOMDocument *content_document;
+       WebKitDOMElement *element;
+
+       if (!element_on_point)
+               element = webkit_dom_document_element_from_point (document, x, y);
+       else {
+               glong left, top;
+               get_total_offsets (element_on_point, &left, &top);
+
+               element = webkit_dom_document_element_from_point (
+                       document, x - left, y - top);
+       }
+
+       if (element_on_point && webkit_dom_node_is_equal_node (
+               WEBKIT_DOM_NODE (element),
+               WEBKIT_DOM_NODE (element_on_point))) {
+               return element_on_point;
+       }
+
+       if (!WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (element))
+               return element_on_point;
+
+       content_document =
+               webkit_dom_html_iframe_element_get_content_document (
+                       WEBKIT_DOM_HTML_IFRAME_ELEMENT (element));
+
+       if (!content_document)
+               return element_on_point;
+
+       return find_element_from_point (content_document, x, y, element);
+}
+
+/* ! This function can be called only from WK2 web-extension ! */
+WebKitDOMElement *
+e_dom_utils_get_element_from_point (WebKitDOMDocument *document,
+                                    gint32 x,
+                                    gint32 y)
+{
+       return find_element_from_point (document, x, y, NULL);
+}
+
+/* ! This function can be called only from WK2 web-extension ! */
+WebKitDOMDocument *
+e_dom_utils_get_document_from_point (WebKitDOMDocument *document,
+                                     gint32 x,
+                                     gint32 y)
+{
+       WebKitDOMElement *element;
+
+       if (x == 0 && y == 0)
+               element = webkit_dom_html_document_get_active_element (WEBKIT_DOM_HTML_DOCUMENT (document));
+       else
+               element = find_element_from_point (document, x, y, NULL);
+
+       if (WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (element))
+               return webkit_dom_html_iframe_element_get_content_document (
+                       WEBKIT_DOM_HTML_IFRAME_ELEMENT (element));
+       else
+               return webkit_dom_node_get_owner_document (
+                       WEBKIT_DOM_NODE (element));
+}
+
 /* VCard Inline Module DOM functions */
 
 static void
diff --git a/e-util/e-dom-utils.h b/e-util/e-dom-utils.h
index 26cb3e1..cff73be 100644
--- a/e-util/e-dom-utils.h
+++ b/e-util/e-dom-utils.h
@@ -81,7 +81,16 @@ void         e_dom_utils_hide_element        (WebKitDOMDocument *document,
                                                  gboolean hide);
 gboolean       e_dom_utils_element_is_hidden   (WebKitDOMDocument *document,
                                                 const gchar *element_id);
-
+WebKitDOMElement *
+               e_dom_utils_get_element_from_point
+                                               (WebKitDOMDocument *document,
+                                                gint32 x,
+                                                gint32 y);
+WebKitDOMDocument *
+               e_dom_utils_get_document_from_point
+                                               (WebKitDOMDocument *document,
+                                                gint32 x,
+                                                gint32 y);
 /* VCard Inline Module DOM functions */
 void           e_dom_utils_module_vcard_inline_bind_dom
                                                (WebKitDOMDocument *document,
diff --git a/modules/prefer-plain/e-mail-display-popup-prefer-plain.c 
b/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
index abe2f44..bbcd6e8 100644
--- a/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
+++ b/modules/prefer-plain/e-mail-display-popup-prefer-plain.c
@@ -172,11 +172,16 @@ toggle_part (GtkAction *action,
                        NULL,
                        NULL);
        if (result) {
-               document_uri = g_variant_get_string (result, NULL);
+               g_variant_get (result, "(&s)", &document_uri);
+               soup_uri = soup_uri_new (document_uri);
                g_variant_unref (result);
        }
 
-       soup_uri = soup_uri_new (document_uri);
+       if (!soup_uri || !soup_uri->query) {
+               if (soup_uri)
+                       soup_uri_free (soup_uri);
+               return;
+       }
 
        query = soup_form_decode (soup_uri->query);
        g_hash_table_replace (
@@ -359,11 +364,11 @@ mail_display_popup_prefer_plain_update_actions (EMailDisplayPopupExtension *exte
                        NULL,
                        NULL);
        if (result) {
-               document_uri = g_variant_get_string (result, NULL);
+               g_variant_get (result, "(&s)", &document_uri);
+               soup_uri = soup_uri_new (document_uri);
                g_variant_unref (result);
        }
 
-       soup_uri = soup_uri_new (document_uri);
        if (!soup_uri || !soup_uri->query) {
                gtk_action_group_set_visible (pp_extension->action_group, FALSE);
                if (soup_uri)
diff --git a/modules/prefer-plain/web-extension/Makefile.am b/modules/prefer-plain/web-extension/Makefile.am
index 8a26881..93d88d0 100644
--- a/modules/prefer-plain/web-extension/Makefile.am
+++ b/modules/prefer-plain/web-extension/Makefile.am
@@ -2,10 +2,13 @@ webextensions_LTLIBRARIES = libmodulepreferplainwebextension.la
 
 libmodulepreferplainwebextension_la_SOURCES =          \
        module-prefer-plain-web-extension.c             \
-       module-prefer-plain-web-extension.h
+       module-prefer-plain-web-extension.h             \
+       $(top_srcdir)/e-util/e-dom-utils.c              \
+       $(top_srcdir)/e-util/e-dom-utils.h
 
 libmodulepreferplainwebextension_la_CPPFLAGS =         \
        -DWEBEXTENSIONS_COMPILATION                     \
+       -DEVOLUTION_IMAGESDIR=\"${imagesdir}\"          \
        $(GNOME_PLATFORM_CFLAGS)                        \
        $(AM_CPPFLAGS)                                  \
        $(WEB_EXTENSIONS_CFLAGS)
diff --git a/modules/prefer-plain/web-extension/module-prefer-plain-web-extension.c 
b/modules/prefer-plain/web-extension/module-prefer-plain-web-extension.c
index 402f66d..29f969c 100644
--- a/modules/prefer-plain/web-extension/module-prefer-plain-web-extension.c
+++ b/modules/prefer-plain/web-extension/module-prefer-plain-web-extension.c
@@ -22,6 +22,8 @@
 #include <gtk/gtk.h>
 #include <webkit2/webkit-web-extension.h>
 
+#include "../../../e-util/e-dom-utils.h"
+
 /* FIXME Clean it */
 static GDBusConnection *dbus_connection;
 
@@ -81,7 +83,7 @@ handle_method_call (GDBusConnection *connection,
                WebKitDOMElement *frame_element;
                const gchar *new_uri;
 
-               g_variant_get (parameters, "(t&s)", &page_id, &new_uri);
+               g_variant_get (parameters, "(&s)", &new_uri);
 
                /* Get frame's window and from the window the actual <iframe> element */
                window = webkit_dom_document_get_default_view (document_saved);
@@ -100,18 +102,7 @@ handle_method_call (GDBusConnection *connection,
                        return;
 
                document = webkit_web_page_get_dom_document (web_page);
-               document_saved = document;
-
-               if (x == 0 && y == 0)
-                       active_element = webkit_dom_html_document_get_active_element 
(WEBKIT_DOM_HTML_DOCUMENT (document));
-               else
-                       active_element = webkit_dom_document_element_from_point (document, x,y);
-
-               if (WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (active_element)) {
-                       document_saved =
-                               webkit_dom_html_iframe_element_get_content_document (
-                                       WEBKIT_DOM_HTML_IFRAME_ELEMENT (active_element));
-               }
+               document_saved = e_dom_utils_get_document_from_point (document, x, y);
 
                g_dbus_method_invocation_return_value (invocation, NULL);
        } else if (g_strcmp0 (method_name, "GetDocumentURI") == 0) {
@@ -119,7 +110,10 @@ handle_method_call (GDBusConnection *connection,
 
                document_uri = webkit_dom_document_get_document_uri (document_saved);
 
-               g_dbus_method_invocation_return_value (invocation, g_variant_new ("(s)", document_uri));
+               g_dbus_method_invocation_return_value (
+                       invocation, g_variant_new ("(s)", document_uri));
+
+               g_free (document_uri);
        }
 }
 


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