1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
|
# -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
# ***** BEGIN LICENSE BLOCK *****
# Version: MPL 1.1/GPL 2.0/LGPL 2.1
#
# 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/
#
# 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.
#
# The Original Code is mozilla.org code.
#
# The Initial Developer of the Original Code is
# Netscape Communications Corporation.
# Portions created by the Initial Developer are Copyright (C) 1998
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Ben Goodger <ben@netscape.com> (Save File)
# Fredrik Holmqvist <thesuckiestemail@yahoo.se>
# Asaf Romano <mozilla.mano@sent.com>
#
# Alternatively, the contents of this file may be used under the terms of
# either 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"),
# 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
# use your version of this file under the terms of the MPL, indicate your
# 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
# the terms of any one of the MPL, the GPL or the LGPL.
#
# ***** END LICENSE BLOCK *****
/**
* openNewTabWith: opens a new tab with the given URL.
*
* @param href The URL to open (as a string).
* @param sourceURL The URL of the document from which the URL came, or null.
* This is used to set the referrer header and to do a security check of whether
* the document as allowed to reference the URL.
* If null, there will be no referrer header and no security check.
* @param postData Form POST data, or null.
* @param event The triggering event (for the purpose of determining whether to open in the background), or null
*/
function openNewTabWith(href, sourceURL, postData, event)
{
if (sourceURL)
urlSecurityCheck(href, sourceURL);
var prefSvc = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
prefSvc = prefSvc.getBranch(null);
// should we open it in a new tab?
var loadInBackground = true;
try {
loadInBackground = prefSvc.getBoolPref("browser.tabs.loadInBackground");
}
catch(ex) {
}
if (event && event.shiftKey)
loadInBackground = !loadInBackground;
// As in openNewWindowWith(), we want to pass the charset of the
// current document over to a new tab.
var wintype = document.firstChild.getAttribute('windowtype');
var originCharset;
if (wintype == "navigator:browser")
originCharset = window.content.document.characterSet;
// open link in new tab
var browser = top.document.getElementById("content");
var referrerURI = sourceURL ? makeURI(sourceURL) : null;
browser.loadOneTab(href, referrerURI, originCharset, postData, loadInBackground);
}
function openNewWindowWith(href, sourceURL, postData)
{
if (sourceURL)
urlSecurityCheck(href, sourceURL);
// 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...
var charsetArg = null;
var wintype = document.firstChild.getAttribute('windowtype');
if (wintype == "navigator:browser")
charsetArg = "charset=" + window.content.document.characterSet;
var referrerURI = sourceURL ? makeURI(sourceURL) : null;
window.openDialog(getBrowserURL(), "_blank", "chrome,all,dialog=no", href, charsetArg, referrerURI, postData);
}
/**
* urlSecurityCheck: JavaScript wrapper for CheckLoadURIStr.
* If |sourceURL| is not allowed to link to |url|, this function throws with an error message.
*
* @param url The URL a page has linked to.
* @param sourceURL The URL of the document from which the URL came.
* @param flags Flags to be passed to checkLoadURIStr. If undefined,
* nsIScriptSecurityManager.STANDARD will be passed to checkLoadURIStr.
*/
function urlSecurityCheck(url, sourceURL, flags)
{
const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(nsIScriptSecurityManager);
if (flags === undefined)
flags = nsIScriptSecurityManager.STANDARD;
try {
secMan.checkLoadURIStr(sourceURL, url, flags);
} catch (e) {
throw "Load of " + url + " from " + sourceURL + " denied.";
}
}
function webPanelSecurityCheck(aSourceURL, aDestURL) {
const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
var secMan = Components.classes["@mozilla.org/scriptsecuritymanager;1"]
.getService(nsIScriptSecurityManager);
try {
secMan.checkLoadURIStr(aSourceURL, aDestURL, nsIScriptSecurityManager.STANDARD);
} catch (e) {
return false;
}
return true;
}
function isContentFrame(aFocusedWindow)
{
if (!aFocusedWindow)
return false;
return (aFocusedWindow.top == window.content);
}
const kSaveAsType_Complete = 0; // Save document with attached objects
// const kSaveAsType_URL = 1; // Save document or URL by itself
const kSaveAsType_Text = 2; // Save document, converting to plain text.
// Clientelle: (Make sure you don't break any of these)
// - File -> Save Page/Frame As...
// - Context -> Save Page/Frame As...
// - Context -> Save Link As...
// - Alt-Click links in web pages
// - Alt-Click links in the UI
//
// 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
// the above method.
// - A linked document using Save Link As...
// - A linked document using Alt-click Save Link As...
//
function saveURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
aSkipPrompt, aReferrer)
{
internalSave(aURL, null, aFileName, null, null, aShouldBypassCache,
aFilePickerTitleKey, null, aReferrer, aSkipPrompt);
}
// Just like saveURL, but will get some info off the image before
// calling internalSave
// Clientelle: (Make sure you don't break any of these)
// - Context -> Save Image As...
const imgICache = Components.interfaces.imgICache;
const nsISupportsCString = Components.interfaces.nsISupportsCString;
function saveImageURL(aURL, aFileName, aFilePickerTitleKey, aShouldBypassCache,
aSkipPrompt, aReferrer)
{
var contentType = null;
var contentDisposition = null;
if (!aShouldBypassCache) {
try {
var imageCache = Components.classes["@mozilla.org/image/cache;1"]
.getService(imgICache);
var props =
imageCache.findEntryProperties(makeURI(aURL, getCharsetforSave(null)));
if (props) {
contentType = props.get("type", nsISupportsCString);
contentDisposition = props.get("content-disposition",
nsISupportsCString);
}
} catch (e) {
// Failure to get type and content-disposition off the image is non-fatal
}
}
internalSave(aURL, null, aFileName, contentDisposition, contentType,
aShouldBypassCache, aFilePickerTitleKey, null, aReferrer, aSkipPrompt);
}
function saveFrameDocument()
{
var focusedWindow = document.commandDispatcher.focusedWindow;
if (isContentFrame(focusedWindow))
saveDocument(focusedWindow.document);
}
function saveDocument(aDocument, aSkipPrompt)
{
if (!aDocument)
throw "Must have a document when calling saveDocument";
// We want to use cached data because the document is currently visible.
var contentDisposition = null;
try {
contentDisposition =
aDocument.defaultView
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindowUtils)
.getDocumentMetadata("content-disposition");
} catch (ex) {
// Failure to get a content-disposition is ok
}
internalSave(aDocument.location.href, aDocument, null, contentDisposition,
aDocument.contentType, false, null, null, aSkipPrompt);
}
/**
* 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 aContentDisposition The caller-provided content-disposition header
* to use.
* @param aContentType The caller-provided content-type to use
* @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.
* @param aReferrer the referrer URI object (not URL string) to use, or null
if no referrer should be sent.
* @param aSkipPrompt If true, the file will be saved to the default download folder.
*/
function internalSave(aURL, aDocument, aDefaultFileName, aContentDisposition,
aContentType, aShouldBypassCache, aFilePickerTitleKey,
aChosenData, aReferrer, aSkipPrompt)
{
if (aSkipPrompt == undefined)
aSkipPrompt = false;
// Note: aDocument == null when this code is used by save-link-as...
var saveMode = GetSaveModeForContentType(aContentType);
var isDocument = aDocument != null && saveMode != SAVEMODE_FILEONLY;
var saveAsType = kSaveAsType_Complete;
var file, fileURL;
// 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)
file = aChosenData.file;
else {
var charset = null;
if (aDocument)
charset = aDocument.characterSet;
else if (aReferrer)
charset = aReferrer.originCharset;
initFileInfo(fileInfo, aURL, aDocument, aContentType, aContentDisposition,
charset);
var fpParams = {
fpTitleKey: aFilePickerTitleKey,
isDocument: isDocument,
fileInfo: fileInfo,
contentType: aContentType,
saveMode: saveMode,
saveAsType: saveAsType,
file: file,
fileURL: fileURL
};
if (!getTargetFile(fpParams, aSkipPrompt))
// If the method returned false this is because the user cancelled from
// the save file picker dialog.
return;
saveAsType = fpParams.saveAsType;
saveMode = fpParams.saveMode;
file = fpParams.file;
fileURL = fpParams.fileURL;
}
if (!fileURL)
fileURL = makeFileURI(file);
// XXX We depend on the following holding true in appendFiltersForContentType():
// If we should save as a complete page, the saveAsType is kSaveAsType_Complete.
// If we should save as text, the saveAsType is kSaveAsType_Text.
var useSaveDocument = isDocument &&
(((saveMode & SAVEMODE_COMPLETE_DOM) && (saveAsType == kSaveAsType_Complete)) ||
((saveMode & SAVEMODE_COMPLETE_TEXT) && (saveAsType == kSaveAsType_Text)));
// If we're saving a document, and are saving either in complete mode or
// 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.
var source = useSaveDocument ? aDocument : fileInfo.uri;
var persistArgs = {
source : source,
contentType : (!aChosenData && useSaveDocument &&
saveAsType == kSaveAsType_Text) ?
"text/plain" : aContentType,
target : fileURL,
postData : isDocument ? getPostData() : null,
bypassCache : aShouldBypassCache
};
var persist = makeWebBrowserPersist();
// Calculate persist flags.
const nsIWBP = Components.interfaces.nsIWebBrowserPersist;
const flags = nsIWBP.PERSIST_FLAGS_REPLACE_EXISTING_FILES;
if (aShouldBypassCache)
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_BYPASS_CACHE;
else
persist.persistFlags = flags | nsIWBP.PERSIST_FLAGS_FROM_CACHE;
// Leave it to WebBrowserPersist to discover the encoding type (or lack thereof):
persist.persistFlags |= nsIWBP.PERSIST_FLAGS_AUTODETECT_APPLY_CONVERSION;
// Create download and initiate it (below)
var tr = Components.classes["@mozilla.org/transfer;1"].createInstance(Components.interfaces.nsITransfer);
if (useSaveDocument) {
// Saving a Document, not a URI:
var filesFolder = null;
if (persistArgs.contentType != "text/plain") {
// Create the local directory into which to save associated files.
filesFolder = file.clone();
var nameWithoutExtension = filesFolder.leafName.replace(/\.[^.]*$/, "");
var filesFolderLeafName = getStringBundle().formatStringFromName("filesFolder",
[nameWithoutExtension],
1);
filesFolder.leafName = filesFolderLeafName;
}
var encodingFlags = 0;
if (persistArgs.contentType == "text/plain") {
encodingFlags |= nsIWBP.ENCODE_FLAGS_FORMATTED;
encodingFlags |= nsIWBP.ENCODE_FLAGS_ABSOLUTE_LINKS;
encodingFlags |= nsIWBP.ENCODE_FLAGS_NOFRAMES_CONTENT;
}
else {
encodingFlags |= nsIWBP.ENCODE_FLAGS_ENCODE_BASIC_ENTITIES;
}
const kWrapColumn = 80;
tr.init((aChosenData ? aChosenData.uri : fileInfo.uri),
persistArgs.target, "", null, null, null, persist);
persist.progressListener = tr;
persist.saveDocument(persistArgs.source, persistArgs.target, filesFolder,
persistArgs.contentType, encodingFlags, kWrapColumn);
} else {
tr.init((aChosenData ? aChosenData.uri : source),
persistArgs.target, "", null, null, null, persist);
persist.progressListener = tr;
persist.saveURI((aChosenData ? aChosenData.uri : source),
null, aReferrer, persistArgs.postData, null,
persistArgs.target);
}
}
/**
* 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;
}
/**
* 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;
}
/**
* 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 we're saving, if it could be
* determined by the caller.
* @param aContentDisposition The content-disposition header for the object
* we're saving, if it could be determined by the caller.
* @param aURLCharset The charset of aURL.
*/
function initFileInfo(aFI, aURL, aDocument, aContentType, aContentDisposition,
aURLCharset)
{
if (!aURLCharset && aDocument && aDocument.characterSet)
aURLCharset = aDocument.characterSet;
try {
// Get an nsIURI object from aURL if possible:
try {
aFI.uri = makeURI(aURL, aURLCharset);
// 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);
aFI.fileExt = url.fileExtension;
} catch (e) {
}
// Get the default filename:
aFI.fileName = getDefaultFileName((aFI.suggestedFileName || aFI.fileName),
aFI.uri, aDocument, aContentDisposition);
// 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 && (/^http(s?):\/\//i.test(aURL))) {
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) {
}
}
function getTargetFile(aFpP, aSkipPrompt)
{
const prefSvcContractID = "@mozilla.org/preferences-service;1";
const prefSvcIID = Components.interfaces.nsIPrefService;
var prefs = Components.classes[prefSvcContractID]
.getService(prefSvcIID).getBranch("browser.download.");
const nsILocalFile = Components.interfaces.nsILocalFile;
// ben 07/31/2003:
// |browser.download.defaultFolder| holds the default download folder for
// all files when the user has elected to have all files automatically
// download to a folder. The values of |defaultFolder| can be either their
// desktop, their downloads folder (My Documents\My Downloads) or some other
// location of their choosing (which is mapped to |browser.download.dir|
// This pref is _unset_ when the user has elected to be asked about where
// to place every download - this will force the prompt to ask the user
// where to put saved files.
var dir = null;
var useDownloadDir = prefs.getBoolPref("useDownloadDir");
function getSpecialFolderKey(aFolderType)
{
if (aFolderType == "Desktop")
return "Desk";
if (aFolderType != "Downloads")
throw "ASSERTION FAILED: folder type should be 'Desktop' or 'Downloads'";
#ifdef XP_WIN
return "Pers";
#else
#ifdef XP_MACOSX
return "UsrDocs";
#else
return "Home";
#endif
#endif
}
function getDownloadsFolder(aFolder)
{
var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
var dir = fileLocator.get(getSpecialFolderKey(aFolder), Components.interfaces.nsILocalFile);
var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
.getService(Components.interfaces.nsIStringBundleService);
bundle = bundle.createBundle("chrome://mozapps/locale/downloads/unknownContentType.properties");
var description = bundle.GetStringFromName("myDownloads");
if (aFolder != "Desktop")
dir.append(description);
return dir;
}
switch (prefs.getIntPref("folderList")) {
case 0:
dir = getDownloadsFolder("Desktop")
break;
case 1:
dir = getDownloadsFolder("Downloads");
break;
case 2:
dir = prefs.getComplexValue("dir", nsILocalFile);
break;
}
if (!aSkipPrompt || !useDownloadDir || !dir) {
// If we're asking the user where to save the file, root the Save As...
// dialog on they place they last picked.
try {
dir = prefs.getComplexValue("lastDir", nsILocalFile);
}
catch (e) {
// No default download location. Default to desktop.
var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
dir = fileLocator.get(getSpecialFolderKey("Desktop"), nsILocalFile);
}
var fp = makeFilePicker();
var titleKey = aFpP.fpTitleKey || "SaveLinkTitle";
var bundle = getStringBundle();
fp.init(window, bundle.GetStringFromName(titleKey),
Components.interfaces.nsIFilePicker.modeSave);
fp.defaultExtension = aFpP.fileInfo.fileExt;
fp.defaultString = getNormalizedLeafName(aFpP.fileInfo.fileName,
aFpP.fileInfo.fileExt);
appendFiltersForContentType(fp, aFpP.contentType, aFpP.fileInfo.fileExt,
aFpP.saveMode);
if (dir)
fp.displayDirectory = dir;
if (aFpP.isDocument) {
try {
fp.filterIndex = prefs.getIntPref("save_converter_index");
}
catch (e) {
}
}
fp.defaultExtension = aFpP.fileInfo.fileExt;
fp.defaultString = getNormalizedLeafName(aFpP.fileInfo.fileName,
aFpP.fileInfo.fileExt);
if (fp.show() == Components.interfaces.nsIFilePicker.returnCancel || !fp.file)
return false;
var directory = fp.file.parent.QueryInterface(nsILocalFile);
prefs.setComplexValue("lastDir", nsILocalFile, directory);
fp.file.leafName = validateFileName(fp.file.leafName);
aFpP.saveAsType = fp.filterIndex;
aFpP.file = fp.file;
aFpP.fileURL = fp.fileURL;
if (aFpP.isDocument)
prefs.setIntPref("save_converter_index", aFpP.saveAsType);
}
else {
dir.append(getNormalizedLeafName(aFpP.fileInfo.fileName,
aFpP.fileInfo.fileExt));
var file = dir;
// Since we're automatically downloading, we don't get the file picker's
// logic to check for existing files, so we need to do that here.
//
// Note - this code is identical to that in
// mozilla/toolkit/mozapps/downloads/src/nsHelperAppDlg.js.in
// If you are updating this code, update that code too! We can't share code
// here since that code is called in a js component.
var collisionCount = 0;
while (file.exists()) {
collisionCount++;
if (collisionCount == 1) {
// Append "(2)" before the last dot in (or at the end of) the filename
// special case .ext.gz etc files so we don't wind up with .tar(2).gz
if (file.leafName.match(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i))
file.leafName = file.leafName.replace(/\.[^\.]{1,3}\.(gz|bz2|Z)$/i, "(2)$&");
else
file.leafName = file.leafName.replace(/(\.[^\.]*)?$/, "(2)$&");
}
else {
// replace the last (n) in the filename with (n+1)
file.leafName = file.leafName.replace(/^(.*\()\d+\)/, "$1" + (collisionCount+1) + ")");
}
}
aFpP.file = file;
}
return true;
}
// 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.
function appendFiltersForContentType(aFilePicker, aContentType, aFileExtension, aSaveMode)
{
var bundle = getStringBundle();
// 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.
switch (aContentType) {
case "text/html":
bundleName = "WebPageHTMLOnlyFilter";
filterString = "*.htm; *.html";
break;
case "application/xhtml+xml":
bundleName = "WebPageXHTMLOnlyFilter";
filterString = "*.xht; *.xhtml";
break;
case "text/xml":
case "application/xml":
bundleName = "WebPageXMLOnlyFilter";
filterString = "*.xml";
break;
default:
if (aSaveMode != SAVEMODE_FILEONLY)
throw "Invalid save mode for type '" + aContentType + "'";
var mimeInfo = getMIMEInfoForType(aContentType, aFileExtension);
if (mimeInfo) {
var extEnumerator = mimeInfo.getFileExtensions();
var extString = "";
while (extEnumerator.hasMore()) {
var extension = extEnumerator.getNext();
if (extString)
extString += "; "; // If adding more than one extension,
// separate by semi-colon
extString += "*." + extension;
}
if (extString)
aFilePicker.appendFilter(mimeInfo.description, extString);
}
break;
}
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);
}
function getPostData()
{
try {
var sessionHistory = getWebNavigation().sessionHistory;
var entry = sessionHistory.getEntryAtIndex(sessionHistory.index, false);
entry = entry.QueryInterface(Components.interfaces.nsISHEntry);
return entry.postData;
}
catch (e) {
}
return null;
}
function getStringBundle()
{
const bundleURL = "chrome://global/locale/contentAreaCommands.properties";
const sbsContractID = "@mozilla.org/intl/stringbundle;1";
const sbsIID = Components.interfaces.nsIStringBundleService;
const sbs = Components.classes[sbsContractID].getService(sbsIID);
const lsContractID = "@mozilla.org/intl/nslocaleservice;1";
const lsIID = Components.interfaces.nsILocaleService;
const ls = Components.classes[lsContractID].getService(lsIID);
var appLocale = ls.getApplicationLocale();
return sbs.createBundle(bundleURL, appLocale);
}
function makeWebBrowserPersist()
{
const persistContractID = "@mozilla.org/embedding/browser/nsWebBrowserPersist;1";
const persistIID = Components.interfaces.nsIWebBrowserPersist;
return Components.classes[persistContractID].createInstance(persistIID);
}
function makeURI(aURL, aOriginCharset, aBaseURI)
{
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
return ioService.newURI(aURL, aOriginCharset, aBaseURI);
}
function makeFileURI(aFile)
{
var ioService = Components.classes["@mozilla.org/network/io-service;1"]
.getService(Components.interfaces.nsIIOService);
return ioService.newFileURI(aFile);
}
function makeFilePicker()
{
const fpContractID = "@mozilla.org/filepicker;1";
const fpIID = Components.interfaces.nsIFilePicker;
return Components.classes[fpContractID].createInstance(fpIID);
}
function getMIMEService()
{
const mimeSvcContractID = "@mozilla.org/mime;1";
const mimeSvcIID = Components.interfaces.nsIMIMEService;
const mimeSvc = Components.classes[mimeSvcContractID].getService(mimeSvcIID);
return mimeSvc;
}
// Given aFileName, find the fileName without the extension on the end.
function getFileBaseName(aFileName, aFileExt)
{
// Remove the file extension from aFileName:
return aFileName.replace(/\.[^.]*$/, "");
}
function getMIMETypeForURI(aURI)
{
try {
return getMIMEService().getTypeFromURI(aURI);
}
catch (e) {
}
return null;
}
function getMIMEInfoForType(aMIMEType, aExtension)
{
if (aMIMEType || aExtension) {
try {
return getMIMEService().getFromTypeAndExtension(aMIMEType, aExtension);
}
catch (e) {
}
}
return null;
}
function getDefaultFileName(aDefaultFileName, aURI, aDocument,
aContentDisposition)
{
// 1) look for a filename in the content-disposition header, if any
if (aContentDisposition) {
const mhpContractID = "@mozilla.org/network/mime-hdrparam;1";
const mhpIID = Components.interfaces.nsIMIMEHeaderParam;
const mhp = Components.classes[mhpContractID].getService(mhpIID);
var dummy = { value: null }; // Need an out param...
var charset = getCharsetforSave(aDocument);
var fileName = null;
try {
fileName = mhp.getParameter(aContentDisposition, "filename", charset,
true, dummy);
}
catch (e) {
try {
fileName = mhp.getParameter(aContentDisposition, "name", charset, true,
dummy);
}
catch (e) {
}
}
if (fileName)
return fileName;
}
try {
var url = aURI.QueryInterface(Components.interfaces.nsIURL);
if (url.fileName != "") {
// 2) Use the actual file name, if present
var textToSubURI = Components.classes["@mozilla.org/intl/texttosuburi;1"]
.getService(Components.interfaces.nsITextToSubURI);
return validateFileName(textToSubURI.unEscapeURIForUI(url.originCharset || "UTF-8", url.fileName));
}
} catch (e) {
// This is something like a data: and so forth URI... no filename here.
}
if (aDocument) {
var docTitle = validateFileName(aDocument.title).replace(/^\s+|\s+$/g, "");
if (docTitle) {
// 3) Use the document title
return docTitle;
}
}
if (aDefaultFileName)
// 4) Use the caller-provided name, if any
return validateFileName(aDefaultFileName);
// 5) If this is a directory, use the last directory name
var path = aURI.path.match(/\/([^\/]+)\/$/);
if (path && path.length > 1)
return validateFileName(path[1]);
try {
if (aURI.host)
// 6) Use the host.
return aURI.host;
} catch (e) {
// Some files have no information at all, like Javascript generated pages
}
try {
// 7) Use the default file name
return getStringBundle().GetStringFromName("DefaultSaveFileName");
} catch (e) {
//in case localized string cannot be found
}
// 8) If all else fails, use "index"
return "index";
}
function validateFileName(aFileName)
{
var re = /[\/]+/g;
if (navigator.appVersion.indexOf("Windows") != -1) {
re = /[\\\/\|]+/g;
aFileName = aFileName.replace(/[\"]+/g, "'");
aFileName = aFileName.replace(/[\*\:\?]+/g, " ");
aFileName = aFileName.replace(/[\<]+/g, "(");
aFileName = aFileName.replace(/[\>]+/g, ")");
}
else if (navigator.appVersion.indexOf("Macintosh") != -1)
re = /[\:\/]+/g;
return aFileName.replace(re, "_");
}
function getNormalizedLeafName(aFile, aDefaultExtension)
{
if (!aDefaultExtension)
return aFile;
#ifdef XP_WIN
// Remove trailing dots and spaces on windows
aFile = aFile.replace(/[\s.]+$/, "");
#endif
// Fix up the file name we're saving to to include the default extension
var i = aFile.lastIndexOf(".");
if (aFile.substr(i + 1) != aDefaultExtension)
return aFile + "." + aDefaultExtension;
return aFile;
}
function getDefaultExtension(aFilename, aURI, aContentType)
{
if (aContentType == "text/plain" || aContentType == "application/octet-stream" || aURI.scheme == "ftp")
return ""; // temporary fix for bug 120327
// 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;
// This mirrors some code in nsExternalHelperAppService::DoContent
// Use the filename first and then the URI if that fails
var mimeInfo = getMIMEInfoForType(aContentType, ext);
if (ext && mimeInfo && mimeInfo.extensionExists(ext))
return ext;
// Well, that failed. Now try the extension from the URI
var urlext;
try {
url = aURI.QueryInterface(Components.interfaces.nsIURL);
urlext = url.fileExtension;
} catch (e) {
}
if (urlext && mimeInfo && mimeInfo.extensionExists(urlext)) {
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;
}
}
}
function GetSaveModeForContentType(aContentType)
{
var saveMode = SAVEMODE_FILEONLY;
switch (aContentType) {
case "text/html":
case "application/xhtml+xml":
saveMode |= SAVEMODE_COMPLETE_TEXT;
// Fall through
case "text/xml":
case "application/xml":
saveMode |= SAVEMODE_COMPLETE_DOM;
break;
}
return saveMode;
}
function getCharsetforSave(aDocument)
{
if (aDocument)
return aDocument.characterSet;
if (document.commandDispatcher.focusedWindow)
return document.commandDispatcher.focusedWindow.document.characterSet;
return window.content.document.characterSet;
}
|