mirror of
https://github.com/gryf/pentadactyl-pm.git
synced 2026-03-04 23:45:47 +01:00
Replace prefs.getDefault with prefs.defaults.get, and remove useless private _load/_store methods.
This commit is contained in:
@@ -990,7 +990,7 @@ var Options = Module("options", {
|
|||||||
context.pushProcessor(0, function (item, text, next) next(item, text.substr(0, 100)));
|
context.pushProcessor(0, function (item, text, next) next(item, text.substr(0, 100)));
|
||||||
context.completions = [
|
context.completions = [
|
||||||
[prefs.get(filter), "Current Value"],
|
[prefs.get(filter), "Current Value"],
|
||||||
[prefs.getDefault(filter), "Default Value"]
|
[prefs.defaults.get(filter), "Default Value"]
|
||||||
].filter(function (k) k[0] != null);
|
].filter(function (k) k[0] != null);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@
|
|||||||
// given in the LICENSE.txt file included with this file.
|
// given in the LICENSE.txt file included with this file.
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
Components.utils.import("resource://dactyl/bootstrap.jsm");
|
Components.utils.import("resource://dactyl/bootstrap.jsm");
|
||||||
defineModule("prefs", {
|
defineModule("prefs", {
|
||||||
exports: ["Prefs", "localPrefs", "prefs"],
|
exports: ["Prefs", "localPrefs", "prefs"],
|
||||||
@@ -18,19 +20,31 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
RESTORE: "extensions.dactyl.restore.",
|
RESTORE: "extensions.dactyl.restore.",
|
||||||
INIT: {},
|
INIT: {},
|
||||||
|
|
||||||
init: function (branch) {
|
init: function (branch, defaults) {
|
||||||
this._prefContexts = [];
|
this._prefContexts = [];
|
||||||
|
|
||||||
util.addObserver(this);
|
this.branch = services.pref[defaults ? "getDefaultBranch" : "getBranch"](branch || "");
|
||||||
this.branch = services.pref.getBranch(branch || "").QueryInterface(Ci.nsIPrefBranch2);
|
if (this.branch instanceof Ci.nsIPrefBranch2)
|
||||||
this.branch.addObserver("", this, false);
|
this.branch.QueryInterface(Ci.nsIPrefBranch2);
|
||||||
this._observers = {};
|
|
||||||
|
|
||||||
this.restore();
|
if (!defaults)
|
||||||
|
this.restore();
|
||||||
|
|
||||||
|
this.defaults = defaults ? this : this.constructor(branch, true);
|
||||||
|
|
||||||
|
this._observers = {};
|
||||||
},
|
},
|
||||||
|
|
||||||
cleanup: function cleanup() {
|
cleanup: function cleanup() {
|
||||||
|
if (this.defaults != this)
|
||||||
|
this.defaults.cleanup();
|
||||||
|
|
||||||
this.branch.removeObserver("", this);
|
this.branch.removeObserver("", this);
|
||||||
|
this._observers = {};
|
||||||
|
if (this.observe) {
|
||||||
|
this.observe.unregister();
|
||||||
|
delete this.observe;
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
observers: {
|
observers: {
|
||||||
@@ -56,6 +70,11 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
* new value of the preference whenever it changes.
|
* new value of the preference whenever it changes.
|
||||||
*/
|
*/
|
||||||
watch: function (pref, callback, strong) {
|
watch: function (pref, callback, strong) {
|
||||||
|
if (!this.observe) {
|
||||||
|
util.addObserver(this);
|
||||||
|
this.branch.addObserver("", this, false);
|
||||||
|
}
|
||||||
|
|
||||||
if (!this._observers[pref])
|
if (!this._observers[pref])
|
||||||
this._observers[pref] = [];
|
this._observers[pref] = [];
|
||||||
this._observers[pref].push(!strong ? Cu.getWeakReference(callback) : { get: function () callback });
|
this._observers[pref].push(!strong ? Cu.getWeakReference(callback) : { get: function () callback });
|
||||||
@@ -107,16 +126,34 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
* @param {value} defaultValue The value to return if the preference
|
* @param {value} defaultValue The value to return if the preference
|
||||||
* is unset.
|
* is unset.
|
||||||
*/
|
*/
|
||||||
get: function (name, defaultValue) this._load(name, defaultValue),
|
get: function get(name, defaultValue) {
|
||||||
|
if (defaultValue == null)
|
||||||
|
defaultValue = null;
|
||||||
|
|
||||||
/**
|
let type = this.branch.getPrefType(name);
|
||||||
* Returns the default value of a preference
|
try {
|
||||||
*
|
switch (type) {
|
||||||
* @param {string} name The preference name.
|
case Ci.nsIPrefBranch.PREF_STRING:
|
||||||
* @param {value} defaultValue The value to return if the preference
|
let value = this.branch.getComplexValue(name, Ci.nsISupportsString).data;
|
||||||
* has no default value.
|
// try in case it's a localized string (will throw an exception if not)
|
||||||
*/
|
if (!this.branch.prefIsLocked(name) && !this.branch.prefHasUserValue(name) &&
|
||||||
getDefault: function (name, defaultValue) this._load(name, defaultValue, true),
|
RegExp("chrome://.+/locale/.+\\.properties").test(value))
|
||||||
|
value = this.branch.getComplexValue(name, Ci.nsIPrefLocalizedString).data;
|
||||||
|
return value;
|
||||||
|
case Ci.nsIPrefBranch.PREF_INT:
|
||||||
|
return this.branch.getIntPref(name);
|
||||||
|
case Ci.nsIPrefBranch.PREF_BOOL:
|
||||||
|
return this.branch.getBoolPref(name);
|
||||||
|
default:
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (e) {
|
||||||
|
return defaultValue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
getDefault: deprecated("Prefs#defaults.get", function getDefault(name, defaultValue) this.defaults.get(name, defaultValue)),
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the names of all preferences.
|
* Returns the names of all preferences.
|
||||||
@@ -127,11 +164,11 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
getNames: function (branch) this.branch.getChildList(branch || "", { value: 0 }),
|
getNames: function (branch) this.branch.getChildList(branch || "", { value: 0 }),
|
||||||
|
|
||||||
_checkSafe: function (name, message, value) {
|
_checkSafe: function (name, message, value) {
|
||||||
let curval = this._load(name, null, false);
|
let curval = this.get(name, null);
|
||||||
if (arguments.length > 2 && curval === value)
|
if (arguments.length > 2 && curval === value)
|
||||||
return;
|
return;
|
||||||
let defval = this._load(name, null, true);
|
let defval = this.defaults.get(name, null);
|
||||||
let saved = this._load(this.SAVED + name);
|
let saved = this.get(this.SAVED + name);
|
||||||
|
|
||||||
if (saved == null && curval != defval || saved != null && curval != saved) {
|
if (saved == null && curval != defval || saved != null && curval != saved) {
|
||||||
let msg = "Warning: setting preference " + name + ", but it's changed from its default value.";
|
let msg = "Warning: setting preference " + name + ", but it's changed from its default value.";
|
||||||
@@ -163,8 +200,8 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
*/
|
*/
|
||||||
safeSet: function (name, value, message, skipSave) {
|
safeSet: function (name, value, message, skipSave) {
|
||||||
this._checkSafe(name, message, value);
|
this._checkSafe(name, message, value);
|
||||||
this._store(name, value);
|
this.set(name, value);
|
||||||
this[skipSave ? "reset" : "_store"](this.SAVED + name, value);
|
this[skipSave ? "reset" : "set"](this.SAVED + name, value);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -174,7 +211,40 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
* @param {value} value The new preference value.
|
* @param {value} value The new preference value.
|
||||||
*/
|
*/
|
||||||
set: function (name, value) {
|
set: function (name, value) {
|
||||||
this._store(name, value);
|
if (this._prefContexts.length) {
|
||||||
|
let val = this.get(name, null);
|
||||||
|
if (val != null)
|
||||||
|
this._prefContexts[this._prefContexts.length - 1][name] = val;
|
||||||
|
}
|
||||||
|
|
||||||
|
function assertType(needType)
|
||||||
|
util.assert(type === Ci.nsIPrefBranch.PREF_INVALID || type === needType,
|
||||||
|
type === Ci.nsIPrefBranch.PREF_INT
|
||||||
|
? "E521: Number required after =: " + name + "=" + value
|
||||||
|
: "E474: Invalid argument: " + name + "=" + value);
|
||||||
|
|
||||||
|
let type = this.branch.getPrefType(name);
|
||||||
|
switch (typeof value) {
|
||||||
|
case "string":
|
||||||
|
assertType(Ci.nsIPrefBranch.PREF_STRING);
|
||||||
|
|
||||||
|
let supportString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
|
||||||
|
supportString.data = value;
|
||||||
|
this.branch.setComplexValue(name, Ci.nsISupportsString, supportString);
|
||||||
|
break;
|
||||||
|
case "number":
|
||||||
|
assertType(Ci.nsIPrefBranch.PREF_INT);
|
||||||
|
|
||||||
|
this.branch.setIntPref(name, value);
|
||||||
|
break;
|
||||||
|
case "boolean":
|
||||||
|
assertType(Ci.nsIPrefBranch.PREF_BOOL);
|
||||||
|
|
||||||
|
this.branch.setBoolPref(name, value);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw FailedAssertion("Unknown preference type: " + typeof value + " (" + name + "=" + value + ")");
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -241,7 +311,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
*/
|
*/
|
||||||
popContext: function () {
|
popContext: function () {
|
||||||
for (let [k, v] in Iterator(this._prefContexts.pop()))
|
for (let [k, v] in Iterator(this._prefContexts.pop()))
|
||||||
this._store(k, v);
|
this.set(k, v);
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -262,71 +332,6 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
|
|||||||
finally {
|
finally {
|
||||||
this.popContext();
|
this.popContext();
|
||||||
}
|
}
|
||||||
},
|
|
||||||
|
|
||||||
_store: function (name, value) {
|
|
||||||
if (this._prefContexts.length) {
|
|
||||||
let val = this._load(name, null);
|
|
||||||
if (val != null)
|
|
||||||
this._prefContexts[this._prefContexts.length - 1][name] = val;
|
|
||||||
}
|
|
||||||
|
|
||||||
function assertType(needType)
|
|
||||||
util.assert(type === Ci.nsIPrefBranch.PREF_INVALID || type === needType,
|
|
||||||
type === Ci.nsIPrefBranch.PREF_INT
|
|
||||||
? "E521: Number required after =: " + name + "=" + value
|
|
||||||
: "E474: Invalid argument: " + name + "=" + value);
|
|
||||||
|
|
||||||
let type = this.branch.getPrefType(name);
|
|
||||||
switch (typeof value) {
|
|
||||||
case "string":
|
|
||||||
assertType(Ci.nsIPrefBranch.PREF_STRING);
|
|
||||||
|
|
||||||
let supportString = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
|
|
||||||
supportString.data = value;
|
|
||||||
this.branch.setComplexValue(name, Ci.nsISupportsString, supportString);
|
|
||||||
break;
|
|
||||||
case "number":
|
|
||||||
assertType(Ci.nsIPrefBranch.PREF_INT);
|
|
||||||
|
|
||||||
this.branch.setIntPref(name, value);
|
|
||||||
break;
|
|
||||||
case "boolean":
|
|
||||||
assertType(Ci.nsIPrefBranch.PREF_BOOL);
|
|
||||||
|
|
||||||
this.branch.setBoolPref(name, value);
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw FailedAssertion("Unknown preference type: " + typeof value + " (" + name + "=" + value + ")");
|
|
||||||
}
|
|
||||||
},
|
|
||||||
|
|
||||||
_load: function (name, defaultValue, defaultBranch) {
|
|
||||||
if (defaultValue == null)
|
|
||||||
defaultValue = null;
|
|
||||||
|
|
||||||
let branch = defaultBranch ? services.pref.getDefaultBranch(this.branch.root) : this.branch;
|
|
||||||
let type = this.branch.getPrefType(name);
|
|
||||||
try {
|
|
||||||
switch (type) {
|
|
||||||
case Ci.nsIPrefBranch.PREF_STRING:
|
|
||||||
let value = branch.getComplexValue(name, Ci.nsISupportsString).data;
|
|
||||||
// try in case it's a localized string (will throw an exception if not)
|
|
||||||
if (!this.branch.prefIsLocked(name) && !this.branch.prefHasUserValue(name) &&
|
|
||||||
RegExp("chrome://.+/locale/.+\\.properties").test(value))
|
|
||||||
value = branch.getComplexValue(name, Ci.nsIPrefLocalizedString).data;
|
|
||||||
return value;
|
|
||||||
case Ci.nsIPrefBranch.PREF_INT:
|
|
||||||
return branch.getIntPref(name);
|
|
||||||
case Ci.nsIPrefBranch.PREF_BOOL:
|
|
||||||
return branch.getBoolPref(name);
|
|
||||||
default:
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (e) {
|
|
||||||
return defaultValue;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
}, {
|
}, {
|
||||||
@@ -349,6 +354,6 @@ defineModule.modules.push(localPrefs);
|
|||||||
|
|
||||||
endModule();
|
endModule();
|
||||||
|
|
||||||
// catch(e){dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack);}
|
} catch(e){ if (!e.stack) e = Error(e); dump(e.fileName+":"+e.lineNumber+": "+e+"\n" + e.stack); }
|
||||||
|
|
||||||
// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
|
// vim: set fdm=marker sw=4 ts=4 et ft=javascript:
|
||||||
|
|||||||
Reference in New Issue
Block a user