]> www.fi.muni.cz Git - evince.git/commitdiff
Add functions to delete temporary files created by evince in a safe way.
authorCarlos Garcia Campos <carlosgc@gnome.org>
Fri, 25 May 2007 16:43:19 +0000 (16:43 +0000)
committerCarlos Garcia Campos <carlosgc@src.gnome.org>
Fri, 25 May 2007 16:43:19 +0000 (16:43 +0000)
2007-05-25  Carlos Garcia Campos  <carlosgc@gnome.org>
* libdocument/ev-image.c: (ev_image_finalize):
* libdocument/ev-document-factory.c: (free_uncompressed_uri):
* libdocument/ev-attachment.c: (ev_attachment_finalize):
* libdocument/ev-file-helpers.[ch]: (ev_tmp_filename_unlink),
(ev_tmp_uri_unlink):
Add functions to delete temporary files created by evince in a safe
way.
* shell/ev-window.c: (ev_window_clear_local_uri),
(open_xfer_update_progress_callback),
(save_xfer_update_progress_callback), (ev_window_save_remote),
(file_save_dialog_response_cb), (ev_window_cmd_save_as),
(image_save_dialog_response_cb), (ev_view_popup_cmd_save_image_as),
(attachment_save_dialog_response_cb),
(ev_attachment_popup_cmd_save_attachment_as):
Allow saving a copy of a document, image or attachment to a remote
location. Fixes bug #440754.

svn path=/trunk/; revision=2478

ChangeLog
libdocument/ev-attachment.c
libdocument/ev-document-factory.c
libdocument/ev-file-helpers.c
libdocument/ev-file-helpers.h
libdocument/ev-image.c
shell/ev-application.c
shell/ev-window.c

index feb0cebdfb161dd7f79e359a207896d1f380a9ad..eebc0bcd803d5591af22f50440859acde771c8a3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,25 @@
+2007-05-25  Carlos Garcia Campos  <carlosgc@gnome.org>
+
+       * libdocument/ev-image.c: (ev_image_finalize):
+       * libdocument/ev-document-factory.c: (free_uncompressed_uri):
+       * libdocument/ev-attachment.c: (ev_attachment_finalize):
+       * libdocument/ev-file-helpers.[ch]: (ev_tmp_filename_unlink),
+       (ev_tmp_uri_unlink):
+
+       Add functions to delete temporary files created by evince in a safe
+       way.
+
+       * shell/ev-window.c: (ev_window_clear_local_uri),
+       (open_xfer_update_progress_callback),
+       (save_xfer_update_progress_callback), (ev_window_save_remote),
+       (file_save_dialog_response_cb), (ev_window_cmd_save_as),
+       (image_save_dialog_response_cb), (ev_view_popup_cmd_save_image_as),
+       (attachment_save_dialog_response_cb),
+       (ev_attachment_popup_cmd_save_attachment_as):
+
+       Allow saving a copy of a document, image or attachment to a remote
+       location. Fixes bug #440754. 
+
 2007-05-22  Carlos Garcia Campos  <carlosgc@gnome.org>
 
        * configure.ac:
index 7e7ca127ea0b35483179d555e11253c852db3b9e..980d0569bf842b7fd09e783143adef9db751bdf8 100644 (file)
@@ -97,7 +97,7 @@ ev_attachment_finalize (GObject *object)
        }
 
        if (attachment->priv->tmp_uri) {
-               g_unlink (attachment->priv->tmp_uri);
+               ev_tmp_filename_unlink (attachment->priv->tmp_uri);
                g_free (attachment->priv->tmp_uri);
                attachment->priv->tmp_uri = NULL;
        }
index 4adec66759b3ee723f2f43b8575b902e39dcbb57..b500fdfb6dbe365a5803b5b4ad84db7260ea1b31 100644 (file)
@@ -312,17 +312,10 @@ get_document_from_uri (const char        *uri,
 static void
 free_uncompressed_uri (gchar *uri_unc)
 {
-       gchar *filename;
-
        if (!uri_unc)
                return;
 
-       filename = g_filename_from_uri (uri_unc, NULL, NULL);
-       if (!filename)
-               return;
-
-       g_unlink (filename);
-       g_free (filename);
+       ev_tmp_uri_unlink (uri_unc);
        g_free (uri_unc);
 }
 
index 82e854336ec90793d0f15eb60cd61dab65848bda..dd3b3e1df8ca6051dc53420ac9e1f133a9cb427a 100644 (file)
@@ -145,6 +145,43 @@ ev_tmp_filename (const gchar *prefix)
        return filename;
 }
 
