2009-02-12 20:41:15 -05:00
|
|
|
/*
|
2009-04-06 22:38:07 -04:00
|
|
|
Preferences Library
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
var value = window['piro.sakura.ne.jp'].prefs.getPref('my.extension.pref');
|
|
|
|
window['piro.sakura.ne.jp'].prefs.setPref('my.extension.pref', true);
|
|
|
|
window['piro.sakura.ne.jp'].prefs.clearPref('my.extension.pref');
|
|
|
|
var listener = {
|
|
|
|
domains : [
|
|
|
|
'browser.tabs',
|
|
|
|
'extensions.someextension'
|
|
|
|
],
|
|
|
|
observe : function(aSubject, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic != 'nsPref:changed') return;
|
|
|
|
var value = window['piro.sakura.ne.jp'].prefs.getPref(aData);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
window['piro.sakura.ne.jp'].prefs.addPrefListener(listener);
|
|
|
|
window['piro.sakura.ne.jp'].prefs.removePrefListener(listener);
|
|
|
|
|
2013-05-29 10:04:40 -04:00
|
|
|
license: The MIT License, Copyright (c) 2009-2013 YUKI "Piro" Hiroshi
|
2009-02-12 20:41:15 -05:00
|
|
|
original:
|
2013-05-29 10:04:40 -04:00
|
|
|
http://github.com/piroor/fxaddonlib-prefs
|
2009-02-12 20:41:15 -05:00
|
|
|
*/
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-08-26 11:40:13 -04:00
|
|
|
/* To work as a JS Code Module */
|
2010-08-27 05:58:57 -04:00
|
|
|
if (typeof window == 'undefined' ||
|
|
|
|
(window && typeof window.constructor == 'function')) {
|
2010-06-22 21:32:31 -04:00
|
|
|
this.EXPORTED_SYMBOLS = ['prefs'];
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-26 11:13:11 -04:00
|
|
|
// If namespace.jsm is available, export symbols to the shared namespace.
|
2013-05-29 10:04:40 -04:00
|
|
|
// See: http://github.com/piroor/fxaddonlibs/blob/master/namespace.jsm
|
2010-06-26 11:13:11 -04:00
|
|
|
try {
|
|
|
|
let ns = {};
|
2013-05-29 10:06:11 -04:00
|
|
|
Components.utils.import('resource://treestyletab-modules/lib/namespace.jsm', ns);
|
2010-06-26 11:13:11 -04:00
|
|
|
/* var */ window = ns.getNamespaceFor('piro.sakura.ne.jp');
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
window = {};
|
|
|
|
}
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
(function() {
|
2013-05-29 10:04:40 -04:00
|
|
|
const currentRevision = 9;
|
2009-02-12 20:41:15 -05:00
|
|
|
|
|
|
|
if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {};
|
|
|
|
|
|
|
|
var loadedRevision = 'prefs' in window['piro.sakura.ne.jp'] ?
|
|
|
|
window['piro.sakura.ne.jp'].prefs.revision :
|
|
|
|
0 ;
|
|
|
|
if (loadedRevision && loadedRevision > currentRevision) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
window['piro.sakura.ne.jp'].prefs = {
|
|
|
|
revision : currentRevision,
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
Prefs : Cc['@mozilla.org/preferences;1']
|
|
|
|
.getService(Ci.nsIPrefBranch)
|
|
|
|
.QueryInterface(Ci.nsIPrefBranch2),
|
2009-07-07 20:09:13 -04:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
DefaultPrefs : Cc['@mozilla.org/preferences-service;1']
|
|
|
|
.getService(Ci.nsIPrefService)
|
|
|
|
.getDefaultBranch(null),
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2010-05-06 22:10:54 -04:00
|
|
|
getPref : function(aPrefstring, aInterface, aBranch)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
2010-05-06 22:10:54 -04:00
|
|
|
if (!aInterface || aInterface instanceof Ci.nsIPrefBranch)
|
|
|
|
[aBranch, aInterface] = [aInterface, aBranch];
|
|
|
|
|
|
|
|
aBranch = aBranch || this.Prefs;
|
|
|
|
|
2013-05-29 10:04:40 -04:00
|
|
|
var type = aBranch.getPrefType(aPrefstring);
|
|
|
|
if (type == aBranch.PREF_INVALID)
|
|
|
|
return null;
|
|
|
|
|
2010-05-06 22:10:54 -04:00
|
|
|
if (aInterface)
|
2013-05-29 10:04:40 -04:00
|
|
|
return aBranch.getComplexValue(aPrefstring, aInterface);
|
2010-05-06 22:10:54 -04:00
|
|
|
|
2013-05-29 10:04:40 -04:00
|
|
|
switch (type)
|
2010-01-24 23:36:08 -05:00
|
|
|
{
|
|
|
|
case aBranch.PREF_STRING:
|
|
|
|
return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring)));
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
case aBranch.PREF_INT:
|
|
|
|
return aBranch.getIntPref(aPrefstring);
|
|
|
|
|
|
|
|
case aBranch.PREF_BOOL:
|
|
|
|
return aBranch.getBoolPref(aPrefstring);
|
|
|
|
|
|
|
|
case aBranch.PREF_INVALID:
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2009-02-12 20:41:15 -05:00
|
|
|
},
|
2009-07-07 20:09:13 -04:00
|
|
|
|
2010-05-06 22:10:54 -04:00
|
|
|
getDefaultPref : function(aPrefstring, aInterface)
|
2009-07-07 20:09:13 -04:00
|
|
|
{
|
2010-05-06 22:10:54 -04:00
|
|
|
return this.getPref(aPrefstring, this.DefaultPrefs, aInterface);
|
2009-07-07 20:09:13 -04:00
|
|
|
},
|
2009-02-12 20:41:15 -05:00
|
|
|
|
2009-07-07 20:09:13 -04:00
|
|
|
setPref : function(aPrefstring, aNewValue, aBranch)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
2010-05-06 22:10:54 -04:00
|
|
|
aBranch = aBranch || this.Prefs;
|
2010-01-24 23:36:08 -05:00
|
|
|
switch (typeof aNewValue)
|
2009-02-12 20:41:15 -05:00
|
|
|
{
|
|
|
|
case 'string':
|
2010-01-24 23:36:08 -05:00
|
|
|
return aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue)));
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
case 'number':
|
2010-01-24 23:36:08 -05:00
|
|
|
return aBranch.setIntPref(aPrefstring, parseInt(aNewValue));
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
default:
|
2010-07-02 07:03:47 -04:00
|
|
|
return aBranch.setBoolPref(aPrefstring, !!aNewValue);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
},
|
2009-07-07 20:09:13 -04:00
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
setDefaultPref : function(aPrefstring, aNewValue)
|
2009-07-07 20:09:13 -04:00
|
|
|
{
|
|
|
|
return this.setPref(aPrefstring, aNewValue, this.DefaultPrefs);
|
|
|
|
},
|
2009-02-12 20:41:15 -05:00
|
|
|
|
|
|
|
clearPref : function(aPrefstring)
|
|
|
|
{
|
2010-01-24 23:36:08 -05:00
|
|
|
if (this.Prefs.prefHasUserValue(aPrefstring))
|
2009-02-12 20:41:15 -05:00
|
|
|
this.Prefs.clearUserPref(aPrefstring);
|
|
|
|
},
|
|
|
|
|
2010-05-06 22:10:54 -04:00
|
|
|
getDescendant : function(aRoot, aBranch)
|
|
|
|
{
|
|
|
|
aBranch = aBranch || this.Prefs;
|
|
|
|
return aBranch.getChildList(aRoot, {}).sort();
|
|
|
|
},
|
|
|
|
|
|
|
|
getChildren : function(aRoot, aBranch)
|
|
|
|
{
|
2013-05-29 10:04:40 -04:00
|
|
|
var foundChildren = {};
|
|
|
|
var possibleChildren = [];
|
|
|
|
var actualChildren = this.getDescendant(aRoot, aBranch)
|
|
|
|
.forEach(function(aPrefstring) {
|
2010-05-06 22:10:54 -04:00
|
|
|
var name = aPrefstring.replace(aRoot, '');
|
|
|
|
if (name.charAt(0) == '.')
|
|
|
|
name = name.substring(1);
|
2013-05-29 10:04:40 -04:00
|
|
|
if (name.indexOf('.') < 0) {
|
|
|
|
if (!(aPrefstring in foundChildren)) {
|
|
|
|
actualChildren.push(aPrefstring);
|
|
|
|
foundChildren[aPrefstring] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
let possibleChildKey = aRoot + name.split('.')[0];
|
|
|
|
if (possibleChildKey && !(possibleChildKey in foundChildren)) {
|
|
|
|
possibleChildren.push(possibleChildKey);
|
|
|
|
foundChildren[possibleChildKey] = true;
|
|
|
|
}
|
|
|
|
}
|
2010-05-06 22:10:54 -04:00
|
|
|
});
|
2013-05-29 10:04:40 -04:00
|
|
|
return possibleChildren.concat(actualChildren).sort();
|
2010-05-06 22:10:54 -04:00
|
|
|
},
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
addPrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
2010-01-24 23:36:08 -05:00
|
|
|
for each (var domain in domains)
|
|
|
|
this.Prefs.addObserver(domain, aObserver, false);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
removePrefListener : function(aObserver)
|
|
|
|
{
|
|
|
|
var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ;
|
|
|
|
try {
|
2010-01-24 23:36:08 -05:00
|
|
|
for each (var domain in domains)
|
|
|
|
this.Prefs.removeObserver(domain, aObserver, false);
|
2009-02-12 20:41:15 -05:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})();
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-22 21:40:10 -04:00
|
|
|
if (window != this) { // work as a JS Code Module
|
|
|
|
this.prefs = window['piro.sakura.ne.jp'].prefs;
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|