1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2026-02-27 18:45:46 +01:00

Cleanup changed preferences on disable.

This commit is contained in:
Kris Maglione
2011-08-02 10:46:06 -04:00
parent 4dcef46f08
commit 377ef1332a
2 changed files with 58 additions and 28 deletions

View File

@@ -611,7 +611,7 @@ function call(fn) {
*/ */
function memoize(obj, key, getter) { function memoize(obj, key, getter) {
if (arguments.length == 1) { if (arguments.length == 1) {
obj = update({}, obj); obj = update({ __proto__: obj.__proto__ }, obj);
for (let prop in Object.getOwnPropertyNames(obj)) { for (let prop in Object.getOwnPropertyNames(obj)) {
let get = __lookupGetter__.call(obj, prop); let get = __lookupGetter__.call(obj, prop);
if (get) if (get)

View File

@@ -16,8 +16,9 @@ defineModule("prefs", {
}, this); }, this);
var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), { var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference]), {
SAVED: "extensions.dactyl.saved.", ORIGINAL: "extensions.dactyl.original.",
RESTORE: "extensions.dactyl.restore.", RESTORE: "extensions.dactyl.restore.",
SAVED: "extensions.dactyl.saved.",
INIT: {}, INIT: {},
init: function init(branch, defaults) { init: function init(branch, defaults) {
@@ -28,6 +29,13 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
this.defaults = defaults ? this : this.constructor(branch, true); this.defaults = defaults ? this : this.constructor(branch, true);
this.branches = memoize({
__proto__: this,
get original() Prefs(this.ORIGINAL + this.root),
get restore() Prefs(this.RESTORE + this.root),
get saved() Prefs(this.SAVED + this.root),
});
if (!defaults) if (!defaults)
this.restore(); this.restore();
@@ -37,6 +45,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
cleanup: function cleanup(reason) { cleanup: function cleanup(reason) {
if (this.defaults != this) if (this.defaults != this)
this.defaults.cleanup(); this.defaults.cleanup();
this._observers = {}; this._observers = {};
if (this.observe) { if (this.observe) {
this.branch.removeObserver("", this); this.branch.removeObserver("", this);
@@ -44,8 +53,18 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
delete this.observe; delete this.observe;
} }
if (reason == "uninstall") if (this == prefs) {
localPrefs.resetBranch(); if (~["uninstall", "disable"].indexOf(reason)) {
for (let name in values(this.branches.saved.getNames()))
this.safeReset(name, null, true);
this.branches.original.resetBranch();
this.branches.saved.resetBranch();
}
if (reason == "uninstall" && this == prefs)
localPrefs.resetBranch();
}
}, },
/** /**
@@ -181,10 +200,15 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
_checkSafe: function _checkSafe(name, message, value) { _checkSafe: function _checkSafe(name, message, value) {
let curval = this.get(name, null); let curval = this.get(name, null);
if (this.branches.original.get(name) == null)
this.branches.original.set(name, curval, true);
if (arguments.length > 2 && curval === value) if (arguments.length > 2 && curval === value)
return; return;
let defval = this.defaults.get(name, null); let defval = this.defaults.get(name, null);
let saved = this.get(this.SAVED + name); let saved = this.branches.saved.get(name);
if (saved == null && curval != defval || saved != null && curval != saved) { if (saved == null && curval != defval || saved != null && curval != saved) {
let msg = _("pref.safeSet.warnChanged", name); let msg = _("pref.safeSet.warnChanged", name);
@@ -200,11 +224,13 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* *
* @param {string} name The preference name. * @param {string} name The preference name.
* @param {value} value The new preference value. * @param {value} value The new preference value.
* @param {boolean} silent Ignore errors.
*/ */
safeReset: function safeReset(name, message) { safeReset: function safeReset(name, message, silent) {
this._checkSafe(name, message); this._checkSafe(name, message);
this.reset(name); this.set(name, this.branches.original.get(name), silent);
this.reset(this.SAVED + name); this.branches.original.reset(name);
this.branches.saved.reset(name);
}, },
/** /**
@@ -217,7 +243,7 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
safeSet: function safeSet(name, value, message, skipSave) { safeSet: function safeSet(name, value, message, skipSave) {
this._checkSafe(name, message, value); this._checkSafe(name, message, value);
this.set(name, value); this.set(name, value);
this[skipSave ? "reset" : "set"](this.SAVED + name, value); this.branches.saved[skipSave ? "reset" : "set"](name, value);
}, },
/** /**
@@ -225,8 +251,9 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
* *
* @param {string} name The preference name. * @param {string} name The preference name.
* @param {value} value The new preference value. * @param {value} value The new preference value.
* @param {boolean} silent Ignore errors.
*/ */
set: function set(name, value) { set: function set(name, value, silent) {
if (this._prefContexts.length) if (this._prefContexts.length)
this._prefContexts[this._prefContexts.length - 1][name] = this.get(name, null); this._prefContexts[this._prefContexts.length - 1][name] = this.get(name, null);
@@ -237,28 +264,31 @@ var Prefs = Module("prefs", XPCOM([Ci.nsIObserver, Ci.nsISupportsWeakReference])
: /*L*/"E474: Invalid argument: " + name + "=" + value); : /*L*/"E474: Invalid argument: " + name + "=" + value);
let type = this.branch.getPrefType(name); let type = this.branch.getPrefType(name);
switch (typeof value) { try {
case "string": switch (typeof value) {
assertType(Ci.nsIPrefBranch.PREF_STRING); case "string":
assertType(Ci.nsIPrefBranch.PREF_STRING);
this.branch.setComplexValue(name, Ci.nsISupportsString, services.String(value)); this.branch.setComplexValue(name, Ci.nsISupportsString, services.String(value));
break; break;
case "number": case "number":
assertType(Ci.nsIPrefBranch.PREF_INT); assertType(Ci.nsIPrefBranch.PREF_INT);
this.branch.setIntPref(name, value); this.branch.setIntPref(name, value);
break; break;
case "boolean": case "boolean":
assertType(Ci.nsIPrefBranch.PREF_BOOL); assertType(Ci.nsIPrefBranch.PREF_BOOL);
this.branch.setBoolPref(name, value); this.branch.setBoolPref(name, value);
break; break;
default: default:
if (value == null && this != this.defaults) if (value == null && this != this.defaults)
this.reset(name); this.reset(name);
else else
throw FailedAssertion("Unknown preference type: " + typeof value + " (" + name + "=" + value + ")"); throw FailedAssertion("Unknown preference type: " + typeof value + " (" + name + "=" + value + ")");
}
} }
catch (e if silent) {}
return value; return value;
}, },