📄 nshelperappdlg.js
字号:
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- *//* ***** 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 the Mozilla browser. * * The Initial Developer of the Original Code is * Netscape Communications Corporation. * Portions created by the Initial Developer are Copyright (C) 2001 * the Initial Developer. All Rights Reserved. * * Contributor(s): * Bill Law <law@netscape.com> * Scott MacGregor <mscott@netscape.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 ***** *//* This file implements the nsIHelperAppLauncherDialog interface. * * The implementation consists of a JavaScript "class" named nsHelperAppDialog, * comprised of: * - a JS constructor function * - a prototype providing all the interface methods and implementation stuff * * In addition, this file implements an nsIModule object that registers the * nsHelperAppDialog component. */const nsIHelperAppLauncherDialog = Components.interfaces.nsIHelperAppLauncherDialog;const REASON_CANTHANDLE = nsIHelperAppLauncherDialog.REASON_CANTHANDLE;const REASON_SERVERREQUEST = nsIHelperAppLauncherDialog.REASON_SERVERREQUEST;const REASON_TYPESNIFFED = nsIHelperAppLauncherDialog.REASON_TYPESNIFFED;/* ctor */function nsHelperAppDialog() { // Initialize data properties. this.mLauncher = null; this.mContext = null; this.mSourcePath = null; this.chosenApp = null; this.givenDefaultApp = false; this.strings = new Array; this.elements = new Array; this.updateSelf = true; this.mTitle = ""; this.mIsMac = false;}nsHelperAppDialog.prototype = { // Turn this on to get debugging messages. debug: false, nsIMIMEInfo : Components.interfaces.nsIMIMEInfo, // Dump text (if debug is on). dump: function( text ) { if ( this.debug ) { dump( text ); } }, // This "class" supports nsIHelperAppLauncherDialog, and nsISupports. QueryInterface: function (iid) { if (iid.equals(Components.interfaces.nsIHelperAppLauncherDialog) || iid.equals(Components.interfaces.nsISupports)) return this; Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE; return null; }, // ---------- nsIHelperAppLauncherDialog methods ---------- // show: Open XUL dialog using window watcher. Since the dialog is not // modal, it needs to be a top level window and the way to open // one of those is via that route). show: function(aLauncher, aContext, aReason) { this.mLauncher = aLauncher; this.mContext = aContext; this.mReason = aReason; // Display the dialog using the Window Watcher interface. var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"] .getService( Components.interfaces.nsIWindowWatcher ); this.mDialog = ww.openWindow( null, // no parent "chrome://global/content/nsHelperAppDlg.xul", null, "chrome,titlebar,dialog=yes", null ); // Hook this object to the dialog. this.mDialog.dialog = this; // Watch for error notifications. this.mIsMac = (this.mDialog.navigator.platform.indexOf( "Mac" ) != -1); this.progressListener.helperAppDlg = this; this.mLauncher.setWebProgressListener( this.progressListener ); }, // promptForSaveToFile: Display file picker dialog and return selected file. promptForSaveToFile: function(aLauncher, aContext, aDefaultFile, aSuggestedFileExtension) { var result = ""; const prefSvcContractID = "@mozilla.org/preferences-service;1"; const prefSvcIID = Components.interfaces.nsIPrefService; var branch = Components.classes[prefSvcContractID].getService(prefSvcIID) .getBranch("browser.download."); var dir = null; const nsILocalFile = Components.interfaces.nsILocalFile; const kDownloadDirPref = "dir"; // Try and pull in download directory pref try { dir = branch.getComplexValue(kDownloadDirPref, nsILocalFile); } catch (e) { } var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"] .getService(Components.interfaces.nsIStringBundleService) .createBundle("chrome://global/locale/nsHelperAppDlg.properties"); var autoDownload = branch.getBoolPref("autoDownload"); // If the autoDownload pref is set then just download to default download directory if (autoDownload && dir && dir.exists()) { if (aDefaultFile == "") aDefaultFile = bundle.GetStringFromName("noDefaultFile") + (aSuggestedFileExtension || ""); dir.append(aDefaultFile); return uniqueFile(dir); } // Use file picker to show dialog. var nsIFilePicker = Components.interfaces.nsIFilePicker; var picker = Components.classes[ "@mozilla.org/filepicker;1" ] .createInstance( nsIFilePicker ); var windowTitle = bundle.GetStringFromName( "saveDialogTitle" ); var parent = aContext .QueryInterface( Components.interfaces.nsIInterfaceRequestor ) .getInterface( Components.interfaces.nsIDOMWindowInternal ); picker.init( parent, windowTitle, nsIFilePicker.modeSave ); picker.defaultString = aDefaultFile; if (aSuggestedFileExtension) { // aSuggestedFileExtension includes the period, so strip it picker.defaultExtension = aSuggestedFileExtension.substring(1); } else { try { picker.defaultExtension = this.mLauncher.MIMEInfo.primaryExtension; } catch (ex) { } } var wildCardExtension = "*"; if ( aSuggestedFileExtension ) { wildCardExtension += aSuggestedFileExtension; picker.appendFilter( wildCardExtension, wildCardExtension ); } picker.appendFilters( nsIFilePicker.filterAll ); try { if (dir.exists()) picker.displayDirectory = dir; } catch (e) { } if (picker.show() == nsIFilePicker.returnCancel || !picker.file) { // Null result means user cancelled. return null; } // If not using specified location save the user's choice of directory if (branch.getBoolPref("lastLocation") || autoDownload) { var directory = picker.file.parent.QueryInterface(nsILocalFile); branch.setComplexValue(kDownloadDirPref, nsILocalFile, directory); } return picker.file; }, // ---------- implementation methods ---------- // Web progress listener so we can detect errors while mLauncher is // streaming the data to a temporary file. progressListener: { // Implementation properties. helperAppDlg: null, // nsIWebProgressListener methods. // Look for error notifications and display alert to user. onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage ) { if ( aStatus != Components.results.NS_OK ) { // Get prompt service. var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ] .getService( Components.interfaces.nsIPromptService ); // Display error alert (using text supplied by back-end). prompter.alert( this.dialog, this.helperAppDlg.mTitle, aMessage ); // Close the dialog. this.helperAppDlg.onCancel(); if ( this.helperAppDlg.mDialog ) { this.helperAppDlg.mDialog.close(); } } }, // Ignore onProgressChange, onStateChange, onLocationChange, and onSecurityChange notifications. onProgressChange: function( aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress ) { }, onProgressChange64: function( aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress ) { }, onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus ) { }, onLocationChange: function( aWebProgress, aRequest, aLocation ) { }, onSecurityChange: function( aWebProgress, aRequest, state ) { } }, // initDialog: Fill various dialog fields with initial content. initDialog : function() { // Put product brand short name in prompt. var prompt = this.dialogElement( "prompt" ); var modified = this.replaceInsert( prompt.firstChild.nodeValue, 1, this.getString( "brandShortName" ) ); prompt.firstChild.nodeValue = modified; // Put file name in window title. var suggestedFileName = this.mLauncher.suggestedFileName; // Some URIs do not implement nsIURL, so we can't just QI. var url = this.mLauncher.source.clone(); try { url.userPass = ""; } catch (ex) {} var fname = ""; this.mSourcePath = url.prePath; try { url = url.QueryInterface( Components.interfaces.nsIURL ); // A url, use file name from it. fname = url.fileName; this.mSourcePath += url.directory; } catch (ex) { // A generic uri, use path. fname = url.path; this.mSourcePath += url.path; } if (suggestedFileName) fname = suggestedFileName; this.mTitle = this.replaceInsert( this.mDialog.document.title, 1, fname); this.mDialog.document.title = this.mTitle; // Put content type, filename and location into intro. this.initIntro(url, fname); var iconString = "moz-icon://" + fname + "?size=32&contentType=" + this.mLauncher.MIMEInfo.MIMEType;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -