home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 October / PCWorld_2003-10_cd.bin / Komunik / Thunderbird / thunderbird-0.2-win32.exe / thunderbird / components / nsHelperAppDlg.js < prev    next >
Text File  |  2003-09-01  |  33KB  |  834 lines

  1. /*
  2. # -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  3. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. # Software distributed under the License is distributed on an "AS IS" basis,
  9. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10. # for the specific language governing rights and limitations under the
  11. # License.
  12. # The Original Code is Mozilla.org Code.
  13. # The Initial Developer of the Original Code is
  14. # Doron Rosenberg.
  15. # Portions created by the Initial Developer are Copyright (C) 2001
  16. # the Initial Developer. All Rights Reserved.
  17. # Contributor(s):
  18. #   Bill Law <law@netscape.com>
  19. #   Scott MacGregor <mscott@netscape.com>
  20. #   Ben Goodger <ben@bengoodger.com> (2.0)
  21. # Alternatively, the contents of this file may be used under the terms of
  22. # either the GNU General Public License Version 2 or later (the "GPL"), or
  23. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  24. # in which case the provisions of the GPL or the LGPL are applicable instead
  25. # of those above. If you wish to allow use of your version of this file only
  26. # under the terms of either the GPL or the LGPL, and not to allow others to
  27. # use your version of this file under the terms of the MPL, indicate your
  28. # decision by deleting the provisions above and replace them with the notice
  29. # and other provisions required by the GPL or the LGPL. If you do not delete
  30. # the provisions above, a recipient may use your version of this file under
  31. # the terms of any one of the MPL, the GPL or the LGPL.
  32. # ***** END LICENSE BLOCK *****
  33. */
  34.  
  35. /* This file implements the nsIHelperAppLauncherDialog interface.
  36.  *
  37.  * The implementation consists of a JavaScript "class" named nsUnknownContentTypeDialog,
  38.  * comprised of:
  39.  *   - a JS constructor function
  40.  *   - a prototype providing all the interface methods and implementation stuff
  41.  *
  42.  * In addition, this file implements an nsIModule object that registers the
  43.  * nsUnknownContentTypeDialog component.
  44.  */
  45.  
  46.  
  47. /* ctor
  48.  */
  49. function nsUnknownContentTypeDialog() {
  50.     // Initialize data properties.
  51.     this.mLauncher = null;
  52.     this.mContext  = null;
  53.     this.mSourcePath = null;
  54.     this.chosenApp = null;
  55.     this.givenDefaultApp = false;
  56.     this.updateSelf = true;
  57.     this.mTitle    = "";
  58. }
  59.  
  60. nsUnknownContentTypeDialog.prototype = {
  61.     nsIMIMEInfo  : Components.interfaces.nsIMIMEInfo,
  62.  
  63.     // This "class" supports nsIHelperAppLauncherDialog, and nsISupports.
  64.     QueryInterface: function (iid) {
  65.         if (!iid.equals(Components.interfaces.nsIHelperAppLauncherDialog) &&
  66.             !iid.equals(Components.interfaces.nsISupports)) {
  67.             throw Components.results.NS_ERROR_NO_INTERFACE;
  68.         }
  69.         return this;
  70.     },
  71.  
  72.     // ---------- nsIHelperAppLauncherDialog methods ----------
  73.  
  74.     // show: Open XUL dialog using window watcher.  Since the dialog is not
  75.     //       modal, it needs to be a top level window and the way to open
  76.     //       one of those is via that route).
  77.     show: function(aLauncher, aContext)  {  
  78.       this.mLauncher = aLauncher;
  79.       this.mContext  = aContext;
  80.       // Display the dialog using the Window Watcher interface.
  81.       var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  82.                 .getService(Components.interfaces.nsIWindowWatcher);
  83.       this.mDialog = ww.openWindow(null, // no parent
  84.                                    "chrome://mozapps/content/downloads/unknownContentType.xul",
  85.                                    null,
  86.                                    "chrome,titlebar,dialog=yes",
  87.                                    null);
  88.       // Hook this object to the dialog.
  89.       this.mDialog.dialog = this;
  90.       
  91.       // Hook up utility functions. 
  92.       // XXXben these lines can disappear if we can get XULPP to run on stuff not 
  93.       //        thus referenced in jar.mns. 
  94.       this.getSpecialFolderKey = this.mDialog.getSpecialFolderKey;
  95.       
  96.       this.mIsMac = (this.mDialog.navigator.platform.indexOf("Mac") != -1);
  97.       this.mIsWin = (this.mDialog.navigator.platform.indexOf("Win") != -1);
  98.       
  99.       // Watch for error notifications.
  100.       this.progressListener.helperAppDlg = this;
  101.       this.mLauncher.setWebProgressListener(this.progressListener);
  102.     },
  103.  
  104.     // promptForSaveToFile:  Display file picker dialog and return selected file.
  105.     //                       This is called by the External Helper App Service
  106.     //                       after the ucth dialog calls |saveToDisk| with a null
  107.     //                       target filename (no target, therefore user must pick).
  108.     //
  109.     //                       Alternatively, if the user has selected to have all
  110.     //                       files download to a specific location, return that
  111.     //                       location and don't ask via the dialog. 
  112.     //
  113.     // Note - this function is called without a dialog, so it cannot access any part
  114.     // of the dialog XUL as other functions on this object do. 
  115.     promptForSaveToFile: function(aLauncher, aContext, aDefaultFile, aSuggestedFileExtension) {
  116.       var result = "";
  117.       
  118.       this.mLauncher = aLauncher;
  119.  
  120.       // If the user is always downloading to the same location, the default download
  121.       // folder is stored in preferences. If a value is found stored, use that 
  122.       // automatically and don't ask via a dialog. 
  123.       const kDownloadFolderPref = "browser.download.defaultFolder";
  124.       var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  125.       try {
  126.         result = prefs.getComplexValue(kDownloadFolderPref, Components.interfaces.nsILocalFile);
  127.         result.append(aDefaultFile);
  128.         
  129.         // Since we're automatically downloading, we don't get the file picker's 
  130.         // logic to check for existing files, so we need to do that here.
  131.         //
  132.         // Note - this code is identical to that in 
  133.         //   browser/base/content/contentAreaUtils.js. 
  134.         // If you are updating this code, update that code too! We can't share code
  135.         // here since this is called in a js component. 
  136.         while (result.exists()) {
  137.           var parts = /.+-(\d+)(\..*)?$/.exec(result.leafName);
  138.           if (parts) {
  139.             result.leafName = result.leafName.replace(/((\d+)\.)/, 
  140.                                                       function (str, p1, part, s) { 
  141.                                                         return (parseInt(part) + 1) + "."; 
  142.                                                       });
  143.           }
  144.           else {
  145.             result.leafName = result.leafName.replace(/\./, "-1$&");
  146.           }
  147.         }
  148.       }
  149.       catch (e) { }
  150.       
  151.       if (!result) {
  152.         // Use file picker to show dialog.
  153.         var nsIFilePicker = Components.interfaces.nsIFilePicker;
  154.         var picker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  155.  
  156.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"].getService(Components.interfaces.nsIStringBundleService);
  157.         bundle = bundle.createBundle("chrome://mozapps/locale/downloads/unknownContentType.properties");
  158.  
  159.         var windowTitle = bundle.GetStringFromName("saveDialogTitle");
  160.         var parent = aContext.QueryInterface(Components.interfaces.nsIInterfaceRequestor).getInterface(Components.interfaces.nsIDOMWindowInternal);
  161.         picker.init(parent, windowTitle, nsIFilePicker.modeSave);
  162.         picker.defaultString = aDefaultFile;
  163.  
  164.         if (aSuggestedFileExtension) {
  165.           // aSuggestedFileExtension includes the period, so strip it
  166.           picker.defaultExtension = aSuggestedFileExtension.substring(1);
  167.         } 
  168.         else {
  169.           try {
  170.             picker.defaultExtension = this.mLauncher.MIMEInfo.primaryExtension;
  171.           } 
  172.           catch (ex) { }
  173.         }
  174.  
  175.         var wildCardExtension = "*";
  176.         if (aSuggestedFileExtension) {
  177.           wildCardExtension += aSuggestedFileExtension;
  178.           picker.appendFilter(this.mLauncher.MIMEInfo.Description, wildCardExtension);
  179.         }
  180.  
  181.         picker.appendFilters( nsIFilePicker.filterAll );
  182.  
  183.         // Pull in the user's preferences and get the default download directory.
  184.         var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  185.         try {
  186.           var startDir = prefs.getComplexValue("browser.download.dir", Components.interfaces.nsILocalFile);
  187.           if (startDir.exists()) {
  188.             picker.displayDirectory = startDir;
  189.           }
  190.         } 
  191.         catch(exception) { }
  192.  
  193.         var dlgResult = picker.show();
  194.  
  195.         if (dlgResult == nsIFilePicker.returnCancel) {
  196.           // null result means user cancelled.
  197.           return null;
  198.         }
  199.  
  200.  
  201.         // Be sure to save the directory the user chose as the new browser.download.dir
  202.         result = picker.file;
  203.  
  204.         if (result) {
  205.           var newDir = result.parent;
  206.           prefs.setComplexValue("browser.download.dir", Components.interfaces.nsILocalFile, newDir);
  207.         }
  208.         
  209.       }
  210.       return result;
  211.     },
  212.     
  213.     // ---------- implementation methods ----------
  214.  
  215.     // Web progress listener so we can detect errors while mLauncher is
  216.     // streaming the data to a temporary file.
  217.     progressListener: {
  218.         // Implementation properties.
  219.         helperAppDlg: null,
  220.  
  221.         // nsIWebProgressListener methods.
  222.         // Look for error notifications and display alert to user.
  223.         onStatusChange: function( aWebProgress, aRequest, aStatus, aMessage ) {
  224.             if ( aStatus != Components.results.NS_OK ) {
  225.                 // Get prompt service.
  226.                 var prompter = Components.classes[ "@mozilla.org/embedcomp/prompt-service;1" ]
  227.                                    .getService( Components.interfaces.nsIPromptService );
  228.                 // Display error alert (using text supplied by back-end).
  229.                 prompter.alert( this.dialog, this.helperAppDlg.mTitle, aMessage );
  230.  
  231.                 // Close the dialog.
  232.                 this.helperAppDlg.onCancel();
  233.                 if ( this.helperAppDlg.mDialog ) {
  234.                     this.helperAppDlg.mDialog.close();
  235.                 }
  236.             }
  237.         },
  238.  
  239.         // Ignore onProgressChange, onStateChange, onLocationChange, and onSecurityChange notifications.
  240.         onProgressChange: function( aWebProgress,
  241.                                     aRequest,
  242.                                     aCurSelfProgress,
  243.                                     aMaxSelfProgress,
  244.                                     aCurTotalProgress,
  245.                                     aMaxTotalProgress ) {
  246.         },
  247.  
  248.         onStateChange: function( aWebProgress, aRequest, aStateFlags, aStatus ) {
  249.         },
  250.  
  251.         onLocationChange: function( aWebProgress, aRequest, aLocation ) {
  252.         },
  253.  
  254.         onSecurityChange: function( aWebProgress, aRequest, state ) {
  255.         }
  256.     },
  257.  
  258.     // initDialog:  Fill various dialog fields with initial content.
  259.     initDialog : function() {
  260.          // Put file name in window title.
  261.          var win   = this.dialogElement( "unknownContentType" );
  262.          var suggestedFileName = this.mLauncher.suggestedFileName;
  263.  
  264.          // Some URIs do not implement nsIURL, so we can't just QI.
  265.          var url   = this.mLauncher.source;
  266.          var fname = "";
  267.          this.mSourcePath = url.prePath;
  268.          try {
  269.              url = url.QueryInterface( Components.interfaces.nsIURL );
  270.              // A url, use file name from it.
  271.              fname = url.fileName;
  272.              this.mSourcePath += url.directory;
  273.          } catch (ex) {
  274.              // A generic uri, use path.
  275.              fname = url.path;
  276.              this.mSourcePath += url.path;
  277.          }
  278.  
  279.          if (suggestedFileName)
  280.            fname = suggestedFileName;
  281.            
  282.  
  283.          this.mTitle = this.dialogElement("strings").getFormattedString("title", [fname]);
  284.          win.setAttribute( "title", this.mTitle );
  285.  
  286.          // Put content type, filename and location into intro.
  287.          this.initIntro(url, fname);
  288.  
  289.          var iconString = "moz-icon://" + fname + "?size=16&contentType=" + this.mLauncher.MIMEInfo.MIMEType;
  290.          this.dialogElement("contentTypeImage").setAttribute("src", iconString);
  291.  
  292.          this.initAppAndSaveToDiskValues();
  293.  
  294.          // Initialize "always ask me" box. This should always be disabled
  295.          // and set to true for the ambiguous type application/octet-stream.
  296.          // We don't also check for application/x-msdownload here since we
  297.          // want users to be able to autodownload .exe files. 
  298.          var rememberChoice = this.dialogElement("rememberChoice");
  299.          if (this.mLauncher.MIMEInfo.MIMEType == "application/octet-stream") {
  300.             rememberChoice.checked = false;
  301.             rememberChoice.disabled = true;
  302.          }
  303.          else {
  304.             rememberChoice.checked = !this.mLauncher.MIMEInfo.alwaysAskBeforeHandling;
  305.          }
  306.          this.toggleRememberChoice(rememberChoice);
  307.          
  308.  
  309.          // XXXben - menulist won't init properly, hack. 
  310.          var openHandler = this.dialogElement("openHandler");
  311.          openHandler.parentNode.removeChild(openHandler);
  312.          var openParent = this.dialogElement("open").parentNode;
  313.          openParent.appendChild(openHandler);
  314.          
  315.          this.mDialog.setTimeout("dialog.postShowCallback()", 0);
  316.     },
  317.     
  318.     postShowCallback: function () {
  319.       // Position the window if it has never been positioned before (i.e. 
  320.       // before XUL persistence has picked up anything to fill the screenX/
  321.       // screenY attributes with. Assume if one is unset, the other will be too).
  322.       if (this.mDialog.document.documentElement.getAttribute("screenX") == "") {
  323.         if (this.mDialog.opener) {
  324.           this.mDialog.moveToAlertPosition();
  325.         } 
  326.         else {
  327.           this.mDialog.centerWindowOnScreen();
  328.         }
  329.       }
  330.       
  331.       this.mDialog.sizeToContent();
  332.  
  333.       // Set initial focus
  334.       this.dialogElement("mode").focus();
  335.     },
  336.  
  337.     // initIntro:
  338.     initIntro: function(url, filename) {
  339.         this.dialogElement( "location" ).value = filename;
  340.  
  341.         // if mSourcePath is a local file, then let's use the pretty path name instead of an ugly
  342.         // url...
  343.         var pathString = this.mSourcePath;
  344.         try 
  345.         {
  346.           var fileURL = url.QueryInterface(Components.interfaces.nsIFileURL);
  347.           if (fileURL)
  348.           {
  349.             var fileObject = fileURL.file;
  350.             if (fileObject)
  351.             {
  352.               var parentObject = fileObject.parent;
  353.               if (parentObject)
  354.               {
  355.                 pathString = parentObject.path;
  356.               }
  357.             }
  358.           }
  359.         } catch(ex) {}
  360.  
  361.         // Set the location text, which is separate from the intro text so it can be cropped
  362.         var location = this.dialogElement( "source" );
  363.         location.value = pathString;
  364.         
  365.         // Show the type of file. 
  366.         var type = this.dialogElement("type");
  367.         var mimeInfo = this.mLauncher.MIMEInfo;
  368.         
  369.         // 1. Try to use the pretty description of the type, if one is available.
  370.         var typeString = mimeInfo.Description;
  371.         
  372.         if (typeString == "") {
  373.           // 2. If there is none, use the extension to identify the file, e.g. "ZIP file"          var primaryExtension = "";          try {            primaryExtension = mimeInfo.primaryExtension;          }          catch (ex) {          }
  374.           if (primaryExtension != "")
  375.             typeString = primaryExtension.toUpperCase() + " file";
  376.           // 3. If we can't even do that, just give up and show the MIME type. 
  377.           else
  378.             typeString = mimeInfo.MIMEType;
  379.         }
  380.         
  381.         type.value = typeString;
  382.     },
  383.  
  384.     // Returns true if opening the default application makes sense.
  385.     openWithDefaultOK: function() {
  386.         var result;
  387.  
  388.         // The checking is different on Windows...
  389.         if ( this.mIsWin ) {
  390.             // Windows presents some special cases.
  391.             // We need to prevent use of "system default" when the file is
  392.             // executable (so the user doesn't launch nasty programs downloaded
  393.             // from the web), and, enable use of "system default" if it isn't
  394.             // executable (because we will prompt the user for the default app
  395.             // in that case).
  396.             
  397.             // Need to get temporary file and check for executable-ness.
  398.             var ignore1 = new Object;
  399.             var ignore2 = new Object;
  400.             var tmpFile = this.mLauncher.getDownloadInfo( ignore1, ignore2 );
  401.             
  402.             //  Default is Ok if the file isn't executable (and vice-versa).
  403.             result = !tmpFile.isExecutable();
  404.         } else {
  405.             // On other platforms, default is Ok if there is a default app.
  406.             // Note that nsIMIMEInfo providers need to ensure that this holds true
  407.             // on each platform.
  408.             result = this.mLauncher.MIMEInfo.defaultApplicationHandler;
  409.         }
  410.         return result;
  411.     },
  412.     
  413.     // Set "default" application description field.
  414.     initDefaultApp: function() {
  415.       // Use description, if we can get one.
  416.       var desc = this.mLauncher.MIMEInfo.defaultDescription;
  417.       if (desc) {
  418.         var defaultApp = this.dialogElement("strings").getFormattedString("defaultApp", [desc]);
  419.         this.dialogElement("defaultHandler").label = defaultApp;
  420.       }
  421.     },
  422.  
  423.     // getPath:
  424.     getPath: function (aFile) {
  425.       if (this.mIsMac)
  426.         return aFile.leafName || aFile.path;
  427.       return aFile.path;
  428.     },
  429.  
  430.     // initAppAndSaveToDiskValues:
  431.     initAppAndSaveToDiskValues: function() {
  432.       var modeGroup = this.dialogElement("mode");
  433.  
  434.       // We don't let users open .exe files or random binary data directly 
  435.       // from the browser at the moment because of security concerns. 
  436.       var mimeType = this.mLauncher.MIMEInfo.MIMEType;
  437.       if (mimeType == "application/octet-stream" ||
  438.           mimeType == "application/x-msdownload") {
  439.         this.dialogElement("open").disabled = true;
  440.         var openHandler = this.dialogElement("openHandler");
  441.         openHandler.disabled = true;
  442.         openHandler.label = "";
  443.         modeGroup.selectedItem = this.dialogElement("save");
  444.         return;
  445.       }
  446.     
  447.       // Fill in helper app info, if there is any.
  448.       this.chosenApp = this.mLauncher.MIMEInfo.preferredApplicationHandler;
  449.       // Initialize "default application" field.
  450.       this.initDefaultApp();
  451.  
  452.       var otherHandler = this.dialogElement("otherHandler");
  453.               
  454.       // Fill application name textbox.
  455.       if (this.chosenApp && this.chosenApp.path) {
  456.         otherHandler.setAttribute("path", this.getPath(this.chosenApp));
  457.         otherHandler.label = this.chosenApp.leafName;
  458.         otherHandler.hidden = false;
  459.       }
  460.  
  461.       var useDefault = this.dialogElement("useSystemDefault");
  462.       var openHandler = this.dialogElement("openHandler");
  463.       openHandler.selectedIndex = 0;
  464.  
  465.       if (this.mLauncher.MIMEInfo.preferredAction == this.nsIMIMEInfo.useSystemDefault) {
  466.         // Open (using system default).
  467.         modeGroup.selectedItem = this.dialogElement("open");
  468.       } else if (this.mLauncher.MIMEInfo.preferredAction == this.nsIMIMEInfo.useHelperApp) {
  469.         // Open with given helper app.
  470.         modeGroup.selectedItem = this.dialogElement("open");
  471.         openHandler.selectedIndex = 1;
  472.       } else {
  473.         // Save to disk.
  474.         modeGroup.selectedItem = this.dialogElement("save");
  475.       }
  476.       
  477.       // If we don't have a "default app" then disable that choice.
  478.       if (!this.openWithDefaultOK()) {
  479.         var useDefault = this.dialogElement("defaultHandler");
  480.         var isSelected = useDefault.selected;
  481.         
  482.         // Disable that choice.
  483.         useDefault.hidden = true;
  484.         // If that's the default, then switch to "save to disk."
  485.         if (isSelected) {
  486.           openHandler.selectedIndex = 1;
  487.           modeGroup.selectedItem = this.dialogElement("save");
  488.         }
  489.       }
  490.       
  491.       // otherHandler is always disabled on Mac
  492.       if (this.mIsMac) 
  493.         otherHandler.hidden = true;
  494.  
  495.       otherHandler.nextSibling.hidden = otherHandler.nextSibling.nextSibling.hidden = this.isMac;
  496.       this.updateOKButton();
  497.     },
  498.  
  499.     // Returns the user-selected application
  500.     helperAppChoice: function() {
  501.       return this.chosenApp;
  502.     },
  503.     
  504.     get saveToDisk() {
  505.       return this.dialogElement("save").selected;
  506.     },
  507.     
  508.     get useOtherHandler() {
  509.       return this.dialogElement("open").selected && this.dialogElement("openHandler").selectedIndex == 1;
  510.     },
  511.     
  512.     get useSystemDefault() {
  513.       return this.dialogElement("open").selected && this.dialogElement("openHandler").selectedIndex == 0;
  514.     },
  515.     
  516.     toggleRememberChoice: function (aCheckbox) {
  517.         this.dialogElement("settingsChange").hidden = !aCheckbox.checked;
  518.         this.mDialog.sizeToContent();
  519.     },
  520.     
  521.     openHandlerCommand: function () {
  522.       if (this.dialogElement("openHandler").selectedItem.id == "choose")
  523.         this.chooseApp();
  524.     },
  525.  
  526.     updateOKButton: function() {
  527.       var ok = false;
  528.       if (this.dialogElement("save").selected) {
  529.         // This is always OK.
  530.         ok = true;
  531.       } 
  532.       else if (this.dialogElement("open").selected) {
  533.         switch (this.dialogElement("openHandler").selectedIndex) {
  534.         case 0:
  535.           // No app need be specified in this case.
  536.           ok = true;
  537.           break;
  538.         case 1:
  539.           // only enable the OK button if we have a default app to use or if 
  540.           // the user chose an app....
  541.           ok = this.chosenApp || /\S/.test(this.dialogElement("otherHandler").getAttribute("path")); 
  542.         break;
  543.         }
  544.       }
  545.  
  546.       // Enable Ok button if ok to press.
  547.       this.mDialog.document.documentElement.getButton("accept").disabled = !ok;
  548.     },
  549.     
  550.     // Returns true iff the user-specified helper app has been modified.
  551.     appChanged: function() {
  552.       return this.helperAppChoice() != this.mLauncher.MIMEInfo.preferredApplicationHandler;
  553.     },
  554.  
  555.     updateMIMEInfo: function() {
  556.       var needUpdate = false;
  557.       // If current selection differs from what's in the mime info object,
  558.       // then we need to update.
  559.       if (this.saveToDisk) {
  560.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.saveToDisk;
  561.         if (needUpdate)
  562.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.saveToDisk;
  563.       } 
  564.       else if (this.useSystemDefault) {
  565.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.useSystemDefault;
  566.         if (needUpdate)
  567.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.useSystemDefault;
  568.       } 
  569.       else {
  570.         // For "open with", we need to check both preferred action and whether the user chose
  571.         // a new app.
  572.         needUpdate = this.mLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.useHelperApp || this.appChanged();
  573.         if (needUpdate) {
  574.           this.mLauncher.MIMEInfo.preferredAction = this.nsIMIMEInfo.useHelperApp;
  575.           // App may have changed - Update application and description
  576.           var app = this.helperAppChoice();
  577.           this.mLauncher.MIMEInfo.preferredApplicationHandler = app;
  578.           this.mLauncher.MIMEInfo.applicationDescription = "";
  579.         }
  580.       }
  581.       // We will also need to update if the "always ask" flag has changed.
  582.       needUpdate = needUpdate || this.mLauncher.MIMEInfo.alwaysAskBeforeHandling != (!this.dialogElement("rememberChoice").checked);
  583.  
  584.       // One last special case: If the input "always ask" flag was false, then we always
  585.       // update.  In that case we are displaying the helper app dialog for the first
  586.       // time for this mime type and we need to store the user's action in the mimeTypes.rdf
  587.       // data source (whether that action has changed or not; if it didn't change, then we need
  588.       // to store the "always ask" flag so the helper app dialog will or won't display
  589.       // next time, per the user's selection).
  590.       needUpdate = needUpdate || !this.mLauncher.MIMEInfo.alwaysAskBeforeHandling;
  591.  
  592.       // Make sure mime info has updated setting for the "always ask" flag.
  593.       this.mLauncher.MIMEInfo.alwaysAskBeforeHandling = !this.dialogElement("rememberChoice").checked;
  594.  
  595.       return needUpdate;        
  596.     },
  597.     
  598.     // See if the user changed things, and if so, update the
  599.     // mimeTypes.rdf entry for this mime type.
  600.     updateHelperAppPref: function() {
  601.       var ha = new this.mDialog.HelperApps();
  602.       ha.updateTypeInfo(this.mLauncher.MIMEInfo);
  603.     },
  604.     
  605.     // onOK:
  606.     onOK: function() {
  607.       // Verify typed app path, if necessary.
  608.       if (this.useOtherHandler) {
  609.         var helperApp = this.helperAppChoice();
  610.         if (!helperApp || !helperApp.exists()) {
  611.           // Show alert and try again.        
  612.           var bundle = this.dialogElement("strings");                    
  613.           var msg = bundle.getFormattedString("badApp", [this.dialogElement("otherHandler").path]);
  614.           var svc = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  615.           svc.alert(this.mDialog, bundle.getString("badApp.title"), msg);
  616.  
  617.           // Disable the OK button.
  618.           this.mDialog.document.documentElement.getButton("accept").disabled = true;
  619.           this.dialogElement("mode").focus();          
  620.  
  621.           // Clear chosen application.
  622.           this.chosenApp = null;
  623.  
  624.           // Leave dialog up.
  625.           return false;
  626.         }
  627.       }
  628.         
  629.       // Remove our web progress listener (a progress dialog will be
  630.       // taking over).
  631.       this.mLauncher.setWebProgressListener(null);
  632.       
  633.       // saveToDisk and launchWithApplication can return errors in 
  634.       // certain circumstances (e.g. The user clicks cancel in the
  635.       // "Save to Disk" dialog. In those cases, we don't want to
  636.       // update the helper application preferences in the RDF file.
  637.       try {
  638.         var needUpdate = this.updateMIMEInfo();
  639.         
  640.         if (this.dialogElement("save").selected) {
  641.           // If we're using a default download location, create a path
  642.           // for the file to be saved to to pass to |saveToDisk| - otherwise
  643.           // we must ask the user to pick a save name.
  644.  
  645.           var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  646.           var targetFile = null;
  647.           try {
  648.             targetFile = prefs.getComplexValue("browser.download.defaultFolder", 
  649.                                                Components.interfaces.nsILocalFile);
  650.             targetFile.append(this.dialogElement("location").value);
  651.           }
  652.           catch(e) {}
  653.           
  654.           this.mLauncher.saveToDisk(targetFile, false);
  655.         }
  656.         else
  657.           this.mLauncher.launchWithApplication(null, false);
  658.  
  659.         // Update user pref for this mime type (if necessary). We do not
  660.         // store anything in the mime type preferences for the ambiguous
  661.         // type application/octet-stream. We do NOT do this for 
  662.         // application/x-msdownload since we want users to be able to 
  663.         // autodownload these to disk. 
  664.         if (needUpdate && this.mLauncher.MIMEInfo.MIMEType != "application/octet-stream")
  665.           this.updateHelperAppPref();
  666.       } catch(e) { }
  667.  
  668.       // Unhook dialog from this object.
  669.       this.mDialog.dialog = null;
  670.  
  671.       // Close up dialog by returning true.
  672.       return true;
  673.     },
  674.  
  675.     // onCancel:
  676.     onCancel: function() {
  677.       // Remove our web progress listener.
  678.       this.mLauncher.setWebProgressListener(null);
  679.  
  680.       // Cancel app launcher.
  681.       try {
  682.         this.mLauncher.Cancel();
  683.       } catch(exception) {
  684.       }
  685.  
  686.       // Unhook dialog from this object.
  687.       this.mDialog.dialog = null;
  688.  
  689.       // Close up dialog by returning true.
  690.       return true;
  691.     },
  692.  
  693.     // dialogElement:  Convenience. 
  694.     dialogElement: function(id) {
  695.       return this.mDialog.document.getElementById(id);
  696.     },
  697.  
  698.     // chooseApp:  Open file picker and prompt user for application.
  699.     chooseApp: function() {
  700.       var nsIFilePicker = Components.interfaces.nsIFilePicker;
  701.       var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  702.       fp.init(this.mDialog,
  703.               this.dialogElement("strings").getString("chooseAppFilePickerTitle"),
  704.               nsIFilePicker.modeOpen);
  705.  
  706.       fp.appendFilters(nsIFilePicker.filterApps);
  707.  
  708.       if (fp.show() == nsIFilePicker.returnOK && fp.file) {
  709.         // Remember the file they chose to run.
  710.         this.chosenApp = fp.file;
  711.         // Update dialog.
  712.         var otherHandler = this.dialogElement("otherHandler");
  713.         otherHandler.removeAttribute("hidden");
  714.         otherHandler.setAttribute("path", this.getPath(this.chosenApp));
  715.         otherHandler.label = this.chosenApp.leafName;
  716.         this.dialogElement("openHandler").selectedIndex = 1;
  717.         
  718.         this.dialogElement("mode").selectedItem = this.dialogElement("open");
  719.       }
  720.     },
  721.  
  722.     // Turn this on to get debugging messages.
  723.     debug: false,
  724.  
  725.     // Dump text (if debug is on).
  726.     dump: function( text ) {
  727.         if ( this.debug ) {
  728.             dump( text ); 
  729.         }
  730.     },
  731.  
  732.     // dumpInfo:
  733.     doDebug: function() {
  734.         const nsIProgressDialog = Components.interfaces.nsIProgressDialog;
  735.         // Open new progress dialog.
  736.         var progress = Components.classes[ "@mozilla.org/progressdialog;1" ]
  737.                          .createInstance( nsIProgressDialog );
  738.         // Show it.
  739.         progress.open( this.mDialog );
  740.     },
  741.  
  742.     // dumpObj:
  743.     dumpObj: function( spec ) {
  744.          var val = "<undefined>";
  745.          try {
  746.              val = eval( "this."+spec ).toString();
  747.          } catch( exception ) {
  748.          }
  749.          this.dump( spec + "=" + val + "\n" );
  750.     },
  751.  
  752.     // dumpObjectProperties
  753.     dumpObjectProperties: function( desc, obj ) {
  754.          for( prop in obj ) {
  755.              this.dump( desc + "." + prop + "=" );
  756.              var val = "<undefined>";
  757.              try {
  758.                  val = obj[ prop ];
  759.              } catch ( exception ) {
  760.              }
  761.              this.dump( val + "\n" );
  762.          }
  763.     }
  764. }
  765.  
  766. // This Component's module implementation.  All the code below is used to get this
  767. // component registered and accessible via XPCOM.
  768. var module = {
  769.     firstTime: true,
  770.  
  771.     // registerSelf: Register this component.
  772.     registerSelf: function (compMgr, fileSpec, location, type) {
  773.         if (this.firstTime) {
  774.             this.firstTime = false;
  775.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  776.         }
  777.         compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  778.  
  779.         compMgr.registerFactoryLocation( this.cid,
  780.                                          "Unknown Content Type Dialog",
  781.                                          this.contractId,
  782.                                          fileSpec,
  783.                                          location,
  784.                                          type );
  785.     },
  786.  
  787.     // getClassObject: Return this component's factory object.
  788.     getClassObject: function (compMgr, cid, iid) {
  789.         if (!cid.equals(this.cid)) {
  790.             throw Components.results.NS_ERROR_NO_INTERFACE;
  791.         }
  792.  
  793.         if (!iid.equals(Components.interfaces.nsIFactory)) {
  794.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  795.         }
  796.  
  797.         return this.factory;
  798.     },
  799.  
  800.     /* CID for this class */
  801.     cid: Components.ID("{F68578EB-6EC2-4169-AE19-8C6243F0ABE1}"),
  802.  
  803.     /* Contract ID for this class */
  804.     contractId: "@mozilla.org/helperapplauncherdialog;1",
  805.  
  806.     /* factory object */
  807.     factory: {
  808.         // createInstance: Return a new nsProgressDialog object.
  809.         createInstance: function (outer, iid) {
  810.             if (outer != null)
  811.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  812.  
  813.             return (new nsUnknownContentTypeDialog()).QueryInterface(iid);
  814.         }
  815.     },
  816.  
  817.     // canUnload: n/a (returns true)
  818.     canUnload: function(compMgr) {
  819.         return true;
  820.     }
  821. };
  822.  
  823. // NSGetModule: Return the nsIModule object.
  824. function NSGetModule(compMgr, fileSpec) {
  825.     return module;
  826. }
  827.