Friday, July 20, 2007

XPCOM interfaces for preferences system -setup proxy

Mozilla exposes its preferences system through a few XPCOM interfaces.
Look in the Resources section below for the link to list of preferences-related interfaces.
Three used interfaces are nsIPrefService, nsIPrefBranch and nsIPrefBranch2.
They are frozen and will not change.
It's worth noting that there also is an nsIPref interface.
Despite it being used in some places, it is deprecated and should not be used.
The preferences service is instantiated in the same way you instantiate any XPCOM service; see the article Creating XPCOM Components at XULPlanet for details.)
To get an nsIPrefBranch, either QueryInterface() the pref service (that will give you the root branch) or call nsIPrefService.getBranch() to get a sub-branch.
Here are two examples:
// Get the root branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

// Get the "extensions.myext." branch
var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("extensions.myext.");

to setup proxy, try this ..
var prefs= Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
prefs = prefs.getBranch("network.proxy.");
prefs.setIntPref("type",1);
prefs.setCharPref("http","localhost");
prefs.setIntPref("http_port",3128);

No comments: