X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;ds=sidebyside;f=common%2Fmodules%2Fprefs.jsm;h=3f865b61b959c901cd7dff5a3d41319e5e254b14;hb=5ebd29f56d17f62011cdd596b1d351947ee534ff;hp=f8e7e403713ddeac9d6e2c2cecd91d87157d3285;hpb=9044153cb63835e39b9de8ec4ade237c03e3888a;p=dactyl.git diff --git a/common/modules/prefs.jsm b/common/modules/prefs.jsm index f8e7e40..3f865b6 100644 --- a/common/modules/prefs.jsm +++ b/common/modules/prefs.jsm @@ -26,7 +26,8 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) this._prefContexts = []; this.branch = services.pref[defaults ? "getDefaultBranch" : "getBranch"](branch || ""); - this.branch instanceof Ci.nsIPrefBranch2; + if ("nsIPrefBranch2" in Ci) + this.branch instanceof Ci.nsIPrefBranch2; this.defaults = defaults ? this : this.constructor(branch, true); @@ -68,103 +69,42 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) } }, - /** - * Returns the full name of this object's preference branch. - */ - get root() this.branch.root, - /** * Returns a new Prefs instance for the sub-branch *branch* of this * branch. * - * @param {string} branch The branch to branch to. + * @param {string} branch The sub-branch to branch to. * @returns {Prefs} */ Branch: function Branch(branch) Prefs(this.root + branch), - observe: null, - observers: { - "nsPref:changed": function (subject, data) { - let observers = this._observers[data]; - if (observers) { - let value = this.get(data, false); - this._observers[data] = observers.filter(function (callback) { - if (!callback.get()) - return false; - util.trapErrors(callback.get(), null, value); - return true; - }); - } - } - }, - /** - * Adds a new preference observer for the given preference. + * Clears the entire branch. * - * @param {string} pref The preference to observe. - * @param {function(object)} callback The callback, called with the - * new value of the preference whenever it changes. + * @param {string} name The name of the preference branch to delete. */ - watch: function watch(pref, callback, strong) { - if (!this.observe) { - util.addObserver(this); - this.branch.addObserver("", this, false); - } - - if (!this._observers[pref]) - this._observers[pref] = []; - this._observers[pref].push(!strong ? util.weakReference(callback) : { get: function () callback }); + clear: function clear(branch) { + this.branch.deleteBranch(branch || ""); }, /** - * Lists all preferences matching *filter* or only those with changed - * values if *onlyNonDefault* is specified. - * - * @param {boolean} onlyNonDefault Limit the list to prefs with a - * non-default value. - * @param {string} filter The list filter. A null filter lists all - * prefs. - * @optional + * Returns the full name of this object's preference branch. */ - list: function list(onlyNonDefault, filter) { - if (!filter) - filter = ""; - - let prefArray = this.getNames(); - prefArray.sort(); - function prefs() { - for (let [, pref] in Iterator(prefArray)) { - let userValue = services.pref.prefHasUserValue(pref); - if (onlyNonDefault && !userValue || pref.indexOf(filter) == -1) - continue; - - let value = this.get(pref); - - let option = { - isDefault: !userValue, - default: this.defaults.get(pref, null), - value: <>={template.highlight(value, true, 100)}, - name: pref, - pre: "\u00a0\u00a0" // Unicode nonbreaking space. - }; - - yield option; - } - }; - - return template.options(_("pref.hostPreferences", config.host), prefs.call(this)); - }, + get root() this.branch.root, /** - * Returns the value of a preference. + * Returns the value of the preference *name*, or *defaultValue* if + * the preference does not exist. * - * @param {string} name The preference name. - * @param {value} defaultValue The value to return if the preference - * is unset. + * @param {string} name The name of the preference to return. + * @param {*} defaultValue The value to return if the preference has no value. + * @optional */ get: function get(name, defaultValue) { if (defaultValue == null) defaultValue = null; + if (isArray(name)) + name = name.join("."); let type = this.branch.getPrefType(name); try { @@ -192,20 +132,27 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) getDefault: deprecated("Prefs#defaults.get", function getDefault(name, defaultValue) this.defaults.get(name, defaultValue)), /** - * Returns the names of all preferences. + * Returns an array of all preference names in this branch or the + * given sub-branch. * - * @param {string} branch The branch in which to search preferences. - * @default "" + * @param {string} branch The sub-branch for which to return preferences. + * @optional */ getNames: function getNames(branch) this.branch.getChildList(branch || "", { value: 0 }), /** - * Returns true if the current branch has the given preference. + * Returns true if the given preference exists in this branch. * - * @param {string} name The preference name. - * @returns {boolean} + * @param {string} name The name of the preference to check. + */ + has: function has(name) this.branch.getPrefType(name) !== 0, + + /** + * Returns true if the given preference is set to its default value. + * + * @param {string} name The name of the preference to check. */ - has: function get(name) this.branch.getPrefType(name) != 0, + isDefault: function isDefault(name) !this.branch.prefHasUserValue(name), _checkSafe: function _checkSafe(name, message, value) { let curval = this.get(name, null); @@ -256,10 +203,11 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) }, /** - * Sets the preference *name* to *value*. + * Sets the preference *name* to *value*. If the preference already + * exists, it must have the same type as the given value. * - * @param {string} name The preference name. - * @param {value} value The new preference value. + * @param {name} name The name of the preference to change. + * @param {string|number|boolean} value The value to set. * @param {boolean} silent Ignore errors. */ set: function set(name, value, silent) { @@ -329,13 +277,11 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) /** * Resets the preference *name* to its default value. * - * @param {string} name The preference name. + * @param {string} name The name of the preference to reset. */ reset: function reset(name) { - try { + if (this.branch.prefHasUserValue(name)) this.branch.clearUserPref(name); - } - catch (e) {} // ignore - thrown if not a user set value }, /** @@ -395,7 +341,80 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]) finally { this.popContext(); } - } + }, + + observe: null, + observers: { + "nsPref:changed": function (subject, data) { + let observers = this._observers[data]; + if (observers) { + let value = this.get(data, false); + this._observers[data] = observers.filter(function (callback) { + if (!callback.get()) + return false; + util.trapErrors(callback.get(), null, value); + return true; + }); + } + } + }, + + /** + * Adds a new preference observer for the given preference. + * + * @param {string} pref The preference to observe. + * @param {function(object)} callback The callback, called with the + * new value of the preference whenever it changes. + */ + watch: function watch(pref, callback, strong) { + if (!this.observe) { + util.addObserver(this); + this.branch.addObserver("", this, false); + } + + if (!this._observers[pref]) + this._observers[pref] = []; + this._observers[pref].push(!strong ? util.weakReference(callback) : { get: function () callback }); + }, + + /** + * Lists all preferences matching *filter* or only those with changed + * values if *onlyNonDefault* is specified. + * + * @param {boolean} onlyNonDefault Limit the list to prefs with a + * non-default value. + * @param {string} filter The list filter. A null filter lists all + * prefs. + * @optional + */ + list: function list(onlyNonDefault, filter) { + if (!filter) + filter = ""; + + let prefArray = this.getNames(); + prefArray.sort(); + function prefs() { + for (let [, pref] in Iterator(prefArray)) { + let userValue = services.pref.prefHasUserValue(pref); + if (onlyNonDefault && !userValue || pref.indexOf(filter) == -1) + continue; + + let value = this.get(pref); + + let option = { + isDefault: !userValue, + default: this.defaults.get(pref, null), + value: <>={template.highlight(value, true, 100)}, + name: pref, + pre: "\u00a0\u00a0" // Unicode nonbreaking space. + }; + + yield option; + } + }; + + return template.options(_("pref.hostPreferences", config.host), prefs.call(this)); + }, }, { }, { completion: function init_completion(dactyl, modules) {