home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / calendar_windows_latest.xpi / components / calendarService.js < prev    next >
Encoding:
Text File  |  2005-07-03  |  13.6 KB  |  415 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Seth Spitzer <sspitzer@netscape.com>
  24.  *   Robert Ginda <rginda@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  28.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. /*
  41.  * This file contains the following calendar related components:
  42.  * 1. Command line handler service, for responding to the -webcal command line
  43.  *    option. (CLineHandler)
  44.  * 2. Content handler for responding to content of type text/calendar
  45.  *    (ICALContentHandler)
  46.  * 3. Protocol handler for supplying a channel to the browser when an webcal://
  47.  *    link is clicked. (ICALProtocolHandler)
  48.  * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
  49.  *    that webcal:// links have the content type text/calendar (BogusChannel)
  50.  */
  51.  
  52. /* components defined in this file */
  53. const CLINE_SERVICE_CONTRACTID =
  54.     "@mozilla.org/commandlinehandler/general-startup;1?type=calendar";
  55. const CLINE_SERVICE_CID =
  56.     Components.ID("{65ef4b0b-d116-4b93-bf8a-84525992bf27}");
  57. const ICALCNT_HANDLER_CONTRACTID =
  58.     "@mozilla.org/uriloader/content-handler;1?type=text/calendar";
  59. const ICALCNT_HANDLER_CID =
  60.     Components.ID("{9ebf4c8a-7770-40a6-aeed-e1738129535a}");
  61. const ICALPROT_HANDLER_CONTRACTID =
  62.     "@mozilla.org/network/protocol;1?name=webcal";
  63. const ICALPROT_HANDLER_CID =
  64.     Components.ID("{d320ba05-88cf-44a6-b718-87a72ef05918}");
  65.  
  66. /* components used in this file */
  67. const MEDIATOR_CONTRACTID =
  68.     "@mozilla.org/appshell/window-mediator;1";
  69. const STANDARDURL_CONTRACTID =
  70.     "@mozilla.org/network/standard-url;1";
  71. const ASS_CONTRACTID =
  72.     "@mozilla.org/appshell/appShellService;1";
  73. const WINDOWWATCHER_CONTRACTID =
  74.     "@mozilla.org/embedcomp/window-watcher;1";
  75.  
  76. /* interafces used in this file */
  77. const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
  78. const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
  79. const nsICategoryManager = Components.interfaces.nsICategoryManager;
  80. const nsIContentHandler  = Components.interfaces.nsIContentHandler;
  81. const nsIFactory         = Components.interfaces.nsIFactory;
  82. const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
  83. const nsIURI             = Components.interfaces.nsIURI;
  84. const nsIStandardURL     = Components.interfaces.nsIStandardURL;
  85. const nsIChannel         = Components.interfaces.nsIChannel;
  86. const nsIRequest         = Components.interfaces.nsIRequest;
  87. const nsIAppShellService = Components.interfaces.nsIAppShellService;
  88. const nsISupports        = Components.interfaces.nsISupports;
  89. const nsIWindowWatcher   = Components.interfaces.nsIWindowWatcher;
  90.  
  91. if ("nsICommandLineHandler" in Components.interfaces)
  92.      const nsICommandLineHandler = Components.interfaces.nsICommandLineHandler;
  93.  
  94. /* Command Line handler service */
  95. function CLineService()
  96. {}
  97.  
  98. CLineService.prototype = {
  99.     /* nsISupports */
  100.     QueryInterface : function service_qi(iid) {
  101.         if (iid.equals(nsISupports))
  102.             return this;
  103.  
  104.         if (nsICmdLineHandler && iid.equals(nsICmdLineHandler))
  105.             return this;
  106.  
  107.         if (nsICommandLineHandler && iid.equals(nsICommandLineHandler))
  108.             return this;
  109.  
  110.         throw Components.results.NS_ERROR_NO_INTERFACE;
  111.     },
  112.  
  113.     /* nsICmdLineHandler */
  114.  
  115.     commandLineArgument : "-calendar",
  116.     prefNameForStartup  : "general.startup.calendar",
  117.     chromeUrlForTask    : "chrome://calendar/content",
  118.     helpText            : "Start with calendar",
  119.     handlesArgs         : false,
  120.     defaultArgs         : "",
  121.     openWindowWithArgs  : true,
  122.  
  123.     /* nsICommandLineHandler */
  124.  
  125.     handle : function service_handle(cmdLine) {
  126.         if (cmdLine.handleFlag("calendar", false)) {
  127.             wwatch = Components.classes[WINDOWWATCHER_CONTRACTID]
  128.                                .getService(nsIWindowWatcher);
  129.             wwatch.openWindow(null, "chrome://calendar/content/",
  130.                               "_blank", "chrome,dialog=no,all", cmdLine);
  131.             cmdLine.preventDefault = true;
  132.         }
  133.     },
  134.  
  135.     helpInfo : "  -calendar            Start with the calendar.\n"
  136. };
  137.  
  138. /* factory for command line handler service (CLineService) */
  139. var CLineFactory = {
  140.     createInstance : function (outer, iid) {
  141.         if (outer != null)
  142.             throw Components.results.NS_ERROR_NO_AGGREGATION;
  143.  
  144.         return new CLineService().QueryInterface(iid);
  145.     }
  146. }
  147.  
  148. /* text/calendar content handler */
  149. function ICALContentHandler ()
  150. {}
  151.  
  152. ICALContentHandler.prototype.QueryInterface =
  153. function (iid) {
  154.  
  155.     if (iid.equals(nsIContentHandler) ||
  156.         iid.equals(nsISupports))
  157.         return this;
  158.  
  159.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  160.     return null;
  161. }
  162.  
  163. ICALContentHandler.prototype.handleContent =
  164. function(aContentType, aWindowTarget, aRequest)
  165. {
  166.     var channel = aRequest.QueryInterface(nsIChannel);
  167.  
  168.     // Cancel the request ...
  169.     var uri = channel.URI;
  170.     const NS_BINDING_ABORTED = 0x804b0002 // from nsNetError.h
  171.     aRequest.cancel(NS_BINDING_ABORTED);
  172.  
  173.     // ... Subscribe to the uri ...
  174.     var calendarManager = Components.classes["@mozilla.org/calendar/manager;1"]
  175.                                     .getService(Components.interfaces.calICalendarManager);
  176.  
  177.     var newCalendar = calendarManager.createCalendar('ics', uri);
  178.     calendarManager.registerCalendar(newCalendar);
  179.  
  180.     // XXX Come up with a better name, like the filename or X-WR-CALNAME
  181.     // XXX Also, make up a color
  182.     newCalendar.name = "temp";
  183.  
  184.     // ... and open or focus a calendar window.
  185.     var windowManager = Components.classes[MEDIATOR_CONTRACTID]
  186.                                   .getService(nsIWindowMediator);
  187.  
  188.     var w = windowManager.getMostRecentWindow("calendarMainWindow");
  189.  
  190.     if (w) {
  191.         w.focus();
  192.     } else {
  193.         var ass = Components.classes[ASS_CONTRACTID]
  194.                             .getService(nsIAppShellService);
  195.         w = ass.hiddenDOMWindow;
  196.  
  197.         var args = new Object ();
  198.         args.channel = channel;
  199.         w.openDialog("chrome://calendar/content/calendar.xul", "calendar", "chrome,menubar,resizable,scrollbars,status,toolbar,dialog=no", args);
  200.     }
  201. }
  202.  
  203. /* content handler factory object (ICALContentHandler) */
  204. var ICALContentHandlerFactory = new Object();
  205.  
  206. ICALContentHandlerFactory.createInstance =
  207. function (outer, iid) {
  208.     if (outer != null)
  209.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  210.  
  211.     if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
  212.         throw Components.results.NS_ERROR_INVALID_ARG;
  213.  
  214.     return new ICALContentHandler();
  215. }
  216.  
  217. /* webcal protocol handler component */
  218. function ICALProtocolHandler()
  219. {
  220. }
  221.  
  222. ICALProtocolHandler.prototype.scheme = "webcal";
  223. ICALProtocolHandler.prototype.defaultPort = 8080;
  224. ICALProtocolHandler.prototype.protocolFlags =
  225.                    nsIProtocolHandler.URI_NORELATIVE |
  226.                    nsIProtocolHandler.ALLOWS_PROXY;
  227.  
  228. ICALProtocolHandler.prototype.allowPort =
  229. function (aPort, aScheme)
  230. {
  231.     return false;
  232. }
  233.  
  234. ICALProtocolHandler.prototype.newURI =
  235. function (aSpec, aCharset, aBaseURI)
  236. {
  237.     var url = Components.classes[STANDARDURL_CONTRACTID].
  238.       createInstance(nsIStandardURL);
  239.     url.init(nsIStandardURL.URLTYPE_STANDARD, 8080, aSpec, aCharset, aBaseURI);
  240.  
  241.     return url.QueryInterface(nsIURI);
  242. }
  243.  
  244. ICALProtocolHandler.prototype.newChannel =
  245. function (aURI)
  246. {
  247.     return new BogusChannel (aURI);
  248. }
  249.  
  250. /* protocol handler factory object (ICALProtocolHandler) */
  251. var ICALProtocolHandlerFactory = new Object();
  252.  
  253. ICALProtocolHandlerFactory.createInstance =
  254. function (outer, iid) {
  255.     if (outer != null)
  256.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  257.  
  258.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  259.         throw Components.results.NS_ERROR_INVALID_ARG;
  260.  
  261.     return new ICALProtocolHandler();
  262. }
  263.  
  264. /* bogus webcal channel used by the ICALProtocolHandler */
  265. function BogusChannel (aURI)
  266. {
  267.     this.URI = aURI;
  268.     this.originalURI = aURI;
  269. }
  270.  
  271. BogusChannel.prototype.QueryInterface =
  272. function (iid) {
  273.  
  274.     if (iid.equals(nsIChannel) ||
  275.         iid.equals(nsIRequest) ||
  276.         iid.equals(nsISupports))
  277.         return this;
  278.  
  279.     Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
  280.     return null;
  281. }
  282.  
  283. /* nsIChannel */
  284. BogusChannel.prototype.loadAttributes = null;
  285. BogusChannel.prototype.contentType = "text/calendar";
  286. BogusChannel.prototype.contentLength = 0;
  287. BogusChannel.prototype.owner = null;
  288. BogusChannel.prototype.loadGroup = null;
  289. BogusChannel.prototype.notificationCallbacks = null;
  290. BogusChannel.prototype.securityInfo = null;
  291.  
  292. BogusChannel.prototype.open =
  293. BogusChannel.prototype.asyncOpen =
  294. function ()
  295. {
  296.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  297. }
  298.  
  299. BogusChannel.prototype.asyncOpen =
  300. function (observer, ctxt)
  301. {
  302.     observer.onStartRequest (this, ctxt);
  303. }
  304.  
  305. BogusChannel.prototype.asyncRead =
  306. function (listener, ctxt)
  307. {
  308.     return listener.onStartRequest (this, ctxt);
  309. }
  310.  
  311. /* nsIRequest */
  312. BogusChannel.prototype.isPending =
  313. function ()
  314. {
  315.     return true;
  316. }
  317.  
  318. BogusChannel.prototype.status = Components.results.NS_OK;
  319.  
  320. BogusChannel.prototype.cancel =
  321. function (aStatus)
  322. {
  323.     this.status = aStatus;
  324. }
  325.  
  326. BogusChannel.prototype.suspend =
  327. BogusChannel.prototype.resume =
  328. function ()
  329. {
  330.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  331. }
  332.  
  333. var CalendarModule = new Object();
  334.  
  335. CalendarModule.registerSelf =
  336. function (compMgr, fileSpec, location, type)
  337. {
  338.     // dump("*** Registering -calendar handler.\n");
  339.  
  340.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  341.  
  342.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  343.                                     "Calendar CommandLine Service",
  344.                                     CLINE_SERVICE_CONTRACTID,
  345.                                     fileSpec,
  346.                                     location,
  347.                                     type);
  348.  
  349.     var catman = Components.classes["@mozilla.org/categorymanager;1"]
  350.                            .getService(nsICategoryManager);
  351.     catman.addCategoryEntry("command-line-argument-handlers",
  352.                             "calendar command line handler",
  353.                             CLINE_SERVICE_CONTRACTID, true, true);
  354.     catman.addCategoryEntry("command-line-handler", "m-calendar",
  355.                             CLINE_SERVICE_CONTRACTID, true, true);
  356.  
  357.     // dump("*** Registering text/calendar handler.\n");
  358.     compMgr.registerFactoryLocation(ICALCNT_HANDLER_CID,
  359.                                     "Webcal Content Handler",
  360.                                     ICALCNT_HANDLER_CONTRACTID,
  361.                                     fileSpec,
  362.                                     location,
  363.                                     type);
  364.  
  365.     // dump("*** Registering webcal protocol handler.\n");
  366.     compMgr.registerFactoryLocation(ICALPROT_HANDLER_CID,
  367.                                     "Webcal protocol handler",
  368.                                     ICALPROT_HANDLER_CONTRACTID,
  369.                                     fileSpec,
  370.                                     location,
  371.                                     type);
  372. }
  373.  
  374. CalendarModule.unregisterSelf =
  375. function(compMgr, fileSpec, location)
  376. {
  377.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  378.  
  379.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, fileSpec);
  380.     compMgr.unregisterFactoryLocation(ICALCNT_HANDLER_CID, fileSpec);
  381.     compMgr.unregisterFactoryLocation(ICALPROT_HANDLER_CID, fileSpec);
  382.  
  383.     catman = Components.classes["@mozilla.org/categorymanager;1"]
  384.                        .getService(nsICategoryManager);
  385.     catman.deleteCategoryEntry("command-line-argument-handlers",
  386.                                "calendar command line handler", true);
  387.     catman.deleteCategoryEntry("command-line-handler",
  388.                                "m-calendar", true);
  389. }
  390.  
  391. CalendarModule.getClassObject =
  392. function (compMgr, cid, iid) {
  393.     if (cid.equals(CLINE_SERVICE_CID))
  394.         return CLineFactory;
  395.  
  396.     if (cid.equals(ICALCNT_HANDLER_CID))
  397.         return ICALContentHandlerFactory;
  398.  
  399.     if (cid.equals(ICALPROT_HANDLER_CID))
  400.         return ICALProtocolHandlerFactory;
  401.  
  402.     throw Components.results.NS_ERROR_NO_INTERFACE;
  403. }
  404.  
  405. CalendarModule.canUnload =
  406. function(compMgr)
  407. {
  408.     return true;
  409. }
  410.  
  411. /* entrypoint */
  412. function NSGetModule(compMgr, fileSpec) {
  413.     return CalendarModule;
  414. }
  415.