moved some sources and icon to separate dirs
[guerillascript.git] / includes / prefs.js
blob8faf7159bc17233950114f588c5cb57173089501
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.
9 */
10 ////////////////////////////////////////////////////////////////////////////////
11 const PREF_BRANCH = "extensions.guerilla.";
13 const PREFS = {
14 openErrorConsoleOnStartup: false,
15 debugMode: false,
16 debugCache: false,
17 logEnabled: true,
18 active: true,
19 // all pathes are relative to profile dir
20 jsdir: "guerillajs",
21 libdir: "guerillajs_libs",
22 dbdir: "guerillajs_data",
23 pkgdir: "guerillajs_packages",
24 pkgdbdir: "guerillajs_packages/_0data",
25 pkgtempdir: "guerillajs_packages/_0temp",
29 ////////////////////////////////////////////////////////////////////////////////
30 function setDefaultPrefs () {
31 let branch = Services.prefs.getDefaultBranch(PREF_BRANCH);
32 for (let [key, val] in Iterator(PREFS)) {
33 switch (typeof(val)) {
34 case "boolean": branch.setBoolPref(key, val); break;
35 case "number": branch.setIntPref(key, val); break;
36 case "string": branch.setCharPref(key, val); break;
40 //exportsGlobal.setDefaultPrefs = setDefaultPrefs;
43 ////////////////////////////////////////////////////////////////////////////////
44 let knownDirs = {};
46 function getUserXXDir (name, createit) {
47 let res = knownDirs[name];
48 if (res) {
49 // cached
50 res = res.clone();
51 } else {
52 // new, cache it
53 let dn = "";
54 try { dn = Services.prefs.getCharPref(PREF_BRANCH+name); } catch (e) {}
55 if (dn.length && dn[0] == '/') {
56 res = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
57 res.initWithPath(dn);
58 } else {
59 res = getProfileDir();
60 if (dn.length) {
61 for (let d of dn.split("/")) if (d.length) res.append(d);
64 knownDirs[name] = res.clone();
66 if (createit && !res.exists()) res.create(res.DIRECTORY_TYPE, 0700);
67 return res;
70 exportsGlobal.resetUserDirCache = function () { knownDirs = {}; };
72 exportsGlobal.getUserJSDir = function () getUserXXDir("jsdir", true);
73 exportsGlobal.getUserDBDir = function () getUserXXDir("dbdir", true);
74 exportsGlobal.getUserLibDir = function () getUserXXDir("libdir", true);
75 exportsGlobal.getUserPkgDir = function () getUserXXDir("pkgdir", true);
76 exportsGlobal.getUserPkgDBDir = function () getUserXXDir("pkgdbdir", true);
77 exportsGlobal.getUserPkgTempDir = function () getUserXXDir("pkgtempdir", true);
80 ////////////////////////////////////////////////////////////////////////////////
81 function getGuerillaPref (name) {
82 if (typeof(name) == "string" && (name in PREFS)) {
83 try {
84 switch (typeof(PREFS[name])) {
85 case "boolean": return Services.prefs.getBoolPref(PREF_BRANCH+name);
86 case "number": return Services.prefs.getIntPref(PREF_BRANCH+name);
87 case "string": return Services.prefs.getCharPref(PREF_BRANCH+name);
89 } catch (e) {}
90 return PREFS[name];
92 logError("guerilla: invalid preference request for '"+name+"'");
93 return null;
97 ////////////////////////////////////////////////////////////////////////////////
98 function setGuerillaPref (name, val) {
99 if (typeof(name) == "string" && (name in PREFS)) {
100 try {
101 switch (typeof(PREFS[name])) {
102 case "boolean": val = !!val; Services.prefs.setBoolPref(PREF_BRANCH+name, val); break;
103 case "number": val = parseInt(val); Services.prefs.setIntPref(PREF_BRANCH+name, val); break;
104 case "string": val = ""+val; Services.prefs.setCharPref(PREF_BRANCH+name, val); break;
106 return val;
107 } catch (e) {}
109 logError("guerilla: invalid preference set request for '"+name+"'");
110 return null;
114 ////////////////////////////////////////////////////////////////////////////////
115 let guerillaOptions = {
116 _cache: {},
117 _resetCache: function () { guerillaOptions._cache = {}; },
118 _getOpt: function (name) {
119 if (!(name in guerillaOptions._cache)) guerillaOptions._cache[name] = getGuerillaPref(name);
120 return guerillaOptions._cache[name];
122 _setOpt: function (name, val) {
123 guerillaOptions._cache[name] = val;
124 return setGuerillaPref(name, val);
126 //TODO: make this generic instead of pasta!
127 get debugMode () guerillaOptions._getOpt("debugMode"),
128 set debugMode (val) guerillaOptions._setOpt("debugMode", !!val),
129 get debugCache () guerillaOptions._getOpt("debugCache"),
130 set debugCache (val) guerillaOptions._setOpt("debugCache", !!val),
131 get logEnabled () guerillaOptions._getOpt("logEnabled"),
132 set logEnabled (val) guerillaOptions._setOpt("logEnabled", !!val),
133 get openConsole () guerillaOptions._getOpt("openErrorConsoleOnStartup"),
134 set openConsole (val) guerillaOptions._setOpt("openErrorConsoleOnStartup", !!val),
135 get active () guerillaOptions._getOpt("active"),
136 set active (val) guerillaOptions._setOpt("active", !!val),
138 exportsGlobal.guerillaOptions = guerillaOptions;
141 ////////////////////////////////////////////////////////////////////////////////
142 let PREF_OBSERVER = {
143 observe: function (aSubject, aTopic, aData) {
144 //conlog("data: ["+aData+"]; subj="+aSubject+"; topic="+aTopic);
145 //let name = aData.substr(PREF_BRANCH.length);
146 //conlog("name: ["+name+"]");
147 //let prf = Services.prefs;
148 //if (name == "openErrorConsoleOnStartup") guerillaOptions.openConsole = prf.getBoolPref(aData);
149 if (aData.indexOf("dir") > 0) {
150 resetUserDirCache();
151 } else {
152 guerillaOptions._resetCache();
158 ////////////////////////////////////////////////////////////////////////////////
159 registerStartupHook("preferences", function () {
160 setDefaultPrefs(); // always set the default prefs as they disappear on restart
161 Services.prefs.addObserver(PREF_BRANCH, PREF_OBSERVER, false);
165 registerShutdownHook("preferences", function () {
166 Services.prefs.removeObserver(PREF_BRANCH, PREF_OBSERVER);