aboutsummaryrefslogtreecommitdiffstats
path: root/embed/mozilla/MozDownload.cpp
diff options
context:
space:
mode:
authorMarco Pesenti Gritti <marco@gnome.org>2003-10-24 18:56:53 +0800
committerMarco Pesenti Gritti <marco@src.gnome.org>2003-10-24 18:56:53 +0800
commit2a93fb20b6618d44c05589bb636fe77b7b04e075 (patch)
tree8291a66e4f65ea6c17c050dc1a066bd7c9a5582c /embed/mozilla/MozDownload.cpp
parent5767e9aeb78133de615e08087cd96c4beb89f1d7 (diff)
downloadgsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar.gz
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar.bz2
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar.lz
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar.xz
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.tar.zst
gsoc2013-epiphany-2a93fb20b6618d44c05589bb636fe77b7b04e075.zip
Add an helper to initialize downloads.
2003-10-24 Marco Pesenti Gritti <marco@gnome.org> * embed/mozilla/MozDownload.cpp: * embed/mozilla/MozDownload.h: Add an helper to initialize downloads. * embed/ephy-embed-persist.h: Add a flag to ask destination. * embed/mozilla/EphyHeaderSniffer.cpp: * embed/mozilla/EphyHeaderSniffer.h: Use the helper. Add code to determine a good filename. * embed/mozilla/mozilla-embed-persist.cpp: Use a MozDownload directly if there is a dest set, it doesnt make sense to use sniffer for favicons.
Diffstat (limited to 'embed/mozilla/MozDownload.cpp')
-rw-r--r--embed/mozilla/MozDownload.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/embed/mozilla/MozDownload.cpp b/embed/mozilla/MozDownload.cpp
index 2c7be9a07..e6ee3d0eb 100644
--- a/embed/mozilla/MozDownload.cpp
+++ b/embed/mozilla/MozDownload.cpp
@@ -48,6 +48,8 @@
#include "netCore.h"
#include "nsIObserver.h"
+const char* const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
+
MozDownload::MozDownload() :
mGotFirstStateChange(false), mIsNetworkTransfer(false),
mUserCanceled(false),
@@ -376,3 +378,97 @@ void
MozDownload::Resume()
{
}
+
+nsresult InitiateMozillaDownload (nsIDOMDocument *domDocument, nsIURI *sourceURI,
+ nsILocalFile* inDestFile, const char *contentType,
+ nsIURI* inOriginalURI, MozillaEmbedPersist *embedPersist,
+ PRBool bypassCache, nsIInputStream *postData)
+{
+ nsresult rv = NS_OK;
+
+ PRBool isHTML = (domDocument && contentType &&
+ (strcmp (contentType, "text/html") == 0 ||
+ strcmp (contentType, "text/xml") == 0 ||
+ strcmp (contentType, "application/xhtml+xml") == 0));
+
+ nsCOMPtr<nsIWebBrowserPersist> webPersist = do_CreateInstance(persistContractID, &rv);
+ if (NS_FAILED(rv)) return rv;
+
+ PRInt64 timeNow = PR_Now();
+
+ nsAutoString fileDisplayName;
+ inDestFile->GetLeafName(fileDisplayName);
+
+ MozDownload *downloader = new MozDownload ();
+ /* dlListener attaches to its progress dialog here, which gains ownership */
+ rv = downloader->InitForEmbed (inOriginalURI, inDestFile, fileDisplayName.get(),
+ nsnull, timeNow, webPersist, embedPersist);
+ if (NS_FAILED(rv)) return rv;
+
+ PRInt32 flags = nsIWebBrowserPersist::PERSIST_FLAGS_NO_CONVERSION |
+ nsIWebBrowserPersist::PERSIST_FLAGS_REPLACE_EXISTING_FILES;
+ if (bypassCache)
+ {
+ flags |= nsIWebBrowserPersist::PERSIST_FLAGS_BYPASS_CACHE;
+ }
+ else
+ {
+ flags |= nsIWebBrowserPersist::PERSIST_FLAGS_FROM_CACHE;
+ }
+
+ webPersist->SetPersistFlags(flags);
+
+ if (!isHTML)
+ {
+ rv = webPersist->SaveURI (sourceURI, nsnull, nsnull,
+ postData, nsnull, inDestFile);
+ }
+ else
+ {
+ if (!domDocument) return rv; /* should never happen */
+
+ PRInt32 encodingFlags = 0;
+ nsCOMPtr<nsILocalFile> filesFolder;
+
+ if (contentType && strcmp (contentType, "text/plain") == 0)
+ {
+ /* Create a local directory in the same dir as our file. It
+ will hold our associated files. */
+
+ filesFolder = do_CreateInstance("@mozilla.org/file/local;1");
+ nsAutoString unicodePath;
+ inDestFile->GetPath(unicodePath);
+ filesFolder->InitWithPath(unicodePath);
+
+ nsAutoString leafName;
+ filesFolder->GetLeafName(leafName);
+ nsAutoString nameMinusExt(leafName);
+ PRInt32 index = nameMinusExt.RFind(".");
+ if (index >= 0)
+ {
+ nameMinusExt.Left(nameMinusExt, index);
+ }
+
+ nameMinusExt += NS_LITERAL_STRING(" Files");
+ filesFolder->SetLeafName(nameMinusExt);
+ PRBool exists = PR_FALSE;
+ filesFolder->Exists(&exists);
+ if (!exists)
+ {
+ rv = filesFolder->Create(nsILocalFile::DIRECTORY_TYPE, 0755);
+ if (NS_FAILED(rv)) return rv;
+ }
+ }
+ else
+ {
+ encodingFlags |= nsIWebBrowserPersist::ENCODE_FLAGS_FORMATTED |
+ nsIWebBrowserPersist::ENCODE_FLAGS_ABSOLUTE_LINKS |
+ nsIWebBrowserPersist::ENCODE_FLAGS_NOFRAMES_CONTENT;
+ }
+
+ rv = webPersist->SaveDocument (domDocument, inDestFile, filesFolder,
+ contentType, encodingFlags, 80);
+ }
+
+ return rv;
+}