From 387173a909442e2dc59f3d8c6ed2196a684c952b Mon Sep 17 00:00:00 2001 From: ketmar Date: Fri, 14 Aug 2015 21:35:08 +0000 Subject: [PATCH] generalized prefengine in carcass FossilOrigin-Name: 0831b74c592b84055a3bd9b211756b9b1d3a6dd4b7c1a775c6eb957649682152 --- chrome/guerilla-overlay.js | 2 +- main/prefsengine.js | 41 ++++++++++++++++++++++++++++++----------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/chrome/guerilla-overlay.js b/chrome/guerilla-overlay.js index 67481f4..5f69dae 100644 --- a/chrome/guerilla-overlay.js +++ b/chrome/guerilla-overlay.js @@ -17,7 +17,7 @@ let addonobj = scp.getAddonObj(); if (!("consoleActivated" in addonobj)) { - addonobj.consoleActivated = !!addonobj.sbox.addonOptions.openConsole; + addonobj.consoleActivated = !addonobj.sbox.addonOptions.openErrorConsoleOnStartup; } function openConsole (win) { diff --git a/main/prefsengine.js b/main/prefsengine.js index 7c9b170..a24b2c7 100644 --- a/main/prefsengine.js +++ b/main/prefsengine.js @@ -111,24 +111,43 @@ let addonOptions = { _resetCache: function () { addonOptions._cache = {}; }, _getOpt: function (name) { if (!(name in addonOptions._cache)) addonOptions._cache[name] = getAddonPref(name); + print("_getOpt: name='", name, "'; val='", addonOptions._cache[name], "'"); return addonOptions._cache[name]; }, _setOpt: function (name, val) { + print("_setOpt: name='", name, "'; val='", val, "'"); addonOptions._cache[name] = val; return setAddonPref(name, val); }, - //TODO: make this generic instead of pasta! - get debugMode () addonOptions._getOpt("debugMode"), - set debugMode (val) addonOptions._setOpt("debugMode", !!val), - get debugCache () addonOptions._getOpt("debugCache"), - set debugCache (val) addonOptions._setOpt("debugCache", !!val), - get logEnabled () addonOptions._getOpt("logEnabled"), - set logEnabled (val) addonOptions._setOpt("logEnabled", !!val), - get openConsole () addonOptions._getOpt("openErrorConsoleOnStartup"), - set openConsole (val) addonOptions._setOpt("openErrorConsoleOnStartup", !!val), - get active () addonOptions._getOpt("active"), - set active (val) addonOptions._setOpt("active", !!val), + _setIntOpt: function (name, val) { + if (typeof(val) !== "number") { + let i = parseInt(""+val, 10); + if (isNaN(i)) throw new Error("invalid integer for option '"+name+"': '"+val+"'"); + val = i; + } else { + val = Math.floor(val); + } + addonOptions._setOpt(name, val); + }, + _setBoolOpt: function (name, val) { addonOptions._setOpt(name, !!val); }, + _setStrOpt: function (name, val) { addonOptions._setOpt(name, ""+val); }, }; + +// fill properties +(function () { + for (let [n, v] in Iterator(PREFS)) { + if (!n || typeof(n) !== "string") continue; // just in case + if (n in PREFS_DIR_NAMES) continue; // this has special getters + let getter = tieto(addonOptions, "_getOpt", n); + let setter = false; + switch (typeof(v)) { + case "boolean": setter = tieto(addonOptions, "_setBoolOpt", n); break; + case "number": setter = tieto(addonOptions, "_setIntOpt", n); break; + case "string": setter = tieto(addonOptions, "_setStrOpt", n); break; + } + if (setter) Object.defineProperty(addonOptions, n, {get:getter, set:setter}); + } +})(); exportsGlobal.addonOptions = addonOptions; -- 2.11.4.GIT