+/* Remove a local temp file created by evince */
+void
+ev_tmp_filename_unlink (const gchar *filename)
+{
+       const gchar *tempdir;
+       
+       if (!filename)
+               return;
+
+       tempdir = g_get_tmp_dir ();
+       if (g_ascii_strncasecmp (filename, tempdir, strlen (tempdir)) == 0) {
+               g_unlink (filename);
+       }
+}
+
+void
+ev_tmp_uri_unlink (const gchar *uri)
+{
+       GnomeVFSURI *vfs_uri;
+       gchar       *filename;
+       
+       if (!uri)
+               return;
+       
+       vfs_uri = gnome_vfs_uri_new (uri);
+       if (!gnome_vfs_uri_is_local (vfs_uri)) {
+               g_warning ("Attempting to delete non local uri: %s\n", uri);
+               gnome_vfs_uri_unref (vfs_uri);
+               return;
+       }
+       gnome_vfs_uri_unref (vfs_uri);
+
+       filename = g_filename_from_uri (uri, NULL, NULL);
+       ev_tmp_filename_unlink (filename);
+       g_free (filename);
+}
+
 gboolean
 ev_xfer_uri_simple (const char *from,
                    const char *to,
index f7d9f9e129a5912dcdb5a3415c29eec8018c4739..6aad12a2eedb55b869a644491a23776fbbcb4078 100644 (file)
@@ -40,10 +40,13 @@ void         ev_file_helpers_init     (void);
 void         ev_file_helpers_shutdown (void);
 
 gchar       *ev_tmp_filename          (const char        *prefix);
+void         ev_tmp_filename_unlink   (const gchar       *filename);
+void         ev_tmp_uri_unlink        (const gchar       *uri);
 
 gboolean     ev_xfer_uri_simple       (const char        *from,
                                       const char        *to,
                                       GError           **error);
+
 gchar       *ev_file_uncompress       (const gchar       *uri,
                                       EvCompressionType  type,
                                       GError           **error);
index f906b0006c9f9da0cf1b496e789ae78bbd6ea002..bcb1e7ddf7bca41fabedc665aeba4cffbda0ce9f 100644 (file)
@@ -42,7 +42,7 @@ ev_image_finalize (GObject *object)
        }
 
        if (image->priv->tmp_uri) {
-               g_unlink (image->priv->tmp_uri);
+               ev_tmp_filename_unlink (image->priv->tmp_uri);
                g_free (image->priv->tmp_uri);
                image->priv->tmp_uri = NULL;
        }
