home *** CD-ROM | disk | FTP | other *** search
/ PC Home 138 / PC Home issue 138.iso / Software / Essentials / Netscape / browser.xpi / bin / components / nsSetPref.js < prev    next >
Encoding:
JavaScript  |  2002-07-11  |  7.1 KB  |  175 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  *
  15.  * The Initial Developer of the Original Code is
  16.  * Netscape Communications Corp.
  17.  * Portions created by the Initial Developer are Copyright (C) 2002
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Bill Law (law@netscape.com)
  22.  *  Bhuvan Racham (bhuvan@netscape.com)
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. /* This file implements the nsICmdLineHandler interface.  See nsICmdLineHandler.idl
  39.  * at http://lxr.mozilla.org/seamonkey/source/xpfe/appshell/public/nsICmdLineHandler.idl.
  40.  *
  41.  * This component handles the startup command line argument of the form:
  42.  *   -setPref pref1,pref2,pref3
  43.  * in order to change the value for every pref called via this commandline,
  44.  * corresponding implementation is need to added.
  45.  *
  46.  * The module is registered under the contractid
  47.  *   "@mozilla.org/commandlinehandler/general-startup;1?type=setPref"
  48.  *
  49.  * The implementation consists of a JavaScript "class" named nsSetPref,
  50.  * comprised of:
  51.  *   - a JS constructor function
  52.  *   - a prototype providing all the interface methods and implementation stuff
  53.  *
  54.  * In addition, this file implements an nsIModule object that registers the
  55.  * nsSetPref component.
  56.  */
  57.  
  58. /* ctor
  59.  */
  60. function nsSetPref() {
  61. }
  62.  
  63. nsSetPref.prototype = {
  64.  
  65.   // nsICmdLineHandler interface
  66.   get commandLineArgument() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  67.   get prefNameForStartup()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  68.   get chromeUrlForTask()    { 
  69.     try {
  70.       // We trust that this has been called during command-line handling during
  71.       // startup from nsAppRunner.cpp.
  72.  
  73.       // We get the command line service and from that the -setPref argument.
  74.       var cmdLine  = Components.classes[ "@mozilla.org/appshell/commandLineService;1" ]
  75.                        .getService( Components.interfaces.nsICmdLineService );
  76.       var prefList = cmdLine.getCmdLineValue( "-setPref" ).split( "," );
  77.  
  78.       // Get pref service.
  79.       var prefs    = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
  80.  
  81.       // For each pref specified on the cmd line, set the default pref value.
  82.       for ( i in prefList ) {
  83.         // Checking to see if we need to default browser.startup.homepage to 
  84.         // our default choice.
  85.         if (prefList[i] == "browser.startup.homepage") {
  86.           try {
  87.             var localizedHomePageUrl = prefs.getComplexValue("browser.default_homepage.url", 
  88.                                                 Components.interfaces.nsIPrefLocalizedString).data;
  89.             var str = Components.classes["@mozilla.org/supports-wstring;1"]
  90.                             .createInstance(Components.interfaces.nsISupportsWString);
  91.             str.data = localizedHomePageUrl;
  92.             prefs.setComplexValue(prefList[i],
  93.                             Components.interfaces.nsISupportsWString, str);
  94.           } catch( e ) {
  95.           }
  96.         }
  97.       }
  98.     } catch( e ) {
  99.     }
  100.  
  101.     // Return an error (so nsAppRunner doesn't think we've opened a window).
  102.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  103.   },
  104.   get helpText()            { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  105.   get handlesArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  106.   get defaultArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  107.   get openWindowWithArgs()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  108.  
  109.   // nsISupports interface
  110.  
  111.   // This "class" supports nsICmdLineHandler and nsISupports.
  112.   QueryInterface: function (iid) {
  113.     if (!iid.equals(Components.interfaces.nsICmdLineHandler) &&
  114.         !iid.equals(Components.interfaces.nsISupports)) {
  115.       throw Components.results.NS_ERROR_NO_INTERFACE;
  116.     }
  117.     return this;
  118.   },
  119.  
  120.   // This Component's module implementation.  All the code below is used to get this
  121.   // component registered and accessible via XPCOM.
  122.   module: {
  123.     // registerSelf: Register this component.
  124.     registerSelf: function (compMgr, fileSpec, location, type) {
  125.       compMgr = compMgr.QueryInterface( Components.interfaces.nsIComponentManagerObsolete );
  126.       compMgr.registerComponentWithType( this.cid,
  127.                                          "Pref Set Component",
  128.                                          this.contractId,
  129.                                          fileSpec,
  130.                                          location,
  131.                                          true,
  132.                                          true,
  133.                                          type );
  134.     },
  135.  
  136.     // getClassObject: Return this component's factory object.
  137.     getClassObject: function (compMgr, cid, iid) {
  138.       if (!cid.equals(this.cid))
  139.         throw Components.results.NS_ERROR_NO_INTERFACE;
  140.  
  141.       if (!iid.equals(Components.interfaces.nsIFactory))
  142.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  143.  
  144.       return this.factory;
  145.     },
  146.  
  147.     /* CID for this class */
  148.     cid: Components.ID("{15ABFAF7-AD4F-4450-899B-0373EE9FAD95}"),
  149.  
  150.     /* Contract ID for this class */
  151.     contractId: "@mozilla.org/commandlinehandler/general-startup;1?type=setPref",
  152.  
  153.     /* factory object */
  154.     factory: {
  155.       // createInstance: Return a new nsSetPref object.
  156.       createInstance: function (outer, iid) {
  157.         if (outer != null)
  158.           throw Components.results.NS_ERROR_NO_AGGREGATION;
  159.  
  160.         return (new nsSetPref()).QueryInterface(iid);
  161.       }
  162.     },
  163.  
  164.     // canUnload: n/a (returns true)
  165.     canUnload: function(compMgr) {
  166.       return true;
  167.     }
  168.   }
  169. }
  170.  
  171. // NSGetModule: Return the nsIModule object.
  172. function NSGetModule(compMgr, fileSpec) {
  173.   return nsSetPref.prototype.module;
  174. }
  175.