From 69837f33cc6701043c9bbef2005c3c8281d5980e Mon Sep 17 00:00:00 2001 From: Matthew Barnes Date: Sat, 27 Jul 2013 01:49:59 -0400 Subject: Add e_web_view_suggest_filename(). Attempts to derive a suggested filename from the given URI for use in a "Save As" dialog. By default the suggested filename is the last path segment of the given URI (the unless the given URI looks like a query), but subclasses can use other mechanisms for custom URI schemes. For example, "cid:" URIs in an email message may refer to a MIME part with a suggested filename in its Content-Disposition header. --- .../evolution-util/evolution-util-sections.txt | 1 + e-util/e-web-view.c | 61 ++++++++++++++++++++++ e-util/e-web-view.h | 4 ++ mail/e-mail-display.c | 40 ++++++++++++++ 4 files changed, 106 insertions(+) diff --git a/doc/reference/evolution-util/evolution-util-sections.txt b/doc/reference/evolution-util/evolution-util-sections.txt index 00cf86d2d1..8643aed5e0 100644 --- a/doc/reference/evolution-util/evolution-util-sections.txt +++ b/doc/reference/evolution-util/evolution-util-sections.txt @@ -4356,6 +4356,7 @@ e_web_view_clear e_web_view_load_string e_web_view_load_uri e_web_view_redirect_uri +e_web_view_suggest_filename e_web_view_reload e_web_view_get_html e_web_view_get_caret_mode diff --git a/e-util/e-web-view.c b/e-util/e-web-view.c index 68ca266ca1..936781a8c1 100644 --- a/e-util/e-web-view.c +++ b/e-util/e-web-view.c @@ -1087,6 +1087,25 @@ web_view_redirect_uri (EWebView *web_view, return g_strdup (uri); } +static gchar * +web_view_suggest_filename (EWebView *web_view, + const gchar *uri) +{ + const gchar *cp; + + /* Try to derive a filename from the last path segment. */ + + cp = strrchr (uri, '/'); + if (cp != NULL) { + if (strchr (cp, '?') == NULL) + cp++; + else + cp = NULL; + } + + return g_strdup (cp); +} + static gboolean web_view_popup_event (EWebView *web_view, const gchar *uri) @@ -1375,6 +1394,7 @@ e_web_view_class_init (EWebViewClass *class) class->load_string = web_view_load_string; class->load_uri = web_view_load_uri; class->redirect_uri = web_view_redirect_uri; + class->suggest_filename = web_view_suggest_filename; class->popup_event = web_view_popup_event; class->stop_loading = web_view_stop_loading; class->update_actions = web_view_update_actions; @@ -1850,6 +1870,47 @@ e_web_view_redirect_uri (EWebView *web_view, return class->redirect_uri (web_view, uri); } +/** + * e_web_view_suggest_filename: + * @web_view: an #EWebView + * @uri: a URI string + * + * Attempts to derive a suggested filename from the @uri for use in a + * "Save As" dialog. + * + * By default the suggested filename is the last path segment of the @uri + * (unless @uri looks like a query), but subclasses can use other mechanisms + * for custom URI schemes. For example, "cid:" URIs in an email message may + * refer to a MIME part with a suggested filename in its Content-Disposition + * header. + * + * The returned string should be freed with g_free() when finished with it, + * but callers should also be prepared for the function to return %NULL if + * a filename cannot be determined. + * + * Returns: a suggested filename, or %NULL + **/ +gchar * +e_web_view_suggest_filename (EWebView *web_view, + const gchar *uri) +{ + EWebViewClass *class; + gchar *filename; + + g_return_val_if_fail (E_IS_WEB_VIEW (web_view), NULL); + g_return_val_if_fail (uri != NULL, NULL); + + class = E_WEB_VIEW_GET_CLASS (web_view); + g_return_val_if_fail (class->suggest_filename != NULL, NULL); + + filename = class->suggest_filename (web_view, uri); + + if (filename != NULL) + e_filename_make_safe (filename); + + return filename; +} + void e_web_view_reload (EWebView *web_view) { diff --git a/e-util/e-web-view.h b/e-util/e-web-view.h index d4b1db650d..df338abd71 100644 --- a/e-util/e-web-view.h +++ b/e-util/e-web-view.h @@ -82,6 +82,8 @@ struct _EWebViewClass { const gchar *load_uri); gchar * (*redirect_uri) (EWebView *web_view, const gchar *uri); + gchar * (*suggest_filename) (EWebView *web_view, + const gchar *uri); void (*set_fonts) (EWebView *web_view, PangoFontDescription **monospace, PangoFontDescription **variable_width); @@ -106,6 +108,8 @@ void e_web_view_load_uri (EWebView *web_view, const gchar *uri); gchar * e_web_view_redirect_uri (EWebView *web_view, const gchar *uri); +gchar * e_web_view_suggest_filename (EWebView *web_view, + const gchar *uri); void e_web_view_reload (EWebView *web_view); gchar * e_web_view_get_html (EWebView *web_view); gboolean e_web_view_get_caret_mode (EWebView *web_view); diff --git a/mail/e-mail-display.c b/mail/e-mail-display.c index 980fc8f984..46cf48a306 100644 --- a/mail/e-mail-display.c +++ b/mail/e-mail-display.c @@ -1477,6 +1477,45 @@ chainup: redirect_uri (web_view, uri); } +static gchar * +mail_display_suggest_filename (EWebView *web_view, + const gchar *uri) +{ + if (g_str_has_prefix (uri, "cid:")) { + EMailDisplay *display; + EMailPartList *part_list; + CamelMimeMessage *message; + CamelMimePart *mime_part; + const gchar *filename; + + /* Note, this assumes the URI comes + * from the currently loaded message. */ + + display = E_MAIL_DISPLAY (web_view); + + part_list = e_mail_display_get_part_list (display); + if (part_list == NULL) + return NULL; + + message = e_mail_part_list_get_message (part_list); + if (message == NULL) + return NULL; + + mime_part = camel_mime_message_get_part_by_content_id ( + message, uri + 4); + if (mime_part == NULL) + return NULL; + + filename = camel_mime_part_get_filename (mime_part); + + return g_strdup (filename); + } + + /* Chain up to parent's suggest_filename() method. */ + return E_WEB_VIEW_CLASS (e_mail_display_parent_class)-> + suggest_filename (web_view, uri); +} + static void mail_display_set_fonts (EWebView *web_view, PangoFontDescription **monospace, @@ -1531,6 +1570,7 @@ e_mail_display_class_init (EMailDisplayClass *class) web_view_class = E_WEB_VIEW_CLASS (class); web_view_class->redirect_uri = mail_display_redirect_uri; + web_view_class->suggest_filename = mail_display_suggest_filename; web_view_class->set_fonts = mail_display_set_fonts; g_object_class_install_property ( -- cgit v1.2.3