home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / 01_03.iso / software / ghostzilla_hit / files / ghostzilla-1.0-plus-install.exe / defaults / autoconfig / prefcalls.js
Encoding:
Text File  |  2002-04-09  |  6.5 KB  |  212 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.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or 
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the NPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the NPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. const nsILDAPURL = Components.interfaces.nsILDAPURL;
  40. const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
  41. const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
  42. const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
  43. const nsIPrefService = Components.interfaces.nsIPrefService;
  44. const PrefServiceContractID = "@mozilla.org/preferences-service;1";
  45.  
  46. function getPrefBranch() {
  47.     
  48.     var prefService = Components.classes[PrefServiceContractID]
  49.                                 .getService(nsIPrefService);    
  50.     return prefService.getBranch(null);
  51. }
  52.  
  53. function pref(prefName, value) {
  54.  
  55.     try { 
  56.         var prefBranch = getPrefBranch();
  57.  
  58.         if (typeof value == "string") {
  59.             prefBranch.setCharPref(prefName, value);
  60.         }
  61.         else if (typeof value == "number") {
  62.             prefBranch.setIntPref(prefName, value);
  63.         }
  64.         else if (typeof value == "boolean") {
  65.             prefBranch.setBoolPref(prefName, value);
  66.         }
  67.     }
  68.     catch(e) {
  69.         displayError("pref failed with error: " + e);
  70.     }
  71. }
  72.  
  73. function defaultPref(prefName, value) {
  74.     
  75.     try {
  76.         var prefService = Components.classes[PrefServiceContractID]
  77.                                     .getService(nsIPrefService);        
  78.         var prefBranch = prefService.getDefaultBranch(null);
  79.         if (typeof value == "string") {
  80.             prefBranch.setCharPref(prefName, value);
  81.         }
  82.         else if (typeof value == "number") {
  83.             prefBranch.setIntPref(prefName, value);
  84.         }
  85.         else if (typeof value == "boolean") {
  86.             prefBranch.setBoolPref(prefName, value);
  87.         }
  88.     }
  89.     catch(e) {
  90.         displayError("defaultPref failed with error: " + e);
  91.     }
  92. }
  93.  
  94. function lockPref(prefName, value) {
  95.     
  96.     try {
  97.         var prefBranch = getPrefBranch();
  98.         
  99.         if (prefBranch.prefIsLocked(prefName))
  100.             prefBranch.unlockPref(prefName);
  101.         
  102.         defaultPref(prefName, value);
  103.         
  104.         prefBranch.lockPref(prefName);
  105.     }
  106.     catch(e) {
  107.         displayError("lockPref failed with error: " + e);
  108.     }
  109. }
  110.  
  111. function unlockPref(prefName) {
  112.  
  113.     try {
  114.  
  115.         var prefBranch = getPrefBranch();
  116.         prefBranch.unlockPref(prefName);
  117.     }
  118.     catch(e) {
  119.         displayError("unlockPref failed with error: " + e);
  120.     }
  121. }
  122.  
  123. function getPref(prefName) {
  124.     
  125.     try {
  126.         var prefBranch = getPrefBranch();
  127.         
  128.         switch (prefBranch.getPrefType(prefName)) {
  129.             
  130.         case prefBranch.PREF_STRING:
  131.             return prefBranch.getCharPref(prefName);
  132.             
  133.         case prefBranch.PREF_INT:
  134.             return prefBranch.getIntPref(prefName);
  135.             
  136.         case prefBranch.PREF_BOOL:
  137.             return prefBranch.getBoolPref(prefName);
  138.         default:
  139.             return null;
  140.         }
  141.     }
  142.     catch(e) {
  143.         displayError("getPref failed with error: " + e);
  144.     }
  145. }
  146.  
  147.  
  148. function getLDAPAttributes(host, base, filter, attribs) {
  149.     
  150.     try {
  151.         var url = Components.classes[LDAPURLContractID].createInstance(nsILDAPURL);
  152.     
  153.         url.spec = "ldap://" + host + "/" + base + "?" + attribs 
  154.                    + "?sub?" +  filter;
  155.         var ldapquery = Components.classes[LDAPSyncQueryContractID]
  156.                                   .createInstance(nsILDAPSyncQuery);
  157.         return ldapquery.getQueryResults(url);
  158.     }
  159.     catch(e) {
  160.         displayError("getLDAPAttibutes failed with error: " + e);
  161.     }
  162. }
  163.  
  164. function getLDAPValue(str, key) {
  165.  
  166.     try {
  167.         if (str == null || key == null)
  168.             return null;
  169.         
  170.         var search_key = "\n" + key + "=";
  171.         
  172.         var start_pos = str.indexOf(search_key);
  173.         if (start_pos == -1)
  174.             return null;
  175.         
  176.         start_pos += search_key.length;
  177.         
  178.         var end_pos = str.indexOf("\n", start_pos);
  179.         if (end_pos == -1)
  180.             end_pos = str.length;
  181.         
  182.         return str.substring(start_pos, end_pos);
  183.     }
  184.     catch(e) {
  185.         displayError("getLDAPValue failed with error: " + e);
  186.     }
  187. }
  188.  
  189. function displayError(message) {
  190.  
  191.     var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  192.                                   .getService(Components.interfaces.nsIPromptService);
  193.     if (promptService) {
  194.         
  195.         title = "AutoConfig Alert";
  196.         err = "Netscape.cfg/AutoConfig failed. Please Contact your system administrator \n Error Message: " + message;
  197.         promptService.alert(null, title, err);
  198.     } 
  199. }
  200.  
  201. function getenv(name) {
  202.     
  203.     try {
  204.         var currentProcess=Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);
  205.         return currentProcess.getEnvironment(name);
  206.     }
  207.     catch(e) {
  208.         displayError("getEnvironment failed with error: " + e);
  209.     }
  210. }
  211.  
  212.