home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / phoenx05.zip / phoenix / components / nsResetPref.js < prev    next >
Text File  |  2002-12-10  |  7KB  |  177 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.  * The Original Code is Mozilla Pref Resetter.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Netscape Communications Corp.
  18.  * Portions created by the Initial Developer are Copyright (C) 2002
  19.  * the Initial Developer. All Rights Reserved.
  20.  *
  21.  * Contributor(s):
  22.  *   Bill Law  <law@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.  *   -resetPref pref1,pref2,pref3
  43.  * by resetting the user prefs pref1, pref2, and pref3 (an arbitrary list of pref names
  44.  * separated by commas).
  45.  *
  46.  * The module is registered under the contractid
  47.  *   "@mozilla.org/commandlinehandler/general-startup;1?type=resetPref"
  48.  *
  49.  * The implementation consists of a JavaScript "class" named nsResetPref,
  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.  * nsResetPref component.
  56.  */
  57.  
  58. /* ctor
  59.  */
  60. function nsResetPref() {
  61. }
  62.  
  63. nsResetPref.prototype = {
  64.     // Turn this on to get debugging messages.
  65.     debug: false,
  66.  
  67.     // nsICmdLineHandler interface
  68.     get commandLineArgument() { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  69.     get prefNameForStartup()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  70.     get chromeUrlForTask()    { 
  71.         try {
  72.             // We trust that this has been called during command-line handling during
  73.             // startup from nsAppRunner.cpp.
  74.  
  75.             // We get the command line service and from that the -resetPref argument.
  76.             var cmdLine  = Components.classes[ "@mozilla.org/appshell/commandLineService;1" ]
  77.                              .getService( Components.interfaces.nsICmdLineService );
  78.             var prefList = cmdLine.getCmdLineValue( "-resetPref" ).split( "," );
  79.  
  80.             // Get pref service.
  81.             var prefs    = Components.classes[ "@mozilla.org/preferences-service;1" ]
  82.                              .getService( Components.interfaces.nsIPrefService );
  83.  
  84.             // For each pref specified on the cmd line, reset the user pref value.
  85.             for ( i in prefList ) {
  86.                 var pref = prefs.getBranch( prefList[ i ] );
  87.                 try {
  88.                     pref.clearUserPref( "" );
  89.                 } catch( e ) {
  90.                 }
  91.             }
  92.         } catch( e ) {
  93.             this.dump( "exception: " + e );
  94.         }
  95.  
  96.         // Return an error (so nsAppRunner doesn't think we've opened a window).
  97.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  98.     },
  99.     get helpText()            { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; },
  100.     get handlesArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  101.     get defaultArgs()         { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  102.     get openWindowWithArgs()  { throw Components.results.NS_ERROR_NOT_IMPLEMENTED; }, 
  103.  
  104.     // nsISupports interface
  105.  
  106.     // This "class" supports nsICmdLineHandler and nsISupports.
  107.     QueryInterface: function (iid) {
  108.         if (!iid.equals(Components.interfaces.nsICmdLineHandler) &&
  109.             !iid.equals(Components.interfaces.nsISupports)) {
  110.             throw Components.results.NS_ERROR_NO_INTERFACE;
  111.         }
  112.         return this;
  113.     },
  114.  
  115.     // Dump text (if debug is on).
  116.     dump: function( text ) {
  117.         if ( this.debug ) {
  118.             dump( "nsResetPref: " + text + "\n" );
  119.         }
  120.     },
  121.  
  122.     // This Component's module implementation.  All the code below is used to get this
  123.     // component registered and accessible via XPCOM.
  124.     module: {
  125.         // registerSelf: Register this component.
  126.         registerSelf: function (compMgr, fileSpec, location, type) {
  127.             compMgr = compMgr.QueryInterface( Components.interfaces.nsIComponentManagerObsolete );
  128.             compMgr.registerComponentWithType( this.cid,
  129.                                                "Pref Reset Component",
  130.                                                this.contractId,
  131.                                                fileSpec,
  132.                                                location,
  133.                                                true,
  134.                                                true,
  135.                                                type );
  136.         },
  137.     
  138.         // getClassObject: Return this component's factory object.
  139.         getClassObject: function (compMgr, cid, iid) {
  140.             if (!cid.equals(this.cid))
  141.                 throw Components.results.NS_ERROR_NO_INTERFACE;
  142.     
  143.             if (!iid.equals(Components.interfaces.nsIFactory))
  144.                 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  145.     
  146.             return this.factory;
  147.         },
  148.     
  149.         /* CID for this class */
  150.         cid: Components.ID("{15ABFAF7-AD4F-4450-899B-0373EE9FAD95}"),
  151.     
  152.         /* Contract ID for this class */
  153.         contractId: "@mozilla.org/commandlinehandler/general-startup;1?type=resetPref",
  154.     
  155.         /* factory object */
  156.         factory: {
  157.             // createInstance: Return a new nsResetPref object.
  158.             createInstance: function (outer, iid) {
  159.                 if (outer != null)
  160.                     throw Components.results.NS_ERROR_NO_AGGREGATION;
  161.     
  162.                 return (new nsResetPref()).QueryInterface(iid);
  163.             }
  164.         },
  165.     
  166.         // canUnload: n/a (returns true)
  167.         canUnload: function(compMgr) {
  168.             return true;
  169.         }
  170.     }
  171. }
  172.  
  173. // NSGetModule: Return the nsIModule object.
  174. function NSGetModule(compMgr, fileSpec) {
  175.     return nsResetPref.prototype.module;
  176. }
  177.