From b9f9bf1a0763492f8bf41b8856bbd1149c5871a9 Mon Sep 17 00:00:00 2001 From: Diego Escalante Urrelo Date: Tue, 18 Jan 2011 11:10:11 -0500 Subject: ephy-download: add the new EphyDownload object EphyDownload is a wrapper object around WebKitDownload that handles common behavior in downloads: auto-destination, default action for the MIME type. It can be used to wrap a WebKitDownload coming from a WebKitView or to download a url: ephy_download_new_for_uri and ephy_download_new_for_download are provided. Its lifetime is not automagic like EphyEmbedPersist, so you have to unref it when you no longer need it. This new object replaces EphyEmbedPersist and enables us to use a single codepath for downloads in all Epiphany. Bug #618443 --- src/Makefile.am | 4 +- src/ephy-session.c | 21 ++----- src/ephy-shell.c | 1 - src/ephy-window.c | 1 - src/epiphany.h.in | 1 + src/popup-commands.c | 160 ++++++++++++++++++++++++-------------------------- src/window-commands.c | 1 - 7 files changed, 83 insertions(+), 106 deletions(-) (limited to 'src') diff --git a/src/Makefile.am b/src/Makefile.am index ad6bf9ebc..dd9a0e13f 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -264,10 +264,10 @@ MAINTAINERCLEANFILES = $(stamp_files) $(BUILT_SOURCES) if HAVE_INTROSPECTION EPHY_GIR_H_FILES = \ + $(top_srcdir)/embed/ephy-download.h \ $(top_srcdir)/embed/ephy-embed.h \ $(top_srcdir)/embed/ephy-embed-container.h \ $(top_srcdir)/embed/ephy-embed-event.h \ - $(top_srcdir)/embed/ephy-embed-persist.h \ $(top_srcdir)/embed/ephy-embed-shell.h \ $(top_srcdir)/embed/ephy-embed-single.h \ $(top_srcdir)/embed/ephy-favicon-cache.h \ @@ -294,10 +294,10 @@ EPHY_GIR_H_FILES = \ $(NULL) EPHY_GIR_C_FILES = \ + $(top_srcdir)/embed/ephy-download.c \ $(top_srcdir)/embed/ephy-embed.c \ $(top_srcdir)/embed/ephy-embed-container.c \ $(top_srcdir)/embed/ephy-embed-event.c \ - $(top_srcdir)/embed/ephy-embed-persist.c \ $(top_srcdir)/embed/ephy-embed-shell.c \ $(top_srcdir)/embed/ephy-embed-single.c \ $(top_srcdir)/embed/ephy-favicon-cache.c \ diff --git a/src/ephy-session.c b/src/ephy-session.c index cee9e490e..65ca3d228 100644 --- a/src/ephy-session.c +++ b/src/ephy-session.c @@ -167,7 +167,6 @@ confirm_shutdown_dialog_weak_ref_cb (InteractData *data, EphySessionPrivate *priv = data->session->priv; EggSMClient *sm_client = data->sm_client; EphyShell *shell; - GObject *dv; gboolean will_quit; LOG ("confirm_shutdown_dialog_weak_ref_cb response %d", data->response); @@ -180,14 +179,6 @@ confirm_shutdown_dialog_weak_ref_cb (InteractData *data, g_object_weak_unref (G_OBJECT (shell), (GWeakNotify) confirm_shutdown_dialog_accept_cb, data); - - dv = ephy_embed_shell_get_downloader_view_nocreate (ephy_embed_shell_get_default ()); - if (dv != NULL) - { - g_object_weak_unref (dv, - (GWeakNotify) confirm_shutdown_dialog_accept_cb, - data); - } } if (data->timeout_id != 0) @@ -208,15 +199,16 @@ client_quit_requested_cb (EggSMClient *sm_client, EphySession *session) { EphySessionPrivate *priv = session->priv; - GObject *dv; GtkWidget *dialog, *box; InteractData *data; + GList *downloads; /* If we're shutting down, check if there are downloads * remaining, since they can't be restarted. */ - if (ephy_shell_get_default () == NULL || - (dv = ephy_embed_shell_get_downloader_view_nocreate (ephy_embed_shell_get_default ())) == NULL) + + downloads = ephy_embed_shell_get_downloads (embed_shell); + if (ephy_shell_get_default () == NULL || g_list_length (downloads) == 0) { egg_sm_client_will_quit (sm_client, TRUE); return; @@ -271,11 +263,6 @@ client_quit_requested_cb (EggSMClient *sm_client, (GWeakNotify) confirm_shutdown_dialog_accept_cb, data); - /* When the download finishes, un-veto the shutdown */ - g_object_weak_ref (dv, - (GWeakNotify) confirm_shutdown_dialog_accept_cb, - data); - g_signal_connect (dialog, "response", G_CALLBACK (confirm_shutdown_dialog_response_cb), data); g_object_weak_ref (G_OBJECT (dialog), diff --git a/src/ephy-shell.c b/src/ephy-shell.c index f15ceda6a..6fddf9d2d 100644 --- a/src/ephy-shell.c +++ b/src/ephy-shell.c @@ -48,7 +48,6 @@ #include "ephy-extensions-manager.h" #include "ephy-session.h" #include "ephy-lockdown.h" -#include "downloader-view.h" #include "egg-toolbars-model.h" #include "ephy-toolbars-model.h" #include "ephy-toolbar.h" diff --git a/src/ephy-window.c b/src/ephy-window.c index 98eb1c44d..b251fb00f 100644 --- a/src/ephy-window.c +++ b/src/ephy-window.c @@ -52,7 +52,6 @@ #include "ephy-fullscreen-popup.h" #include "ephy-action-helper.h" #include "ephy-find-toolbar.h" -#include "ephy-embed-persist.h" #include "ephy-location-entry.h" #include "ephy-web-view.h" diff --git a/src/epiphany.h.in b/src/epiphany.h.in index a722c556c..8664c15bd 100644 --- a/src/epiphany.h.in +++ b/src/epiphany.h.in @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include diff --git a/src/popup-commands.c b/src/popup-commands.c index 1bbce9dce..0bec4fbb2 100644 --- a/src/popup-commands.c +++ b/src/popup-commands.c @@ -21,12 +21,13 @@ #include "config.h" #include "popup-commands.h" +#include "ephy-download.h" #include "ephy-shell.h" #include "ephy-embed-container.h" -#include "ephy-embed-persist.h" #include "ephy-embed-utils.h" #include "ephy-prefs.h" #include "ephy-file-helpers.h" +#include "ephy-file-chooser.h" #include "ephy-bookmarks-ui.h" #include "ephy-web-view.h" @@ -179,28 +180,25 @@ popup_cmd_copy_link_address (GtkAction *action, } static void -save_property_url_completed_cb (EphyEmbedPersist *persist) +response_cb (GtkDialog *dialog, + int response_id, + EphyDownload *download) { - if (!(ephy_embed_persist_get_flags (persist) & - EPHY_EMBED_PERSIST_ASK_DESTINATION)) + if (response_id == GTK_RESPONSE_ACCEPT) { - const char *dest; - GFile *dest_file; - guint32 user_time; + char *uri; - user_time = ephy_embed_persist_get_user_time (persist); - dest = ephy_embed_persist_get_dest (persist); - - g_return_if_fail (dest != NULL); - - dest_file = g_file_new_for_path (dest); - - g_return_if_fail (dest_file != NULL); - /* If save location is the desktop, nautilus will not open */ - ephy_file_browse_to (dest_file, user_time); - - g_object_unref (dest_file); + uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog)); + ephy_download_set_destination_uri (download, uri); + ephy_download_start (download); + g_free (uri); + } + else + { + ephy_download_cancel (download); } + + gtk_widget_destroy (GTK_WIDGET (dialog)); } static void @@ -213,38 +211,44 @@ save_property_url (GtkAction *action, EphyEmbedEvent *event; const char *location; GValue value = { 0, }; - EphyEmbedPersist *persist; - EphyEmbed *embed; + EphyDownload *download; event = ephy_window_get_context_event (window); g_return_if_fail (event != NULL); - embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window)); - g_return_if_fail (embed != NULL); - ephy_embed_event_get_property (event, property, &value); location = g_value_get_string (&value); - persist = EPHY_EMBED_PERSIST - (g_object_new (EPHY_TYPE_EMBED_PERSIST, NULL)); - - ephy_embed_persist_set_fc_title (persist, title); - ephy_embed_persist_set_fc_parent (persist, GTK_WINDOW (window)); - ephy_embed_persist_set_flags - (persist, EPHY_EMBED_PERSIST_FROM_CACHE | - (ask_dest ? EPHY_EMBED_PERSIST_ASK_DESTINATION : 0)); - ephy_embed_persist_set_persist_key - (persist, EPHY_PREFS_STATE_SAVE_DIR); - ephy_embed_persist_set_source (persist, location); - ephy_embed_persist_set_embed (persist, embed); - - g_signal_connect (persist, "completed", - G_CALLBACK (save_property_url_completed_cb), NULL); - - ephy_embed_persist_save (persist); - - g_object_unref (G_OBJECT (persist)); + download = ephy_download_new_for_uri (location); + ephy_download_set_window (download, GTK_WIDGET (window)); g_value_unset (&value); + + if (ask_dest) + { + EphyFileChooser *dialog; + char *base; + + base = g_path_get_basename (location); + dialog = ephy_file_chooser_new (title, GTK_WIDGET (window), + GTK_FILE_CHOOSER_ACTION_SAVE, + EPHY_PREFS_STATE_SAVE_DIR, + EPHY_FILE_FILTER_ALL); + + gtk_file_chooser_set_do_overwrite_confirmation + (GTK_FILE_CHOOSER (dialog), TRUE); + gtk_file_chooser_set_current_name + (GTK_FILE_CHOOSER (dialog), base); + g_signal_connect (dialog, "response", + G_CALLBACK (response_cb), download); + gtk_widget_show (GTK_WIDGET (dialog)); + + g_free (base); + } + else + { + ephy_download_set_auto_destination (download); + ephy_download_start (download); + } } void @@ -293,15 +297,15 @@ popup_cmd_save_image_as (GtkAction *action, #define GNOME_APPEARANCE_PROPERTIES "gnome-appearance-properties.desktop" static void -background_download_completed (EphyEmbedPersist *persist, +background_download_completed (EphyDownload *download, GtkWidget *window) { - const char *bg; + char *bg; guint32 user_time; - user_time = ephy_embed_persist_get_user_time (persist); + user_time = ephy_download_get_start_time (download); - bg = ephy_embed_persist_get_dest (persist); + bg = g_filename_from_uri (ephy_download_get_destination_uri (download), NULL, NULL); /* open the Appearance Properties capplet on the Background tab */ if (!ephy_file_launch_desktop_file (GNOME_APPEARANCE_PROPERTIES, bg, user_time, window)) @@ -315,8 +319,7 @@ background_download_completed (EphyEmbedPersist *persist, ephy_file_launch_desktop_file ("gnome-background.desktop", bg, user_time, window); } } - - g_object_unref (persist); + g_free (bg); } void @@ -325,43 +328,36 @@ popup_cmd_set_image_as_background (GtkAction *action, { EphyEmbedEvent *event; const char *location; - char *dest, *base, *base_converted; + char *dest_uri, *dest, *base, *base_converted; GValue value = { 0, }; - EphyEmbedPersist *persist; - EphyEmbed *embed; + EphyDownload *download; event = ephy_window_get_context_event (window); g_return_if_fail (event != NULL); - embed = ephy_embed_container_get_active_child (EPHY_EMBED_CONTAINER (window)); - g_return_if_fail (embed != NULL); - ephy_embed_event_get_property (event, "image-uri", &value); location = g_value_get_string (&value); - persist = EPHY_EMBED_PERSIST - (g_object_new (EPHY_TYPE_EMBED_PERSIST, NULL)); + download = ephy_download_new_for_uri (location); + ephy_download_set_window (download, GTK_WIDGET (window)); base = g_path_get_basename (location); base_converted = g_filename_from_utf8 (base, -1, NULL, NULL, NULL); dest = g_build_filename (ephy_dot_dir (), base_converted, NULL); + dest_uri = g_filename_to_uri (dest, NULL, NULL); - ephy_embed_persist_set_dest (persist, dest); - ephy_embed_persist_set_flags (persist, EPHY_EMBED_PERSIST_NO_VIEW | - EPHY_EMBED_PERSIST_FROM_CACHE); - ephy_embed_persist_set_source (persist, location); + ephy_download_set_destination_uri (download, dest_uri); - g_signal_connect (persist, "completed", - G_CALLBACK (background_download_completed), - window); + g_signal_connect (download, "completed", + G_CALLBACK (background_download_completed), window); - ephy_embed_persist_save (persist); - g_object_unref (persist); + ephy_download_start (download); g_value_unset (&value); - g_free (dest); g_free (base); g_free (base_converted); + g_free (dest); + g_free (dest_uri); } void @@ -425,19 +421,19 @@ image_open_uri (GFile *file, } static void -save_source_completed_cb (EphyEmbedPersist *persist) +save_source_completed_cb (EphyDownload *download) { const char *dest; const char *source; guint32 user_time; GFile *file; - user_time = ephy_embed_persist_get_user_time (persist); - dest = ephy_embed_persist_get_dest (persist); - source = ephy_embed_persist_get_source (persist); + user_time = ephy_download_get_start_time (download); + dest = ephy_download_get_destination_uri (download); + source = ephy_download_get_source_uri (download); g_return_if_fail (dest != NULL); - file = g_file_new_for_path (dest); + file = g_file_new_for_uri (dest); image_open_uri (file, source, user_time); g_object_unref (file); @@ -446,9 +442,9 @@ save_source_completed_cb (EphyEmbedPersist *persist) static void save_temp_source (const char *address) { - EphyEmbedPersist *persist; + EphyDownload *download; const char *static_temp_dir; - char *base, *tmp_name, *tmp_path, *dest; + char *base, *tmp_name, *tmp_path, *dest, *dest_uri; if (address == NULL) return; @@ -467,21 +463,17 @@ save_temp_source (const char *address) if (dest == NULL) return; - persist = EPHY_EMBED_PERSIST - (g_object_new (EPHY_TYPE_EMBED_PERSIST, NULL)); - - ephy_embed_persist_set_source (persist, address); - ephy_embed_persist_set_dest (persist, dest); - ephy_embed_persist_set_flags (persist, EPHY_EMBED_PERSIST_FROM_CACHE | - EPHY_EMBED_PERSIST_NO_VIEW); + dest_uri = g_filename_to_uri (dest, NULL, NULL); + download = ephy_download_new_for_uri (address); + ephy_download_set_destination_uri (download, dest_uri); - g_signal_connect (persist, "completed", + g_signal_connect (download, "completed", G_CALLBACK (save_source_completed_cb), NULL); - ephy_embed_persist_save (persist); - g_object_unref (persist); + ephy_download_start (download); g_free (dest); + g_free (dest_uri); } void diff --git a/src/window-commands.c b/src/window-commands.c index ff3647c59..6b70b09e9 100644 --- a/src/window-commands.c +++ b/src/window-commands.c @@ -27,7 +27,6 @@ #include "ephy-embed-single.h" #include "ephy-embed-utils.h" #include "ephy-shell.h" -#include "ephy-embed-persist.h" #include "ephy-debug.h" #include "window-commands.h" #include "ephy-prefs.h" -- cgit