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 const dirSvcProps
= Cc
["@mozilla.org/file/directory_service;1"].getService(Ci
.nsIProperties
);
46 // get profile directory
47 res
= dirSvcProps
.get("ProfD", Ci
.nsIFile
);
49 for (let d
of dn
.split("/")) if (d
.length
) res
.append(d
);
52 //logError("dir ["+name+"] = ["+res.path+"]");
53 knownDirs
[name
] = res
.clone();
55 if (createit
&& !res
.exists()) res
.create(res
.DIRECTORY_TYPE
, 0700);
59 exportsGlobal
.resetUserDirCache = function () { knownDirs
= {}; };
63 if ("PREFS_DIR_NAMES" in this) {
65 for (let [iname
, oname
] in Iterator(PREFS_DIR_NAMES
)) {
66 let getter
= tieto(this, getUserXXDir
, iname
, true);
67 exportsGlobal
["getUser"+oname
] = getter
;
68 Object
.defineProperty(userdirs
, oname
, {get: getter
});
70 exportsGlobal
.userdirs
= userdirs
;
74 ////////////////////////////////////////////////////////////////////////////////
75 function getAddonPref (name
) {
76 if (typeof(name
) == "string" && (name
in PREFS
)) {
78 switch (typeof(PREFS
[name
])) {
79 case "boolean": return Services
.prefs
.getBoolPref(PREFS_BRANCH
+name
);
80 case "number": return Services
.prefs
.getIntPref(PREFS_BRANCH
+name
);
81 case "string": return Services
.prefs
.getCharPref(PREFS_BRANCH
+name
);
86 logError("k8extcarcass: invalid preference request for '"+name
+"'");
91 ////////////////////////////////////////////////////////////////////////////////
92 function setAddonPref (name
, val
) {
93 if (typeof(name
) == "string" && (name
in PREFS
)) {
95 switch (typeof(PREFS
[name
])) {
96 case "boolean": val
= !!val
; Services
.prefs
.setBoolPref(PREFS_BRANCH
+name
, val
); break;
97 case "number": val
= parseInt(val
); Services
.prefs
.setIntPref(PREFS_BRANCH
+name
, val
); break;
98 case "string": val
= ""+val
; Services
.prefs
.setCharPref(PREFS_BRANCH
+name
, val
); break;
103 logError("k8extcarcass: invalid preference set request for '"+name
+"'");
108 ////////////////////////////////////////////////////////////////////////////////
111 _resetCache: function () { addonOptions
._cache
= {}; },
112 _getOpt: function (name
) {
113 if (!(name
in addonOptions
._cache
)) addonOptions
._cache
[name
] = getAddonPref(name
);
114 return addonOptions
._cache
[name
];
116 _setOpt: function (name
, val
) {
117 addonOptions
._cache
[name
] = val
;
118 return setAddonPref(name
, val
);
120 //TODO: make this generic instead of pasta!
121 get debugMode () addonOptions
._getOpt("debugMode"),
122 set debugMode (val
) addonOptions
._setOpt("debugMode", !!val
),
123 get debugCache () addonOptions
._getOpt("debugCache"),
124 set debugCache (val
) addonOptions
._setOpt("debugCache", !!val
),
125 get logEnabled () addonOptions
._getOpt("logEnabled"),
126 set logEnabled (val
) addonOptions
._setOpt("logEnabled", !!val
),
127 get openConsole () addonOptions
._getOpt("openErrorConsoleOnStartup"),
128 set openConsole (val
) addonOptions
._setOpt("openErrorConsoleOnStartup", !!val
),
129 get active () addonOptions
._getOpt("active"),
130 set active (val
) addonOptions
._setOpt("active", !!val
),
132 exportsGlobal
.addonOptions
= addonOptions
;
135 ////////////////////////////////////////////////////////////////////////////////
137 observe: function (aSubject
, aTopic
, aData
) {
138 //conlog("data: ["+aData+"]; subj="+aSubject+"; topic="+aTopic);
139 //let name = aData.substr(PREFS_BRANCH.length);
140 //conlog("name: ["+name+"]");
141 //let prf = Services.prefs;
142 //if (name == "openErrorConsoleOnStartup") addonOptions.openConsole = prf.getBoolPref(aData);
143 if (aData
.indexOf("dir") > 0) {
146 addonOptions
._resetCache();
152 ////////////////////////////////////////////////////////////////////////////////
153 registerStartupHook("preferences", function () {
154 setDefaultPrefs(); // always set the default prefs as they disappear on restart
155 Services
.prefs
.addObserver(PREFS_BRANCH
, prefObserver
, false);
159 registerShutdownHook("preferences", function () {
160 Services
.prefs
.removeObserver(PREFS_BRANCH
, prefObserver
);