home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Chrome / comm.jar / content / communicator / pref / pref-applications-new.js < prev    next >
Text File  |  2003-06-08  |  6KB  |  183 lines

  1. /* -*- Mode: Java; tab-width: 2; c-basic-offset: 2; -*-
  2.  * 
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  *
  18.  * Contributor(s):
  19.  */
  20.  
  21. var gDescriptionField = null;
  22. var gExtensionField   = null;
  23. var gMIMEField        = null;
  24. var gAppPath          = null;
  25.  
  26. var gPrefApplicationsBundle = null;
  27.  
  28. function Startup()
  29. {
  30.   doSetOKCancel(onOK);
  31.   
  32.   gDescriptionField = document.getElementById("description");
  33.   gExtensionField   = document.getElementById("extensions");
  34.   gMIMEField        = document.getElementById("mimeType");
  35.   gAppPath          = document.getElementById("appPath");
  36.     
  37.   gPrefApplicationsBundle = document.getElementById("bundle_prefApplications");
  38.  
  39.   // If an arg was passed, then it's an nsIHelperAppLauncherDialog
  40.   if ( "arguments" in window && window.arguments[0] ) {
  41.       // Get mime info.
  42.       var info = window.arguments[0].mLauncher.MIMEInfo;
  43.  
  44.       // Fill the fields we can from this.
  45.       gDescriptionField.value = info.Description;
  46.       gExtensionField.value   = info.primaryExtension;
  47.       gMIMEField.value        = info.MIMEType;
  48.       // an app may have been selected in the opening dialog but not in the mimeinfo
  49.       var app = info.preferredApplicationHandler || window.arguments[0].chosenApp;
  50.       if ( app ) {
  51.           gAppPath.value      = app.path;
  52.       }
  53.  
  54.       // Don't let user change mime type.
  55.       gMIMEField.setAttribute( "readonly", "true" );
  56.  
  57.       // Start user in app field.
  58.       gAppPath.focus();
  59.   } else {
  60.       gDescriptionField.focus();
  61.   }
  62.   sizeToContent();
  63.   moveToAlertPosition();
  64. }
  65.  
  66. function chooseApp()
  67. {
  68.   const nsIFilePicker = Components.interfaces.nsIFilePicker;
  69.   var filePicker = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  70.   if (filePicker) {
  71.     const FP = Components.interfaces.nsIFilePicker
  72.     var windowTitle = gPrefApplicationsBundle.getString("chooseHandler");
  73.     var programsFilter = gPrefApplicationsBundle.getString("programsFilter");
  74.     filePicker.init(window, windowTitle, FP.modeOpen);
  75.     if (navigator.platform == "Win32")
  76.       filePicker.appendFilter(programsFilter, "*.exe; *.com");
  77.     else
  78.       filePicker.appendFilters(FP.filterAll);
  79.     var filePicked = filePicker.show();
  80.     if (filePicked == nsIFilePicker.returnOK && filePicker.file) {
  81.       var file = filePicker.file.QueryInterface(Components.interfaces.nsILocalFile);
  82.       gAppPath.value = file.path;
  83.       gAppPath.select();
  84.     }
  85.   }
  86. }
  87.  
  88. var gDS = null;
  89. function onOK()
  90. {
  91.   // Make sure all fields are filled in OK.
  92.   if ( !checkInput() ) {
  93.     return false;
  94.   }
  95.  
  96.   const mimeTypes = "UMimTyp";
  97.   var fileLocator = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
  98.   
  99.   var file = fileLocator.get(mimeTypes, Components.interfaces.nsIFile);
  100.   
  101.   var ioService = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  102.   var fileHandler = ioService.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  103.   
  104.   gDS = gRDF.GetDataSource(fileHandler.getURLSpecFromFile(file));
  105.  
  106.   gMIMEField.value = gMIMEField.value.toLowerCase();
  107.     
  108.   // figure out if this mime type already exists. 
  109.   var exists = mimeHandlerExists(gMIMEField.value);
  110.   if (exists) {
  111.     var titleMsg = gPrefApplicationsBundle.getString("handlerExistsTitle");
  112.     var dialogMsg = gPrefApplicationsBundle.getString("handlerExists");
  113.     dialogMsg = dialogMsg.replace(/%mime%/g, gMIMEField.value);
  114.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);
  115.     var replace = promptService.confirm(window, titleMsg, dialogMsg);
  116.     if (!replace)
  117.     {
  118.       window.close();
  119.       return;
  120.     }
  121.   }
  122.   
  123.   
  124.   // now save the information
  125.   var handlerInfo = new HandlerOverride(MIME_URI(gMIMEField.value));
  126.   handlerInfo.mUpdateMode = exists; // XXX Somewhat sleazy, I know...
  127.   handlerInfo.mimeType = gMIMEField.value;
  128.   handlerInfo.description = gDescriptionField.value;
  129.   
  130.   var extensionString = gExtensionField.value.replace(/[*.;]/g, "").toLowerCase();
  131.   var extensions = extensionString.split(" ");
  132.   for (var i = 0; i < extensions.length; i++) {
  133.     var currExtension = extensions[i];
  134.     handlerInfo.addExtension(currExtension);
  135.   }
  136.   handlerInfo.appPath = gAppPath.value;
  137.  
  138.   // other info we need to set (not reflected in UI)
  139.   handlerInfo.isEditable = true;
  140.   handlerInfo.saveToDisk = false;
  141.   handlerInfo.handleInternal = false;
  142.   handlerInfo.alwaysAsk = true;
  143.   file = Components.classes["@mozilla.org/file/local;1"].createInstance();
  144.   if (file)
  145.     file = file.QueryInterface(Components.interfaces.nsILocalFile);
  146.   if (file) {
  147.     try {
  148.       file.initWithPath(gAppPath.value);
  149.       handlerInfo.appDisplayName = file.leafName;
  150.     }
  151.     catch(e) {
  152.       handlerInfo.appDisplayName = gAppPath.value;    
  153.     }
  154.   }
  155.   // do the rest of the work (ugly yes, but it works)
  156.   handlerInfo.buildLinks();
  157.   
  158.   // flush the ds to disk.   
  159.   var remoteDS = gDS.QueryInterface(Components.interfaces.nsIRDFRemoteDataSource);
  160.   if (remoteDS)
  161.     remoteDS.Flush();
  162.   
  163.   // If an arg was passed, then it's an nsIHelperAppLauncherDialog
  164.   // and we need to update its MIMEInfo.
  165.   if ( "arguments" in window && window.arguments[0] ) {
  166.       // Get mime info.
  167.       var info = window.arguments[0].mLauncher.MIMEInfo;
  168.  
  169.       // Update fields that might have changed.
  170.       info.preferredAction = Components.interfaces.nsIMIMEInfo.useHelperApp;
  171.       info.Description = gDescriptionField.value;
  172.       info.preferredApplicationHandler = file;
  173.       info.applicationDescription = handlerInfo.appDisplayName;
  174.  
  175.       // Tell the nsIHelperAppLauncherDialog to update to the changes
  176.       window.arguments[0].updateSelf = true;
  177.   }
  178.  
  179.   window.opener.gNewTypeRV = gMIMEField.value;
  180.   window.close();  
  181. }
  182.  
  183.