From 493334ce5b250010cd55fddfef9d83470b6d0c70 Mon Sep 17 00:00:00 2001 From: Xan Lopez Date: Thu, 27 Nov 2003 23:46:32 +0000 Subject: Do not blindly overwrite when downloading a file with the same name than a * embed/mozilla/ContentHandler.cpp: * embed/mozilla/EphyHeaderSniffer.cpp: * embed/mozilla/MozDownload.cpp: * embed/mozilla/MozDownload.h: Do not blindly overwrite when downloading a file with the same name than a local one, append a number after the name. Fixes (more or less) #12775 --- ChangeLog | 11 ++++++ embed/mozilla/ContentHandler.cpp | 44 ++--------------------- embed/mozilla/EphyHeaderSniffer.cpp | 40 ++------------------- embed/mozilla/MozDownload.cpp | 70 +++++++++++++++++++++++++++++++++++++ embed/mozilla/MozDownload.h | 1 + 5 files changed, 87 insertions(+), 79 deletions(-) diff --git a/ChangeLog b/ChangeLog index 274631c0a..0fe6640ce 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,14 @@ +2003-11-28 Xan Lopez + + * embed/mozilla/ContentHandler.cpp: + * embed/mozilla/EphyHeaderSniffer.cpp: + * embed/mozilla/MozDownload.cpp: + * embed/mozilla/MozDownload.h: + + Do not blindly overwrite when downloading a file with the same + name than a local one, append a number after the name. + Fixes (more or less) #12775 + 2003-11-27 Piers Cornwell * data/glade/prefs-dialog.glade: diff --git a/embed/mozilla/ContentHandler.cpp b/embed/mozilla/ContentHandler.cpp index 7286ba3ff..1ad9095ff 100644 --- a/embed/mozilla/ContentHandler.cpp +++ b/embed/mozilla/ContentHandler.cpp @@ -25,6 +25,7 @@ #include "ContentHandler.h" #include "MozillaPrivate.h" +#include "MozDownload.h" #include "nsCOMPtr.h" #include "nsString.h" @@ -104,47 +105,8 @@ NS_IMETHODIMP GContentHandler::PromptForSaveToFile( const PRUnichar *aSuggestedFileExtension, nsILocalFile **_retval) { - char *path, *download_dir; - - download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR); - if (!download_dir) - { - /* Emergency download destination */ - download_dir = g_strdup (g_get_home_dir ()); - } - - if (!strcmp (download_dir, "Desktop")) - { - if (eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR)) - { - path = g_build_filename - (g_get_home_dir (), - NS_ConvertUCS2toUTF8 (aDefaultFile).get(), - NULL); - } - else - { - path = g_build_filename - (g_get_home_dir (), "Desktop", - NS_ConvertUCS2toUTF8 (aDefaultFile).get(), - NULL); - } - } - else - { - path = g_build_filename - (gnome_vfs_expand_initial_tilde (download_dir), - NS_ConvertUCS2toUTF8 (aDefaultFile).get(), - NULL); - } - g_free (download_dir); - - nsCOMPtr destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); - destFile->InitWithNativePath (nsDependentCString (path)); - g_free (path); - - NS_IF_ADDREF (*_retval = destFile); - return NS_OK; + return BuildDownloadPath (NS_ConvertUCS2toUTF8 (aDefaultFile).get(), + _retval); } #if MOZILLA_SNAPSHOT < 10 diff --git a/embed/mozilla/EphyHeaderSniffer.cpp b/embed/mozilla/EphyHeaderSniffer.cpp index a1e621fad..b6bc1ffb0 100644 --- a/embed/mozilla/EphyHeaderSniffer.cpp +++ b/embed/mozilla/EphyHeaderSniffer.cpp @@ -324,46 +324,10 @@ nsresult EphyHeaderSniffer::PerformSave (nsIURI* inOriginalURI) return NS_OK; } - /* FIXME ask user if overwriting ? */ - /* FIXME: how to inform user of failed save ? */ - download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR); - if (!download_dir) - { - /* Emergency download destination */ - download_dir = g_strdup (g_get_home_dir ()); - } - - if (!strcmp (download_dir, "Desktop")) - { - if (eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR)) - { - path = g_build_filename - (g_get_home_dir (), - NS_ConvertUCS2toUTF8 (defaultFileName).get(), - NULL); - } - else - { - path = g_build_filename - (g_get_home_dir (), "Desktop", - NS_ConvertUCS2toUTF8 (defaultFileName).get(), - NULL); - } - } - else - { - path = g_build_filename - (gnome_vfs_expand_initial_tilde (download_dir), - NS_ConvertUCS2toUTF8 (defaultFileName).get(), - NULL); - } - g_free (download_dir); - - nsCOMPtr destFile = do_CreateInstance (NS_LOCAL_FILE_CONTRACTID); - destFile->InitWithNativePath (nsDependentCString (path)); - g_free (path); + nsILocalFile *destFile; + BuildDownloadPath (NS_ConvertUCS2toUTF8 (defaultFileName).get(), &destFile); return InitiateDownload (destFile); } diff --git a/embed/mozilla/MozDownload.cpp b/embed/mozilla/MozDownload.cpp index d4931737c..8e7df243c 100644 --- a/embed/mozilla/MozDownload.cpp +++ b/embed/mozilla/MozDownload.cpp @@ -43,6 +43,9 @@ #include "MozDownload.h" #include "mozilla-download.h" +#include "eel-gconf-extensions.h" +#include "ephy-prefs.h" +#include #include "nsIExternalHelperAppService.h" #include "nsDirectoryServiceDefs.h" @@ -472,3 +475,70 @@ nsresult InitiateMozillaDownload (nsIDOMDocument *domDocument, nsIURI *sourceURI return rv; } + +static char* +GetFilePath (const char *filename) +{ + char *path, *download_dir; + + download_dir = eel_gconf_get_string (CONF_STATE_DOWNLOAD_DIR); + if (!download_dir) + { + /* Emergency download destination */ + download_dir = g_strdup (g_get_home_dir ()); + } + + if (!strcmp (download_dir, "Desktop")) + { + if (eel_gconf_get_boolean (CONF_DESKTOP_IS_HOME_DIR)) + { + path = g_build_filename + (g_get_home_dir (), + filename, + NULL); + } + else + { + path = g_build_filename + (g_get_home_dir (), "Desktop", + filename, + NULL); + } + } + else + { + path = g_build_filename + (gnome_vfs_expand_initial_tilde (download_dir), + filename, + NULL); + } + g_free (download_dir); + + return path; +} + +nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval) +{ + char *path; + int i = 0; + + path = GetFilePath (defaultFileName); + + while (g_file_test (path, G_FILE_TEST_EXISTS)) + { + g_free (path); + + char *tmp_path; + tmp_path = g_strdup_printf ("%s.%d", defaultFileName, ++i); + path = GetFilePath (tmp_path); + g_free (tmp_path); + } + + nsCOMPtr destFile (do_CreateInstance(NS_LOCAL_FILE_CONTRACTID)); + destFile->InitWithNativePath (nsDependentCString (path)); + g_free (path); + + NS_IF_ADDREF (*_retval = destFile); + return NS_OK; +} + diff --git a/embed/mozilla/MozDownload.h b/embed/mozilla/MozDownload.h index 89d20b8c3..930e110b8 100644 --- a/embed/mozilla/MozDownload.h +++ b/embed/mozilla/MozDownload.h @@ -80,6 +80,7 @@ nsresult InitiateMozillaDownload (nsIDOMDocument *domDocument, nsIURI *sourceUri nsILocalFile* inDestFile, const char *contentType, nsIURI* inOriginalURI, MozillaEmbedPersist *embedPersist, nsIInputStream *postData, nsISupports *aCacheKey); +nsresult BuildDownloadPath (const char *defaultFileName, nsILocalFile **_retval); class MozDownload : public nsIDownload, public nsIWebProgressListener -- cgit v1.2.3