[epiphany] downloads: Use the filename suggested by the server when asking the user



commit 1227c9732890da40a14e746080792a784b9131bb
Author: Carlos Garcia Campos <cgarcia igalia com>
Date:   Thu Jan 9 13:30:52 2014 +0100

    downloads: Use the filename suggested by the server when asking the user
    
    https://bugzilla.gnome.org/show_bug.cgi?id=674291

 src/popup-commands.c |  109 ++++++++++++++++++-------------------------------
 1 files changed, 40 insertions(+), 69 deletions(-)
---
diff --git a/src/popup-commands.c b/src/popup-commands.c
index 5146b25..10c3d44 100644
--- a/src/popup-commands.c
+++ b/src/popup-commands.c
@@ -150,71 +150,58 @@ popup_cmd_copy_link_address (GtkAction *action,
        }
 }
 
-static void
-save_property_url_to_destination (EphyWindow *window,
-                                 const char *location,
-                                 const char *destination)
+static gboolean
+cancel_download_idle_cb (EphyDownload *download)
 {
-       EphyDownload *download;
+       ephy_download_cancel (download);
 
-       download = ephy_download_new_for_uri (location, GTK_WINDOW (window));
-
-       if (destination)
-               ephy_download_set_destination_uri (download, destination);
-       ephy_window_add_download (window, download);
-       g_object_unref (download);
+       return FALSE;
 }
 
 static void
-response_cb (GtkDialog *dialog,
-            int response_id,
-            char *location)
+filename_suggested_cb (EphyDownload *download,
+                      const char *suggested_filename,
+                      char *title)
 {
-       if (response_id == GTK_RESPONSE_ACCEPT)
-       {
-               char *uri;
-               GtkWindow *window;
+       EphyWindow *window;
+       EphyFileChooser *dialog;
+       char *sanitized_filename;
 
-               window = gtk_window_get_transient_for (GTK_WINDOW (dialog));
+       window = EPHY_WINDOW (ephy_download_get_window (download));
 
-               uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
-               save_property_url_to_destination (EPHY_WINDOW (window), location, uri);
-               g_free (uri);
-       }
+       dialog = ephy_file_chooser_new (title,
+                                       GTK_WIDGET (window),
+                                       GTK_FILE_CHOOSER_ACTION_SAVE,
+                                       EPHY_FILE_FILTER_NONE);
+       gtk_window_set_modal (GTK_WINDOW (dialog), TRUE);
+       gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (dialog), TRUE);
 
-       g_free (location);
-       gtk_widget_destroy (GTK_WIDGET (dialog));
-}
+       sanitized_filename = ephy_sanitize_filename (g_strdup (suggested_filename));
+       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), sanitized_filename);
+       g_free (sanitized_filename);
 
-static char *
-get_suggested_filename (EphyWebView *view)
-{
-       char *suggested_filename = NULL;
-       const char *mimetype;
-       WebKitURIResponse *response;
-       WebKitWebResource *web_resource;
+       if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
+       {
+               char *uri;
 
-       web_resource = webkit_web_view_get_main_resource (WEBKIT_WEB_VIEW (view));
-       response = webkit_web_resource_get_response (web_resource);
-       mimetype = webkit_uri_response_get_mime_type (response);
+               uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
+               ephy_download_set_destination_uri (download, uri);
+               g_free (uri);
 
-       if ((g_ascii_strncasecmp (mimetype, "text/html", 9)) == 0)
-       {
-               /* Web Title will be used as suggested filename */
-               suggested_filename = g_strconcat (ephy_web_view_get_title (view), ".mhtml", NULL);
+               ephy_window_add_download (window, download);
        }
        else
        {
-               suggested_filename = g_strdup (webkit_uri_response_get_suggested_filename (response));
-               if (!suggested_filename)
-               {
-                       SoupURI *soup_uri = soup_uri_new (webkit_web_resource_get_uri (web_resource));
-                       suggested_filename = g_path_get_basename (soup_uri->path);
-                       soup_uri_free (soup_uri);
-               }
+               g_idle_add_full (G_PRIORITY_DEFAULT,
+                                (GSourceFunc)cancel_download_idle_cb,
+                                g_object_ref (download),
+                                g_object_unref);
        }
 
-       return suggested_filename;
+       gtk_widget_destroy (GTK_WIDGET (dialog));
+       g_free (title);
+
+       g_object_unref (download);
 }
 
 static void
@@ -224,10 +211,7 @@ save_property_url (const char *title,
 {
        EphyEmbedEvent *event;
        const char *location;
-       EphyFileChooser *dialog;
-       EphyEmbed *embed;
-       EphyWebView *view;
-       char *suggested_filename;
+       EphyDownload *download;
        GValue value = { 0, };
 
        event = ephy_window_get_context_event (window);
@@ -235,23 +219,10 @@ save_property_url (const char *title,
 
        ephy_embed_event_get_property (event, property, &value);
        location = g_value_get_string (&value);
-
-       embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window));
-       view = ephy_embed_get_web_view (embed);
-
-       dialog = ephy_file_chooser_new (title, GTK_WIDGET (window),
-                                       GTK_FILE_CHOOSER_ACTION_SAVE,
-                                       EPHY_FILE_FILTER_NONE);
-
-       suggested_filename = ephy_sanitize_filename (get_suggested_filename (view));
-       gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (dialog), suggested_filename);
-       g_free (suggested_filename);
-
-       gtk_file_chooser_set_do_overwrite_confirmation
-               (GTK_FILE_CHOOSER (dialog), TRUE);
-       g_signal_connect (dialog, "response",
-                         G_CALLBACK (response_cb), g_strdup (location));
-       gtk_widget_show (GTK_WIDGET (dialog));
+       download = ephy_download_new_for_uri (location, GTK_WINDOW (window));
+       g_signal_connect (download, "filename-suggested",
+                         G_CALLBACK (filename_suggested_cb),
+                         g_strdup (title));
 
        g_value_unset (&value);
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]