home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 January / 01_03.iso / software / ghostzilla_hit / files / ghostzilla-1.0-plus-install.exe / components / chatzilla-service.js < prev    next >
Encoding:
Text File  |  2002-05-12  |  10.9 KB  |  362 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1999 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): 
  20.  * Seth Spitzer <sspitzer@netscape.com>
  21.  * Robert Ginda <rginda@netscape.com>
  22.  */
  23.  
  24. /*
  25.  * This file contains the following chatzilla related components:
  26.  * 1. Command line handler service, for responding to the -chat command line
  27.  *    option. (CLineHandler)
  28.  * 2. Content handler for responding to content of type x-application-irc
  29.  *    (IRCContentHandler)
  30.  * 3. Protocol handler for supplying a channel to the browser when an irc://
  31.  *    link is clicked. (IRCProtocolHandler)
  32.  * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
  33.  *    that irc:// links have the content type x-application-irc (BogusChannel)
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CONTRACTID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=chat";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{38a95514-1dd2-11b2-97e7-9da958640f2c}");
  41. const IRCCNT_HANDLER_CONTRACTID =
  42.     "@mozilla.org/uriloader/content-handler;1?type=x-application-irc";
  43. const IRCCNT_HANDLER_CID =
  44.     Components.ID("{98919a14-1dd1-11b2-be1a-b84344307f0a}");
  45. const IRCPROT_HANDLER_CONTRACTID =
  46.     "@mozilla.org/network/protocol;1?name=irc";
  47. const IRCPROT_HANDLER_CID =
  48.     Components.ID("{f21c35f4-1dd1-11b2-a503-9bf8a539ea39}");
  49.  
  50. /* components used in this file */
  51. const MEDIATOR_CONTRACTID =
  52.     "@mozilla.org/rdf/datasource;1?name=window-mediator";
  53. const STANDARDURL_CONTRACTID = 
  54.     "@mozilla.org/network/standard-url;1";
  55. const ASS_CONTRACTID =
  56.     "@mozilla.org/appshell/appShellService;1";
  57.  
  58. /* interafces used in this file */
  59. const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
  60. const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
  61. const nsICategoryManager = Components.interfaces.nsICategoryManager;
  62. const nsIContentHandler  = Components.interfaces.nsIContentHandler;
  63. const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
  64. const nsIURI             = Components.interfaces.nsIURI;
  65. const nsIStandardURL     = Components.interfaces.nsIStandardURL;
  66. const nsIChannel         = Components.interfaces.nsIChannel;
  67. const nsIRequest         = Components.interfaces.nsIRequest;
  68. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  69. const nsISupports        = Components.interfaces.nsISupports;
  70.  
  71. /* Command Line handler service */
  72. function CLineService()
  73. {}
  74.  
  75. CLineService.prototype.commandLineArgument = "-chat";
  76. CLineService.prototype.prefNameForStartup = "general.startup.chat";
  77. CLineService.prototype.chromeUrlForTask="chrome://chatzilla/content";
  78. CLineService.prototype.helpText = "Start with an IRC chat client";
  79. CLineService.prototype.handlesArgs=false;
  80. CLineService.prototype.defaultArgs ="";
  81. CLineService.prototype.openWindowWithArgs=false;
  82.  
  83. /* factory for command line handler service (CLineService) */
  84. var CLineFactory = new Object();
  85.  
  86. CLineFactory.createInstance =
  87. function (outer, iid) {
  88.     if (outer != null)
  89.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  90.  
  91.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  92.         throw Components.results.NS_ERROR_INVALID_ARG;
  93.  
  94.     return new CLineService();
  95. }
  96.  
  97. /* x-application-irc content handler */
  98. function IRCContentHandler ()
  99. {}
  100.  
  101. IRCContentHandler.prototype.QueryInterface =
  102. function (iid) {
  103.  
  104.     if (!iid.equals(nsIContentHandler))
  105.         throw Components.results.NS_ERROR_NO_INTERFACE;
  106.  
  107.     return this;
  108. }
  109.  
  110. IRCContentHandler.prototype.handleContent =
  111. function (aContentType, aCommand, aWindowTarget, aRequest)
  112. {
  113.     var e;
  114.     var channel = aRequest.QueryInterface(nsIChannel);
  115.     
  116.     /*
  117.     debug ("ircLoader.handleContent (" + aContentType + ", " +
  118.           aCommand + ", " + aWindowTarget + ", " + channel.URI.spec + ")\n");
  119.     */
  120.  
  121.     var windowManager =
  122.         Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);
  123.  
  124.     var w = windowManager.getMostRecentWindow("irc:chatzilla");
  125.  
  126.     if (w)
  127.     {
  128.         w.focus();
  129.         w.gotoIRCURL(channel.URI.spec);
  130.     }
  131.     else
  132.     {
  133.         var ass =
  134.             Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  135.         w = ass.hiddenDOMWindow;
  136.  
  137.         var args = new Object ();
  138.         args.url = channel.URI.spec;
  139.  
  140.         w.openDialog("chrome://chatzilla/content/chatzilla.xul", "_blank",
  141.                      "chrome,menubar,toolbar,resizable,dialog=no", args);
  142.     }
  143.     
  144. }
  145.  
  146. /* content handler factory object (IRCContentHandler) */
  147. var IRCContentHandlerFactory = new Object();
  148.  
  149. IRCContentHandlerFactory.createInstance =
  150. function (outer, iid) {
  151.     if (outer != null)
  152.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  153.  
  154.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  155.         throw Components.results.NS_ERROR_INVALID_ARG;
  156.  
  157.     return new IRCContentHandler();
  158. }
  159.  
  160. /* irc protocol handler component */
  161. function IRCProtocolHandler()
  162. {
  163. }
  164.  
  165. IRCProtocolHandler.prototype.scheme = "irc";
  166. IRCProtocolHandler.prototype.defaultPort = 6667;
  167. IRCProtocolHandler.prototype.protocolFlags = 
  168.                    nsIProtocolHandler.URI_NORELATIVE |
  169.                    nsIProtocolHandler.ALLOWS_PROXY;
  170.  
  171. IRCProtocolHandler.prototype.allowPort =
  172. function (aPort, aScheme)
  173. {
  174.     return false;
  175. }
  176.  
  177. IRCProtocolHandler.prototype.newURI =
  178. function (aSpec, aCharset, aBaseURI)
  179. {
  180.     if (aBaseURI)
  181.     {
  182.         debug ("-*- ircHandler: aBaseURI passed to newURI, bailing.\n");
  183.         return null;
  184.     }
  185.     
  186.     var url = Components.classes[STANDARDURL_CONTRACTID].
  187.       createInstance(nsIStandardURL);
  188.     url.init(nsIStandardURL.URLTYPE_STANDARD, 6667, aSpec, aCharset, aBaseURI);
  189.     
  190.     return url.QueryInterface(nsIURI);
  191. }
  192.  
  193. IRCProtocolHandler.prototype.newChannel =
  194. function (aURI)
  195. {
  196.     return new BogusChannel (aURI);
  197. }
  198.  
  199. /* protocol handler factory object (IRCProtocolHandler) */
  200. var IRCProtocolHandlerFactory = new Object();
  201.  
  202. IRCProtocolHandlerFactory.createInstance =
  203. function (outer, iid) {
  204.     if (outer != null)
  205.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  206.  
  207.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  208.         throw Components.results.NS_ERROR_INVALID_ARG;
  209.  
  210.     return new IRCProtocolHandler();
  211. }
  212.  
  213. /* bogus IRC channel used by the IRCProtocolHandler */
  214. function BogusChannel (aURI)
  215. {
  216.     this.URI = aURI;
  217.     this.originalURI = aURI;
  218. }
  219.  
  220. BogusChannel.prototype.QueryInterface =
  221. function (iid) {
  222.  
  223.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  224.         !iid.equals(nsISupports))
  225.         throw Components.results.NS_ERROR_NO_INTERFACE;
  226.  
  227.     return this;
  228. }
  229.  
  230. /* nsIChannel */
  231. BogusChannel.prototype.loadAttributes = null;
  232. BogusChannel.prototype.contentType = "x-application-irc";
  233. BogusChannel.prototype.contentLength = 0;
  234. BogusChannel.prototype.owner = null;
  235. BogusChannel.prototype.loadGroup = null;
  236. BogusChannel.prototype.notificationCallbacks = null;
  237. BogusChannel.prototype.securityInfo = null;
  238.  
  239. BogusChannel.prototype.open =
  240. BogusChannel.prototype.asyncOpen =
  241. function ()
  242. {
  243.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  244. }
  245.  
  246. BogusChannel.prototype.asyncOpen =
  247. function (observer, ctxt)
  248. {
  249.     observer.onStartRequest (this, ctxt);
  250. }
  251.  
  252. BogusChannel.prototype.asyncRead =
  253. function (listener, ctxt)
  254. {
  255.     return listener.onStartRequest (this, ctxt);
  256. }
  257.  
  258. /* nsIRequest */
  259. BogusChannel.prototype.isPending =
  260. function ()
  261. {
  262.     return true;
  263. }
  264.  
  265. BogusChannel.prototype.status = Components.results.NS_OK;
  266.  
  267. BogusChannel.prototype.cancel =
  268. function (aStatus)
  269. {
  270.     this.status = aStatus;
  271. }
  272.  
  273. BogusChannel.prototype.suspend =
  274. BogusChannel.prototype.resume =
  275. function ()
  276. {
  277.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  278. }
  279.  
  280. var ChatzillaModule = new Object();
  281.  
  282. ChatzillaModule.registerSelf =
  283. function (compMgr, fileSpec, location, type)
  284. {
  285.     debug("*** Registering -chat handler.\n");
  286.     
  287.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  288.  
  289.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  290.                                     "Chatzilla CommandLine Service",
  291.                                     CLINE_SERVICE_CONTRACTID, 
  292.                                     fileSpec,
  293.                                     location, 
  294.                                     type);
  295.     
  296.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  297.         .getService(nsICategoryManager);
  298.     catman.addCategoryEntry("command-line-argument-handlers",
  299.                             "chatzilla command line handler",
  300.                             CLINE_SERVICE_CONTRACTID, true, true);
  301.  
  302.     debug("*** Registering x-application-irc handler.\n");
  303.     compMgr.registerFactoryLocation(IRCCNT_HANDLER_CID,
  304.                                     "IRC Content Handler",
  305.                                     IRCCNT_HANDLER_CONTRACTID, 
  306.                                     fileSpec,
  307.                                     location, 
  308.                                     type);
  309.  
  310.     debug("*** Registering irc protocol handler.\n");
  311.     compMgr.registerFactoryLocation(IRCPROT_HANDLER_CID,
  312.                                     "IRC protocol handler",
  313.                                     IRCPROT_HANDLER_CONTRACTID, 
  314.                                     fileSpec, 
  315.                                     location,
  316.                                     type);
  317.  
  318. }
  319.  
  320. ChatzillaModule.unregisterSelf =
  321. function(compMgr, fileSpec, location)
  322. {
  323.  
  324.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  325.  
  326.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, 
  327.                                       fileSpec);
  328.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  329.         .getService(nsICategoryManager);
  330.     catman.deleteCategoryEntry("command-line-argument-handlers",
  331.                                CLINE_SERVICE_CONTRACTID, true);
  332. }
  333.  
  334. ChatzillaModule.getClassObject =
  335. function (compMgr, cid, iid) {
  336.     if (cid.equals(CLINE_SERVICE_CID))
  337.         return CLineFactory;
  338.  
  339.     if (cid.equals(IRCCNT_HANDLER_CID))
  340.         return IRCContentHandlerFactory;
  341.  
  342.     if (cid.equals(IRCPROT_HANDLER_CID))
  343.         return IRCProtocolHandlerFactory;
  344.     
  345.     if (!iid.equals(Components.interfaces.nsIFactory))
  346.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  347.  
  348.     throw Components.results.NS_ERROR_NO_INTERFACE;
  349.     
  350. }
  351.  
  352. ChatzillaModule.canUnload =
  353. function(compMgr)
  354. {
  355.     return true;
  356. }
  357.  
  358. /* entrypoint */
  359. function NSGetModule(compMgr, fileSpec) {
  360.     return ChatzillaModule;
  361. }
  362.