From 691993a6c3a5cecf27ae4afffddfbae6f55e92d0 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Sun, 25 Jun 2006 07:31:01 +0000 Subject: [PATCH] Added Scholar.Prefs, a front-end to the preferences service with simple get(pref) and set(pref, value) methods that will retrieve and set based on the type of the default value, which I believe should generally should fine -- for more fine-grained control, use Scholar.Prefs.prefBranch to access the branch directly Scholar.Prefs also registers itself as a preferences observer and can be used to trigger actions when certain prefs are changed by editing the switch statement in the observe() method Updated preferences.js to use Scholar.Prefs --- .../content/scholar/preferences.js | 9 +- .../content/scholar/xpcom/scholar.js | 96 +++++++++++++++++++ 2 files changed, 101 insertions(+), 4 deletions(-) diff --git a/chrome/chromeFiles/content/scholar/preferences.js b/chrome/chromeFiles/content/scholar/preferences.js index f5e4ad1c3..c277b8b3a 100644 --- a/chrome/chromeFiles/content/scholar/preferences.js +++ b/chrome/chromeFiles/content/scholar/preferences.js @@ -1,5 +1,3 @@ -var prefManager = Components.classes["@mozilla.org/preferences-service;1"] - .getService(Components.interfaces.nsIPrefBranch); var autoUpdateBox; /* @@ -10,16 +8,19 @@ var autoUpdateBox; b) add lines to init() function c) add line to accept() function 3) add a control to prefs.xul + 4) (Optional) To add an observer for a preference change, + add an appropriate case in the switch statement + in Scholar.Prefs.observe() */ function init() { autoUpdateBox = document.getElementById('autoUpdateBox'); - autoUpdateBox.checked = prefManager.getBoolPref('extensions.scholar.automaticScraperUpdates'); + autoUpdateBox.checked = Scholar.Prefs.get('automaticScraperUpdates'); } function accept() { - prefManager.setBoolPref('extensions.scholar.automaticScraperUpdates', autoUpdateBox.checked); + Scholar.Prefs.set('automaticScraperUpdates', autoUpdateBox.checked) } \ No newline at end of file diff --git a/chrome/chromeFiles/content/scholar/xpcom/scholar.js b/chrome/chromeFiles/content/scholar/xpcom/scholar.js index af3a01ac0..e7af2cac5 100644 --- a/chrome/chromeFiles/content/scholar/xpcom/scholar.js +++ b/chrome/chromeFiles/content/scholar/xpcom/scholar.js @@ -39,6 +39,9 @@ var Scholar = new function(){ return false; } + // Load in the preferences branch for the extension + Scholar.Prefs.init(); + // Load in the extension version from the extension manager var nsIUpdateItem = Components.interfaces.nsIUpdateItem; var gExtensionManager = @@ -281,6 +284,99 @@ var Scholar = new function(){ +Scholar.Prefs = new function(){ + // Privileged methods + this.init = init; + this.get = get; + this.set = set; + + this.register = register; + this.unregister = unregister; + this.observe = observe; + + // Public properties + this.prefBranch; // set in Scholar.init() + + function init(){ + var prefs = Components.classes["@mozilla.org/preferences-service;1"] + .getService(Components.interfaces.nsIPrefService); + this.prefBranch = prefs.getBranch("extensions.scholar."); + + // Register observer to handle pref changes + this.register(); + } + + + /** + * Retrieve a preference + **/ + function get(pref){ + try { + switch (this.prefBranch.getPrefType(pref)){ + case this.prefBranch.PREF_BOOL: + return this.prefBranch.getBoolPref(pref); + case this.prefBranch.PREF_STRING: + return this.prefBranch.getCharPref(pref); + case this.prefBranch.PREF_INT: + return this.prefBranch.getIntPref(pref); + } + } + catch (e){ + throw ("Invalid preference '" + pref + "'"); + } + } + + + /** + * Set a preference + **/ + function set(pref, value){ + try { + switch (this.prefBranch.getPrefType(pref)){ + case this.prefBranch.PREF_BOOL: + return this.prefBranch.setBoolPref(pref, value); + case this.prefBranch.PREF_STRING: + return this.prefBranch.setCharPref(pref, value); + case this.prefBranch.PREF_INT: + return this.prefBranch.setIntPref(pref, value); + } + } + catch (e){ + throw ("Invalid preference '" + pref + "'"); + } + } + + + // + // Methods to register a preferences observer + // + function register(){ + this.prefBranch.QueryInterface(Components.interfaces.nsIPrefBranch2); + this.prefBranch.addObserver("", this, false); + } + + function unregister(){ + if (!this.prefBranch){ + return; + } + this.prefBranch.removeObserver("", this); + } + + function observe(subject, topic, data){ + if(topic!="nsPref:changed"){ + return; + } + // subject is the nsIPrefBranch we're observing (after appropriate QI) + // data is the name of the pref that's been changed (relative to subject) + switch (data){ + case "pref1": + // pref1 changed + break; + } + } +} + + /** * Class for creating hash arrays that behave a bit more sanely