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 / Defaults / autoconfig / prefcalls.js < prev    next >
Text File  |  2003-06-07  |  7KB  |  221 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  ***** BEGIN LICENSE BLOCK *****
  3.  * Version: NPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Netscape Public License
  6.  * Version 1.1 (the "License"); you may not use this file except in
  7.  * compliance with the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/NPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is 
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1998
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Mitesh Shah <mitesh@netscape.com>
  24.  * Brian Nesse <bnesse@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the NPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the NPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. const nsILDAPURL = Components.interfaces.nsILDAPURL;
  41. const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
  42. const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
  43. const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
  44. const nsIPrefService = Components.interfaces.nsIPrefService;
  45. const PrefServiceContractID = "@mozilla.org/preferences-service;1";
  46.  
  47. // set on a platform specific basis in platform.js
  48. platform = { value: "" };
  49.  
  50.  
  51. function getPrefBranch() {
  52.     
  53.     var prefService = Components.classes[PrefServiceContractID]
  54.                                 .getService(nsIPrefService);    
  55.     return prefService.getBranch(null);
  56. }
  57.  
  58. function pref(prefName, value) {
  59.  
  60.     try { 
  61.         var prefBranch = getPrefBranch();
  62.  
  63.         if (typeof value == "string") {
  64.             prefBranch.setCharPref(prefName, value);
  65.         }
  66.         else if (typeof value == "number") {
  67.             prefBranch.setIntPref(prefName, value);
  68.         }
  69.         else if (typeof value == "boolean") {
  70.             prefBranch.setBoolPref(prefName, value);
  71.         }
  72.     }
  73.     catch(e) {
  74.         displayError("pref", e);
  75.     }
  76. }
  77.  
  78. function defaultPref(prefName, value) {
  79.     
  80.     try {
  81.         var prefService = Components.classes[PrefServiceContractID]
  82.                                     .getService(nsIPrefService);        
  83.         var prefBranch = prefService.getDefaultBranch(null);
  84.         if (typeof value == "string") {
  85.             prefBranch.setCharPref(prefName, value);
  86.         }
  87.         else if (typeof value == "number") {
  88.             prefBranch.setIntPref(prefName, value);
  89.         }
  90.         else if (typeof value == "boolean") {
  91.             prefBranch.setBoolPref(prefName, value);
  92.         }
  93.     }
  94.     catch(e) {
  95.         displayError("defaultPref", e);
  96.     }
  97. }
  98.  
  99. function lockPref(prefName, value) {
  100.     
  101.     try {
  102.         var prefBranch = getPrefBranch();
  103.         
  104.         if (prefBranch.prefIsLocked(prefName))
  105.             prefBranch.unlockPref(prefName);
  106.         
  107.         defaultPref(prefName, value);
  108.         
  109.         prefBranch.lockPref(prefName);
  110.     }
  111.     catch(e) {
  112.         displayError("lockPref", e);
  113.     }
  114. }
  115.  
  116. function unlockPref(prefName) {
  117.  
  118.     try {
  119.  
  120.         var prefBranch = getPrefBranch();
  121.         prefBranch.unlockPref(prefName);
  122.     }
  123.     catch(e) {
  124.         displayError("unlockPref", e);
  125.     }
  126. }
  127.  
  128. function getPref(prefName) {
  129.     
  130.     try {
  131.         var prefBranch = getPrefBranch();
  132.         
  133.         switch (prefBranch.getPrefType(prefName)) {
  134.             
  135.         case prefBranch.PREF_STRING:
  136.             return prefBranch.getCharPref(prefName);
  137.             
  138.         case prefBranch.PREF_INT:
  139.             return prefBranch.getIntPref(prefName);
  140.             
  141.         case prefBranch.PREF_BOOL:
  142.             return prefBranch.getBoolPref(prefName);
  143.         default:
  144.             return null;
  145.         }
  146.     }
  147.     catch(e) {
  148.         displayError("getPref", e);
  149.     }
  150. }
  151.  
  152.  
  153. function getLDAPAttributes(host, base, filter, attribs) {
  154.     
  155.     try {
  156.         var url = Components.classes[LDAPURLContractID].createInstance(nsILDAPURL);
  157.     
  158.         url.spec = "ldap://" + host + "/" + base + "?" + attribs 
  159.                    + "?sub?" +  filter;
  160.         var ldapquery = Components.classes[LDAPSyncQueryContractID]
  161.                                   .createInstance(nsILDAPSyncQuery);
  162.         processLDAPValues(ldapquery.getQueryResults(url));     // user supplied method
  163.     }
  164.     catch(e) {
  165.         displayError("getLDAPAttibutes", e);
  166.     }
  167. }
  168.  
  169. function getLDAPValue(str, key) {
  170.  
  171.     try {
  172.         if (str == null || key == null)
  173.             return null;
  174.         
  175.         var search_key = "\n" + key + "=";
  176.         
  177.         var start_pos = str.indexOf(search_key);
  178.         if (start_pos == -1)
  179.             return null;
  180.         
  181.         start_pos += search_key.length;
  182.         
  183.         var end_pos = str.indexOf("\n", start_pos);
  184.         if (end_pos == -1)
  185.             end_pos = str.length;
  186.         
  187.         return str.substring(start_pos, end_pos);
  188.     }
  189.     catch(e) {
  190.         displayError("getLDAPValue", e);
  191.     }
  192. }
  193.  
  194. function displayError(funcname, message) {
  195.  
  196.     try {
  197.         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  198.                                       .getService(Components.interfaces.nsIPromptService);
  199.         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
  200.                                .getService(Components.interfaces.nsIStringBundleService)
  201.                                .createBundle("chrome://autoconfig/locale/autoconfig.properties");
  202.  
  203.          var title = bundle.GetStringFromName("autoConfigTitle");
  204.          var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
  205.          promptService.alert(null, title, msg + " " + message);
  206.     }
  207.     catch(e) { }
  208. }
  209.  
  210. function getenv(name) {
  211.     
  212.     try {
  213.         var currentProcess=Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
  214.         return currentProcess.getEnvironment(name);
  215.     }
  216.     catch(e) {
  217.         displayError("getEnvironment", e);
  218.     }
  219. }
  220.  
  221.