now it works again
[guerillascript.git] / includes / prefs.js
blob6c3cba0042b912b9b6a341c8540343453d6cd736
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 // WARNING! there is no `require` yet!
11 //Cu.import("resource://gre/modules/Services.jsm", this);
14 ////////////////////////////////////////////////////////////////////////////////
15 const PREF_BRANCH = "extensions.guerilla.";
16 const PREFS = {
17 //someIntPref: 1,
18 //libdir: "%PROFILE%/guerillajs/libs",
19 openErrorConsoleOnStartup: false,
20 debugMode: false,
21 debugCache: false,
22 logEnabled: true,
23 active: true,
24 jsdir: "guerillajs", // relative to profile dir
25 libdir: "guerillajs/libs", // relative to profile dir
26 dbdir: "guerillajs_data", // relative to profile dir
27 pkgdir: "guerillajs_packages", // relative to profile dir
31 ////////////////////////////////////////////////////////////////////////////////
32 let PREF_OBSERVER = {
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 switch (name) {
39 case "openErrorConsoleOnStartup": debugOptions.openConsole = prf.getBoolPref(aData); break;
40 case "debugMode": debugOptions.debugMode = prf.getBoolPref(aData); break;
41 case "debugCache": debugOptions.debugCache = prf.getBoolPref(aData); break;
42 case "logEnabled": debugOptions.logEnabled = prf.getBoolPref(aData); break;
43 case "active": guerillaOptions.active = prf.getBoolPref(aData); break;
49 function initPrefs () {
50 Services.prefs.addObserver(PREF_BRANCH, PREF_OBSERVER, false);
54 function deinitPrefs () {
55 Services.prefs.removeObserver(PREF_BRANCH, PREF_OBSERVER);
59 ////////////////////////////////////////////////////////////////////////////////
60 function setDefaultPrefs () {
61 Cu.reportError("*** setDefaultPrefs ***");
62 let branch = Services.prefs.getDefaultBranch(PREF_BRANCH);
63 for (let [key, val] in Iterator(PREFS)) {
64 switch (typeof val) {
65 case "boolean": branch.setBoolPref(key, val); break;
66 case "number": branch.setIntPref(key, val); break;
67 case "string": branch.setCharPref(key, val); break;
73 ////////////////////////////////////////////////////////////////////////////////
74 function getUserXXDir (name, createit) {
75 let dn = "";
76 try {
77 dn = Services.prefs.getCharPref(PREF_BRANCH+name);
78 } catch (e) {}
79 let res;
80 if (dn.length && dn[0] == '/') {
81 res = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
82 res.initWithPath(dn);
83 } else {
84 res = getProfileDir();
85 Cu.reportError("name=["+name+"]; dn=["+dn+"]");
86 if (dn.length) {
87 for (let d of dn.split("/")) if (d.length) res.append(d);
89 if (createit && !res.exists()) res.create(res.DIRECTORY_TYPE, 0750);
91 return res;
95 ////////////////////////////////////////////////////////////////////////////////
96 // get userjs directory
97 function getUserJSDir () { return getUserXXDir("jsdir", true); }
98 addExport("getUserJSDir", getUserJSDir);
100 function getUserDBDir () { return getUserXXDir("dbdir", true); }
101 addExport("getUserDBDir", getUserDBDir);
103 function getUserLibDir () { return getUserXXDir("libdir", true); }
104 addExport("getUserLibDir", getUserLibDir);
106 function getUserPkgDir () { return getUserXXDir("pkgdir", true); }
107 addExport("getUserPkgDir", getUserPkgDir);
110 ////////////////////////////////////////////////////////////////////////////////
111 function getGuerillaPref (name) {
112 if (typeof(name) == "string" && (name in PREFS)) {
113 try {
114 return Services.prefs.getBoolPref(PREF_BRANCH+name);
115 } catch (e) {}
116 return PREFS[name];
118 logError("guerilla: invalid preference request for '"+name+"'");
119 return null;
121 addExport("getGuerillaPref", getGuerillaPref);
124 ////////////////////////////////////////////////////////////////////////////////
125 debugOptions.debugMode = getGuerillaPref("debugMode");
126 debugOptions.debugCache = getGuerillaPref("debugCache");
127 debugOptions.logEnabled = getGuerillaPref("logEnabled");
128 debugOptions.openConsole = getGuerillaPref("openErrorConsoleOnStartup");
129 guerillaOptions.active = getGuerillaPref("active");