home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / toolkit.jar / content / global / helperAppLauncher.js < prev    next >
Text File  |  2001-02-14  |  7KB  |  184 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released March
  14.  * 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape Communications
  17.  * Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  */
  23.  
  24. function nsHelperAppLauncherDialog() {
  25.     // Initialize data properties.
  26.     this.userChoseApp = false;
  27.     this.chosenApp    = null;
  28.  
  29.     try {
  30.         // App launcher is passed as dialog argument.
  31.         this.appLauncher = window.arguments[0];
  32.  
  33.         // Initialize the dialog contents.
  34.         this.initDialog();
  35.     } catch( e ) {
  36.         // On error, close dialog.
  37.         dump( "nsHelperAppLauncherDialog error: " + e + "\n" );
  38.         window.close();
  39.     }
  40. }
  41.  
  42. nsHelperAppLauncherDialog.prototype= {
  43.     // Statics.
  44.     nsIHelperAppLauncher : Components.interfaces.nsIHelperAppLauncher,
  45.     nsIMIMEInfo             : Components.interfaces.nsIMIMEInfo,
  46.     nsIFilePicker        : Components.interfaces.nsIFilePicker,
  47.  
  48.     // Fill dialog from app launcher attributes.
  49.     initDialog : function () {
  50.         // "Always ask me" is always set (or else we wouldn't have got here!).
  51.         document.getElementById( "alwaysAskMe" ).checked = true;
  52.         document.getElementById( "alwaysAskMe" ).setAttribute("disabled", "true");
  53.   
  54.         // Pre-select the choice the user made last time.
  55.         this.chosenApp = this.appLauncher.MIMEInfo.preferredApplicationHandler;
  56.         var applicationDescription = this.appLauncher.MIMEInfo.applicationDescription;
  57.         if (applicationDescription != "")
  58.            document.getElementById( "appName" ).value = applicationDescription;
  59.         else if (this.chosenApp)
  60.         {
  61.           // If a user-chosen application, show its path.
  62.           document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  63.         }
  64.  
  65.         if ( applicationDescription  && this.appLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.saveToDisk ) {
  66.             document.getElementById( "runApp" ).checked = true;         
  67.         } else {
  68.             // Save to disk.
  69.             document.getElementById( "saveToDisk" ).checked = true;
  70.             // Disable choose app button.
  71.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  72.         }
  73.  
  74.         // Put content type into dialog text.
  75.         var html = document.getElementById( "intro" );
  76.         if ( html && html.childNodes && html.childNodes.length ) {
  77.             // Get raw text.
  78.             var text = html.childNodes[ 0 ].nodeValue;
  79.             // Substitute content type for "#1".
  80.             text = text.replace( /#1/, this.appLauncher.MIMEInfo.MIMEType );
  81.  
  82.             // Replace #2 with product name.
  83.             var brandBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  84.             if ( brandBundle ) {
  85.                 var product = brandBundle.GetStringFromName( "brandShortName" );
  86.                 text = text.replace( /#2/, product );
  87.             }
  88.  
  89.             // Replace text in document.
  90.             html.childNodes[ 0 ].nodeValue = text;
  91.         }
  92.  
  93.         // Set up dialog button callbacks.
  94.         var object = this;
  95.         doSetOKCancel( function () { return object.onOK(); },
  96.                        function () { return object.onCancel(); } );
  97.         moveToAlertPosition();
  98.     },
  99.  
  100.     // If the user presses OK, we do as requested...
  101.     onOK : function () {
  102.         // Get boolean switch from checkbox.
  103.         var dontAskNextTime = !document.getElementById( "alwaysAskMe" ).checked;
  104.  
  105.         // this.appLauncher.MIMEInfo.alwaysAskBeforeHandling = document.getElementById( "alwaysAskMe" ).checked;
  106.     
  107.         if ( document.getElementById( "runApp" ).checked ) {
  108.             // Update preferred action if the user chose an app.
  109.             if ( this.userChoseApp ) {
  110.                 this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.useHelperApp;
  111.             }
  112.             this.appLauncher.launchWithApplication( this.chosenApp, dontAskNextTime );
  113.         } else {
  114.             this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.saveToDisk;
  115.             try {
  116.               this.appLauncher.saveToDisk( null, dontAskNextTime );
  117.             } catch (exception) { 
  118.             }
  119.         }
  120.     
  121.         window.close();
  122.     },
  123.  
  124.     // If the user presses cancel, tell the app launcher and close the dialog...
  125.     onCancel : function () {
  126.         // Cancel app launcher.
  127.         try {
  128.             this.appLauncher.Cancel();
  129.         } catch( exception ) {
  130.         }
  131.     
  132.         // Close up dialog by returning true.
  133.         return true;
  134.     },
  135.  
  136.     // Enable pick app button if the user chooses that option.
  137.     toggleChoice : function () {
  138.         // See what option is checked.
  139.         if ( document.getElementById( "runApp" ).checked ) {
  140.             // We can enable the pick app button.
  141.             document.getElementById( "chooseApp" ).removeAttribute( "disabled" );
  142.         } else {
  143.             // We can disable the pick app button.
  144.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  145.         }
  146.     },
  147.  
  148.     // Choose a new/different app...
  149.     chooseApp : function () {
  150.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( this.nsIFilePicker );
  151.         fp.init( window,
  152.                  this.getString( "chooseAppFilePickerTitle" ),
  153.                  this.nsIFilePicker.modeOpen );
  154.         // XXX - We want to say nsIFilePicker.filterExecutable or something
  155.         fp.appendFilters( this.nsIFilePicker.filterAll );
  156.     
  157.         if ( fp.show() == this.nsIFilePicker.returnOK && fp.file ) {
  158.             // Remember the file they chose to run.
  159.             this.userChoseApp = true;
  160.             this.chosenApp    = fp.file;
  161.             // Update dialog.
  162.             document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  163.         }
  164.     },
  165.  
  166.     // Get string from bundle.
  167.     getString : function ( id ) {
  168.         // String of last resort is the id.
  169.         var result = id;
  170.     
  171.         // Get string bundle (if not done previously).
  172.         if ( !this.strings ) {
  173.             this.strings = srGetStrBundle("chrome://global/locale/helperAppLauncher.properties");
  174.         }
  175.     
  176.         if ( this.strings ) {
  177.             // Get string from bundle.
  178.             result = this.strings.GetStringFromName( id );
  179.         }
  180.     
  181.         return result;
  182.     },
  183. }
  184.