1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/extensions/pref/autoconfig/src/prefcalls.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,210 @@ 1.4 +/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +const nsILDAPURL = Components.interfaces.nsILDAPURL; 1.10 +const LDAPURLContractID = "@mozilla.org/network/ldap-url;1"; 1.11 +const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery; 1.12 +const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1"; 1.13 +const nsIPrefService = Components.interfaces.nsIPrefService; 1.14 +const PrefServiceContractID = "@mozilla.org/preferences-service;1"; 1.15 + 1.16 +// set on a platform specific basis in platform.js 1.17 +platform = { value: "" }; 1.18 + 1.19 +var gVersion; 1.20 + 1.21 +function getPrefBranch() { 1.22 + 1.23 + var prefService = Components.classes[PrefServiceContractID] 1.24 + .getService(nsIPrefService); 1.25 + return prefService.getBranch(null); 1.26 +} 1.27 + 1.28 +function pref(prefName, value) { 1.29 + 1.30 + try { 1.31 + var prefBranch = getPrefBranch(); 1.32 + 1.33 + if (typeof value == "string") { 1.34 + prefBranch.setCharPref(prefName, value); 1.35 + } 1.36 + else if (typeof value == "number") { 1.37 + prefBranch.setIntPref(prefName, value); 1.38 + } 1.39 + else if (typeof value == "boolean") { 1.40 + prefBranch.setBoolPref(prefName, value); 1.41 + } 1.42 + } 1.43 + catch(e) { 1.44 + displayError("pref", e); 1.45 + } 1.46 +} 1.47 + 1.48 +function defaultPref(prefName, value) { 1.49 + 1.50 + try { 1.51 + var prefService = Components.classes[PrefServiceContractID] 1.52 + .getService(nsIPrefService); 1.53 + var prefBranch = prefService.getDefaultBranch(null); 1.54 + if (typeof value == "string") { 1.55 + prefBranch.setCharPref(prefName, value); 1.56 + } 1.57 + else if (typeof value == "number") { 1.58 + prefBranch.setIntPref(prefName, value); 1.59 + } 1.60 + else if (typeof value == "boolean") { 1.61 + prefBranch.setBoolPref(prefName, value); 1.62 + } 1.63 + } 1.64 + catch(e) { 1.65 + displayError("defaultPref", e); 1.66 + } 1.67 +} 1.68 + 1.69 +function lockPref(prefName, value) { 1.70 + 1.71 + try { 1.72 + var prefBranch = getPrefBranch(); 1.73 + 1.74 + if (prefBranch.prefIsLocked(prefName)) 1.75 + prefBranch.unlockPref(prefName); 1.76 + 1.77 + defaultPref(prefName, value); 1.78 + 1.79 + prefBranch.lockPref(prefName); 1.80 + } 1.81 + catch(e) { 1.82 + displayError("lockPref", e); 1.83 + } 1.84 +} 1.85 + 1.86 +function unlockPref(prefName) { 1.87 + 1.88 + try { 1.89 + 1.90 + var prefBranch = getPrefBranch(); 1.91 + prefBranch.unlockPref(prefName); 1.92 + } 1.93 + catch(e) { 1.94 + displayError("unlockPref", e); 1.95 + } 1.96 +} 1.97 + 1.98 +function getPref(prefName) { 1.99 + 1.100 + try { 1.101 + var prefBranch = getPrefBranch(); 1.102 + 1.103 + switch (prefBranch.getPrefType(prefName)) { 1.104 + 1.105 + case prefBranch.PREF_STRING: 1.106 + return prefBranch.getCharPref(prefName); 1.107 + 1.108 + case prefBranch.PREF_INT: 1.109 + return prefBranch.getIntPref(prefName); 1.110 + 1.111 + case prefBranch.PREF_BOOL: 1.112 + return prefBranch.getBoolPref(prefName); 1.113 + default: 1.114 + return null; 1.115 + } 1.116 + } 1.117 + catch(e) { 1.118 + displayError("getPref", e); 1.119 + } 1.120 +} 1.121 + 1.122 +function clearPref(prefName) { 1.123 + 1.124 + try { 1.125 + var prefBranch = getPrefBranch(); 1.126 + prefBranch.clearUserPref(prefName); 1.127 + } 1.128 + catch(e) { 1.129 + } 1.130 + 1.131 +} 1.132 + 1.133 +function setLDAPVersion(version) { 1.134 + gVersion = version; 1.135 +} 1.136 + 1.137 + 1.138 +function getLDAPAttributes(host, base, filter, attribs) { 1.139 + 1.140 + try { 1.141 + var urlSpec = "ldap://" + host + "/" + base + "?" + attribs + "?sub?" + 1.142 + filter; 1.143 + 1.144 + var url = Components.classes["@mozilla.org/network/io-service;1"] 1.145 + .getService(Components.interfaces.nsIIOService) 1.146 + .newURI(urlSpec, null, null) 1.147 + .QueryInterface(Components.interfaces.nsILDAPURL); 1.148 + 1.149 + var ldapquery = Components.classes[LDAPSyncQueryContractID] 1.150 + .createInstance(nsILDAPSyncQuery); 1.151 + // default to LDAP v3 1.152 + if (!gVersion) 1.153 + gVersion = Components.interfaces.nsILDAPConnection.VERSION3 1.154 + // user supplied method 1.155 + processLDAPValues(ldapquery.getQueryResults(url, gVersion)); 1.156 + } 1.157 + catch(e) { 1.158 + displayError("getLDAPAttibutes", e); 1.159 + } 1.160 +} 1.161 + 1.162 +function getLDAPValue(str, key) { 1.163 + 1.164 + try { 1.165 + if (str == null || key == null) 1.166 + return null; 1.167 + 1.168 + var search_key = "\n" + key + "="; 1.169 + 1.170 + var start_pos = str.indexOf(search_key); 1.171 + if (start_pos == -1) 1.172 + return null; 1.173 + 1.174 + start_pos += search_key.length; 1.175 + 1.176 + var end_pos = str.indexOf("\n", start_pos); 1.177 + if (end_pos == -1) 1.178 + end_pos = str.length; 1.179 + 1.180 + return str.substring(start_pos, end_pos); 1.181 + } 1.182 + catch(e) { 1.183 + displayError("getLDAPValue", e); 1.184 + } 1.185 +} 1.186 + 1.187 +function displayError(funcname, message) { 1.188 + 1.189 + try { 1.190 + var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"] 1.191 + .getService(Components.interfaces.nsIPromptService); 1.192 + var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"] 1.193 + .getService(Components.interfaces.nsIStringBundleService) 1.194 + .createBundle("chrome://autoconfig/locale/autoconfig.properties"); 1.195 + 1.196 + var title = bundle.GetStringFromName("autoConfigTitle"); 1.197 + var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1); 1.198 + promptService.alert(null, title, msg + " " + message); 1.199 + } 1.200 + catch(e) { } 1.201 +} 1.202 + 1.203 +function getenv(name) { 1.204 + try { 1.205 + var environment = Components.classes["@mozilla.org/process/environment;1"]. 1.206 + getService(Components.interfaces.nsIEnvironment); 1.207 + return environment.get(name); 1.208 + } 1.209 + catch(e) { 1.210 + displayError("getEnvironment", e); 1.211 + } 1.212 +} 1.213 +