From 94f729f6b3eb5871f5da1909d1b8bbf94a52c940 Mon Sep 17 00:00:00 2001 From: Christian Dywan Date: Tue, 22 May 2012 00:00:57 +0200 Subject: [PATCH] Fallback for missing filename in midori_browser_save_uri If there's only a path, eg. pastebin services do this, take the URI as a fallback and add an extension if it makes sense. --- midori/midori-browser.c | 23 ++++++++++++++++++++--- midori/midori-view.c | 4 +++- midori/midori-view.h | 4 ++++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/midori/midori-browser.c b/midori/midori-browser.c index de250d4f..d1ae4e82 100644 --- a/midori/midori-browser.c +++ b/midori/midori-browser.c @@ -1017,7 +1017,6 @@ midori_browser_save_uri (MidoriBrowser* browser, gboolean folder_set = FALSE; GtkWidget* dialog; gchar* filename; - gchar* dirname; gchar* last_slash; if (!gtk_widget_get_visible (GTK_WIDGET (browser))) @@ -1029,6 +1028,8 @@ midori_browser_save_uri (MidoriBrowser* browser, if (uri) { + gchar* dirname; + /* Base the start folder on the current view's uri if it is local */ filename = g_filename_from_uri (uri, NULL, NULL); if (filename) @@ -1043,13 +1044,29 @@ midori_browser_save_uri (MidoriBrowser* browser, g_free (dirname); g_free (filename); } + } + + if (uri == NULL) + uri = midori_view_get_display_uri (view); /* Try to provide a good default filename, UTF-8 encoded */ filename = soup_uri_decode (uri); last_slash = g_strrstr (filename, "/") + 1; - gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), last_slash); + /* Take the rest of the URI if needed */ + if (*last_slash == '\0') + { + const gchar* extension = midori_view_fallback_extension (view, NULL); + gchar* guessed; + gchar* no_scheme; + last_slash = g_strdelimit (filename, "/\\<>:\"|?*", '-'); + guessed = g_strconcat (filename, extension, NULL); + no_scheme = strstr (guessed, "---") + 3; + gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), no_scheme); + g_free (guessed); + } + else + gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), last_slash); g_free (filename); - } if (!folder_set && last_dir && *last_dir) gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (dialog), last_dir); diff --git a/midori/midori-view.c b/midori/midori-view.c index 4db44ba3..b6fe7ff1 100644 --- a/midori/midori-view.c +++ b/midori/midori-view.c @@ -5318,7 +5318,7 @@ midori_view_get_uri_extension (const gchar* uri) return g_strdup (period); } -static const gchar* +const gchar* midori_view_fallback_extension (MidoriView* view, const gchar* extension) { @@ -5332,6 +5332,8 @@ midori_view_fallback_extension (MidoriView* view, return ".js"; if (strstr (view->mime_type, "html")) return ".htm"; + if (strstr (view->mime_type, "plain")) + return ".txt"; return ""; } diff --git a/midori/midori-view.h b/midori/midori-view.h index feba0526..7c943d86 100644 --- a/midori/midori-view.h +++ b/midori/midori-view.h @@ -272,6 +272,10 @@ void midori_view_save_speed_dial_config (MidoriView* view, GKeyFile* key_file); +const gchar* +midori_view_fallback_extension (MidoriView* view, + const gchar* extension); + G_END_DECLS #endif /* __MIDORI_VIEW_H__ */ -- 2.39.5