home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 114 / CDRom114.iso / internet / calendar / prog2.xpi / components / calendarService.js < prev    next >
Encoding:
Text File  |  2004-11-12  |  11.2 KB  |  370 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 calendar related components:
  26.  * 1. Command line handler service, for responding to the -webcal command line
  27.  *    option. (CLineHandler)
  28.  * 2. Content handler for responding to content of type text/calendar
  29.  *    (ICALContentHandler)
  30.  * 3. Protocol handler for supplying a channel to the browser when an webcal://
  31.  *    link is clicked. (ICALProtocolHandler)
  32.  * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
  33.  *    that webcal:// links have the content type text/calendar (BogusChannel)
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CONTRACTID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=calendar";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{65ef4b0b-d116-4b93-bf8a-84525992bf27}");
  41. const ICALCNT_HANDLER_CONTRACTID =
  42.     "@mozilla.org/uriloader/content-handler;1?type=text/calendar";
  43. const ICALCNT_HANDLER_CID =
  44.     Components.ID("{9ebf4c8a-7770-40a6-aeed-e1738129535a}");
  45. const ICALPROT_HANDLER_CONTRACTID =
  46.     "@mozilla.org/network/protocol;1?name=webcal";
  47. const ICALPROT_HANDLER_CID =
  48.     Components.ID("{d320ba05-88cf-44a6-b718-87a72ef05918}");
  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 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 = "-calendar";
  76. CLineService.prototype.prefNameForStartup = "general.startup.calendar";
  77. CLineService.prototype.chromeUrlForTask = "chrome://calendar/content";
  78. CLineService.prototype.helpText = "Start with calendar";
  79. CLineService.prototype.handlesArgs = false;
  80. CLineService.prototype.defaultArgs = "";
  81. CLineService.prototype.openWindowWithArgs = true;
  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. /* text/calendar content handler */
  98. function ICALContentHandler ()
  99. {}
  100.  
  101. ICALContentHandler.prototype.QueryInterface =
  102. function (iid) {
  103.  
  104.     if (iid.equals(nsIContentHandler) ||
  105.         iid.equals(nsISupports))
  106.         return this;
  107.  
  108.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  109.     return null;
  110. }
  111.  
  112. ICALContentHandler.prototype.handleContent =
  113. function (aContentType, param2, param3, param4)
  114. {
  115.  
  116.     var aWindowTarget, aRequest;
  117.     if (param4 === undefined)
  118.     {// Moz1.8+ uses 3 params: (aContentType, aWindowTarget, aRequest)
  119.         aWindowTarget = param2;
  120.         aRequest      = param3;
  121.     }
  122.     else
  123.     {// Moz1.7- uses 4 params: (aContentType, aCommand, aWindowTarget, aRequest)
  124.         aWindowTarget = param3;
  125.         aRequest      = param4;
  126.     }
  127.  
  128.     var e;
  129.     var channel = aRequest.QueryInterface(nsIChannel);
  130.  
  131.     /*
  132.     dump ("ICALContentHandler.handleContent (" + aContentType + ", " +
  133.            aWindowTarget + ", " + channel.URI.spec + ")\n");
  134.     */
  135.  
  136.     var windowManager =
  137.         Components.classes[MEDIATOR_CONTRACTID].getService(nsIWindowMediator);
  138.  
  139.     var w = windowManager.getMostRecentWindow("calendarMainWindow");
  140.  
  141.     if (w)
  142.     {
  143.         w.focus();
  144.  
  145.         w.gCalendarWindow.calendarManager.checkCalendarURL( channel );
  146.     }
  147.     else
  148.     {
  149.         var ass =
  150.             Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
  151.         w = ass.hiddenDOMWindow;
  152.  
  153.         var args = new Object ();
  154.         args.channel = channel;
  155.         w.openDialog("chrome://calendar/content/calendar.xul", "calendar", "chrome,menubar,resizable,scrollbars,status,toolbar,dialog=no", args);
  156.     }
  157.  
  158. }
  159.  
  160. /* content handler factory object (ICALContentHandler) */
  161. var ICALContentHandlerFactory = new Object();
  162.  
  163. ICALContentHandlerFactory.createInstance =
  164. function (outer, iid) {
  165.     if (outer != null)
  166.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  167.  
  168.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  169.         throw Components.results.NS_ERROR_INVALID_ARG;
  170.  
  171.     return new ICALContentHandler();
  172. }
  173.  
  174. /* webcal protocol handler component */
  175. function ICALProtocolHandler()
  176. {
  177. }
  178.  
  179. ICALProtocolHandler.prototype.scheme = "webcal";
  180. ICALProtocolHandler.prototype.defaultPort = 8080;
  181. ICALProtocolHandler.prototype.protocolFlags =
  182.                    nsIProtocolHandler.URI_NORELATIVE |
  183.                    nsIProtocolHandler.ALLOWS_PROXY;
  184.  
  185. ICALProtocolHandler.prototype.allowPort =
  186. function (aPort, aScheme)
  187. {
  188.     return false;
  189. }
  190.  
  191. ICALProtocolHandler.prototype.newURI =
  192. function (aSpec, aCharset, aBaseURI)
  193. {
  194.     var url = Components.classes[STANDARDURL_CONTRACTID].
  195.       createInstance(nsIStandardURL);
  196.     url.init(nsIStandardURL.URLTYPE_STANDARD, 8080, aSpec, aCharset, aBaseURI);
  197.  
  198.     return url.QueryInterface(nsIURI);
  199. }
  200.  
  201. ICALProtocolHandler.prototype.newChannel =
  202. function (aURI)
  203. {
  204.     return new BogusChannel (aURI);
  205. }
  206.  
  207. /* protocol handler factory object (ICALProtocolHandler) */
  208. var ICALProtocolHandlerFactory = new Object();
  209.  
  210. ICALProtocolHandlerFactory.createInstance =
  211. function (outer, iid) {
  212.     if (outer != null)
  213.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  214.  
  215.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  216.         throw Components.results.NS_ERROR_INVALID_ARG;
  217.  
  218.     return new ICALProtocolHandler();
  219. }
  220.  
  221. /* bogus webcal channel used by the ICALProtocolHandler */
  222. function BogusChannel (aURI)
  223. {
  224.     this.URI = aURI;
  225.     this.originalURI = aURI;
  226. }
  227.  
  228. BogusChannel.prototype.QueryInterface =
  229. function (iid) {
  230.  
  231.     if (iid.equals(nsIChannel) ||
  232.         iid.equals(nsIRequest) ||
  233.         iid.equals(nsISupports))
  234.         return this;
  235.  
  236.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  237.     return null;
  238. }
  239.  
  240. /* nsIChannel */
  241. BogusChannel.prototype.loadAttributes = null;
  242. BogusChannel.prototype.contentType = "text/calendar";
  243. BogusChannel.prototype.contentLength = 0;
  244. BogusChannel.prototype.owner = null;
  245. BogusChannel.prototype.loadGroup = null;
  246. BogusChannel.prototype.notificationCallbacks = null;
  247. BogusChannel.prototype.securityInfo = null;
  248.  
  249. BogusChannel.prototype.open =
  250. BogusChannel.prototype.asyncOpen =
  251. function ()
  252. {
  253.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  254. }
  255.  
  256. BogusChannel.prototype.asyncOpen =
  257. function (observer, ctxt)
  258. {
  259.     observer.onStartRequest (this, ctxt);
  260. }
  261.  
  262. BogusChannel.prototype.asyncRead =
  263. function (listener, ctxt)
  264. {
  265.     return listener.onStartRequest (this, ctxt);
  266. }
  267.  
  268. /* nsIRequest */
  269. BogusChannel.prototype.isPending =
  270. function ()
  271. {
  272.     return true;
  273. }
  274.  
  275. BogusChannel.prototype.status = Components.results.NS_OK;
  276.  
  277. BogusChannel.prototype.cancel =
  278. function (aStatus)
  279. {
  280.     this.status = aStatus;
  281. }
  282.  
  283. BogusChannel.prototype.suspend =
  284. BogusChannel.prototype.resume =
  285. function ()
  286. {
  287.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  288. }
  289.  
  290. var CalendarModule = new Object();
  291.  
  292. CalendarModule.registerSelf =
  293. function (compMgr, fileSpec, location, type)
  294. {
  295.     dump("*** Registering -calendar handler.\n");
  296.  
  297.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  298.  
  299.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  300.                                     "Calendar CommandLine Service",
  301.                                     CLINE_SERVICE_CONTRACTID,
  302.                                     fileSpec,
  303.                                     location,
  304.                                     type);
  305.  
  306.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  307.                        .getService(nsICategoryManager);
  308.     catman.addCategoryEntry("command-line-argument-handlers",
  309.                             "calendar command line handler",
  310.                             CLINE_SERVICE_CONTRACTID, true, true);
  311.  
  312.     dump("*** Registering text/calendar handler.\n");
  313.     compMgr.registerFactoryLocation(ICALCNT_HANDLER_CID,
  314.                                     "Webcal Content Handler",
  315.                                     ICALCNT_HANDLER_CONTRACTID,
  316.                                     fileSpec,
  317.                                     location,
  318.                                     type);
  319.  
  320.     dump("*** Registering webcal protocol handler.\n");
  321.     compMgr.registerFactoryLocation(ICALPROT_HANDLER_CID,
  322.                                     "Webcal protocol handler",
  323.                                     ICALPROT_HANDLER_CONTRACTID,
  324.                                     fileSpec,
  325.                                     location,
  326.                                     type);
  327. }
  328.  
  329. CalendarModule.unregisterSelf =
  330. function(compMgr, fileSpec, location)
  331. {
  332.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  333.  
  334.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID,
  335.                                       fileSpec);
  336.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  337.                        .getService(nsICategoryManager);
  338.     catman.deleteCategoryEntry("command-line-argument-handlers",
  339.                                CLINE_SERVICE_CONTRACTID, true);
  340. }
  341.  
  342. CalendarModule.getClassObject =
  343. function (compMgr, cid, iid) {
  344.     if (cid.equals(CLINE_SERVICE_CID))
  345.         return CLineFactory;
  346.  
  347.     if (cid.equals(ICALCNT_HANDLER_CID))
  348.         return ICALContentHandlerFactory;
  349.  
  350.     if (cid.equals(ICALPROT_HANDLER_CID))
  351.         return ICALProtocolHandlerFactory;
  352.  
  353.     if (!iid.equals(Components.interfaces.nsIFactory))
  354.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  355.  
  356.     throw Components.results.NS_ERROR_NO_INTERFACE;
  357.  
  358. }
  359.  
  360. CalendarModule.canUnload =
  361. function(compMgr)
  362. {
  363.     return true;
  364. }
  365.  
  366. /* entrypoint */
  367. function NSGetModule(compMgr, fileSpec) {
  368.     return CalendarModule;
  369. }
  370.