index d2fe2dcae8eb4c55ce1bb9a0349de4a47d2a337c..7633fb7a3cde6f203aed2fbe9b35a3123850f9d4 100644 (file)
@@ -578,7 +578,7 @@ ev_application_open_uri (EvApplication  *application,
        EvLinkDest      *dest = NULL;
        EvWindowRunMode  mode = EV_WINDOW_MODE_NORMAL;
        gboolean         unlink_temp_file = FALSE;
-       const gchar     *print_settings;
+       const gchar     *print_settings = NULL;
        GdkScreen       *screen = NULL;
 
        if (args) {
index dbba0990df2f2e89820844cb8eed0672cb7f18db..48d9b2a2b7b7ac37df06040b1d20af361b472b27 100644 (file)
@@ -89,6 +89,7 @@
 #include <gtk/gtk.h>
 #include <libgnomevfs/gnome-vfs-utils.h>
 #include <libgnomevfs/gnome-vfs-async-ops.h>
+#include <libgnomevfs/gnome-vfs-ops.h>
 #include <gconf/gconf-client.h>
 
 #include <errno.h>
@@ -1231,14 +1232,8 @@ ev_window_clear_load_job (EvWindow *ev_window)
 static void
 ev_window_clear_local_uri (EvWindow *ev_window)
 {
-    char *filename;
-    
     if (ev_window->priv->local_uri) {
-           filename = g_filename_from_uri (ev_window->priv->local_uri, NULL, NULL);
-           if (filename != NULL) {
-                   g_unlink (filename);
-                   g_free (filename);
-           }
+           ev_tmp_uri_unlink (ev_window->priv->local_uri);
            g_free (ev_window->priv->local_uri);
            ev_window->priv->local_uri = NULL;
     }
@@ -1413,9 +1408,9 @@ ev_window_close_dialogs (EvWindow *ev_window)
 }
 
 static gint
-xfer_update_progress_callback (GnomeVFSAsyncHandle      *handle,
-                              GnomeVFSXferProgressInfo *info,
-                              EvWindow                 *ev_window)
+open_xfer_update_progress_callback (GnomeVFSAsyncHandle      *handle,
+                                   GnomeVFSXferProgressInfo *info,
+                                   EvWindow                 *ev_window)
 {
        switch (info->status) {
                case GNOME_VFS_XFER_PROGRESS_STATUS_OK:
@@ -1499,7 +1494,8 @@ ev_window_open_uri (EvWindow       *ev_window,
                                      GNOME_VFS_XFER_ERROR_MODE_ABORT,
                                      GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
                                      GNOME_VFS_PRIORITY_DEFAULT,
-                                     (GnomeVFSAsyncXferProgressCallback)xfer_update_progress_callback,
+                                     (GnomeVFSAsyncXferProgressCallback)
+                                     open_xfer_update_progress_callback,
                                      ev_window,
                                      NULL, NULL); 
                
@@ -1560,9 +1556,9 @@ ev_window_cmd_file_open (GtkAction *action, EvWindow *window)
        } else if (window->priv->uri != NULL) {
                gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (chooser),
                                          window->priv->uri);
-       }
-       else {
+       } else {
                char *folder;
+               
                folder = xdg_user_dir_lookup ("DOCUMENTS");
                gtk_file_chooser_set_current_folder (GTK_FILE_CHOOSER (chooser),
                                                     folder);
@@ -1891,28 +1887,107 @@ ev_window_setup_recent (EvWindow *ev_window)
 #endif /* HAVE_GTK_RECENT */
 }
 
+static gint
+save_xfer_update_progress_callback (GnomeVFSAsyncHandle      *handle,
+                                   GnomeVFSXferProgressInfo *info,
+                                   GnomeVFSURI              *tmp_uri)
+{
+       switch (info->status) {
+               case GNOME_VFS_XFER_PROGRESS_STATUS_OK:
+                       if (info->phase == GNOME_VFS_XFER_PHASE_COMPLETED) {
+                               gchar *uri;
+
+                               uri = gnome_vfs_uri_to_string (tmp_uri, 0);
+                               ev_tmp_uri_unlink (uri);
+                               g_free (uri);
+                               gnome_vfs_uri_unref (tmp_uri);
+                       }
+                       return 1;
+               case GNOME_VFS_XFER_PROGRESS_STATUS_VFSERROR:
+                       if (info->vfs_status != GNOME_VFS_OK) {
+                               GtkWidget *dialog;
+                               gchar     *uri;
+
+                               dialog = gtk_message_dialog_new (NULL,
+                                                                GTK_DIALOG_DESTROY_WITH_PARENT,
+                                                                GTK_MESSAGE_ERROR,
+                                                                GTK_BUTTONS_CLOSE,
+                                                                _("The file could not be saved as “%s”."),
+                                                                info->target_name);
+                               gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
+                                                                         gnome_vfs_result_to_string (info->vfs_status));
+
+                               g_signal_connect (dialog, "response",
+                                                 G_CALLBACK (gtk_widget_destroy),
+                                                 NULL);
+                               gtk_widget_show (dialog);
+
+                               uri = gnome_vfs_uri_to_string (tmp_uri, 0);
+                               ev_tmp_uri_unlink (uri);
+                               g_free (uri);
+                               gnome_vfs_uri_unref (tmp_uri);
+                       }
+                       return 1;
+               case GNOME_VFS_XFER_PROGRESS_STATUS_OVERWRITE:
+               case GNOME_VFS_XFER_PROGRESS_STATUS_DUPLICATE:
+                       return 1;
+               default:
+                       g_assert_not_reached ();
+       }
+
+       return 0;
+}
+
+static void
+ev_window_save_remote (EvWindow    *ev_window,
+                      GnomeVFSURI *src,
+                      GnomeVFSURI *dst)
+{
+       GnomeVFSAsyncHandle *handle;
+       GList               *slist = NULL;
+       GList               *tlist = NULL;
+       
+       slist = g_list_prepend (slist, src);
+       tlist = g_list_prepend (tlist, dst);
+       gnome_vfs_async_xfer (&handle, slist, tlist,
+                             GNOME_VFS_XFER_DEFAULT | GNOME_VFS_XFER_FOLLOW_LINKS,
+                             GNOME_VFS_XFER_ERROR_MODE_ABORT,
+                             GNOME_VFS_XFER_OVERWRITE_MODE_REPLACE,
+                             GNOME_VFS_PRIORITY_DEFAULT,
+                             (GnomeVFSAsyncXferProgressCallback)
+                             save_xfer_update_progress_callback,
+                             gnome_vfs_uri_ref (src),
+                             NULL, NULL);
+       g_list_free (slist);
+       g_list_free (tlist);
+}
+
 static void
 file_save_dialog_response_cb (GtkWidget *fc,
                              gint       response_id,
                              EvWindow  *ev_window)
 {
-       const gchar *uri_unc;
-       gint         fd;
-       gchar       *filename;
-       gchar       *tmp_filename;
-       GError      *error = NULL;
+       gchar  *uri;
+       gchar  *local_uri;
+       gint    fd;
+       gchar  *filename;
+       gchar  *tmp_filename;
+       GError *error = NULL;
 
        if (response_id != GTK_RESPONSE_OK) {
                gtk_widget_destroy (fc);
                return;
        }
-       
 
-       filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (fc));
+       uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
+
+       filename = ev_tmp_filename ("saveacopy");
        tmp_filename = g_strdup_printf ("%s.XXXXXX", filename);
+       g_free (filename);
        
        fd = g_mkstemp (tmp_filename);
        if (fd == -1) {
+               gchar *msg;
                gchar *display_name;
                gint   save_errno = errno;
                
@@ -1923,74 +1998,95 @@ file_save_dialog_response_cb (GtkWidget *fc,
                             _("Failed to create file “%s”: %s"),
                             display_name, g_strerror (save_errno));
                g_free (display_name);
-       } else {
-               gchar *uri;
-               
-               uri = g_filename_to_uri (tmp_filename, NULL, NULL);
-               
-               ev_document_doc_mutex_lock ();
-               ev_document_save (ev_window->priv->document, uri, &error);
-               ev_document_doc_mutex_unlock ();
-               
+
+               msg = g_strdup_printf (_("The file could not be saved as “%s”."), uri);
+               ev_window_error_dialog (GTK_WINDOW (ev_window), msg, error);
+
+               g_free (tmp_filename);
                g_free (uri);
-               close (fd);
+               g_free (msg);
+               g_error_free (error);
+               gtk_widget_destroy (fc);
+               
+               return;
        }
 
+       /* Save document to temp filename */
+       local_uri = g_filename_to_uri (tmp_filename, NULL, NULL);
+       
+       ev_document_doc_mutex_lock ();
+       ev_document_save (ev_window->priv->document, local_uri, &error);
+       ev_document_doc_mutex_unlock ();
+       
+       close (fd);
+
        if (!error) {
-               uri_unc = g_object_get_data (G_OBJECT (ev_window->priv->document),
-                                            "uri-uncompressed");
-               if (uri_unc) {
-                       EvCompressionType ctype;
-                       gchar            *uri_comp;
-                       gchar            *uri;
+               /* If original document was compressed,
+                * compress it again before saving
+                */
+               if (g_object_get_data (G_OBJECT (ev_window->priv->document),
+                                      "uri-uncompressed")) {
+                       EvCompressionType ctype = EV_COMPRESSION_NONE;
                        const gchar      *ext;
+                       gchar            *uri_comp;
 
-                       ctype = EV_COMPRESSION_NONE;
-                       
                        ext = g_strrstr (ev_window->priv->uri, ".gz");
                        if (ext && g_ascii_strcasecmp (ext, ".gz") == 0)
                                ctype = EV_COMPRESSION_GZIP;
-                       
+
                        ext = g_strrstr (ev_window->priv->uri, ".bz2");
                        if (ext && g_ascii_strcasecmp (ext, ".bz2") == 0)
                                ctype = EV_COMPRESSION_BZIP2;
                        
-                       uri = g_filename_to_uri (tmp_filename, NULL, NULL);
-                       uri_comp = ev_file_compress (uri, ctype, &error);
-                       g_free (uri);
-                       g_unlink (tmp_filename);
-                       g_free (tmp_filename);
-
+                       uri_comp = ev_file_compress (local_uri, ctype, &error);
+                       g_free (local_uri);
+                       ev_tmp_filename_unlink (tmp_filename);
+                       
                        if (!uri_comp || error) {
-                               tmp_filename = NULL;
+                               local_uri = NULL;
                        } else {
-                               tmp_filename = g_filename_from_uri (uri_comp,
-                                                                   NULL, NULL);
+                               local_uri = uri_comp;
                        }
-                       
-                       g_free (uri_comp);
                }
        }
 
-       if (tmp_filename && g_rename (tmp_filename, filename) == -1) {
-               g_unlink (tmp_filename);
-       }
-       
+       g_free (tmp_filename);
+
        if (error) {
                gchar *msg;
-               gchar *uri;
-               
-               uri = g_filename_to_uri (filename, NULL, NULL);
+
                msg = g_strdup_printf (_("The file could not be saved as “%s”."), uri);
                ev_window_error_dialog (GTK_WINDOW (ev_window), msg, error);
-               g_free (msg);
                g_free (uri);
+               g_free (msg);
                g_error_free (error);
+               g_free (local_uri);
+               gtk_widget_destroy (fc);
+
+               return;
        }
-       
-       g_free (tmp_filename);
-       g_free (filename);
 
+       if (local_uri) {
+               GnomeVFSURI *target_uri;
+
+               target_uri = gnome_vfs_uri_new (uri);
+               if (gnome_vfs_uri_is_local (target_uri)) {
+                       /* If target uri is local, just rename local_uri */
+                       if (gnome_vfs_move (local_uri, uri, TRUE) != GNOME_VFS_OK)
+                               ev_tmp_uri_unlink (local_uri);
+               } else {
+                       GnomeVFSURI *source_uri;
+
+                       source_uri = gnome_vfs_uri_new (local_uri);
+                       ev_window_save_remote (ev_window, source_uri, target_uri);
+                       gnome_vfs_uri_unref (source_uri);
+               }
+
+               gnome_vfs_uri_unref (target_uri);
+               g_free (local_uri);
+       }
+
+       g_free (uri);
        gtk_widget_destroy (fc);
 }
 
@@ -2012,7 +2108,8 @@ ev_window_cmd_save_as (GtkAction *action, EvWindow *ev_window)
        ev_document_factory_add_filters (fc, ev_window->priv->document);
        gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
 
-       gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER (fc), TRUE);    
+       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
+       gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);   
        file_name = gnome_vfs_format_uri_for_display (ev_window->priv->uri);
        base_name = g_path_get_basename (file_name);
         folder = xdg_user_dir_lookup ("DOCUMENTS");
