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
This commit is contained in:
parent
b679bc6327
commit
691993a6c3
2 changed files with 101 additions and 4 deletions
|
@ -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)
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue