From 2a93fb20b6618d44c05589bb636fe77b7b04e075 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Fri, 24 Oct 2003 10:56:53 +0000 Subject: Add an helper to initialize downloads. 2003-10-24 Marco Pesenti Gritti * 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. --- embed/mozilla/MozDownload.cpp | 96 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'embed/mozilla/MozDownload.cpp') 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 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 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; +} -- cgit v1.2.3