diff --git a/modules/prefs.js b/modules/prefs.js index 399745ad..ae263f80 100644 --- a/modules/prefs.js +++ b/modules/prefs.js @@ -22,13 +22,14 @@ var window = {}; window['piro.sakura.ne.jp'].prefs.addPrefListener(listener); window['piro.sakura.ne.jp'].prefs.removePrefListener(listener); - lisence: The MIT License, Copyright (c) 2009 SHIMODA "Piro" Hiroshi + lisence: The MIT License, Copyright (c) 2009-2010 SHIMODA "Piro" Hiroshi http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt original: http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.js + http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/prefs.test.js */ (function() { - const currentRevision = 2; + const currentRevision = 4; if (!('piro.sakura.ne.jp' in window)) window['piro.sakura.ne.jp'] = {}; @@ -39,48 +40,38 @@ var window = {}; return; } + const Cc = Components.classes; + const Ci = Components.interfaces; + window['piro.sakura.ne.jp'].prefs = { revision : currentRevision, - get Prefs() - { - if (!this._Prefs) { - this._Prefs = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch); - } - return this._Prefs; - }, - _Prefs : null, + Prefs : Cc['@mozilla.org/preferences;1'] + .getService(Ci.nsIPrefBranch) + .QueryInterface(Ci.nsIPrefBranch2), - get DefaultPrefs() - { - if (!this._DefaultPrefs) { - this._DefaultPrefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefService).getDefaultBranch(null); - } - return this._DefaultPrefs; - }, - _DefaultPrefs : null, + DefaultPrefs : Cc['@mozilla.org/preferences-service;1'] + .getService(Ci.nsIPrefService) + .getDefaultBranch(null), getPref : function(aPrefstring, aBranch) { if (!aBranch) aBranch = this.Prefs; - try { - switch (aBranch.getPrefType(aPrefstring)) - { - case this.Prefs.PREF_STRING: - return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring))); - break; - case this.Prefs.PREF_INT: - return aBranch.getIntPref(aPrefstring); - break; - default: - return aBranch.getBoolPref(aPrefstring); - break; - } - } - catch(e) { - } + switch (aBranch.getPrefType(aPrefstring)) + { + case aBranch.PREF_STRING: + return decodeURIComponent(escape(aBranch.getCharPref(aPrefstring))); - return null; + case aBranch.PREF_INT: + return aBranch.getIntPref(aPrefstring); + + case aBranch.PREF_BOOL: + return aBranch.getBoolPref(aPrefstring); + + case aBranch.PREF_INVALID: + default: + return null; + } }, getDefaultPref : function(aPrefstring) @@ -91,53 +82,36 @@ var window = {}; setPref : function(aPrefstring, aNewValue, aBranch) { if (!aBranch) aBranch = this.Prefs; - - var type; - try { - type = typeof aNewValue; - } - catch(e) { - type = null; - } - - switch (type) + switch (typeof aNewValue) { case 'string': - aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue))); - break; + return aBranch.setCharPref(aPrefstring, unescape(encodeURIComponent(aNewValue))); + case 'number': - aBranch.setIntPref(aPrefstring, parseInt(aNewValue)); - break; + return aBranch.setIntPref(aPrefstring, parseInt(aNewValue)); + default: - aBranch.setBoolPref(aPrefstring, aNewValue); - break; + return aBranch.setBoolPref(aPrefstring, aNewValue); } - return true; }, - setDefaultPref : function(aPrefstring) + setDefaultPref : function(aPrefstring, aNewValue) { return this.setPref(aPrefstring, aNewValue, this.DefaultPrefs); }, clearPref : function(aPrefstring) { - try { + if (this.Prefs.prefHasUserValue(aPrefstring)) this.Prefs.clearUserPref(aPrefstring); - } - catch(e) { - } - - return; }, addPrefListener : function(aObserver) { var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ; try { - var pbi = this.Prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal); - for (var i = 0; i < domains.length; i++) - pbi.addObserver(domains[i], aObserver, false); + for each (var domain in domains) + this.Prefs.addObserver(domain, aObserver, false); } catch(e) { } @@ -147,9 +121,8 @@ var window = {}; { var domains = ('domains' in aObserver) ? aObserver.domains : [aObserver.domain] ; try { - var pbi = this.Prefs.QueryInterface(Components.interfaces.nsIPrefBranchInternal); - for (var i = 0; i < domains.length; i++) - pbi.removeObserver(domains[i], aObserver, false); + for each (var domain in domains) + this.Prefs.removeObserver(domain, aObserver, false); } catch(e) { }