1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is The JavaScript Debugger.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
24 * Robert Ginda, <rginda@netscape.com>, original author
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.
38 * ***** END LICENSE BLOCK ***** */
40 function PrefManager (branchName)
42 var prefManager = this;
44 function pm_observe (prefService, topic, prefName)
46 prefManager.dirtyPrefs[prefName] = true;
49 const PREF_CTRID = "@mozilla.org/preferences-service;1";
50 const nsIPrefService = Components.interfaces.nsIPrefService;
51 const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
52 const nsIPrefBranchInternal = Components.interfaces.nsIPrefBranchInternal;
55 Components.classes[PREF_CTRID].getService(nsIPrefService);
56 this.prefBranch = this.prefService.getBranch(branchName);
57 this.prefNames = new Array();
58 this.dirtyPrefs = new Object();
59 this.prefs = new Object();
60 this.observer = { observe: pm_observe };
62 this.prefBranchInternal =
63 this.prefBranch.QueryInterface(nsIPrefBranchInternal);
64 this.prefBranchInternal.addObserver("", this.observer, false);
68 PrefManager.prototype.destroy =
71 this.prefBranchInternal.removeObserver("", this.observer);
74 PrefManager.prototype.listPrefs =
75 function pm_listprefs (prefix)
77 var list = new Array();
78 var names = this.prefNames;
79 for (var i = 0; i < names.length; ++i)
81 if (!prefix || names[i].indexOf(prefix) == 0)
88 PrefManager.prototype.readPrefs =
89 function pm_readprefs ()
91 const nsIPrefBranch = Components.interfaces.nsIPrefBranch;
93 var list = this.prefBranch.getChildList("", {});
94 for (var i = 0; i < list.length; ++i)
96 if (!(list[i] in this))
98 var type = this.prefBranch.getPrefType (list[i]);
103 case nsIPrefBranch.PREF_INT:
107 case nsIPrefBranch.PREF_BOOL:
108 defaultValue = false;
115 this.addPref(list[i], defaultValue);
120 PrefManager.prototype.addPrefs =
121 function pm_addprefs (prefSpecs)
123 for (var i = 0; i < prefSpecs.length; ++i)
124 this.addPref(prefSpecs[i][0], prefSpecs[i][1]);
127 PrefManager.prototype.addPref =
128 function pm_addpref (prefName, defaultValue)
132 var prefManager = this;
134 function prefGetter ()
136 //dd ("getter for ``" + prefName + "''");
137 //dd ("default: " + defaultValue);
138 //dd ("real: " + realValue);
140 if (typeof realValue == "undefined" ||
141 prefName in prefManager.dirtyPrefs)
145 if (typeof defaultValue == "boolean")
147 realValue = prefManager.prefBranch.getBoolPref(prefName);
149 else if (typeof defaultValue == "number")
151 realValue = prefManager.prefBranch.getIntPref(prefName);
153 else if (defaultValue instanceof Array)
155 realValue = prefManager.prefBranch.getCharPref(prefName);
156 realValue = realValue.split(/s*;\s*/);
157 for (i = 0; i < realValue.length; ++i)
158 realValue[i] = unencode(realValue[i]);
160 else if (typeof defaultValue == "string")
162 realValue = prefManager.prefBranch.getCharPref(prefName);
166 realValue = defaultValue;
171 //dd ("caught exception reading pref ``" + prefName + "''\n" +
173 realValue = defaultValue;
177 delete prefManager.dirtyPrefs[prefName];
181 function prefSetter (value)
185 if (typeof defaultValue == "boolean")
187 prefManager.prefBranch.setBoolPref(prefName, value);
189 else if (typeof defaultValue == "number")
191 prefManager.prefBranch.setIntPref(prefName, value);
193 else if (defaultValue instanceof Array)
195 var ary = new Array();
196 for (i = 0; i < value.length; ++i)
197 ary[i] = encode(value[i]);
199 prefManager.prefBranch.setCharPref(prefName, ary.join("; "));
203 prefManager.prefBranch.setCharPref(prefName, value);
206 prefManager.prefService.savePrefFile(null);
210 dd ("caught exception writing pref ``" + prefName + "''\n" + ex);
216 if (!arrayContains(prefManager.prefNames, prefName))
217 prefManager.prefNames.push(prefName);
219 prefManager.prefNames.sort();
220 prefManager.prefs.__defineGetter__(prefName, prefGetter);
221 prefManager.prefs.__defineSetter__(prefName, prefSetter);