home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / mozilla / mozilla-win32-M18-mathml-svg-xslt.exe / chrome / toolkit / content / global / helperAppLauncher.js < prev    next >
Text File  |  2000-10-04  |  7KB  |  183 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.         if ( this.chosenApp && this.appLauncher.MIMEInfo.preferredAction != this.nsIMIMEInfo.saveToDisk ) {
  57.             // Run app.
  58.             document.getElementById( "runApp" ).checked = true;            
  59.         } else {
  60.             // Save to disk.
  61.             document.getElementById( "saveToDisk" ).checked = true;
  62.             // Disable choose app button.
  63.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  64.         }
  65.  
  66.         var applicationDescription = this.appLauncher.MIMEInfo.applicationDescription;
  67.         if (applicationDescription != "")
  68.            document.getElementById( "appName" ).value = applicationDescription;
  69.         else if (this.chosenApp)
  70.         {
  71.           // If a user-chosen application, show its path.
  72.           document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  73.         }
  74.  
  75.         // Put content type into dialog text.
  76.         var html = document.getElementById( "intro" );
  77.         if ( html && html.childNodes && html.childNodes.length ) {
  78.             // Get raw text.
  79.             var text = html.childNodes[ 0 ].nodeValue;
  80.             // Substitute content type for "#1".
  81.             text = text.replace( /#1/, this.appLauncher.MIMEInfo.MIMEType );
  82.  
  83.             // Replace #2 with product name.
  84.             var brandBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  85.             if ( brandBundle ) {
  86.                 var product = brandBundle.GetStringFromName( "brandShortName" );
  87.                 text = text.replace( /#2/, product );
  88.             }
  89.  
  90.             // Replace text in document.
  91.             html.childNodes[ 0 ].nodeValue = text;
  92.         }
  93.  
  94.         // Set up dialog button callbacks.
  95.         var object = this;
  96.         doSetOKCancel( function () { return object.onOK(); },
  97.                        function () { return object.onCancel(); } );
  98.         moveToAlertPosition();
  99.     },
  100.  
  101.     // If the user presses OK, we do as requested...
  102.     onOK : function () {
  103.         // Get boolean switch from checkbox.
  104.         var dontAskNextTime = !document.getElementById( "alwaysAskMe" ).checked;
  105.     
  106.         if ( document.getElementById( "runApp" ).checked ) {
  107.             // Update preferred action if the user chose an app.
  108.             if ( this.userChoseApp ) {
  109.                 this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.useHelperApp;
  110.             }
  111.             this.appLauncher.launchWithApplication( this.chosenApp, dontAskNextTime );
  112.         } else {
  113.             this.appLauncher.MIMEInfo.preferredAction = this.nsIHelperAppLauncher.saveToDisk;
  114.             try {
  115.               this.appLauncher.saveToDisk( null, dontAskNextTime );
  116.             } catch (exception) { 
  117.             }
  118.         }
  119.     
  120.         window.close();
  121.     },
  122.  
  123.     // If the user presses cancel, tell the app launcher and close the dialog...
  124.     onCancel : function () {
  125.         // Cancel app launcher.
  126.         try {
  127.             this.appLauncher.Cancel();
  128.         } catch( exception ) {
  129.         }
  130.     
  131.         // Close up dialog by returning true.
  132.         return true;
  133.     },
  134.  
  135.     // Enable pick app button if the user chooses that option.
  136.     toggleChoice : function () {
  137.         // See what option is checked.
  138.         if ( document.getElementById( "runApp" ).checked ) {
  139.             // We can enable the pick app button.
  140.             document.getElementById( "chooseApp" ).removeAttribute( "disabled" );
  141.         } else {
  142.             // We can disable the pick app button.
  143.             document.getElementById( "chooseApp" ).setAttribute( "disabled", "true" );
  144.         }
  145.     },
  146.  
  147.     // Choose a new/different app...
  148.     chooseApp : function () {
  149.         var fp = Components.classes["@mozilla.org/filepicker;1"].createInstance( this.nsIFilePicker );
  150.         fp.init( window,
  151.                  this.getString( "chooseAppFilePickerTitle" ),
  152.                  this.nsIFilePicker.modeOpen );
  153.         // XXX - We want to say nsIFilePicker.filterExecutable or something
  154.         fp.appendFilters( this.nsIFilePicker.filterAll );
  155.     
  156.         if ( fp.show() == this.nsIFilePicker.returnOK && fp.file ) {
  157.             // Remember the file they chose to run.
  158.             this.userChoseApp = true;
  159.             this.chosenApp    = fp.file;
  160.             // Update dialog.
  161.             document.getElementById( "appName" ).value = this.chosenApp.unicodePath;
  162.         }
  163.     },
  164.  
  165.     // Get string from bundle.
  166.     getString : function ( id ) {
  167.         // String of last resort is the id.
  168.         var result = id;
  169.     
  170.         // Get string bundle (if not done previously).
  171.         if ( !this.strings ) {
  172.             this.strings = srGetStrBundle("chrome://global/locale/helperAppLauncher.properties");
  173.         }
  174.     
  175.         if ( this.strings ) {
  176.             // Get string from bundle.
  177.             result = this.strings.GetStringFromName( id );
  178.         }
  179.     
  180.         return result;
  181.     },
  182. }
  183.