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 const PREF_BRANCH = "extensions.guerilla.";
14 openErrorConsoleOnStartup: false,
19 // all pathes are relative to profile dir
21 libdir: "guerillajs_libs",
22 dbdir: "guerillajs_data",
23 pkgdir: "guerillajs_data/packages",
27 ////////////////////////////////////////////////////////////////////////////////
31 ////////////////////////////////////////////////////////////////////////////////
33 observe: function (aSubject, aTopic, aData) {
34 //conlog("data: ["+aData+"]; subj="+aSubject+"; topic="+aTopic);
35 //let name = aData.substr(PREF_BRANCH.length);
36 //conlog("name: ["+name+"]");
37 //let prf = Services.prefs;
38 //if (name == "openErrorConsoleOnStartup") guerillaOptions.openConsole = prf.getBoolPref(aData);
39 guerillaOptions._resetCache();
45 function initPrefs () {
46 Services.prefs.addObserver(PREF_BRANCH, PREF_OBSERVER, false);
50 function deinitPrefs () {
51 Services.prefs.removeObserver(PREF_BRANCH, PREF_OBSERVER);
55 ////////////////////////////////////////////////////////////////////////////////
56 function setDefaultPrefs () {
57 let branch = Services.prefs.getDefaultBranch(PREF_BRANCH);
58 for (let [key, val] in Iterator(PREFS)) {
59 switch (typeof(val)) {
60 case "boolean": branch.setBoolPref(key, val); break;
61 case "number": branch.setIntPref(key, val); break;
62 case "string": branch.setCharPref(key, val); break;
68 ////////////////////////////////////////////////////////////////////////////////
69 function getUserXXDir (name, createit) {
70 let res = knownDirs[name];
77 try { dn = Services.prefs.getCharPref(PREF_BRANCH+name); } catch (e) {}
78 if (dn.length && dn[0] == '/') {
79 res = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
82 res = getProfileDir();
84 for (let d of dn.split("/")) if (d.length) res.append(d);
87 knownDirs[name] = res.clone();
89 if (createit && !res.exists()) res.create(res.DIRECTORY_TYPE, 0750);
94 ////////////////////////////////////////////////////////////////////////////////
95 function getUserJSDir () { return getUserXXDir("jsdir", true); }
96 addExport("getUserJSDir", getUserJSDir);
98 function getUserDBDir () { return getUserXXDir("dbdir", true); }
99 addExport("getUserDBDir", getUserDBDir);
101 function getUserLibDir () { return getUserXXDir("libdir", true); }
102 addExport("getUserLibDir", getUserLibDir);
104 function getUserPkgDir () { return getUserXXDir("pkgdir", true); }
105 addExport("getUserPkgDir", getUserPkgDir);
108 ////////////////////////////////////////////////////////////////////////////////
109 function getGuerillaPref (name) {
110 if (typeof(name) == "string" && (name in PREFS)) {
112 switch (typeof(PREFS[name])) {
113 case "boolean": return Services.prefs.getBoolPref(PREF_BRANCH+name);
114 case "number": return Services.prefs.getIntPref(PREF_BRANCH+name);
115 case "string": return Services.prefs.getCharPref(PREF_BRANCH+name);
120 logError("guerilla: invalid preference request for '"+name+"'");
123 addExport("getGuerillaPref", getGuerillaPref);
126 ////////////////////////////////////////////////////////////////////////////////
127 function setGuerillaPref (name, val) {
128 if (typeof(name) == "string" && (name in PREFS)) {
130 switch (typeof(PREFS[name])) {
131 case "boolean": val = !!val; Services.prefs.setBoolPref(PREF_BRANCH+name, val); break;
132 case "number": val = parseInt(val); Services.prefs.setIntPref(PREF_BRANCH+name, val); break;
133 case "string": val = ""+val; Services.prefs.setCharPref(PREF_BRANCH+name, val); break;
138 logError("guerilla: invalid preference set request for '"+name+"'");
141 addExport("setGuerillaPref", setGuerillaPref);
144 ////////////////////////////////////////////////////////////////////////////////
145 var guerillaOptions = {
147 _resetCache: function () { guerillaOptions._cache = {}; },
148 _getOpt: function (name) {
149 if (!(name in guerillaOptions._cache)) guerillaOptions._cache[name] = getGuerillaPref(name);
150 return guerillaOptions._cache[name];
152 _setOpt: function (name, val) {
153 guerillaOptions._cache[name] = val;
154 return setGuerillaPref(name, val);
156 //TODO: make this generic instead of pasta!
157 get debugMode () guerillaOptions._getOpt("debugMode"),
158 set debugMode (val) guerillaOptions._setOpt("debugMode", !!val),
159 get debugCache () guerillaOptions._getOpt("debugCache"),
160 set debugCache (val) guerillaOptions._setOpt("debugCache", !!val),
161 get logEnabled () guerillaOptions._getOpt("logEnabled"),
162 set logEnabled (val) guerillaOptions._setOpt("logEnabled", !!val),
163 get openConsole () guerillaOptions._getOpt("openErrorConsoleOnStartup"),
164 set openConsole (val) guerillaOptions._setOpt("openErrorConsoleOnStartup", !!val),
165 get active () guerillaOptions._getOpt("active"),
166 set active (val) guerillaOptions._setOpt("active", !!val),
168 addExport("guerillaOptions", global.guerillaOptions);