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);
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
lisence: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi
|
2009-02-12 20:41:15 -05:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
|
|
|
|
original:
|
2009-04-06 22:38:07 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.js
|
2010-01-24 23:36:08 -05:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.test.js
|
2009-02-12 20:41:15 -05:00
|
|
|
*/
|
2010-06-22 19:46:51 -04:00
|
|
|
|
2010-06-22 21:32:31 -04:00
|
|
|
/* To work as a JS Code Module (*require namespace.jsm)
|
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/namespace.jsm */
|
|
|
|
if (typeof window == 'undefined') {
|
|
|
|
this.EXPORTED_SYMBOLS = ['prefs'];
|
2010-06-22 19:46:51 -04:00
|
|
|
|
|
|
|
let ns = {};
|
|
|
|
Components.utils.import('resource://treestyletab-modules/namespace.jsm', ns);
|
2010-06-22 21:32:31 -04:00
|
|
|
/* var */ window = ns.getNamespaceFor('piro.sakura.ne.jp');
|
2010-06-22 19:46:51 -04:00
|
|
|
}
|
|
|
|
|
2009-02-12 20:41:15 -05:00
|
|
|
(function() {
|
2010-05-06 22:10:54 -04:00
|
|
|
const currentRevision = 6;
|
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;
|
|
|
|
|
|
|
|
if (aInterface)
|
|
|
|
return (aBranch.getPrefType(aPrefstring) == aBranch.PREF_INVALID) ?
|
|
|
|
null :
|
|
|
|
aBranch.getComplexValue(aPrefstring, aInterface);
|
|
|
|
|
2010-01-24 23:36:08 -05:00
|
|
|
switch (aBranch.getPrefType(aPrefstring))
|
|
|
|
{
|
|
|
|
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-01-24 23:36:08 -05: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)
|
|
|
|
{
|
|
|
|
return this.getDescendant(aRoot, aBranch)
|
|
|
|
.filter(function(aPrefstring) {
|
|
|
|
var name = aPrefstring.replace(aRoot, '');
|
|
|
|
if (name.charAt(0) == '.')
|
|
|
|
name = name.substring(1);
|
|
|
|
return name.indexOf('.') < 0;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
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
|
|
|
}
|