@@ -4757,9 +4854,11 @@ image_save_dialog_response_cb (GtkWidget *fc,
                               gint       response_id,
                               EvWindow  *ev_window)
 {
-       gchar  *uri;
-       gchar  *filename;
-       GError *error = NULL;
+       GnomeVFSURI *target_uri;
+       gchar       *uri;
+       gchar       *filename;
+       gboolean     is_local;
+       GError      *error = NULL;
        
        if (response_id != GTK_RESPONSE_OK) {
                gtk_widget_destroy (fc);
@@ -4767,7 +4866,15 @@ image_save_dialog_response_cb (GtkWidget *fc,
        }
 
        uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
-       filename = g_filename_from_uri (uri, NULL, NULL);
+       target_uri = gnome_vfs_uri_new (uri);
+       is_local = gnome_vfs_uri_is_local (target_uri);
+       
+       if (is_local) {
+               filename = g_filename_from_uri (uri, NULL, NULL);
+       } else {
+               filename = ev_tmp_filename ("saveimage");
+       }
+       
        g_free (uri);
        
        /* FIXME: allow saving in other image formats than png */
@@ -4779,10 +4886,26 @@ image_save_dialog_response_cb (GtkWidget *fc,
                                        _("The image could not be saved."),
                                        error);
                g_error_free (error);
+               g_free (filename);
+               gnome_vfs_uri_unref (target_uri);
+               gtk_widget_destroy (fc);
+
+               return;
        }
 
-       g_free (filename);
+       if (!is_local) {
+               GnomeVFSURI *source_uri;
+               gchar       *local_uri;
 
+               local_uri = g_filename_to_uri (filename, NULL, NULL);
+               source_uri = gnome_vfs_uri_new (local_uri);
+               g_free (local_uri);
+               ev_window_save_remote (ev_window, source_uri, target_uri);
+               gnome_vfs_uri_unref (source_uri);
+       }
+       
+       g_free (filename);
+       gnome_vfs_uri_unref (target_uri);
        gtk_widget_destroy (fc);
 }
 
