/extensions/pref/autoconfig/src/prefcalls.js

http://github.com/zpao/v8monkey · JavaScript · 244 lines · 158 code · 45 blank · 41 comment · 27 complexity · 8ac9c167aca54fd39b9c9fa93fe8303a MD5 · raw file

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  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 MPL, 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 MPL, the GPL or the LGPL.
  37. *
  38. * ***** END LICENSE BLOCK ***** */
  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. // set on a platform specific basis in platform.js
  46. platform = { value: "" };
  47. var gVersion;
  48. function getPrefBranch() {
  49. var prefService = Components.classes[PrefServiceContractID]
  50. .getService(nsIPrefService);
  51. return prefService.getBranch(null);
  52. }
  53. function pref(prefName, value) {
  54. try {
  55. var prefBranch = getPrefBranch();
  56. if (typeof value == "string") {
  57. prefBranch.setCharPref(prefName, value);
  58. }
  59. else if (typeof value == "number") {
  60. prefBranch.setIntPref(prefName, value);
  61. }
  62. else if (typeof value == "boolean") {
  63. prefBranch.setBoolPref(prefName, value);
  64. }
  65. }
  66. catch(e) {
  67. displayError("pref", e);
  68. }
  69. }
  70. function defaultPref(prefName, value) {
  71. try {
  72. var prefService = Components.classes[PrefServiceContractID]
  73. .getService(nsIPrefService);
  74. var prefBranch = prefService.getDefaultBranch(null);
  75. if (typeof value == "string") {
  76. prefBranch.setCharPref(prefName, value);
  77. }
  78. else if (typeof value == "number") {
  79. prefBranch.setIntPref(prefName, value);
  80. }
  81. else if (typeof value == "boolean") {
  82. prefBranch.setBoolPref(prefName, value);
  83. }
  84. }
  85. catch(e) {
  86. displayError("defaultPref", e);
  87. }
  88. }
  89. function lockPref(prefName, value) {
  90. try {
  91. var prefBranch = getPrefBranch();
  92. if (prefBranch.prefIsLocked(prefName))
  93. prefBranch.unlockPref(prefName);
  94. defaultPref(prefName, value);
  95. prefBranch.lockPref(prefName);
  96. }
  97. catch(e) {
  98. displayError("lockPref", e);
  99. }
  100. }
  101. function unlockPref(prefName) {
  102. try {
  103. var prefBranch = getPrefBranch();
  104. prefBranch.unlockPref(prefName);
  105. }
  106. catch(e) {
  107. displayError("unlockPref", e);
  108. }
  109. }
  110. function getPref(prefName) {
  111. try {
  112. var prefBranch = getPrefBranch();
  113. switch (prefBranch.getPrefType(prefName)) {
  114. case prefBranch.PREF_STRING:
  115. return prefBranch.getCharPref(prefName);
  116. case prefBranch.PREF_INT:
  117. return prefBranch.getIntPref(prefName);
  118. case prefBranch.PREF_BOOL:
  119. return prefBranch.getBoolPref(prefName);
  120. default:
  121. return null;
  122. }
  123. }
  124. catch(e) {
  125. displayError("getPref", e);
  126. }
  127. }
  128. function clearPref(prefName) {
  129. try {
  130. var prefBranch = getPrefBranch();
  131. prefBranch.clearUserPref(prefName);
  132. }
  133. catch(e) {
  134. }
  135. }
  136. function setLDAPVersion(version) {
  137. gVersion = version;
  138. }
  139. function getLDAPAttributes(host, base, filter, attribs) {
  140. try {
  141. var urlSpec = "ldap://" + host + "/" + base + "?" + attribs + "?sub?" +
  142. filter;
  143. var url = Components.classes["@mozilla.org/network/io-service;1"]
  144. .getService(Components.interfaces.nsIIOService)
  145. .newURI(urlSpec, null, null)
  146. .QueryInterface(Components.interfaces.nsILDAPURL);
  147. var ldapquery = Components.classes[LDAPSyncQueryContractID]
  148. .createInstance(nsILDAPSyncQuery);
  149. // default to LDAP v3
  150. if (!gVersion)
  151. gVersion = Components.interfaces.nsILDAPConnection.VERSION3
  152. // user supplied method
  153. processLDAPValues(ldapquery.getQueryResults(url, gVersion));
  154. }
  155. catch(e) {
  156. displayError("getLDAPAttibutes", e);
  157. }
  158. }
  159. function getLDAPValue(str, key) {
  160. try {
  161. if (str == null || key == null)
  162. return null;
  163. var search_key = "\n" + key + "=";
  164. var start_pos = str.indexOf(search_key);
  165. if (start_pos == -1)
  166. return null;
  167. start_pos += search_key.length;
  168. var end_pos = str.indexOf("\n", start_pos);
  169. if (end_pos == -1)
  170. end_pos = str.length;
  171. return str.substring(start_pos, end_pos);
  172. }
  173. catch(e) {
  174. displayError("getLDAPValue", e);
  175. }
  176. }
  177. function displayError(funcname, message) {
  178. try {
  179. var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
  180. .getService(Components.interfaces.nsIPromptService);
  181. var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
  182. .getService(Components.interfaces.nsIStringBundleService)
  183. .createBundle("chrome://autoconfig/locale/autoconfig.properties");
  184. var title = bundle.GetStringFromName("autoConfigTitle");
  185. var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
  186. promptService.alert(null, title, msg + " " + message);
  187. }
  188. catch(e) { }
  189. }
  190. function getenv(name) {
  191. try {
  192. var environment = Components.classes["@mozilla.org/process/environment;1"].
  193. getService(Components.interfaces.nsIEnvironment);
  194. return environment.get(name);
  195. }
  196. catch(e) {
  197. displayError("getEnvironment", e);
  198. }
  199. }