PageRenderTime 32ms CodeModel.GetById 10ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 40const nsILDAPURL = Components.interfaces.nsILDAPURL;
 41const LDAPURLContractID = "@mozilla.org/network/ldap-url;1";
 42const nsILDAPSyncQuery = Components.interfaces.nsILDAPSyncQuery;
 43const LDAPSyncQueryContractID = "@mozilla.org/ldapsyncquery;1";
 44const nsIPrefService = Components.interfaces.nsIPrefService;
 45const PrefServiceContractID = "@mozilla.org/preferences-service;1";
 46
 47// set on a platform specific basis in platform.js
 48platform = { value: "" };
 49
 50var gVersion;
 51
 52function getPrefBranch() {
 53    
 54    var prefService = Components.classes[PrefServiceContractID]
 55                                .getService(nsIPrefService);    
 56    return prefService.getBranch(null);
 57}
 58
 59function pref(prefName, value) {
 60
 61    try { 
 62        var prefBranch = getPrefBranch();
 63
 64        if (typeof value == "string") {
 65            prefBranch.setCharPref(prefName, value);
 66        }
 67        else if (typeof value == "number") {
 68            prefBranch.setIntPref(prefName, value);
 69        }
 70        else if (typeof value == "boolean") {
 71            prefBranch.setBoolPref(prefName, value);
 72        }
 73    }
 74    catch(e) {
 75        displayError("pref", e);
 76    }
 77}
 78
 79function defaultPref(prefName, value) {
 80    
 81    try {
 82        var prefService = Components.classes[PrefServiceContractID]
 83                                    .getService(nsIPrefService);        
 84        var prefBranch = prefService.getDefaultBranch(null);
 85        if (typeof value == "string") {
 86            prefBranch.setCharPref(prefName, value);
 87        }
 88        else if (typeof value == "number") {
 89            prefBranch.setIntPref(prefName, value);
 90        }
 91        else if (typeof value == "boolean") {
 92            prefBranch.setBoolPref(prefName, value);
 93        }
 94    }
 95    catch(e) {
 96        displayError("defaultPref", e);
 97    }
 98}
 99
100function lockPref(prefName, value) {
101    
102    try {
103        var prefBranch = getPrefBranch();
104        
105        if (prefBranch.prefIsLocked(prefName))
106            prefBranch.unlockPref(prefName);
107        
108        defaultPref(prefName, value);
109        
110        prefBranch.lockPref(prefName);
111    }
112    catch(e) {
113        displayError("lockPref", e);
114    }
115}
116
117function unlockPref(prefName) {
118
119    try {
120
121        var prefBranch = getPrefBranch();
122        prefBranch.unlockPref(prefName);
123    }
124    catch(e) {
125        displayError("unlockPref", e);
126    }
127}
128
129function getPref(prefName) {
130    
131    try {
132        var prefBranch = getPrefBranch();
133        
134        switch (prefBranch.getPrefType(prefName)) {
135            
136        case prefBranch.PREF_STRING:
137            return prefBranch.getCharPref(prefName);
138            
139        case prefBranch.PREF_INT:
140            return prefBranch.getIntPref(prefName);
141            
142        case prefBranch.PREF_BOOL:
143            return prefBranch.getBoolPref(prefName);
144        default:
145            return null;
146        }
147    }
148    catch(e) {
149        displayError("getPref", e);
150    }
151}
152
153function clearPref(prefName) {
154
155    try {
156        var prefBranch = getPrefBranch();
157            prefBranch.clearUserPref(prefName);
158    }
159    catch(e) {
160    }
161        
162}
163
164function setLDAPVersion(version) {
165    gVersion = version;
166}
167
168
169function getLDAPAttributes(host, base, filter, attribs) {
170    
171    try {
172        var urlSpec = "ldap://" + host + "/" + base + "?" + attribs + "?sub?" +
173                      filter;
174
175        var url = Components.classes["@mozilla.org/network/io-service;1"]
176                            .getService(Components.interfaces.nsIIOService)
177                            .newURI(urlSpec, null, null)
178                            .QueryInterface(Components.interfaces.nsILDAPURL);
179
180        var ldapquery = Components.classes[LDAPSyncQueryContractID]
181                                  .createInstance(nsILDAPSyncQuery);
182        // default to LDAP v3
183        if (!gVersion)
184          gVersion = Components.interfaces.nsILDAPConnection.VERSION3
185 	// user supplied method
186        processLDAPValues(ldapquery.getQueryResults(url, gVersion));
187    }
188    catch(e) {
189        displayError("getLDAPAttibutes", e);
190    }
191}
192
193function getLDAPValue(str, key) {
194
195    try {
196        if (str == null || key == null)
197            return null;
198        
199        var search_key = "\n" + key + "=";
200        
201        var start_pos = str.indexOf(search_key);
202        if (start_pos == -1)
203            return null;
204        
205        start_pos += search_key.length;
206        
207        var end_pos = str.indexOf("\n", start_pos);
208        if (end_pos == -1)
209            end_pos = str.length;
210        
211        return str.substring(start_pos, end_pos);
212    }
213    catch(e) {
214        displayError("getLDAPValue", e);
215    }
216}
217
218function displayError(funcname, message) {
219
220    try {
221        var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
222                                      .getService(Components.interfaces.nsIPromptService);
223        var bundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
224                               .getService(Components.interfaces.nsIStringBundleService)
225                               .createBundle("chrome://autoconfig/locale/autoconfig.properties");
226
227         var title = bundle.GetStringFromName("autoConfigTitle");
228         var msg = bundle.formatStringFromName("autoConfigMsg", [funcname], 1);
229         promptService.alert(null, title, msg + " " + message);
230    }
231    catch(e) { }
232}
233
234function getenv(name) {
235    try {
236        var environment = Components.classes["@mozilla.org/process/environment;1"].
237            getService(Components.interfaces.nsIEnvironment);
238        return environment.get(name);
239    }
240    catch(e) {
241        displayError("getEnvironment", e);
242    }
243}
244