|
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/. */ |
|
5 |
|
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"; |
|
12 |
|
13 // set on a platform specific basis in platform.js |
|
14 platform = { value: "" }; |
|
15 |
|
16 var gVersion; |
|
17 |
|
18 function getPrefBranch() { |
|
19 |
|
20 var prefService = Components.classes[PrefServiceContractID] |
|
21 .getService(nsIPrefService); |
|
22 return prefService.getBranch(null); |
|
23 } |
|
24 |
|
25 function pref(prefName, value) { |
|
26 |
|
27 try { |
|
28 var prefBranch = getPrefBranch(); |
|
29 |
|
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 } |
|
44 |
|
45 function defaultPref(prefName, value) { |
|
46 |
|
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 } |
|
65 |
|
66 function lockPref(prefName, value) { |
|
67 |
|
68 try { |
|
69 var prefBranch = getPrefBranch(); |
|
70 |
|
71 if (prefBranch.prefIsLocked(prefName)) |
|
72 prefBranch.unlockPref(prefName); |
|
73 |
|
74 defaultPref(prefName, value); |
|
75 |
|
76 prefBranch.lockPref(prefName); |
|
77 } |
|
78 catch(e) { |
|
79 displayError("lockPref", e); |
|
80 } |
|
81 } |
|
82 |
|
83 function unlockPref(prefName) { |
|
84 |
|
85 try { |
|
86 |
|
87 var prefBranch = getPrefBranch(); |
|
88 prefBranch.unlockPref(prefName); |
|
89 } |
|
90 catch(e) { |
|
91 displayError("unlockPref", e); |
|
92 } |
|
93 } |
|
94 |
|
95 function getPref(prefName) { |
|
96 |
|
97 try { |
|
98 var prefBranch = getPrefBranch(); |
|
99 |
|
100 switch (prefBranch.getPrefType(prefName)) { |
|
101 |
|
102 case prefBranch.PREF_STRING: |
|
103 return prefBranch.getCharPref(prefName); |
|
104 |
|
105 case prefBranch.PREF_INT: |
|
106 return prefBranch.getIntPref(prefName); |
|
107 |
|
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 } |
|
118 |
|
119 function clearPref(prefName) { |
|
120 |
|
121 try { |
|
122 var prefBranch = getPrefBranch(); |
|
123 prefBranch.clearUserPref(prefName); |
|
124 } |
|
125 catch(e) { |
|
126 } |
|
127 |
|
128 } |
|
129 |
|
130 function setLDAPVersion(version) { |
|
131 gVersion = version; |
|
132 } |
|
133 |
|
134 |
|
135 function getLDAPAttributes(host, base, filter, attribs) { |
|
136 |
|
137 try { |
|
138 var urlSpec = "ldap://" + host + "/" + base + "?" + attribs + "?sub?" + |
|
139 filter; |
|
140 |
|
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); |
|
145 |
|
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 } |
|
158 |
|
159 function getLDAPValue(str, key) { |
|
160 |
|
161 try { |
|
162 if (str == null || key == null) |
|
163 return null; |
|
164 |
|
165 var search_key = "\n" + key + "="; |
|
166 |
|
167 var start_pos = str.indexOf(search_key); |
|
168 if (start_pos == -1) |
|
169 return null; |
|
170 |
|
171 start_pos += search_key.length; |
|
172 |
|
173 var end_pos = str.indexOf("\n", start_pos); |
|
174 if (end_pos == -1) |
|
175 end_pos = str.length; |
|
176 |
|
177 return str.substring(start_pos, end_pos); |
|
178 } |
|
179 catch(e) { |
|
180 displayError("getLDAPValue", e); |
|
181 } |
|
182 } |
|
183 |
|
184 function displayError(funcname, message) { |
|
185 |
|
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"); |
|
192 |
|
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 } |
|
199 |
|
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 } |
|
210 |