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.)

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

mercurial