/* ** Copyright (C) 2011 Dirk-Jan C. Binnema ** ** This program is free software; you can redistribute it and/or modify it ** under the terms of the GNU General Public License as published by the ** Free Software Foundation; either version 3, or (at your option) any ** later version. ** ** This program is distributed in the hope that it will be useful, ** but WITHOUT ANY WARRANTY; without even the implied warranty of ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ** GNU General Public License for more details. ** ** You should have received a copy of the GNU General Public License ** along with this program; if not, write to the Free Software Foundation, ** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. ** */ #include #include #include "mu-msg-body-view.h" #include /* 'private'/'protected' functions */ static void mu_msg_body_view_class_init (MuMsgBodyViewClass *klass); static void mu_msg_body_view_init (MuMsgBodyView *obj); static void mu_msg_body_view_finalize (GObject *obj); /* list my signals */ enum { ACTION_REQUESTED, /* MY_SIGNAL_2, */ LAST_SIGNAL }; struct _MuMsgBodyViewPrivate { WebKitWebSettings *_settings; MuMsg *_message; /* 'mu_mode' means some internal mode where cmd: urls are * acceptable */ gboolean _mu_mode; }; #define MU_MSG_BODY_VIEW_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE((o), \ MU_TYPE_MSG_BODY_VIEW, \ MuMsgBodyViewPrivate)) /* globals */ static WebKitWebViewClass *parent_class = NULL; static guint signals[LAST_SIGNAL] = {0}; G_DEFINE_TYPE (MuMsgBodyView, mu_msg_body_view, WEBKIT_TYPE_WEB_VIEW); static void mu_msg_body_view_class_init (MuMsgBodyViewClass *klass) { GObjectClass *gobject_class; gobject_class = (GObjectClass*) klass; parent_class = g_type_class_peek_parent (klass); gobject_class->finalize = mu_msg_body_view_finalize; g_type_class_add_private (gobject_class, sizeof(MuMsgBodyViewPrivate)); signals[ACTION_REQUESTED] = g_signal_new ("action-requested", G_TYPE_FROM_CLASS (gobject_class), G_SIGNAL_RUN_FIRST, G_STRUCT_OFFSET (MuMsgBodyViewClass, action_requested), NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING); } static char* save_file_for_cid (MuMsg *msg, const char* cid) { gint idx; gchar *filepath; gboolean rv; g_return_val_if_fail (msg, NULL); g_return_val_if_fail (cid, NULL); idx = mu_msg_part_find_cid (msg, cid); if (idx < 0) { g_warning ("%s: cannot find %s", __FUNCTION__, cid); return NULL; } filepath = mu_msg_part_filepath_cache (msg, idx); if (!filepath) { g_warning ("%s: cannot create filepath", filepath); return NULL; } rv = mu_msg_part_save (msg, filepath, idx, FALSE, TRUE); if (!rv) { g_warning ("%s: failed to save %s", __FUNCTION__, filepath); g_free (filepath); filepath = NULL; } return filepath; } static WebKitNavigationResponse on_navigation_requested (MuMsgBodyView *self, WebKitWebFrame *frame, WebKitNetworkRequest *request, gpointer data) { const char* uri; uri = webkit_network_request_get_uri (request); /* if there are 'cmd:" links in the body text of * mu-internal messages (ie., notification from mu, not real * e-mail messages), we emit the 'action requested' * signal. this allows e.g triggering a database refresh from * a Refresh link */ if (g_ascii_strncasecmp (uri, "cmd:", 4) == 0) { if (self->_priv->_mu_mode) { g_signal_emit (G_OBJECT(self), signals[ACTION_REQUESTED], 0, uri + 4); } return WEBKIT_NAVIGATION_RESPONSE_IGNORE; } /* don't try to play files on our local file system, this is not something * external content should do.*/ if (!mu_util_is_local_file(uri)) mu_util_play (uri, FALSE, TRUE); return WEBKIT_NAVIGATION_RESPONSE_IGNORE; } static void on_resource_request_starting (MuMsgBodyView *self, WebKitWebFrame *frame, WebKitWebResource *resource, WebKitNetworkRequest *request, WebKitNetworkResponse *response, gpointer data) { const char* uri; MuMsg *msg; msg = self->_priv->_message; uri = webkit_network_request_get_uri (request); if (g_ascii_strncasecmp (uri, "cid:", 4) == 0) { gchar *filepath; filepath = save_file_for_cid (msg, uri); if (filepath) { gchar *fileuri; fileuri = g_strdup_printf ("file://%s", filepath); webkit_network_request_set_uri (request, fileuri); g_free (fileuri); g_free (filepath); } } } static void mu_msg_body_view_init (MuMsgBodyView *obj) { obj->_priv = MU_MSG_BODY_VIEW_GET_PRIVATE(obj); obj->_priv->_message = NULL; obj->_priv->_mu_mode = FALSE; obj->_priv->_settings = webkit_web_settings_new (); g_object_set (G_OBJECT(obj->_priv->_settings), "enable-scripts", FALSE, "auto-load-images", TRUE, "enable-plugins", FALSE, NULL); webkit_web_view_set_settings (WEBKIT_WEB_VIEW(obj), obj->_priv->_settings); webkit_web_view_set_editable (WEBKIT_WEB_VIEW(obj), FALSE); /* to support cid: */ g_signal_connect (obj, "resource-request-starting", G_CALLBACK (on_resource_request_starting), NULL); /* handle navigation requests */ g_signal_connect (obj, "navigation-requested", G_CALLBACK (on_navigation_requested), (void*)0x666); } static void mu_msg_body_view_finalize (GObject *obj) { MuMsgBodyViewPrivate *priv; priv = MU_MSG_BODY_VIEW_GET_PRIVATE(obj); if (priv && priv->_settings) g_object_unref (priv->_settings); if (priv->_message) mu_msg_unref (priv->_message); G_OBJECT_CLASS(parent_class)->finalize (obj); } GtkWidget* mu_msg_body_view_new (void) { return GTK_WIDGET(g_object_new(MU_TYPE_MSG_BODY_VIEW, NULL)); } void set_html (MuMsgBodyView *self, const char* html) { g_return_if_fail (MU_IS_MSG_BODY_VIEW(self)); webkit_web_view_load_string (WEBKIT_WEB_VIEW(self), html ? html : "", "text/html", "utf-8", ""); } static void set_text (MuMsgBodyView *self, const char* txt) { g_return_if_fail (MU_IS_MSG_BODY_VIEW(self)); webkit_web_view_load_string (WEBKIT_WEB_VIEW(self), txt ? txt : "", "text/plain", "utf-8", ""); } void mu_msg_body_view_set_message (MuMsgBodyView *self, MuMsg *msg) { const char* data; g_return_if_fail (self); if (self->_priv->_message) { mu_msg_unref (self->_priv->_message); self->_priv->_message = NULL; } self->_priv->_message = msg ? mu_msg_ref (msg) : NULL; data = msg ? mu_msg_get_body_html (msg) : ""; if (data) set_html (self, data); else set_text (self, mu_msg_get_body_text (msg)); self->_priv->_mu_mode = FALSE; } void mu_msg_body_view_set_note (MuMsgBodyView *self, const gchar *html) { g_return_if_fail (self); g_return_if_fail (html); if (self->_priv->_message) { mu_msg_unref (self->_priv->_message); self->_priv->_message = NULL; } set_html (self, html); self->_priv->_mu_mode = TRUE; }