@@ -4804,6 +4927,7 @@ ev_view_popup_cmd_save_image_as (GtkAction *action, EvWindow *window)
                                          NULL);
 
        gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
+       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
        gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
 
        filter = gtk_file_filter_new ();
@@ -4862,10 +4986,12 @@ attachment_save_dialog_response_cb (GtkWidget *fc,
                                    gint       response_id,
                                    EvWindow  *ev_window)
 {
+       GnomeVFSURI          *target_uri;
        gchar                *uri;
        GList                *l;
        GtkFileChooserAction  fc_action;
        gboolean              is_dir;
+       gboolean              is_local;
        
        if (response_id != GTK_RESPONSE_OK) {
                gtk_widget_destroy (fc);
@@ -4873,9 +4999,10 @@ attachment_save_dialog_response_cb (GtkWidget *fc,
        }
 
        uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (fc));
-       
+       target_uri = gnome_vfs_uri_new (uri);
        g_object_get (G_OBJECT (fc), "action", &fc_action, NULL);
        is_dir = (fc_action == GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER);
+       is_local = gnome_vfs_uri_is_local (target_uri);
        
        for (l = ev_window->priv->attach_list; l && l->data; l = g_list_next (l)) {
                EvAttachment *attachment;
@@ -4884,26 +5011,57 @@ attachment_save_dialog_response_cb (GtkWidget *fc,
                
                attachment = (EvAttachment *) l->data;
 
-               if (is_dir) {
-                       filename = g_strjoin ("/", uri,
-                                             ev_attachment_get_name (attachment),
-                                             NULL);
+               if (is_local) {
+                       if (is_dir) {
+                               filename = g_strjoin ("/", uri,
+                                                     ev_attachment_get_name (attachment),
+                                                     NULL);
+                       } else {
+                               filename = g_strdup (uri);
+                       }
                } else {
-                       filename = g_strdup (uri);
+                       filename = ev_tmp_filename ("saveattachment");
                }
-               
+
                ev_attachment_save (attachment, filename, &error);
-               g_free (filename);
                
                if (error) {
                        ev_window_error_dialog (GTK_WINDOW (ev_window),
                                                _("The attachment could not be saved."),
                                                error);
                        g_error_free (error);
+                       g_free (filename);
+
+                       continue;
+               }
+
+               if (!is_local) {
+                       GnomeVFSURI *src_uri;
+                       GnomeVFSURI *dest_uri;
+                       gchar       *local_uri;
+
+                       if (is_dir) {
+                               const gchar *name = ev_attachment_get_name (attachment);
+
+                               dest_uri = gnome_vfs_uri_append_file_name (target_uri,
+                                                                          name);
+                       } else {
+                               dest_uri = gnome_vfs_uri_ref (target_uri);
+                       }
+                       
+                       local_uri = g_filename_to_uri (filename, NULL, NULL);
+                       src_uri = gnome_vfs_uri_new (local_uri);
+                       g_free (local_uri);
+                       ev_window_save_remote (ev_window, src_uri, dest_uri);
+                       gnome_vfs_uri_unref (src_uri);
+                       gnome_vfs_uri_unref (dest_uri);
                }
+
+               g_free (filename);
        }
 
        g_free (uri);
+       gnome_vfs_uri_unref (target_uri);
 
        gtk_widget_destroy (fc);
 }
@@ -4931,6 +5089,7 @@ ev_attachment_popup_cmd_save_attachment_as (GtkAction *action, EvWindow *window)
 
        gtk_dialog_set_default_response (GTK_DIALOG (fc), GTK_RESPONSE_OK);
        gtk_file_chooser_set_do_overwrite_confirmation (GTK_FILE_CHOOSER (fc), TRUE);
+       gtk_file_chooser_set_local_only (GTK_FILE_CHOOSER (fc), FALSE);
 
        if (attachment)
                gtk_file_chooser_set_current_name (GTK_FILE_CHOOSER (fc),