extensions/pref/autoconfig/src/prefcalls.js

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

     1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 const nsILDAPURL = Components.interfaces.nsILDAPURL;
     7 const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
     8 const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
     9 const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
    10 const nsIPrefService = Components.interfaces.nsIPrefService;
    11 const PrefServiceContractID = "@mozilla.org/preferences-service;1";
    13 // set on a platform specific basis in platform.js
    14 platform = { value: "" };
    16 var gVersion;
    18 function getPrefBranch() {
    20     var prefService = Components.classes[PrefServiceContractID]
    21                                 .getService(nsIPrefService);    
    22     return prefService.getBranch(null);
    23 }
    25 function pref(prefName, value) {
    27     try { 
    28         var prefBranch = getPrefBranch();
    30         if (typeof value == "string") {
    31             prefBranch.setCharPref(prefName, value);
    32         }
    33         else if (typeof value == "number") {
    34             prefBranch.setIntPref(prefName, value);
    35         }
    36         else if (typeof value == "boolean") {
    37             prefBranch.setBoolPref(prefName, value);
    38         }
    39     }
    40     catch(e) {
    41         displayError("pref", e);
    42     }
    43 }
    45 function defaultPref(prefName, value) {
    47     try {
    48         var prefService = Components.classes[PrefServiceContractID]
    49                                     .getService(nsIPrefService);        
    50         var prefBranch = prefService.getDefaultBranch(null);
    51         if (typeof value == "string") {
    52             prefBranch.setCharPref(prefName, value);
    53         }
    54         else if (typeof value == "number") {
    55             prefBranch.setIntPref(prefName, value);
    56         }
    57         else if (typeof value == "boolean") {
    58             prefBranch.setBoolPref(prefName, value);
    59         }
    60     }
    61     catch(e) {
    62         displayError("defaultPref", e);
    63     }
    64 }
    66 function lockPref(prefName, value) {
    68     try {
    69         var prefBranch = getPrefBranch();
    71         if (prefBranch.prefIsLocked(prefName))
    72             prefBranch.unlockPref(prefName);
    74         defaultPref(prefName, value);
    76         prefBranch.lockPref(prefName);
    77     }
    78     catch(e) {
    79         displayError("lockPref", e);
    80     }
    81 }
    83 function unlockPref(prefName) {
    85     try {
    87         var prefBranch = getPrefBranch();
    88         prefBranch.unlockPref(prefName);
    89     }
    90     catch(e) {
    91         displayError("unlockPref", e);
    92     }
    93 }
    95 function getPref(prefName) {
    97     try {
    98         var prefBranch = getPrefBranch();
   100         switch (prefBranch.getPrefType(prefName)) {
   102         case prefBranch.PREF_STRING:
   103             return prefBranch.getCharPref(prefName);
   105         case prefBranch.PREF_INT:
   106             return prefBranch.getIntPref(prefName);
   108         case prefBranch.PREF_BOOL:
   109             return prefBranch.getBoolPref(prefName);
   110         default:
   111             return null;
   112         }
   113     }
   114     catch(e) {
   115         displayError("getPref", e);
   116     }
   117 }
   119 function clearPref(prefName) {
   121     try {
   122         var prefBranch = getPrefBranch();
   123             prefBranch.clearUserPref(prefName);
   124     }
   125     catch(e) {
   126     }
   128 }
   130 function setLDAPVersion(version) {
   131     gVersion = version;
   132 }
   135 function getLDAPAttributes(host, base, filter, attribs) {
   137     try {
   138         var urlSpec = "ldap://" + host + "/" + base + "?" + attribs + "?sub?" +
   139                       filter;
   141         var url = Components.classes["@mozilla.org/network/io-service;1"]
   142                             .getService(Components.interfaces.nsIIOService)
   143                             .newURI(urlSpec, null, null)
   144                             .QueryInterface(Components.interfaces.nsILDAPURL);
   146         var ldapquery = Components.classes[LDAPSyncQueryContractID]
   147                                   .createInstance(nsILDAPSyncQuery);
   148         // default to LDAP v3
   149         if (!gVersion)
   150           gVersion = Components.interfaces.nsILDAPConnection.VERSION3
   151  	// user supplied method
   152         processLDAPValues(ldapquery.getQueryResults(url, gVersion));
   153     }
   154     catch(e) {
   155         displayError("getLDAPAttibutes", e);
   156     }
   157 }
   159 function getLDAPValue(str, key) {
   161     try {
   162         if (str == null || key == null)
   163             return null;
   165         var search_key = "\n" + key + "=";
   167         var start_pos = str.indexOf(search_key);
   168         if (start_pos == -1)
   169             return null;
   171         start_pos += search_key.length;
   173         var end_pos = str.indexOf("\n", start_pos);
   174         if (end_pos == -1)
   175             end_pos = str.length;
   177         return str.substring(start_pos, end_pos);
   178     }
   179     catch(e) {
   180         displayError("getLDAPValue", e);
   181     }
   182 }
   184 function displayError(funcname, message) {
   186     try {
   187         var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
   188                                       .getService(Components.interfaces.nsIPromptService);
   189         var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
   190                                .getService(Components.interfaces.nsIStringBundleService)
   191                                .createBundle("chrome://autoconfig/locale/autoconfig.properties");
   193          var title = bundle.GetStringFromName("autoConfigTitle");
   194          var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
   195          promptService.alert(null, title, msg + " " + message);
   196     }
   197     catch(e) { }
   198 }
   200 function getenv(name) {
   201     try {
   202         var environment = Components.classes["@mozilla.org/process/environment;1"].
   203             getService(Components.interfaces.nsIEnvironment);
   204         return environment.get(name);
   205     }
   206     catch(e) {
   207         displayError("getEnvironment", e);
   208     }
   209 }

mercurial