2006-07-29 05:38:04 +00:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2006-07-29 05:43:04 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2006-07-29 05:33:03 +00:00
|
|
|
*
|
2006-07-29 05:43:04 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2006-07-29 05:33:03 +00:00
|
|
|
*
|
2006-07-29 05:38:04 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2006-07-29 05:33:03 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2006-07-29 05:43:04 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2006-07-29 05:38:04 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2006-07-29 05:33:03 +00:00
|
|
|
*
|
2006-07-29 05:38:04 +00:00
|
|
|
* Contributor(s):
|
2006-07-29 05:39:02 +00:00
|
|
|
* Ben Goodger <ben@netscape.com> (Save File)
|
2006-07-29 05:38:04 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2006-07-29 05:43:04 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2006-07-29 05:38:04 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2006-07-29 05:43:04 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2006-07-29 05:38:04 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2006-07-29 05:43:04 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2006-07-29 05:38:04 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2006-07-29 05:33:03 +00:00
|
|
|
|
2006-07-29 05:33:17 +00:00
|
|
|
/**
|
|
|
|
* Determine whether or not a given focused DOMWindow is in the content
|
|
|
|
* area.
|
|
|
|
**/
|
2006-07-29 05:41:24 +00:00
|
|
|
function isContentFrame(aFocusedWindow)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:41:25 +00:00
|
|
|
if (!aFocusedWindow)
|
|
|
|
return false;
|
|
|
|
|
2006-07-29 05:43:09 +00:00
|
|
|
var focusedTop = new XPCNativeWrapper(aFocusedWindow, 'top').top;
|
2006-07-29 05:41:24 +00:00
|
|
|
|
|
|
|
return (focusedTop == window.content);
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:33:17 +00:00
|
|
|
|
2006-07-29 05:43:08 +00:00
|
|
|
function urlSecurityCheck(url, doc)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
|
|
|
// URL Loading Security Check
|
|
|
|
var focusedWindow = doc.commandDispatcher.focusedWindow;
|
2006-07-29 05:41:24 +00:00
|
|
|
var sourceURL = getContentFrameURI(focusedWindow);
|
2006-07-29 05:39:02 +00:00
|
|
|
const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
|
2006-07-29 05:41:24 +00:00
|
|
|
var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
|
|
|
|
.getService(nsIScriptSecurityManager);
|
2006-07-29 05:39:02 +00:00
|
|
|
try {
|
2006-07-29 05:41:24 +00:00
|
|
|
secMan.checkLoadURIStr(sourceURL, url, nsIScriptSecurityManager.STANDARD);
|
2006-07-29 05:39:02 +00:00
|
|
|
} catch (e) {
|
2006-07-29 05:41:24 +00:00
|
|
|
throw "Load of " + url + " denied.";
|
2006-07-29 05:37:09 +00:00
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:37:09 +00:00
|
|
|
|
2006-07-29 05:41:24 +00:00
|
|
|
function getContentFrameURI(aFocusedWindow)
|
|
|
|
{
|
|
|
|
var contentFrame = isContentFrame(aFocusedWindow) ? aFocusedWindow : window.content;
|
2006-07-29 05:43:09 +00:00
|
|
|
return new XPCNativeWrapper(contentFrame, "location").location.href;
|
2006-07-29 05:41:24 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:18 +00:00
|
|
|
function getReferrer(doc)
|
|
|
|
{
|
|
|
|
var focusedWindow = doc.commandDispatcher.focusedWindow;
|
2006-07-29 05:41:24 +00:00
|
|
|
var sourceURL = getContentFrameURI(focusedWindow);
|
|
|
|
|
2006-07-29 05:39:18 +00:00
|
|
|
try {
|
2006-07-29 05:43:15 +00:00
|
|
|
return makeURI(sourceURL);
|
2006-07-29 05:39:18 +00:00
|
|
|
} catch (e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:08 +00:00
|
|
|
function openNewWindowWith(url, sendReferrer)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
|
|
|
urlSecurityCheck(url, document);
|
2006-07-29 05:37:09 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
// if and only if the current window is a browser window and it has a document with a character
|
|
|
|
// set, then extract the current charset menu setting from the current document and use it to
|
|
|
|
// initialize the new browser window...
|
2006-07-29 05:40:23 +00:00
|
|
|
var charsetArg = null;
|
|
|
|
var wintype = document.firstChild.getAttribute('windowtype');
|
|
|
|
if (wintype == "navigator:browser")
|
2006-07-29 05:43:14 +00:00
|
|
|
charsetArg = "charset=" + window.content.document.characterSet;
|
2006-07-29 05:37:09 +00:00
|
|
|
|
2006-07-29 05:41:33 +00:00
|
|
|
var referrer = sendReferrer ? getReferrer(document) : null;
|
2006-07-29 05:40:23 +00:00
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url, charsetArg, referrer);
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:37:42 +00:00
|
|
|
|
2006-07-29 05:42:06 +00:00
|
|
|
function openTopBrowserWith(url)
|
|
|
|
{
|
2006-07-29 05:42:11 +00:00
|
|
|
urlSecurityCheck(url, document);
|
|
|
|
|
2006-07-29 05:42:06 +00:00
|
|
|
var windowMediator = Components.classes["@mozilla.org/appshell/window-mediator;1"].getService(Components.interfaces.nsIWindowMediator);
|
|
|
|
var browserWin = windowMediator.getMostRecentWindow("navigator:browser");
|
|
|
|
|
|
|
|
// if there's an existing browser window, open this url in one
|
2006-07-29 05:42:07 +00:00
|
|
|
if (browserWin) {
|
2006-07-29 05:42:06 +00:00
|
|
|
browserWin.getBrowser().loadURI(url); // Just do a normal load.
|
2006-07-29 05:42:08 +00:00
|
|
|
browserWin.content.focus();
|
2006-07-29 05:42:07 +00:00
|
|
|
}
|
2006-07-29 05:42:06 +00:00
|
|
|
else
|
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", url, null, null);
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:08 +00:00
|
|
|
function openNewTabWith(url, sendReferrer, reverseBackgroundPref)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:41:35 +00:00
|
|
|
var browser;
|
|
|
|
try {
|
|
|
|
// if we're running in a browser window, this should work
|
|
|
|
//
|
|
|
|
browser = getBrowser();
|
|
|
|
|
|
|
|
} catch (ex if ex instanceof ReferenceError) {
|
|
|
|
|
|
|
|
// must be running somewhere else (eg mailnews message pane); need to
|
|
|
|
// find a browser window first
|
|
|
|
//
|
|
|
|
var windowMediator =
|
|
|
|
Components.classes["@mozilla.org/appshell/window-mediator;1"]
|
|
|
|
.getService(Components.interfaces.nsIWindowMediator);
|
|
|
|
|
|
|
|
var browserWin = windowMediator.getMostRecentWindow("navigator:browser");
|
|
|
|
|
2006-07-29 05:42:19 +00:00
|
|
|
// if there's no existing browser window, then, as long as
|
|
|
|
// we are allowed to, open this url in one and return
|
2006-07-29 05:41:35 +00:00
|
|
|
//
|
|
|
|
if (!browserWin) {
|
2006-07-29 05:42:19 +00:00
|
|
|
urlSecurityCheck(url, document);
|
2006-07-29 05:43:08 +00:00
|
|
|
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no",
|
2006-07-29 05:41:35 +00:00
|
|
|
url, null, referrer);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, get the existing browser object
|
|
|
|
//
|
|
|
|
browser = browserWin.getBrowser();
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:41:36 +00:00
|
|
|
// Get the XUL document that the browser is actually contained in.
|
|
|
|
// This is needed if we are trying to load a URL from a non-navigator
|
|
|
|
// window such as the JS Console.
|
|
|
|
var browserDocument = browser.ownerDocument;
|
|
|
|
|
|
|
|
urlSecurityCheck(url, browserDocument);
|
|
|
|
|
|
|
|
var referrer = sendReferrer ? getReferrer(browserDocument) : null;
|
|
|
|
|
2006-07-29 05:42:17 +00:00
|
|
|
// As in openNewWindowWith(), we want to pass the charset of the
|
2006-07-29 05:43:08 +00:00
|
|
|
// current document over to a new tab.
|
2006-07-29 05:42:17 +00:00
|
|
|
var wintype = browserDocument.firstChild.getAttribute('windowtype');
|
|
|
|
var originCharset;
|
|
|
|
if (wintype == "navigator:browser") {
|
2006-07-29 05:43:14 +00:00
|
|
|
originCharset = window.content.document.characterSet;
|
2006-07-29 05:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// open link in new tab
|
2006-07-29 05:43:08 +00:00
|
|
|
var tab = browser.addTab(url, referrer, originCharset);
|
2006-07-29 05:41:33 +00:00
|
|
|
if (pref) {
|
|
|
|
var loadInBackground = pref.getBoolPref("browser.tabs.loadInBackground");
|
|
|
|
if (reverseBackgroundPref)
|
|
|
|
loadInBackground = !loadInBackground;
|
2006-07-29 05:37:42 +00:00
|
|
|
|
2006-07-29 05:41:33 +00:00
|
|
|
if (!loadInBackground)
|
|
|
|
browser.selectedTab = tab;
|
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function findParentNode(node, parentNode)
|
|
|
|
{
|
|
|
|
if (node && node.nodeType == Node.TEXT_NODE) {
|
|
|
|
node = node.parentNode;
|
2006-07-29 05:37:42 +00:00
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
while (node) {
|
|
|
|
var nodeName = node.localName;
|
|
|
|
if (!nodeName)
|
|
|
|
return null;
|
|
|
|
nodeName = nodeName.toLowerCase();
|
|
|
|
if (nodeName == "body" || nodeName == "html" ||
|
|
|
|
nodeName == "#document") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (nodeName == parentNode)
|
|
|
|
return node;
|
|
|
|
node = node.parentNode;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clientelle: (Make sure you don't break any of these)
|
|
|
|
// - File -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Page/Frame As...
|
|
|
|
// - Context -> Save Link As...
|
|
|
|
// - Context -> Save Image As...
|
|
|
|
// - Shift-Click Save Link As
|
|
|
|
//
|
|
|
|
// Try saving each of these types:
|
|
|
|
// - A complete webpage using File->Save Page As, and Context->Save Page As
|
|
|
|
// - A webpage as HTML only using the above methods
|
|
|
|
// - A webpage as Text only using the above methods
|
|
|
|
// - An image with an extension (e.g. .jpg) in its file name, using
|
|
|
|
// Context->Save Image As...
|
|
|
|
// - An image without an extension (e.g. a banner ad on cnn.com) using
|
2006-07-29 05:43:08 +00:00
|
|
|
// the above method.
|
2006-07-29 05:39:02 +00:00
|
|
|
// - A linked document using Save Link As...
|
|
|
|
// - A linked document using shift-click Save Link As...
|
|
|
|
//
|
2006-07-29 05:39:15 +00:00
|
|
|
function saveURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache)
|
2006-07-29 05:39:04 +00:00
|
|
|
{
|
2006-07-29 05:43:15 +00:00
|
|
|
internalSave(aURL, null, aFileName, aShouldBypassCache,
|
|
|
|
aFilePickerTitleKey, null);
|
2006-07-29 05:39:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:35 +00:00
|
|
|
function saveFrameDocument()
|
|
|
|
{
|
|
|
|
var focusedWindow = document.commandDispatcher.focusedWindow;
|
2006-07-29 05:41:24 +00:00
|
|
|
if (isContentFrame(focusedWindow))
|
2006-07-29 05:39:35 +00:00
|
|
|
saveDocument(focusedWindow.document);
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:04 +00:00
|
|
|
function saveDocument(aDocument)
|
|
|
|
{
|
2006-07-29 05:43:08 +00:00
|
|
|
// In both cases here, we want to use cached data because the
|
|
|
|
// document is currently visible.
|
|
|
|
if (aDocument)
|
2006-07-29 05:43:15 +00:00
|
|
|
internalSave(aDocument.location.href, aDocument,
|
|
|
|
null, false, null, null);
|
2006-07-29 05:39:15 +00:00
|
|
|
else
|
2006-07-29 05:43:15 +00:00
|
|
|
internalSave(content.location.href, null,
|
|
|
|
null, false, null, null);
|
2006-07-29 05:39:04 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* internalSave: Used when saving a document or URL. This method:
|
|
|
|
* - Determines a local target filename to use (unless parameter
|
|
|
|
* aChosenData is non-null)
|
|
|
|
* - Determines content-type if possible
|
|
|
|
* - Prompts the user to confirm the destination filename and save mode
|
|
|
|
* (content-type affects this)
|
|
|
|
* - Creates a 'Persist' object (which will perform the saving in the
|
|
|
|
* background) and then starts it.
|
|
|
|
*
|
|
|
|
* @param aURL The String representation of the URL of the document being saved
|
|
|
|
* @param aDocument The document to be saved
|
|
|
|
* @param aDefaultFileName The caller-provided suggested filename if we don't
|
|
|
|
* find a better one
|
|
|
|
* @param aShouldBypassCache If true, the document will always be refetched
|
|
|
|
* from the server
|
|
|
|
* @param aFilePickerTitleKey Alternate title for the file picker
|
|
|
|
* @param aChosenData If non-null this contains an instance of object AutoChosen
|
|
|
|
* (see below) which holds pre-determined data so that the user does not
|
|
|
|
* need to be prompted for a target filename.
|
|
|
|
*/
|
|
|
|
function internalSave(aURL, aDocument, aDefaultFileName, aShouldBypassCache,
|
|
|
|
aFilePickerTitleKey, aChosenData)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:43:15 +00:00
|
|
|
// Note: aDocument == null when this code is used by save-link-as...
|
|
|
|
var contentType = (aDocument ? aDocument.contentType : null);
|
2006-07-29 05:39:55 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Find the URI object for aURL and the FileName/Extension to use when saving.
|
|
|
|
// FileName/Extension will be ignored if aChosenData supplied.
|
|
|
|
var fileInfo = new FileInfo(aDefaultFileName);
|
|
|
|
if (!aChosenData)
|
|
|
|
initFileInfo(fileInfo, aURL, aDocument, contentType);
|
2006-07-29 05:39:02 +00:00
|
|
|
|
2006-07-29 05:42:05 +00:00
|
|
|
var saveMode = GetSaveModeForContentType(contentType);
|
2006-07-29 05:43:15 +00:00
|
|
|
var isDocument = aDocument != null && saveMode != SAVEMODE_FILEONLY;
|
|
|
|
|
|
|
|
// Show the file picker that allows the user to confirm the target filename:
|
|
|
|
var prefs = getPrefsBrowserDownload("browser.download.");
|
|
|
|
var fpParams = {
|
|
|
|
fp: makeFilePicker(),
|
|
|
|
prefs: prefs,
|
|
|
|
userDirectory: getUserDownloadDir(prefs),
|
|
|
|
fpTitleKey: aFilePickerTitleKey,
|
|
|
|
isDocument: isDocument,
|
|
|
|
fileInfo: fileInfo,
|
|
|
|
contentType: contentType,
|
|
|
|
saveMode: saveMode
|
|
|
|
};
|
|
|
|
// If aChosenData is null then the file picker is shown.
|
|
|
|
if (!aChosenData) {
|
|
|
|
if (!poseFilePicker(fpParams))
|
|
|
|
// If the method returned false this is because the user cancelled from
|
|
|
|
// the save file picker dialog.
|
|
|
|
return;
|
2006-07-29 05:39:15 +00:00
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:42:05 +00:00
|
|
|
|
|
|
|
// XXX We depend on the following holding true in appendFiltersForContentType():
|
|
|
|
// If we should save as a complete page, the filterIndex is 0.
|
|
|
|
// If we should save as text, the filterIndex is 2.
|
|
|
|
var useSaveDocument = isDocument &&
|
2006-07-29 05:43:15 +00:00
|
|
|
(((fpParams.saveMode & SAVEMODE_COMPLETE_DOM) && (fpParams.fp.filterIndex == 0)) ||
|
|
|
|
((fpParams.saveMode & SAVEMODE_COMPLETE_TEXT) && (fpParams.fp.filterIndex == 2)));
|
2006-07-29 05:42:05 +00:00
|
|
|
|
2006-07-29 05:43:08 +00:00
|
|
|
// If we're saving a document, and are saving either in complete mode or
|
2006-07-29 05:39:15 +00:00
|
|
|
// as converted text, pass the document to the web browser persist component.
|
|
|
|
// If we're just saving the HTML (second option in the list), send only the URI.
|
2006-07-29 05:43:15 +00:00
|
|
|
var source = useSaveDocument ? aDocument : fileInfo.uri;
|
2006-07-29 05:39:02 +00:00
|
|
|
var persistArgs = {
|
2006-07-29 05:39:15 +00:00
|
|
|
source : source,
|
2006-07-29 05:43:15 +00:00
|
|
|
contentType : (!aChosenData && useSaveDocument && fpParams.fp.filterIndex == 2) ? "text/plain" : contentType,
|
|
|
|
target : (aChosenData ? makeFileURI(aChosenData.file) : fpParams.fp.fileURL),
|
2006-07-29 05:42:03 +00:00
|
|
|
postData : isDocument ? getPostData() : null,
|
2006-07-29 05:43:15 +00:00
|
|
|
bypassCache : aShouldBypassCache
|
2006-07-29 05:39:02 +00:00
|
|
|
};
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:48 +00:00
|
|
|
var persist = makeWebBrowserPersist();
|
|
|
|
|
|
|
|
// Calculate persist flags.
|
|
|
|
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
|
2006-07-29 05:43:15 +00:00
|
|
|
const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
|
|
|
|
if (aShouldBypassCache)
|
2006-07-29 05:39:48 +00:00
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
|
2006-07-29 05:43:08 +00:00
|
|
|
else
|
2006-07-29 05:39:48 +00:00
|
|
|
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Leave it to WebBrowserPersist to discover the encoding type (or lack thereof):
|
|
|
|
persist.persistFlags |= nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:40:22 +00:00
|
|
|
// Create download and initiate it (below)
|
|
|
|
var dl = Components.classes["@mozilla.org/download;1"].createInstance(Components.interfaces.nsIDownload);
|
|
|
|
|
2006-07-29 05:42:05 +00:00
|
|
|
if (useSaveDocument) {
|
2006-07-29 05:39:48 +00:00
|
|
|
// Saving a Document, not a URI:
|
|
|
|
var filesFolder = null;
|
|
|
|
if (persistArgs.contentType != "text/plain") {
|
2006-07-29 05:43:08 +00:00
|
|
|
// Create the local directory into which to save associated files.
|
2006-07-29 05:43:15 +00:00
|
|
|
var destFile = (aChosenData ? aChosenData.file : fpParams.fp.file);
|
|
|
|
filesFolder = destFile.clone();
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:42:10 +00:00
|
|
|
var nameWithoutExtension = filesFolder.leafName.replace(/\.[^.]*$/, "");
|
2006-07-29 05:39:48 +00:00
|
|
|
var filesFolderLeafName = getStringBundle().formatStringFromName("filesFolder",
|
|
|
|
[nameWithoutExtension],
|
|
|
|
1);
|
|
|
|
|
|
|
|
filesFolder.leafName = filesFolderLeafName;
|
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:48 +00:00
|
|
|
var encodingFlags = 0;
|
|
|
|
if (persistArgs.contentType == "text/plain") {
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
|
2006-07-29 05:43:08 +00:00
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
|
2006-07-29 05:39:48 +00:00
|
|
|
}
|
2006-07-29 05:42:09 +00:00
|
|
|
else {
|
|
|
|
encodingFlags |= nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:48 +00:00
|
|
|
const kWrapColumn = 80;
|
2006-07-29 05:43:15 +00:00
|
|
|
dl.init((aChosenData ? aChosenData.uri : fileInfo.uri),
|
|
|
|
persistArgs.target, null, null, null, persist);
|
2006-07-29 05:43:08 +00:00
|
|
|
persist.saveDocument(persistArgs.source, persistArgs.target, filesFolder,
|
2006-07-29 05:39:48 +00:00
|
|
|
persistArgs.contentType, encodingFlags, kWrapColumn);
|
|
|
|
} else {
|
2006-07-29 05:43:15 +00:00
|
|
|
dl.init((aChosenData ? aChosenData.uri : source),
|
|
|
|
persistArgs.target, null, null, null, persist);
|
2006-07-29 05:42:35 +00:00
|
|
|
var referer = getReferrer(document);
|
2006-07-29 05:43:15 +00:00
|
|
|
persist.saveURI((aChosenData ? aChosenData.uri : source),
|
|
|
|
null, referer, persistArgs.postData, null, persistArgs.target);
|
2006-07-29 05:39:48 +00:00
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* Structure for holding info about automatically supplied parameters for
|
|
|
|
* internalSave(...). This allows parameters to be supplied so the user does not
|
|
|
|
* need to be prompted for file info.
|
|
|
|
* @param aFileAutoChosen This is an nsILocalFile object that has been
|
|
|
|
* pre-determined as the filename for the target to save to
|
|
|
|
* @param aUriAutoChosen This is the nsIURI object for the target
|
|
|
|
*/
|
|
|
|
function AutoChosen(aFileAutoChosen, aUriAutoChosen) {
|
|
|
|
this.file = aFileAutoChosen;
|
|
|
|
this.uri = aUriAutoChosen;
|
|
|
|
}
|
2006-07-29 05:39:38 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* Structure for holding info about a URL and the target filename it should be
|
|
|
|
* saved to. This structure is populated by initFileInfo(...).
|
|
|
|
* @param aSuggestedFileName This is used by initFileInfo(...) when it
|
|
|
|
* cannot 'discover' the filename from the url
|
|
|
|
* @param aFileName The target filename
|
|
|
|
* @param aFileBaseName The filename without the file extension
|
|
|
|
* @param aFileExt The extension of the filename
|
|
|
|
* @param aUri An nsIURI object for the url that is being saved
|
|
|
|
*/
|
|
|
|
function FileInfo(aSuggestedFileName, aFileName, aFileBaseName, aFileExt, aUri) {
|
|
|
|
this.suggestedFileName = aSuggestedFileName;
|
|
|
|
this.fileName = aFileName;
|
|
|
|
this.fileBaseName = aFileBaseName;
|
|
|
|
this.fileExt = aFileExt;
|
|
|
|
this.uri = aUri;
|
|
|
|
}
|
2006-07-29 05:39:38 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* Determine what the 'default' filename string is, its file extension and the
|
|
|
|
* filename without the extension. This filename is used when prompting the user
|
|
|
|
* for confirmation in the file picker dialog.
|
|
|
|
* @param aFI A FileInfo structure into which we'll put the results of this method.
|
|
|
|
* @param aURL The String representation of the URL of the document being saved
|
|
|
|
* @param aDocument The document to be saved
|
|
|
|
* @param aContentType The content type of the document, if it could be
|
|
|
|
* determined by the caller.
|
|
|
|
*/
|
|
|
|
function initFileInfo(aFI, aURL, aDocument, aContentType)
|
|
|
|
{
|
|
|
|
var docCharset = (aDocument ? aDocument.characterSet : null);
|
2006-07-29 05:42:35 +00:00
|
|
|
try {
|
2006-07-29 05:43:15 +00:00
|
|
|
// Get an nsIURI object from aURL if possible:
|
|
|
|
try {
|
|
|
|
aFI.uri = makeURI(aURL, docCharset);
|
|
|
|
// Assuming nsiUri is valid, calling QueryInterface(...) on it will
|
|
|
|
// populate extra object fields (eg filename and file extension).
|
|
|
|
var url = aFI.uri.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2006-07-29 05:42:35 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Get the default filename:
|
|
|
|
aFI.fileName = getDefaultFileName((aFI.suggestedFileName || aFI.fileName),
|
|
|
|
aFI.uri, aDocument);
|
|
|
|
aFI.fileExt = url.fileExtension;
|
|
|
|
// If aFI.fileExt is still blank, consider: aFI.suggestedFileName is supplied
|
|
|
|
// if saveURL(...) was the original caller (hence both aContentType and
|
|
|
|
// aDocument are blank). If they were saving a link to a website then make
|
|
|
|
// the extension .htm .
|
|
|
|
if (!aFI.fileExt && !aDocument && !aContentType && (aURL.length > 7) &&
|
|
|
|
aURL.substring(0,7).toUpperCase() == "HTTP://") {
|
|
|
|
aFI.fileExt = "htm";
|
|
|
|
aFI.fileBaseName = aFI.fileName;
|
|
|
|
} else {
|
|
|
|
aFI.fileExt = getDefaultExtension(aFI.fileName, aFI.uri, aContentType);
|
|
|
|
aFI.fileBaseName = getFileBaseName(aFI.fileName, aFI.fileExt);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* Given the Filepicker Parameters (aFpP), show the file picker dialog,
|
|
|
|
* prompting the user to confirm (or change) the fileName.
|
|
|
|
* @param aFpP a structure (see definition in internalSave(...) method)
|
|
|
|
* containing all the data used within this method.
|
|
|
|
* @return true if the user confirmed a filename in the picker; false if they
|
|
|
|
* dismissed the picker.
|
|
|
|
*/
|
|
|
|
function poseFilePicker(aFpP)
|
|
|
|
{
|
|
|
|
var titleKey = aFpP.fpTitleKey || "SaveLinkTitle";
|
|
|
|
var bundle = getStringBundle();
|
|
|
|
var fp = aFpP.fp; // simply for smaller readable code
|
|
|
|
fp.init(window, bundle.GetStringFromName(titleKey),
|
|
|
|
Components.interfaces.nsIFilePicker.modeSave);
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
const nsILocalFile = Components.interfaces.nsILocalFile;
|
|
|
|
try {
|
|
|
|
fp.displayDirectory = aFpP.userDirectory;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
2006-07-29 05:39:53 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
fp.defaultExtension = aFpP.fileInfo.fileExt;
|
|
|
|
fp.defaultString = getNormalizedLeafName(aFpP.fileInfo.fileName,
|
|
|
|
aFpP.fileInfo.fileExt);
|
|
|
|
appendFiltersForContentType(fp, aFpP.contentType, aFpP.fileInfo.fileExt,
|
|
|
|
aFpP.saveMode);
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
if (aFpP.isDocument) {
|
2006-07-29 05:39:38 +00:00
|
|
|
try {
|
2006-07-29 05:43:15 +00:00
|
|
|
fp.filterIndex = aFpP.prefs.getIntPref("save_converter_index");
|
2006-07-29 05:39:38 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:43:15 +00:00
|
|
|
}
|
2006-07-29 05:39:53 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel || !fp.file)
|
|
|
|
return false;
|
2006-07-29 05:39:53 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
if (aFpP.isDocument)
|
|
|
|
aFpP.prefs.setIntPref("save_converter_index", fp.filterIndex);
|
2006-07-29 05:39:02 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Now that the user has had a chance to change the directory and/or filename,
|
|
|
|
// re-read those values...
|
|
|
|
var directory = fp.file.parent.QueryInterface(nsILocalFile);
|
|
|
|
aFpP.prefs.setComplexValue("dir", nsILocalFile, directory);
|
|
|
|
fp.file.leafName = validateFileName(fp.file.leafName);
|
|
|
|
return true;
|
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
|
2006-07-29 05:42:05 +00:00
|
|
|
// We have no DOM, and can only save the URL as is.
|
|
|
|
const SAVEMODE_FILEONLY = 0x00;
|
|
|
|
// We have a DOM and can save as complete.
|
|
|
|
const SAVEMODE_COMPLETE_DOM = 0x01;
|
|
|
|
// We have a DOM which we can serialize as text.
|
|
|
|
const SAVEMODE_COMPLETE_TEXT = 0x02;
|
|
|
|
|
|
|
|
// If we are able to save a complete DOM, the 'save as complete' filter
|
|
|
|
// must be the first filter appended. The 'save page only' counterpart
|
|
|
|
// must be the second filter appended. And the 'save as complete text'
|
|
|
|
// filter must be the third filter appended.
|
2006-07-29 05:42:27 +00:00
|
|
|
function appendFiltersForContentType(aFilePicker, aContentType, aFileExtension, aSaveMode)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
|
|
|
var bundle = getStringBundle();
|
2006-07-29 05:42:05 +00:00
|
|
|
// The bundle name for saving only a specific content type.
|
|
|
|
var bundleName;
|
|
|
|
// The corresponding filter string for a specific content type.
|
|
|
|
var filterString;
|
|
|
|
|
|
|
|
// XXX all the cases that are handled explicitly here MUST be handled
|
|
|
|
// in GetSaveModeForContentType to return a non-fileonly filter.
|
2006-07-29 05:39:02 +00:00
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
2006-07-29 05:42:05 +00:00
|
|
|
bundleName = "WebPageHTMLOnlyFilter";
|
|
|
|
filterString = "*.htm; *.html";
|
2006-07-29 05:39:02 +00:00
|
|
|
break;
|
2006-07-29 05:42:03 +00:00
|
|
|
|
|
|
|
case "application/xhtml+xml":
|
2006-07-29 05:42:05 +00:00
|
|
|
bundleName = "WebPageXHTMLOnlyFilter";
|
|
|
|
filterString = "*.xht; *.xhtml";
|
2006-07-29 05:42:03 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "text/xml":
|
|
|
|
case "application/xml":
|
2006-07-29 05:42:05 +00:00
|
|
|
bundleName = "WebPageXMLOnlyFilter";
|
|
|
|
filterString = "*.xml";
|
2006-07-29 05:42:03 +00:00
|
|
|
break;
|
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
default:
|
2006-07-29 05:42:05 +00:00
|
|
|
if (aSaveMode != SAVEMODE_FILEONLY) {
|
|
|
|
throw "Invalid save mode for type '" + aContentType + "'";
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:42:27 +00:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, aFileExtension);
|
2006-07-29 05:39:02 +00:00
|
|
|
if (mimeInfo) {
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:42:12 +00:00
|
|
|
var extEnumerator = mimeInfo.getFileExtensions();
|
2006-07-29 05:42:05 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
var extString = "";
|
2006-07-29 05:42:12 +00:00
|
|
|
while (extEnumerator.hasMore()) {
|
|
|
|
var extension = extEnumerator.getNext();
|
|
|
|
if (extString)
|
|
|
|
extString += "; "; // If adding more than one extension,
|
|
|
|
// separate by semi-colon
|
|
|
|
extString += "*." + extension;
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:42:12 +00:00
|
|
|
if (extString) {
|
2006-07-29 05:43:06 +00:00
|
|
|
aFilePicker.appendFilter(mimeInfo.description, extString);
|
2006-07-29 05:42:05 +00:00
|
|
|
}
|
2006-07-29 05:33:03 +00:00
|
|
|
}
|
2006-07-29 05:42:05 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-07-29 05:42:05 +00:00
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_DOM) {
|
|
|
|
aFilePicker.appendFilter(bundle.GetStringFromName("WebPageCompleteFilter"), filterString);
|
|
|
|
// We should always offer a choice to save document only if
|
|
|
|
// we allow saving as complete.
|
|
|
|
aFilePicker.appendFilter(bundle.GetStringFromName(bundleName), filterString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSaveMode & SAVEMODE_COMPLETE_TEXT) {
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterText);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always append the all files (*) filter
|
|
|
|
aFilePicker.appendFilters(Components.interfaces.nsIFilePicker.filterAll);
|
2006-07-29 05:43:08 +00:00
|
|
|
}
|
2006-07-29 05:33:03 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
function getPostData()
|
|
|
|
{
|
|
|
|
try {
|
2006-07-29 05:43:05 +00:00
|
|
|
var sessionHistory = getWebNavigation().sessionHistory;
|
|
|
|
return sessionHistory.getEntryAtIndex(sessionHistory.index, false)
|
|
|
|
.QueryInterface(Components.interfaces.nsISHEntry)
|
|
|
|
.postData;
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
function getStringBundle()
|
|
|
|
{
|
|
|
|
const bundleURL = "chrome://communicator/locale/contentAreaCommands.properties";
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
const sbsContractID = "@mozilla.org/intl/stringbundle;1";
|
|
|
|
const sbsIID = Components.interfaces.nsIStringBundleService;
|
|
|
|
const sbs = Components.classes[sbsContractID].getService(sbsIID);
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
const lsContractID = "@mozilla.org/intl/nslocaleservice;1";
|
|
|
|
const lsIID = Components.interfaces.nsILocaleService;
|
|
|
|
const ls = Components.classes[lsContractID].getService(lsIID);
|
2006-07-29 05:42:22 +00:00
|
|
|
var appLocale = ls.getApplicationLocale();
|
2006-07-29 05:43:08 +00:00
|
|
|
return sbs.createBundle(bundleURL, appLocale);
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Get the preferences branch ("browser.download." for normal 'save' mode)...
|
|
|
|
function getPrefsBrowserDownload(branch)
|
|
|
|
{
|
|
|
|
const prefSvcContractID = "@mozilla.org/preferences-service;1";
|
|
|
|
const prefSvcIID = Components.interfaces.nsIPrefService;
|
|
|
|
return Components.classes[prefSvcContractID].getService(prefSvcIID).getBranch(branch);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the current user download directory, return it to the caller:
|
|
|
|
function getUserDownloadDir(prefs)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
return prefs.getComplexValue("dir", Components.interfaces.nsILocalFile);
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
function makeWebBrowserPersist()
|
|
|
|
{
|
|
|
|
const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
|
|
|
|
const persistIID = Components.interfaces.nsIWebBrowserPersist;
|
|
|
|
return Components.classes[persistContractID].createInstance(persistIID);
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
/**
|
|
|
|
* Constructs a new URI, using nsIIOService.
|
|
|
|
* @param aURL The URI spec.
|
|
|
|
* @param aOriginCharset The charset of the URI.
|
|
|
|
* @return an nsIURI object based on aURL.
|
|
|
|
*/
|
|
|
|
function makeURI(aURL, aOriginCharset)
|
2006-07-29 05:39:48 +00:00
|
|
|
{
|
2006-07-29 05:39:54 +00:00
|
|
|
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService);
|
2006-07-29 05:43:15 +00:00
|
|
|
return ioService.newURI(aURL, aOriginCharset, null);
|
2006-07-29 05:42:37 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
function makeFileURI(aFile)
|
2006-07-29 05:42:37 +00:00
|
|
|
{
|
|
|
|
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
|
|
|
|
.getService(Components.interfaces.nsIIOService);
|
|
|
|
return ioService.newFileURI(aFile);
|
2006-07-29 05:39:48 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:02 +00:00
|
|
|
function makeFilePicker()
|
|
|
|
{
|
|
|
|
const fpContractID = "@mozilla.org/filepicker;1";
|
|
|
|
const fpIID = Components.interfaces.nsIFilePicker;
|
|
|
|
return Components.classes[fpContractID].createInstance(fpIID);
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:38 +00:00
|
|
|
function getMIMEService()
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:39:37 +00:00
|
|
|
const mimeSvcContractID = "@mozilla.org/mime;1";
|
2006-07-29 05:39:02 +00:00
|
|
|
const mimeSvcIID = Components.interfaces.nsIMIMEService;
|
2006-07-29 05:39:37 +00:00
|
|
|
const mimeSvc = Components.classes[mimeSvcContractID].getService(mimeSvcIID);
|
2006-07-29 05:39:38 +00:00
|
|
|
return mimeSvc;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// Given aFileName, find the fileName without the extension on the end.
|
|
|
|
function getFileBaseName(aFileName, aFileExt)
|
|
|
|
{
|
|
|
|
// Remove the file extension from aFileName:
|
|
|
|
return aFileName.replace(/\.[^.]*$/, "");
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:42:24 +00:00
|
|
|
function getMIMETypeForURI(aURI)
|
2006-07-29 05:39:38 +00:00
|
|
|
{
|
2006-07-29 05:43:08 +00:00
|
|
|
try {
|
2006-07-29 05:42:26 +00:00
|
|
|
return getMIMEService().getTypeFromURI(aURI);
|
2006-07-29 05:39:38 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
|
2006-07-29 05:42:27 +00:00
|
|
|
function getMIMEInfoForType(aMIMEType, aExtension)
|
2006-07-29 05:39:38 +00:00
|
|
|
{
|
2006-07-29 05:43:08 +00:00
|
|
|
try {
|
2006-07-29 05:42:27 +00:00
|
|
|
return getMIMEService().getFromTypeAndExtension(aMIMEType, aExtension);
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
function getDefaultFileName(aDefaultFileName, aDocumentURI, aDocument)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:40:26 +00:00
|
|
|
try {
|
|
|
|
var url = aDocumentURI.QueryInterface(Components.interfaces.nsIURL);
|
2006-07-29 05:41:19 +00:00
|
|
|
if (url.fileName != "") {
|
2006-07-29 05:43:15 +00:00
|
|
|
// 1) Use the actual file name, if present
|
2006-07-29 05:42:28 +00:00
|
|
|
return validateFileName(decodeURIComponent(url.fileName));
|
2006-07-29 05:41:19 +00:00
|
|
|
}
|
2006-07-29 05:40:26 +00:00
|
|
|
} catch (e) {
|
2006-07-29 05:41:22 +00:00
|
|
|
try {
|
|
|
|
// the file name might be non ASCII
|
|
|
|
// try unescape again with a characterSet
|
|
|
|
var textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
|
|
|
|
.getService(Components.interfaces.nsITextToSubURI);
|
2006-07-29 05:42:13 +00:00
|
|
|
var charset = getCharsetforSave(aDocument);
|
2006-07-29 05:42:29 +00:00
|
|
|
return validateFileName(textToSubURI.unEscapeURIForUI(charset, url.fileName));
|
2006-07-29 05:41:22 +00:00
|
|
|
} catch (e) {
|
|
|
|
// This is something like a wyciwyg:, data:, and so forth
|
|
|
|
// URI... no usable filename here.
|
|
|
|
}
|
2006-07-29 05:40:26 +00:00
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:41:19 +00:00
|
|
|
if (aDocument) {
|
2006-07-29 05:42:23 +00:00
|
|
|
var docTitle = GenerateValidFilename(aDocument.title, "");
|
2006-07-29 05:39:15 +00:00
|
|
|
|
2006-07-29 05:42:23 +00:00
|
|
|
if (docTitle) {
|
2006-07-29 05:43:15 +00:00
|
|
|
// 2) Use the document title
|
2006-07-29 05:41:20 +00:00
|
|
|
return docTitle;
|
2006-07-29 05:41:19 +00:00
|
|
|
}
|
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:15 +00:00
|
|
|
if (aDefaultFileName)
|
2006-07-29 05:43:15 +00:00
|
|
|
// 3) Use the caller-provided name, if any
|
2006-07-29 05:41:17 +00:00
|
|
|
return validateFileName(aDefaultFileName);
|
2006-07-29 05:39:15 +00:00
|
|
|
|
2006-07-29 05:43:15 +00:00
|
|
|
// 4) If this is a directory, use the last directory name
|
2006-07-29 05:42:15 +00:00
|
|
|
var path = aDocumentURI.path.match(/\/([^\/]+)\/$/);
|
2006-07-29 05:42:14 +00:00
|
|
|
if (path && path.length > 1) {
|
2006-07-29 05:43:15 +00:00
|
|
|
return validateFileName(path[1]);
|
2006-07-29 05:42:14 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:41:17 +00:00
|
|
|
try {
|
|
|
|
if (aDocumentURI.host)
|
2006-07-29 05:43:15 +00:00
|
|
|
// 5) Use the host.
|
2006-07-29 05:41:17 +00:00
|
|
|
return aDocumentURI.host;
|
|
|
|
} catch (e) {
|
|
|
|
// Some files have no information at all, like Javascript generated pages
|
|
|
|
}
|
|
|
|
try {
|
2006-07-29 05:43:15 +00:00
|
|
|
// 6) Use the default file name
|
2006-07-29 05:41:17 +00:00
|
|
|
return getStringBundle().GetStringFromName("DefaultSaveFileName");
|
|
|
|
} catch (e) {
|
|
|
|
//in case localized string cannot be found
|
|
|
|
}
|
2006-07-29 05:43:15 +00:00
|
|
|
// 7) If all else fails, use "index"
|
2006-07-29 05:41:17 +00:00
|
|
|
return "index";
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
function getNormalizedLeafName(aFile, aDefaultExtension)
|
2006-07-29 05:39:02 +00:00
|
|
|
{
|
2006-07-29 05:39:46 +00:00
|
|
|
if (!aDefaultExtension)
|
|
|
|
return aFile;
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
// Fix up the file name we're saving to to include the default extension
|
|
|
|
const stdURLContractID = "@mozilla.org/network/standard-url;1";
|
|
|
|
const stdURLIID = Components.interfaces.nsIURL;
|
|
|
|
var url = Components.classes[stdURLContractID].createInstance(stdURLIID);
|
|
|
|
url.filePath = aFile;
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
if (url.fileExtension != aDefaultExtension) {
|
|
|
|
return aFile + "." + aDefaultExtension;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aFile;
|
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
function getDefaultExtension(aFilename, aURI, aContentType)
|
|
|
|
{
|
2006-07-29 05:40:27 +00:00
|
|
|
if (aContentType == "text/plain" || aContentType == "application/octet-stream" || aURI.scheme == "ftp")
|
|
|
|
return ""; // temporary fix for bug 120327
|
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
// First try the extension from the filename
|
|
|
|
const stdURLContractID = "@mozilla.org/network/standard-url;1";
|
|
|
|
const stdURLIID = Components.interfaces.nsIURL;
|
|
|
|
var url = Components.classes[stdURLContractID].createInstance(stdURLIID);
|
|
|
|
url.filePath = aFilename;
|
|
|
|
|
|
|
|
var ext = url.fileExtension;
|
|
|
|
|
2006-07-29 05:42:27 +00:00
|
|
|
// This mirrors some code in nsExternalHelperAppService::DoContent
|
|
|
|
// Use the filename first and then the URI if that fails
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:42:27 +00:00
|
|
|
var mimeInfo = getMIMEInfoForType(aContentType, ext);
|
|
|
|
|
2006-07-29 05:43:06 +00:00
|
|
|
if (ext && mimeInfo && mimeInfo.extensionExists(ext)) {
|
2006-07-29 05:39:46 +00:00
|
|
|
return ext;
|
|
|
|
}
|
2006-07-29 05:43:08 +00:00
|
|
|
|
2006-07-29 05:39:46 +00:00
|
|
|
// Well, that failed. Now try the extension from the URI
|
|
|
|
var urlext;
|
|
|
|
try {
|
|
|
|
url = aURI.QueryInterface(Components.interfaces.nsIURL);
|
|
|
|
urlext = url.fileExtension;
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
|
2006-07-29 05:43:06 +00:00
|
|
|
if (urlext && mimeInfo && mimeInfo.extensionExists(urlext)) {
|
2006-07-29 05:39:46 +00:00
|
|
|
return urlext;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
return mimeInfo.primaryExtension;
|
|
|
|
}
|
|
|
|
catch (e) {
|
|
|
|
// Fall back on the extensions in the filename and URI for lack
|
|
|
|
// of anything better.
|
|
|
|
return ext || urlext;
|
|
|
|
}
|
2006-07-29 05:33:12 +00:00
|
|
|
}
|
2006-07-29 05:39:02 +00:00
|
|
|
}
|
2006-07-29 05:33:14 +00:00
|
|
|
|
2006-07-29 05:42:05 +00:00
|
|
|
function GetSaveModeForContentType(aContentType)
|
2006-07-29 05:39:15 +00:00
|
|
|
{
|
2006-07-29 05:42:05 +00:00
|
|
|
var saveMode = SAVEMODE_FILEONLY;
|
2006-07-29 05:39:15 +00:00
|
|
|
switch (aContentType) {
|
|
|
|
case "text/html":
|
|
|
|
case "application/xhtml+xml":
|
2006-07-29 05:42:05 +00:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_TEXT;
|
|
|
|
// Fall through
|
|
|
|
case "text/xml":
|
2006-07-29 05:40:24 +00:00
|
|
|
case "application/xml":
|
2006-07-29 05:42:05 +00:00
|
|
|
saveMode |= SAVEMODE_COMPLETE_DOM;
|
|
|
|
break;
|
2006-07-29 05:39:15 +00:00
|
|
|
}
|
2006-07-29 05:42:05 +00:00
|
|
|
|
|
|
|
return saveMode;
|
2006-07-29 05:39:15 +00:00
|
|
|
}
|
2006-07-29 05:42:13 +00:00
|
|
|
|
|
|
|
function getCharsetforSave(aDocument)
|
|
|
|
{
|
|
|
|
if (aDocument)
|
|
|
|
return aDocument.characterSet;
|
|
|
|
|
|
|
|
if (document.commandDispatcher.focusedWindow)
|
|
|
|
return document.commandDispatcher.focusedWindow.document.characterSet;
|
|
|
|
|
2006-07-29 05:43:14 +00:00
|
|
|
return window.content.document.characterSet;
|
2006-07-29 05:42:13 +00:00
|
|
|
}
|