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.
This commit is contained in:
Matthew Barnes
2013-07-27 01:49:59 -04:00
parent 51b74eee72
commit 69837f33cc
4 changed files with 106 additions and 0 deletions

View File

@ -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

View File

@ -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)
{

View File

@ -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);

View File

@ -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 (