home *** CD-ROM | disk | FTP | other *** search
/ ftp.swcp.com / ftp.swcp.com.zip / ftp.swcp.com / mac / mozilla-macos9-1.3.1.sea.bin / Mozilla1.3.1 / Components / chatzilla-service.js < prev    next >
Text File  |  2003-06-07  |  11KB  |  363 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/appshell/window-mediator;1";
  53. const STANDARDURL_CONTRACTID = 
  54.     "@mozilla.org/network/standard-url;1";
  55. const IOSERVICE_CONTRACTID = 
  56.     "@mozilla.org/network/io-service;1";
  57. const ASS_CONTRACTID =
  58.     "@mozilla.org/appshell/appShellService;1";
  59.  
  60. /* interafces used in this file */
  61. const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
  62. const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
  63. const nsICategoryManager = Components.interfaces.nsICategoryManager;
  64. const nsIContentHandler  = Components.interfaces.nsIContentHandler;
  65. const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
  66. const nsIURI             = Components.interfaces.nsIURI;
  67. const nsIStandardURL     = Components.interfaces.nsIStandardURL;
  68. const nsIChannel         = Components.interfaces.nsIChannel;
  69. const nsIRequest         = Components.interfaces.nsIRequest;
  70. const nsIIOService       = Components.interfaces.nsIIOService;
  71. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  72. const nsISupports        = Components.interfaces.nsISupports;
  73.  
  74. /* Command Line handler service */
  75. function CLineService()
  76. {}
  77.  
  78. CLineService.prototype.commandLineArgument = "-chat";
  79. CLineService.prototype.prefNameForStartup = "general.startup.chat";
  80. CLineService.prototype.chromeUrlForTask = "chrome://chatzilla/content";
  81. CLineService.prototype.helpText = "Start with an IRC chat client";
  82. CLineService.prototype.handlesArgs = false;
  83. CLineService.prototype.defaultArgs = "";
  84. CLineService.prototype.openWindowWithArgs = false;
  85.  
  86. /* factory for command line handler service (CLineService) */
  87. var CLineFactory = new Object();
  88.  
  89. CLineFactory.createInstance =
  90. function (outer, iid)
  91. {
  92.     if (outer != null)
  93.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  94.  
  95.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  96.         throw Components.results.NS_ERROR_INVALID_ARG;
  97.  
  98.     return new CLineService();
  99. }
  100.  
  101. /* x-application-irc content handler */
  102. function IRCContentHandler ()
  103. {}
  104.  
  105. IRCContentHandler.prototype.QueryInterface =
  106. function (iid)
  107. {
  108.     if (!iid.equals(nsIContentHandler))
  109.         throw Components.results.NS_ERROR_NO_INTERFACE;
  110.  
  111.     return this;
  112. }
  113.  
  114. IRCContentHandler.prototype.handleContent =
  115. function (contentType, command, windowTarget, request)
  116. {
  117.     var e;
  118.     var channel = request.QueryInterface(nsIChannel);
  119.     
  120.     var windowManager =
  121.         Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);
  122.  
  123.     var w = windowManager.getMostRecentWindow("irc:chatzilla");
  124.  
  125.     if (w)
  126.     {
  127.         w.focus();
  128.         w.gotoIRCURL(channel.URI.spec);
  129.     }
  130.     else
  131.     {
  132.         var ass =
  133.             Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  134.         w = ass.hiddenDOMWindow;
  135.  
  136.         var args = new Object ();
  137.         args.url = channel.URI.spec;
  138.  
  139.         w.openDialog("chrome://chatzilla/content/chatzilla.xul", "_blank",
  140.                      "chrome,menubar,toolbar,resizable,dialog=no", args);
  141.     }
  142.     
  143. }
  144.  
  145. /* content handler factory object (IRCContentHandler) */
  146. var IRCContentHandlerFactory = new Object();
  147.  
  148. IRCContentHandlerFactory.createInstance =
  149. function (outer, iid)
  150. {
  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 (port, scheme)
  173. {
  174.     return false;
  175. }
  176.  
  177. IRCProtocolHandler.prototype.newURI =
  178. function (spec, charset, baseURI)
  179. {
  180.     var cls = Components.classes[STANDARDURL_CONTRACTID];
  181.     var url = cls.createInstance(nsIStandardURL);
  182.     url.init(nsIStandardURL.URLTYPE_STANDARD, 6667, spec, charset, baseURI);
  183.  
  184.     return url.QueryInterface(nsIURI);
  185. }
  186.  
  187. IRCProtocolHandler.prototype.newChannel =
  188. function (URI)
  189. {
  190.     ios = Components.classes[IOSERVICE_CONTRACTID].getService(nsIIOService);
  191.     if (!ios.allowPort(URI.port, URI.scheme))
  192.         throw Components.results.NS_ERROR_FAILURE;
  193.  
  194.     return new BogusChannel (URI);
  195. }
  196.  
  197. /* protocol handler factory object (IRCProtocolHandler) */
  198. var IRCProtocolHandlerFactory = new Object();
  199.  
  200. IRCProtocolHandlerFactory.createInstance =
  201. function (outer, iid)
  202. {
  203.     if (outer != null)
  204.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  205.  
  206.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  207.         throw Components.results.NS_ERROR_INVALID_ARG;
  208.  
  209.     return new IRCProtocolHandler();
  210. }
  211.  
  212. /* bogus IRC channel used by the IRCProtocolHandler */
  213. function BogusChannel (URI)
  214. {
  215.     this.URI = URI;
  216.     this.originalURI = URI;
  217. }
  218.  
  219. BogusChannel.prototype.QueryInterface =
  220. function (iid)
  221. {
  222.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  223.         !iid.equals(nsISupports))
  224.         throw Components.results.NS_ERROR_NO_INTERFACE;
  225.  
  226.     return this;
  227. }
  228.  
  229. /* nsIChannel */
  230. BogusChannel.prototype.loadAttributes = null;
  231. BogusChannel.prototype.contentType = "x-application-irc";
  232. BogusChannel.prototype.contentLength = 0;
  233. BogusChannel.prototype.owner = null;
  234. BogusChannel.prototype.loadGroup = null;
  235. BogusChannel.prototype.notificationCallbacks = null;
  236. BogusChannel.prototype.securityInfo = null;
  237.  
  238. BogusChannel.prototype.open =
  239. BogusChannel.prototype.asyncOpen =
  240. function ()
  241. {
  242.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  243. }
  244.  
  245. BogusChannel.prototype.asyncOpen =
  246. function (observer, ctxt)
  247. {
  248.     observer.onStartRequest (this, ctxt);
  249. }
  250.  
  251. BogusChannel.prototype.asyncRead =
  252. function (listener, ctxt)
  253. {
  254.     return listener.onStartRequest (this, ctxt);
  255. }
  256.  
  257. /* nsIRequest */
  258. BogusChannel.prototype.isPending =
  259. function ()
  260. {
  261.     return true;
  262. }
  263.  
  264. BogusChannel.prototype.status = Components.results.NS_OK;
  265.  
  266. BogusChannel.prototype.cancel =
  267. function (status)
  268. {
  269.     this.status = status;
  270. }
  271.  
  272. BogusChannel.prototype.suspend =
  273. BogusChannel.prototype.resume =
  274. function ()
  275. {
  276.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  277. }
  278.  
  279. var ChatzillaModule = new Object();
  280.  
  281. ChatzillaModule.registerSelf =
  282. function (compMgr, fileSpec, location, type)
  283. {
  284.     debug("*** Registering -chat handler.\n");
  285.     
  286.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  287.  
  288.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  289.                                     "Chatzilla CommandLine Service",
  290.                                     CLINE_SERVICE_CONTRACTID, 
  291.                                     fileSpec,
  292.                                     location, 
  293.                                     type);
  294.     
  295.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  296.         .getService(nsICategoryManager);
  297.     catman.addCategoryEntry("command-line-argument-handlers",
  298.                             "chatzilla command line handler",
  299.                             CLINE_SERVICE_CONTRACTID, true, true);
  300.  
  301.     debug("*** Registering x-application-irc handler.\n");
  302.     compMgr.registerFactoryLocation(IRCCNT_HANDLER_CID,
  303.                                     "IRC Content Handler",
  304.                                     IRCCNT_HANDLER_CONTRACTID, 
  305.                                     fileSpec,
  306.                                     location, 
  307.                                     type);
  308.  
  309.     debug("*** Registering irc protocol handler.\n");
  310.     compMgr.registerFactoryLocation(IRCPROT_HANDLER_CID,
  311.                                     "IRC protocol handler",
  312.                                     IRCPROT_HANDLER_CONTRACTID, 
  313.                                     fileSpec, 
  314.                                     location,
  315.                                     type);
  316.  
  317. }
  318.  
  319. ChatzillaModule.unregisterSelf =
  320. function(compMgr, fileSpec, location)
  321. {
  322.  
  323.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  324.  
  325.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, 
  326.                                       fileSpec);
  327.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  328.         .getService(nsICategoryManager);
  329.     catman.deleteCategoryEntry("command-line-argument-handlers",
  330.                                CLINE_SERVICE_CONTRACTID, true);
  331. }
  332.  
  333. ChatzillaModule.getClassObject =
  334. function (compMgr, cid, iid)
  335. {
  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. {
  361.     return ChatzillaModule;
  362. }
  363.