[Project_owners] Apply Options dialog changes

HJ bugus at universum.org
Tue Nov 30 01:31:20 EST 2004


Ben Bucksch wrote:
> HJ wrote:
> 
>> Chris Means wrote:
>>
>>> I'm finishing up an extension that updates the status bar with a users'
>>> current SETI @ Home statistics.
>>
>>
> Where's your project hosted (i.e. URL)?
> 
>> I'm not sure what you want/need, but this might help:
>>
>> // Setup observer
>> observerService = 
>> Components.classes["@mozilla.org/observer-service;1"].getService(Components.interfaces.nsIObserverService); 
>>
>> observerService.addObserver(setiObserver, "updateSETI", false);
>>
>> // Trigger update (should be used in Options dialog)
>> observerService.notifyObservers(null, "updateSETI", "update");
> 
> 
> There's a special observer for pref changes (assuming your options are 
> stored in Mozilla's pref.js), see e.g. nsHttpHandler::Init() in 
> netwerk/protocols/http/. Not sure why they use (the internal) 
> nsIPrefBranchInternal, though.
> 
> Ben

Yes indeed, so this info might be useful:

Adding a pref observer can be done with: addPrefListener(setiPrefListener);
and removing with: removePrefListener(setiPrefListener);
and the prefListener could be something like:

const setiPrefListener =
{
   domains: [ "seti.pref.string-1",
              "seti.pref.string-2" ],

   observe: function(subject, topic, aPrefString) {
     // Is this a pref change?
     if (topic != "nsPref:changed")
       return;

     var prefValue;
     var prefType = pref.getPrefType(aPrefString);
     var prefModified = prefHasUserValue(aPrefString);

     switch(prefType) {
       case nsIPrefBranch.PREF_STRING: // 32
            prefValue = pref.getComplexValue(aPrefString).data;
            break;
       case nsIPrefBranch.PREF_INT: // 64
            prefValue = pref.getIntPref(aPrefString);
            break;
       case nsIPrefBranch.PREF_BOOL: // 128
            prefValue = pref.getBoolPref(aPrefString);
            break;
       default: return; // PREF_INVALID; // 0
     }

     if (aPrefString == this.domains[0]) {
       // do whatever is needed here
     }
     else if (aPrefString == this.domains[1]) {
       // do whatever is needed here
     }
   }
};

and a possible replacement for function addPrefListener could be 
something like:

function addPrefListener(observer)
{
   var domains = ("domains" in observer) ? observer.domains : 
[observer.domain];
   try {
     var prefBranchInternal = 
pref.QueryInterface(Components.interfaces.nsIPrefBranchInternal);
     for (var i = 0; i < domains.length; i++)
       prefBranchInternal.addObserver(domains[i], observer, false);
   } catch(ex) {
     dump("Failed to observe pref: " + domains[i] + "\n");
   }
}

because this function is capable of adding a set of observers in one run.

See also: 
http://lxr.mozilla.org/seamonkey/source/modules/libpref/public/nsIPrefBranchInternal.idl
http://lxr.mozilla.org/seamonkey/source/modules/libpref/public/nsIPrefBranch.idl
for more info.

/HJ


More information about the Project_owners mailing list