From 6d2c382788a4042d53f49a080acd11b499aa52f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dan=20Vr=C3=A1til?= Date: Wed, 28 Mar 2012 18:38:11 +0200 Subject: WebKit port - port formatter and mail module --- mail/e-mail-display.c | 1430 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 1298 insertions(+), 132 deletions(-) (limited to 'mail/e-mail-display.c') diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c index 7461e595f3..07f45ad461 100644 --- a/mail/e-mail-display.c +++ b/mail/e-mail-display.c @@ -23,14 +23,33 @@ #include #endif +#define LIBSOUP_USE_UNSTABLE_REQUEST_API + #include "e-mail-display.h" #include +#include +#include "e-util/e-marshal.h" #include "e-util/e-util.h" #include "e-util/e-plugin-ui.h" #include "mail/em-composer-utils.h" #include "mail/em-utils.h" +#include "mail/e-mail-request.h" +#include "mail/em-format-html-display.h" +#include "mail/e-mail-attachment-bar.h" +#include "widgets/misc/e-attachment-button.h" + +#include + +#include +#include + +#include + +#define d(x) + +G_DEFINE_TYPE (EMailDisplay, e_mail_display, E_TYPE_WEB_VIEW) #define E_MAIL_DISPLAY_GET_PRIVATE(obj) \ (G_TYPE_INSTANCE_GET_PRIVATE \ @@ -38,13 +57,29 @@ struct _EMailDisplayPrivate { EMFormatHTML *formatter; + + EMFormatWriteMode mode; + gboolean headers_collapsable; + gboolean headers_collapsed; + + GtkActionGroup *mailto_actions; + GtkActionGroup *images_actions; + + gint force_image_load: 1; }; enum { PROP_0, - PROP_FORMATTER + PROP_FORMATTER, + PROP_MODE, + PROP_HEADERS_COLLAPSABLE, + PROP_HEADERS_COLLAPSED, }; +static gpointer parent_class; + +static CamelDataCache *emd_global_http_cache = 0; + static const gchar *ui = "" " " @@ -61,6 +96,15 @@ static const gchar *ui = " " ""; +static const gchar *image_ui = +"" +" " +" " +" " +" " +" " +""; + static GtkActionEntry mailto_entries[] = { { "add-to-address-book", @@ -88,7 +132,7 @@ static GtkActionEntry mailto_entries[] = { NULL, N_("Send _Reply To..."), NULL, - N_("Send a reply message to this address"), + N_("Send a reply message to this address"), NULL /* Handled by EMailReader */ }, /*** Menus ***/ @@ -101,7 +145,54 @@ static GtkActionEntry mailto_entries[] = { NULL } }; -G_DEFINE_TYPE (EMailDisplay, e_mail_display, E_TYPE_WEB_VIEW) +static GtkActionEntry image_entries[] = { + + { "image-save", + GTK_STOCK_SAVE, + N_("Save _Image..."), + NULL, + N_("Save the image to a file"), + NULL /* Handled by EMailReader */ }, + +}; + +static void +mail_display_webview_update_actions (EWebView *web_view, + gpointer user_data) +{ + const gchar *image_src; + gboolean visible; + GtkAction *action; + + g_return_if_fail (web_view != NULL); + + image_src = e_web_view_get_cursor_image_src (web_view); + visible = image_src && g_str_has_prefix (image_src, "cid:"); + if (!visible && image_src) { + CamelStream *image_stream; + + image_stream = camel_data_cache_get (emd_global_http_cache, "http", image_src, NULL); + + visible = image_stream != NULL; + + if (image_stream) + g_object_unref (image_stream); + } + + action = e_web_view_get_action (web_view, "image-save"); + if (action) + gtk_action_set_visible (action, visible); +} + +static void +formatter_image_loading_policy_changed_cb (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + EMailDisplay *display = user_data; + + e_mail_display_load_images (display); +} static void mail_display_update_formatter_colors (EMailDisplay *display) @@ -115,6 +206,9 @@ mail_display_update_formatter_colors (EMailDisplay *display) state = gtk_widget_get_state (GTK_WIDGET (display)); formatter = display->priv->formatter; + if (!display->priv->formatter) + return; + style = gtk_widget_get_style (GTK_WIDGET (display)); if (style == NULL) return; @@ -156,6 +250,21 @@ mail_display_set_property (GObject *object, E_MAIL_DISPLAY (object), g_value_get_object (value)); return; + case PROP_MODE: + e_mail_display_set_mode ( + E_MAIL_DISPLAY (object), + g_value_get_int (value)); + return; + case PROP_HEADERS_COLLAPSABLE: + e_mail_display_set_headers_collapsable ( + E_MAIL_DISPLAY (object), + g_value_get_boolean (value)); + return; + case PROP_HEADERS_COLLAPSED: + e_mail_display_set_headers_collapsed ( + E_MAIL_DISPLAY (object), + g_value_get_boolean (value)); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -173,6 +282,21 @@ mail_display_get_property (GObject *object, value, e_mail_display_get_formatter ( E_MAIL_DISPLAY (object))); return; + case PROP_MODE: + g_value_set_int ( + value, e_mail_display_get_mode ( + E_MAIL_DISPLAY (object))); + return; + case PROP_HEADERS_COLLAPSABLE: + g_value_set_boolean ( + value, e_mail_display_get_headers_collapsable ( + E_MAIL_DISPLAY (object))); + return; + case PROP_HEADERS_COLLAPSED: + g_value_set_boolean ( + value, e_mail_display_get_headers_collapsed ( + E_MAIL_DISPLAY (object))); + return; } G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); @@ -191,14 +315,14 @@ mail_display_dispose (GObject *object) } /* Chain up to parent's dispose() method. */ - G_OBJECT_CLASS (e_mail_display_parent_class)->dispose (object); + G_OBJECT_CLASS (parent_class)->dispose (object); } static void mail_display_realize (GtkWidget *widget) { /* Chain up to parent's realize() method. */ - GTK_WIDGET_CLASS (e_mail_display_parent_class)->realize (widget); + GTK_WIDGET_CLASS (parent_class)->realize (widget); mail_display_update_formatter_colors (E_MAIL_DISPLAY (widget)); } @@ -207,62 +331,28 @@ static void mail_display_style_set (GtkWidget *widget, GtkStyle *previous_style) { - EMailDisplayPrivate *priv; + EMailDisplay *display = E_MAIL_DISPLAY (widget); - priv = E_MAIL_DISPLAY_GET_PRIVATE (widget); + mail_display_update_formatter_colors (display); /* Chain up to parent's style_set() method. */ - GTK_WIDGET_CLASS (e_mail_display_parent_class)-> - style_set (widget, previous_style); - - mail_display_update_formatter_colors (E_MAIL_DISPLAY (widget)); - em_format_queue_redraw (EM_FORMAT (priv->formatter)); -} - -static void -mail_display_load_string (EWebView *web_view, - const gchar *string) -{ - EMailDisplayPrivate *priv; - - priv = E_MAIL_DISPLAY_GET_PRIVATE (web_view); - g_return_if_fail (priv->formatter != NULL); - - if (em_format_busy (EM_FORMAT (priv->formatter))) - return; - - /* Chain up to parent's load_string() method. */ - E_WEB_VIEW_CLASS (e_mail_display_parent_class)-> - load_string (web_view, string); -} - -static void -mail_display_url_requested (GtkHTML *html, - const gchar *uri, - GtkHTMLStream *stream) -{ - /* XXX Sadly, we must block the default method - * until EMFormatHTML is made asynchronous. */ + GTK_WIDGET_CLASS (parent_class)->style_set (widget, previous_style); } static gboolean mail_display_process_mailto (EWebView *web_view, - const gchar *mailto_uri) + const gchar *mailto_uri, + gpointer user_data) { - g_return_val_if_fail (web_view != NULL, FALSE); + g_return_val_if_fail (E_IS_WEB_VIEW (web_view), FALSE); g_return_val_if_fail (mailto_uri != NULL, FALSE); - g_return_val_if_fail (E_IS_MAIL_DISPLAY (web_view), FALSE); if (g_ascii_strncasecmp (mailto_uri, "mailto:", 7) == 0) { - EMailDisplayPrivate *priv; EMFormat *format; CamelFolder *folder = NULL; EShell *shell; - priv = E_MAIL_DISPLAY_GET_PRIVATE (web_view); - g_return_val_if_fail (priv->formatter != NULL, FALSE); - - format = EM_FORMAT (priv->formatter); + format = (EMFormat *) E_MAIL_DISPLAY (web_view)->priv->formatter; if (format != NULL && format->folder != NULL) folder = format->folder; @@ -277,79 +367,825 @@ mail_display_process_mailto (EWebView *web_view, return FALSE; } -static void -mail_display_link_clicked (GtkHTML *html, - const gchar *uri) +static gboolean +mail_display_link_clicked (WebKitWebView *web_view, + WebKitWebFrame *frame, + WebKitNetworkRequest *request, + WebKitWebNavigationAction *navigation_action, + WebKitWebPolicyDecision *policy_decision, + gpointer user_data) { - EMailDisplayPrivate *priv; + EMailDisplay *display; + const gchar *uri = webkit_network_request_get_uri (request); - priv = E_MAIL_DISPLAY_GET_PRIVATE (html); - g_return_if_fail (priv->formatter != NULL); - - if (g_str_has_prefix (uri, "##")) { - guint32 flags; - - flags = priv->formatter->header_wrap_flags; - - if (strcmp (uri, "##TO##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_TO)) - flags |= EM_FORMAT_HTML_HEADER_TO; - else - flags &= ~EM_FORMAT_HTML_HEADER_TO; - } else if (strcmp (uri, "##CC##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_CC)) - flags |= EM_FORMAT_HTML_HEADER_CC; - else - flags &= ~EM_FORMAT_HTML_HEADER_CC; - } else if (strcmp (uri, "##BCC##") == 0) { - if (!(flags & EM_FORMAT_HTML_HEADER_BCC)) - flags |= EM_FORMAT_HTML_HEADER_BCC; - else - flags &= ~EM_FORMAT_HTML_HEADER_BCC; - } else if (strcmp (uri, "##HEADERS##") == 0) { - EMFormatHTMLHeadersState state; - - state = em_format_html_get_headers_state ( - priv->formatter); - - if (state == EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED) - state = EM_FORMAT_HTML_HEADERS_STATE_EXPANDED; - else - state = EM_FORMAT_HTML_HEADERS_STATE_COLLAPSED; - - em_format_html_set_headers_state ( - priv->formatter, state); - } + display = E_MAIL_DISPLAY (web_view); + if (display->priv->formatter == NULL) + return FALSE; - priv->formatter->header_wrap_flags = flags; - em_format_queue_redraw (EM_FORMAT (priv->formatter)); - - } else if (mail_display_process_mailto (E_WEB_VIEW (html), uri)) { + if (mail_display_process_mailto (E_WEB_VIEW (web_view), uri, NULL)) { /* do nothing, function handled the "mailto:" uri already */ - } else if (*uri == '#') - gtk_html_jump_to_anchor (html, uri + 1); + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; - else if (g_ascii_strncasecmp (uri, "thismessage:", 12) == 0) + } else if (g_ascii_strncasecmp (uri, "thismessage:", 12) == 0) { /* ignore */ ; + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; - else if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) + } else if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) { /* ignore */ ; + webkit_web_policy_decision_ignore (policy_decision); + return TRUE; + + } + + /* Let webkit handle it */ + return FALSE; +} + +static void +webkit_request_load_from_file (WebKitNetworkRequest *request, + const gchar *path) +{ + gchar *data = NULL; + gsize length = 0; + gboolean status; + gchar *b64, *new_uri; + gchar *ct; + + status = g_file_get_contents (path, &data, &length, NULL); + if (!status) + return; + + b64 = g_base64_encode ((guchar *) data, length); + ct = g_content_type_guess (path, NULL, 0, NULL); + + new_uri = g_strdup_printf ("data:%s;base64,%s", ct, b64); + webkit_network_request_set_uri (request, new_uri); + + g_free (b64); + g_free (new_uri); + g_free (ct); + g_free (data); +} + +static void +mail_display_resource_requested (WebKitWebView *web_view, + WebKitWebFrame *frame, + WebKitWebResource *resource, + WebKitNetworkRequest *request, + WebKitNetworkResponse *response, + gpointer user_data) +{ + EMailDisplay *display = E_MAIL_DISPLAY (web_view); + EMFormat *formatter = EM_FORMAT (display->priv->formatter); + const gchar *uri = webkit_network_request_get_uri (request); + + if (!formatter) { + webkit_network_request_set_uri (request, "invalid://uri"); + return; + } + + /* Redirect cid:part_id to mail://mail_id/cid:part_id */ + if (g_str_has_prefix (uri, "cid:")) { + + /* Always write raw content of CID object */ + gchar *new_uri = em_format_build_mail_uri (formatter->folder, + formatter->message_uid, + "part_id", G_TYPE_STRING, uri, + "mode", G_TYPE_INT, EM_FORMAT_WRITE_MODE_RAW, NULL); + + webkit_network_request_set_uri (request, new_uri); + + g_free (new_uri); + + /* WebKit won't allow to load a local file when displaing "remote" mail://, + protocol, so we need to handle this manually */ + } else if (g_str_has_prefix (uri, "file:")) { + gchar *path; + + path = g_filename_from_uri (uri, NULL, NULL); + if (!path) + return; + + webkit_request_load_from_file (request, path); + + g_free (path); + + /* Redirect http(s) request to evo-http(s) protocol. See EMailRequest for + * further details about this. */ + } else if (g_str_has_prefix (uri, "http:") || g_str_has_prefix (uri, "https")) { + + gchar *new_uri, *mail_uri, *enc; + SoupURI *soup_uri; + GHashTable *query; + gchar *uri_md5; + CamelStream *stream; + + /* Open Evolution's cache */ + uri_md5 = g_compute_checksum_for_string (G_CHECKSUM_MD5, uri, -1); + stream = camel_data_cache_get ( + emd_global_http_cache, "http", uri_md5, NULL); + g_free (uri_md5); + + /* If the URI is not cached and we are not allowed to load it + * then redirect to invalid URI, so that webkit would display + * a native placeholder for it. */ + if (!stream && !display->priv->force_image_load && + !em_format_html_can_load_images (display->priv->formatter)) { + webkit_network_request_set_uri (request, "invalid://protocol"); + return; + } + + new_uri = g_strconcat ("evo-", uri, NULL); + mail_uri = em_format_build_mail_uri (formatter->folder, + formatter->message_uid, NULL, NULL); + + soup_uri = soup_uri_new (new_uri); + if (soup_uri->query) { + query = soup_form_decode (soup_uri->query); + } else { + query = g_hash_table_new_full (g_str_hash, g_str_equal, + g_free, g_free); + } + enc = soup_uri_encode (mail_uri, NULL); + g_hash_table_insert (query, g_strdup ("__evo-mail"), enc); + + if (display->priv->force_image_load) { + g_hash_table_insert (query, + g_strdup ("__evo-load-images"), + g_strdup ("true")); + } + + g_free (mail_uri); + + soup_uri_set_query_from_form (soup_uri, query); + g_free (new_uri); + + new_uri = soup_uri_to_string (soup_uri, FALSE); + webkit_network_request_set_uri (request, new_uri); + + g_free (new_uri); + soup_uri_free (soup_uri); + g_hash_table_unref (query); + } +} + +static WebKitDOMElement * +find_element_by_id (WebKitDOMDocument *document, + const gchar *id) +{ + WebKitDOMNodeList *frames; + WebKitDOMElement *element; + gulong i, length; + + /* Try to look up the element in this DOM document */ + element = webkit_dom_document_get_element_by_id (document, id); + if (element) + return element; + + /* If the element is not here then recursively scan all frames */ + frames = webkit_dom_document_get_elements_by_tag_name(document, "iframe"); + length = webkit_dom_node_list_get_length (frames); + for (i = 0; i < length; i++) { + + WebKitDOMHTMLIFrameElement *iframe = + WEBKIT_DOM_HTML_IFRAME_ELEMENT ( + webkit_dom_node_list_item (frames, i)); + + WebKitDOMDocument *frame_doc = + webkit_dom_html_iframe_element_get_content_document (iframe); + + WebKitDOMElement *el = + find_element_by_id (frame_doc, id); + + if (el) + return el; + } + + return NULL; +} + +static void +mail_display_plugin_widget_resize (GObject *object, + gpointer dummy, + EMailDisplay *display) +{ + GtkWidget *widget; + WebKitDOMElement *parent_element; + gchar *dim; + gint height; + + widget = GTK_WIDGET (object); + gtk_widget_get_preferred_height (widget, &height, NULL); + parent_element = g_object_get_data (object, "parent_element"); + + if (!parent_element || !WEBKIT_DOM_IS_ELEMENT (parent_element)) { + d(printf("%s: %s does not have (valid) parent element!\n", + G_STRFUNC, (gchar *) g_object_get_data (object, "uri"))); + return; + } + + /* Int -> Str */ + dim = g_strdup_printf ("%d", height); + + /* Set height of the containment to match height of the + * GtkWidget it contains */ + webkit_dom_html_object_element_set_height ( + WEBKIT_DOM_HTML_OBJECT_ELEMENT (parent_element), dim); + g_free (dim); +} + +static void +mail_display_plugin_widget_realize_cb (GtkWidget *widget, + gpointer user_data) +{ + /* Initial resize of the element when the widget + * is displayed for the first time. */ + mail_display_plugin_widget_resize (G_OBJECT (widget), NULL, user_data); +} + +static void +plugin_widget_set_parent_element (GtkWidget *widget, + EMailDisplay *display) +{ + const gchar *uri; + WebKitDOMDocument *document; + WebKitDOMElement *element; + + uri = g_object_get_data (G_OBJECT (widget), "uri"); + if (!uri || !*uri) + return; + + document = webkit_web_view_get_dom_document (WEBKIT_WEB_VIEW (display)); + element = find_element_by_id (document, uri); + + if (!element || !WEBKIT_DOM_IS_ELEMENT (element)) { + g_warning ("Failed to find parent for '%s' - no ID set?", uri); + return; + } + + /* Assign the WebKitDOMElement to "parent_element" data of the GtkWidget + * and the GtkWidget to "widget" data of the DOM Element */ + g_object_set_data (G_OBJECT (widget), "parent_element", element); + g_object_set_data (G_OBJECT (element), "widget", widget); +} + +static void +attachment_button_expanded (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + EAttachmentButton *button = E_ATTACHMENT_BUTTON (object); + WebKitDOMElement *attachment = user_data; + WebKitDOMCSSStyleDeclaration *css; + gboolean expanded; + + d(printf("Attachment button %s (%p) expansion state toggled!\n", + (gchar *) g_object_get_data (object, "uri"), object)); + + expanded = e_attachment_button_get_expanded (button) && + gtk_widget_get_visible (GTK_WIDGET (button)); + + if (!WEBKIT_DOM_IS_ELEMENT (attachment)) { + d(printf("%s: Parent element for button %s does not exist!\n", + G_STRFUNC, (gchar *) g_object_get_data (object, "uri"))); + return; + } + + /* Show or hide the DIV which contains the attachment (iframe, image...) */ + css = webkit_dom_element_get_style (attachment); + webkit_dom_css_style_declaration_set_property ( + css, "display", expanded ? "block" : "none", "", NULL); +} + +static void +constraint_widget_visibility (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + GtkWidget *widget = GTK_WIDGET (object); + EAttachmentButton *button = user_data; + + gboolean can_show = e_attachment_button_get_expanded (button); + gboolean is_visible = gtk_widget_get_visible (widget); + + if (is_visible && !can_show) + gtk_widget_hide (widget); + else if (!is_visible && can_show) + gtk_widget_show (widget); + + /* Otherwise it's OK */ +} + +static void +bind_iframe_content_visibility (EAttachmentButton *button, + WebKitDOMElement *iframe) +{ + WebKitDOMDocument *document; + WebKitDOMNodeList *nodes; + gulong i, length; + + if (!WEBKIT_DOM_IS_HTML_IFRAME_ELEMENT (iframe)) + return; + + document = webkit_dom_html_iframe_element_get_content_document ( + WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)); + nodes = webkit_dom_document_get_elements_by_tag_name (document, "object"); + length = webkit_dom_node_list_get_length (nodes); - else { - /* Chain up to parent's link_clicked() method. */ - GTK_HTML_CLASS (e_mail_display_parent_class)-> - link_clicked (html, uri); + d(printf("Found %ld objects within iframe %s\n", length, + webkit_dom_html_iframe_element_get_name ( + WEBKIT_DOM_HTML_IFRAME_ELEMENT (iframe)))); + + /* Iterate through all s and bind visibility of their widget + * with expanded-state of related attachment button */ + for (i = 0; i < length; i++) { + + WebKitDOMNode *node = webkit_dom_node_list_item (nodes, i); + GtkWidget *widget; + + widget = g_object_get_data (G_OBJECT (node), "widget"); + if (!widget) + continue; + + d(printf("Binding visibility of widget %s (%p) with button %s (%p)\n", + (gchar *) g_object_get_data (G_OBJECT (widget), "uri"), widget, + (gchar *) g_object_get_data (G_OBJECT (button), "uri"), button)); + + g_object_bind_property ( + button, "expanded", + widget, "visible", + G_BINDING_SYNC_CREATE); + + /* Ensure that someone won't attempt to _show() the widget when + * it is supposed to be hidden and vice versa. */ + g_signal_connect (widget, "notify::visible", + G_CALLBACK (constraint_widget_visibility), button); } } +static void +bind_attachment_iframe_visibility (GObject *object, + GParamSpec *pspec, + gpointer user_data) +{ + WebKitWebFrame *webframe; + const gchar *frame_name; + gchar *button_uri; + WebKitDOMDocument *document; + WebKitDOMElement *attachment; + WebKitDOMElement *button_element; + WebKitDOMNodeList *nodes; + gulong i, length; + GtkWidget *button; + + /* Whenever an