1 /* coded by Ketmar // Invisible Vector (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
10 ////////////////////////////////////////////////////////////////////////////////
11 // user code should define `PREFS_BRANCH`, `PREFS` and `PREFS_DIR_NAMES`
14 ////////////////////////////////////////////////////////////////////////////////
15 function setDefaultPrefs () {
16 let branch = Services.prefs.getDefaultBranch(PREFS_BRANCH);
17 for (let [key, val] in Iterator(PREFS)) {
18 switch (typeof(val)) {
19 case "boolean": branch.setBoolPref(key, val); break;
20 case "number": branch.setIntPref(key, val); break;
21 case "string": branch.setCharPref(key, val); break;
27 ////////////////////////////////////////////////////////////////////////////////
30 function getUserXXDir (name, createit) {
31 let res = knownDirs[name];
36 if (!(name in PREFS_DIR_NAMES)) throw new Error("unknown 'known' dir: '"+name+"'");
37 //logError("getting dir '"+name+"'");
40 try { dn = Services.prefs.getCharPref(PREFS_BRANCH+name); } catch (e) {}
41 if (dn.length && dn[0] == '/') {
42 res = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
45 res = getProfileDir();
47 for (let d of dn.split("/")) if (d.length) res.append(d);
50 //logError("dir ["+name+"] = ["+res.path+"]");
51 knownDirs[name] = res.clone();
53 if (createit && !res.exists()) res.create(res.DIRECTORY_TYPE, 0700);
57 exportsGlobal.resetUserDirCache = function () { knownDirs = {}; };
61 if ("PREFS_DIR_NAMES" in this) {
63 for (let [iname, oname] in Iterator(PREFS_DIR_NAMES)) {
64 let getter = tieto(this, getUserXXDir, iname, true);
65 exportsGlobal["getUser"+oname] = getter;
66 Object.defineProperty(userdirs, oname, {get: function () getter});
69 exportsGlobal.userdirs = userdirs;
72 ////////////////////////////////////////////////////////////////////////////////
73 function getAddonPref (name) {
74 if (typeof(name) == "string" && (name in PREFS)) {
76 switch (typeof(PREFS[name])) {
77 case "boolean": return Services.prefs.getBoolPref(PREFS_BRANCH+name);
78 case "number": return Services.prefs.getIntPref(PREFS_BRANCH+name);
79 case "string": return Services.prefs.getCharPref(PREFS_BRANCH+name);
84 logError("k8extcarcass: invalid preference request for '"+name+"'");
89 ////////////////////////////////////////////////////////////////////////////////
90 function setAddonPref (name, val) {
91 if (typeof(name) == "string" && (name in PREFS)) {
93 switch (typeof(PREFS[name])) {
94 case "boolean": val = !!val; Services.prefs.setBoolPref(PREFS_BRANCH+name, val); break;
95 case "number": val = parseInt(val); Services.prefs.setIntPref(PREFS_BRANCH+name, val); break;
96 case "string": val = ""+val; Services.prefs.setCharPref(PREFS_BRANCH+name, val); break;
101 logError("k8extcarcass: invalid preference set request for '"+name+"'");
106 ////////////////////////////////////////////////////////////////////////////////
109 _resetCache: function () { addonOptions._cache = {}; },
110 _getOpt: function (name) {
111 if (!(name in addonOptions._cache)) addonOptions._cache[name] = getAddonPref(name);
112 return addonOptions._cache[name];
114 _setOpt: function (name, val) {
115 addonOptions._cache[name] = val;
116 return setAddonPref(name, val);
118 //TODO: make this generic instead of pasta!
119 get debugMode () addonOptions._getOpt("debugMode"),
120 set debugMode (val) addonOptions._setOpt("debugMode", !!val),
121 get debugCache () addonOptions._getOpt("debugCache"),
122 set debugCache (val) addonOptions._setOpt("debugCache", !!val),
123 get logEnabled () addonOptions._getOpt("logEnabled"),
124 set logEnabled (val) addonOptions._setOpt("logEnabled", !!val),
125 get openConsole () addonOptions._getOpt("openErrorConsoleOnStartup"),
126 set openConsole (val) addonOptions._setOpt("openErrorConsoleOnStartup", !!val),
127 get active () addonOptions._getOpt("active"),
128 set active (val) addonOptions._setOpt("active", !!val),
130 exportsGlobal.addonOptions = addonOptions;
133 ////////////////////////////////////////////////////////////////////////////////
135 observe: function (aSubject, aTopic, aData) {
136 //conlog("data: ["+aData+"]; subj="+aSubject+"; topic="+aTopic);
137 //let name = aData.substr(PREFS_BRANCH.length);
138 //conlog("name: ["+name+"]");
139 //let prf = Services.prefs;
140 //if (name == "openErrorConsoleOnStartup") addonOptions.openConsole = prf.getBoolPref(aData);
141 if (aData.indexOf("dir") > 0) {
144 addonOptions._resetCache();
150 ////////////////////////////////////////////////////////////////////////////////
151 registerStartupHook("preferences", function () {
152 setDefaultPrefs(); // always set the default prefs as they disappear on restart
153 Services.prefs.addObserver(PREFS_BRANCH, prefObserver, false);
157 registerShutdownHook("preferences", function () {
158 Services.prefs.removeObserver(PREFS_BRANCH, prefObserver);