home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / mozilla / mozilla-win32-M18-mathml-svg-xslt.exe / components / chatzilla-service.js < prev    next >
Text File  |  2000-09-15  |  11KB  |  338 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 SIMPLEURI_CONTRACTID = 
  54.     "@mozilla.org/network/simple-uri;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 nsIChannel         = Components.interfaces.nsIChannel;
  66. const nsIRequest         = Components.interfaces.nsIRequest;
  67. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  68. const nsISupports        = Components.interfaces.nsISupports;
  69.  
  70. /* Command Line handler service */
  71. function CLineService()
  72. {}
  73.  
  74. CLineService.prototype.commandLineArgument = "-chat";
  75. CLineService.prototype.prefNameForStartup = "general.startup.chat";
  76. CLineService.prototype.chromeUrlForTask="chrome://chatzilla/content";
  77. CLineService.prototype.helpText = "Start with an IRC chat client";
  78. CLineService.prototype.handlesArgs=false;
  79. CLineService.prototype.defaultArgs ="";
  80. CLineService.prototype.openWindowWithArgs=false;
  81.  
  82. /* factory for command line handler service (CLineService) */
  83. CLineFactory = new Object();
  84.  
  85. CLineFactory.createInstance =
  86. function (outer, iid) {
  87.     if (outer != null)
  88.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  89.  
  90.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  91.         throw Components.results.NS_ERROR_INVALID_ARG;
  92.  
  93.     return new CLineService();
  94. }
  95.  
  96. /* x-application-irc content handler */
  97. function IRCContentHandler ()
  98. {}
  99.  
  100. IRCContentHandler.prototype.QueryInterface =
  101. function (iid) {
  102.  
  103.     if (!iid.equals(nsIContentHandler))
  104.         throw Components.results.NS_ERROR_NO_INTERFACE;
  105.  
  106.     return this;
  107. }
  108.  
  109. IRCContentHandler.prototype.handleContent =
  110. function (aContentType, aCommand, aWindowTarget, aSourceContext, aChannel)
  111. {
  112.     var e;
  113.     
  114.     dump ("ircLoader.handleContent (" + aContentType + ", " +
  115.           aCommand + ", " + aWindowTarget + ", " + aSourceContext + ", " +
  116.           aChannel.URI.spec + ")\n");
  117.     
  118.     var windowManager =
  119.         Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);
  120.  
  121.     var w = windowManager.getMostRecentWindow("irc:chatzilla");
  122.  
  123.     if (w)
  124.     {
  125.         w.focus();
  126.         w.gotoIRCURL(aChannel.URI.spec);
  127.     }
  128.     else
  129.     {
  130.         var ass = Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  131.         var w = ass.getHiddenDOMWindow();
  132.         w.open("chrome://chatzilla/content/chatzilla.xul?" + aChannel.URI.spec,
  133.                "_blank", "chrome,menubar,toolbar,resizable");
  134.     }
  135.     
  136. }
  137.  
  138. /* content handler factory object (IRCContentHandler) */
  139. IRCContentHandlerFactory = new Object();
  140.  
  141. IRCContentHandlerFactory.createInstance =
  142. function (outer, iid) {
  143.     if (outer != null)
  144.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  145.  
  146.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  147.         throw Components.results.NS_ERROR_INVALID_ARG;
  148.  
  149.     return new IRCContentHandler();
  150. }
  151.  
  152. /* irc protocol handler component */
  153. function IRCProtocolHandler()
  154. {
  155. }
  156.  
  157. IRCProtocolHandler.prototype.scheme = "irc";
  158. IRCProtocolHandler.prototype.defaultPort = 6667;
  159.  
  160. IRCProtocolHandler.prototype.newURI =
  161. function (aSpec, aBaseURI)
  162. {
  163.     if (aBaseURI)
  164.     {
  165.         dump ("-*- ircHandler: aBaseURI passed to newURI, bailing.\n");
  166.         return null;
  167.     }
  168.     
  169.     var uri = Components.classes[SIMPLEURI_CONTRACTID].createInstance(nsIURI);
  170.     uri.spec = aSpec;
  171.     
  172.     return uri;
  173. }
  174.  
  175. IRCProtocolHandler.prototype.newChannel =
  176. function (aURI)
  177. {
  178.     return new BogusChannel (aURI);
  179. }
  180.  
  181. /* protocol handler factory object (IRCProtocolHandler) */
  182. IRCProtocolHandlerFactory = new Object();
  183.  
  184. IRCProtocolHandlerFactory.createInstance =
  185. function (outer, iid) {
  186.     if (outer != null)
  187.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  188.  
  189.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  190.         throw Components.results.NS_ERROR_INVALID_ARG;
  191.  
  192.     return new IRCProtocolHandler();
  193. }
  194.  
  195. /* bogus IRC channel used by the IRCProtocolHandler */
  196. function BogusChannel (aURI)
  197. {
  198.     this.URI = aURI;
  199.     this.originalURI = aURI;
  200. }
  201.  
  202. BogusChannel.prototype.QueryInterface =
  203. function (iid) {
  204.  
  205.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  206.         !iid.equals(nsISupports))
  207.         throw Components.results.NS_ERROR_NO_INTERFACE;
  208.  
  209.     return this;
  210. }
  211.  
  212. /* nsIChannel */
  213. BogusChannel.prototype.transferOffset = 0;
  214. BogusChannel.prototype.transferCount = 0;
  215. BogusChannel.prototype.loadAttributes = null;
  216. BogusChannel.prototype.contentType = "x-application-irc";
  217. BogusChannel.prototype.contentLength = 0;
  218. BogusChannel.prototype.owner = null;
  219. BogusChannel.prototype.loadGroup = null;
  220. BogusChannel.prototype.notificationCallbacks = null;
  221. BogusChannel.prototype.securityInfo = null;
  222. BogusChannel.prototype.bufferSegmentSize = 0;
  223. BogusChannel.prototype.bufferMaxSize = 0;
  224. BogusChannel.prototype.shouldCache = false;
  225. BogusChannel.prototype.pipeliningAllowed = false;
  226.  
  227. BogusChannel.prototype.openInputStream =
  228. BogusChannel.prototype.openOutputStream =
  229. BogusChannel.prototype.asyncWrite =
  230. function ()
  231. {
  232.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  233. }
  234.  
  235. BogusChannel.prototype.asyncOpen =
  236. function (observer, ctxt)
  237. {
  238.     observer.onStartRequest (this, ctxt);
  239. }
  240.  
  241. BogusChannel.prototype.asyncRead =
  242. function (listener, ctxt)
  243. {
  244.     return listener.onStartRequest (this, ctxt);
  245. }
  246.  
  247. /* nsIRequest */
  248. BogusChannel.prototype.isPending =
  249. function ()
  250. {
  251.     return true;
  252. }
  253.  
  254. BogusChannel.prototype.status = Components.results.NS_OK;
  255.  
  256. BogusChannel.prototype.cancel =
  257. function (aStatus)
  258. {
  259.     this.status = aStatus;
  260. }
  261.  
  262. BogusChannel.prototype.suspend =
  263. BogusChannel.prototype.resume =
  264. function ()
  265. {
  266.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  267. }
  268.  
  269. var ChatzillaModule = new Object();
  270.  
  271. ChatzillaModule.registerSelf =
  272. function (compMgr, fileSpec, location, type)
  273. {
  274.     dump("*** Registering -chat handler.\n");
  275.     compMgr.registerComponentWithType(CLINE_SERVICE_CID,
  276.                                       "Chatzilla CommandLine Service",
  277.                                       CLINE_SERVICE_CONTRACTID, fileSpec,
  278.                                       location, true, true, type);
  279.     
  280.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  281.         .getService(nsICategoryManager);
  282.     catman.addCategoryEntry("command-line-argument-handlers",
  283.                             "chatzilla command line handler",
  284.                             CLINE_SERVICE_CONTRACTID, true, true);
  285.  
  286.     dump("*** Registering x-application-irc handler.\n");
  287.     compMgr.registerComponentWithType(IRCCNT_HANDLER_CID,
  288.                                       "IRC Content Handler",
  289.                                       IRCCNT_HANDLER_CONTRACTID, fileSpec,
  290.                                       location, true, true, type);
  291.  
  292.     dump("*** Registering irc protocol handler.\n");
  293.     compMgr.registerComponentWithType(IRCPROT_HANDLER_CID,
  294.                                       "IRC protocol handler",
  295.                                       IRCPROT_HANDLER_CONTRACTID, fileSpec, location,
  296.                                       true, true, type);
  297.  
  298. }
  299.  
  300. ChatzillaModule.unregisterSelf =
  301. function(compMgr, fileSpec, location)
  302. {
  303.     compMgr.unregisterComponentSpec(CLINE_SERVICE_CID, fileSpec);
  304.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  305.         .getService(nsICategoryManager);
  306.     catman.deleteCategoryEntry("command-line-argument-handlers",
  307.                                CLINE_SERVICE_CONTRACTID, true);
  308. }
  309.  
  310. ChatzillaModule.getClassObject =
  311. function (compMgr, cid, iid) {
  312.     if (cid.equals(CLINE_SERVICE_CID))
  313.         return CLineFactory;
  314.  
  315.     if (cid.equals(IRCCNT_HANDLER_CID))
  316.         return IRCContentHandlerFactory;
  317.  
  318.     if (cid.equals(IRCPROT_HANDLER_CID))
  319.         return IRCProtocolHandlerFactory;
  320.     
  321.     if (!iid.equals(Components.interfaces.nsIFactory))
  322.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  323.  
  324.     throw Components.results.NS_ERROR_NO_INTERFACE;
  325.     
  326. }
  327.  
  328. ChatzillaModule.canUnload =
  329. function(compMgr)
  330. {
  331.     return true;
  332. }
  333.  
  334. /* entrypoint */
  335. function NSGetModule(compMgr, fileSpec) {
  336.     return ChatzillaModule;
  337. }
  338.