From 8fb65cf8aeaaf66636698310c0168929fc1a2ce0 Mon Sep 17 00:00:00 2001 From: Einar Egilsson Date: Thu, 1 Sep 2011 21:33:28 +0200 Subject: Better pref handling --- chrome/code/browserOverlay.xul.js | 2 +- chrome/code/redirectorprefs.js | 72 +++++++++++++++------------------------ 2 files changed, 29 insertions(+), 45 deletions(-) (limited to 'chrome/code') diff --git a/chrome/code/browserOverlay.xul.js b/chrome/code/browserOverlay.xul.js index 146e073..20ff5c6 100644 --- a/chrome/code/browserOverlay.xul.js +++ b/chrome/code/browserOverlay.xul.js @@ -16,7 +16,7 @@ var RedirectorOverlay = { this.strings = document.getElementById("redirector-strings"); this.prefs = new RedirectorPrefs(); this.changedPrefs(this.prefs); - this.prefs.addListener(this); + this.prefs.addListener(this); document.addEventListener('keypress', function(event) { if ((event.charCode == 114) && event.altKey) { //alt+r RedirectorOverlay.toggleEnabled(); diff --git a/chrome/code/redirectorprefs.js b/chrome/code/redirectorprefs.js index 40e8737..75ef936 100644 --- a/chrome/code/redirectorprefs.js +++ b/chrome/code/redirectorprefs.js @@ -6,42 +6,7 @@ function RedirectorPrefs() { RedirectorPrefs.prototype = { - //Preferences: - _version : null, - _enabled : null, - _showStatusBarIcon : null, - _showContextMenu : null, - _debugEnabled : null, - _defaultDir : null, - _redirects : null, - _proxyServerPort : null, - _prefBranch : null, - _listeners : null, - - //Preferences props - - get version() { return this._version; }, - set version(value) { this._prefBranch.setCharPref('version', value); }, - - get enabled() { return this._enabled; }, - set enabled(value) { this._prefBranch.setBoolPref('enabled', value); }, - - get showStatusBarIcon() { return this._showStatusBarIcon; }, - set showStatusBarIcon(value) { this._prefBranch.setBoolPref('showStatusBarIcon', value); }, - - get showContextMenu() { return this._showContextMenu; }, - set showContextMenu(value) { this._prefBranch.setBoolPref('showContextMenu', value); }, - - get debugEnabled() { return this._debugEnabled; }, - set debugEnabled(value) { this._prefBranch.setBoolPref('debugEnabled', value); }, - - get proxyServerPort() { return this._proxyServerPort; }, - set proxyServerPort(value) { this._prefBranch.setIntPref('proxyServerPort', value); }, - - get defaultDir() { return this._defaultDir; }, - set defaultDir(value) { this._prefBranch.setCharPref('defaultDir', value); }, - init : function() { this._prefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("extensions.redirector."); this.reload(); @@ -54,13 +19,32 @@ RedirectorPrefs.prototype = { this.service.removeObserver('extensions.redirector', this); }, - reload : function() { - this._version = this._prefBranch.getCharPref('version'); - this._enabled = this._prefBranch.getBoolPref('enabled'); - this._showStatusBarIcon = this._prefBranch.getBoolPref('showStatusBarIcon'); - this._showContextMenu = this._prefBranch.getBoolPref('showContextMenu'); - this._debugEnabled = this._prefBranch.getBoolPref('debugEnabled'); - this._defaultDir = this._prefBranch.getCharPref('defaultDir'); + reload : function(addWatch) { + var b = this._prefBranch; + for each (var name in b.getChildList('')){ + this.unwatch(name); + var type = b.getPrefType(name); + if (type == b.PREF_STRING) { + this[name] = b.getCharPref(name); + } else if (type == b.PREF_INT) { + this[name] = b.getIntPref(name); + } else if (type == b.PREF_BOOL) { + this[name] = b.getBoolPref(name); + } + + this.watch(name, function(id,oldval,newval) { + var type = b.getPrefType(id); + if (type == b.PREF_STRING) { + b.setCharPref(id,newval); + } else if (type == b.PREF_INT) { + b.setIntPref(id, newval); + } else if (type == b.PREF_BOOL) { + dump(id+ ' ' + newval) + b.setBoolPref(id, newval); + } + return newval; + }); + } }, get service() { @@ -71,7 +55,7 @@ RedirectorPrefs.prototype = { if (topic != 'nsPref:changed') { return; } - this.reload(); + this.reload(false); for each (var listener in this._listeners) { listener && listener.changedPrefs && listener.changedPrefs(this); } @@ -83,8 +67,8 @@ RedirectorPrefs.prototype = { removeListener : function(listener) { for (var i = 0; i < this._listeners.length; i++) { - if (this._listeners[i] == listener) { this._listeners.splice(i,1); + if (this._listeners[i] == listener) { return; } } -- cgit v1.2.3-70-g09d2