home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / calendar_windows_latest.xpi / components / calEvent.js < prev    next >
Encoding:
JavaScript  |  2005-12-15  |  8.5 KB  |  264 lines

  1. /* -*- Mode: javascript; tab-width: 20; 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 Oracle Corporation code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  *  Oracle Corporation
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
  24.  *   Mike Shaver <shaver@off.net>
  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. // calEvent.js
  42. //
  43.  
  44. //
  45. // constructor
  46. //
  47. function calEvent() {
  48.     this.wrappedJSObject = this;
  49.     this.initItemBase();
  50.     this.initEvent();
  51.  
  52.     this.eventPromotedProps = {
  53.         "DTSTART": true,
  54.         "DTEND": true,
  55.         "DTSTAMP": true,
  56.         __proto__: this.itemBasePromotedProps
  57.     }
  58. }
  59.  
  60. // var trickery to suppress lib-as-component errors from loader
  61. var calItemBase;
  62.  
  63. var calEventClassInfo = {
  64.     getInterfaces: function (count) {
  65.         var ifaces = [
  66.             Components.interfaces.nsISupports,
  67.             Components.interfaces.calIItemBase,
  68.             Components.interfaces.calIEvent,
  69.             Components.interfaces.calIInternalShallowCopy,
  70.             Components.interfaces.nsIClassInfo
  71.         ];
  72.         count.value = ifaces.length;
  73.         return ifaces;
  74.     },
  75.  
  76.     getHelperForLanguage: function (language) {
  77.         return null;
  78.     },
  79.  
  80.     contractID: "@mozilla.org/calendar/event;1",
  81.     classDescription: "Calendar Event",
  82.     classID: Components.ID("{974339d5-ab86-4491-aaaf-2b2ca177c12b}"),
  83.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  84.     flags: 0
  85. };
  86.  
  87. calEvent.prototype = {
  88.     __proto__: calItemBase ? (new calItemBase()) : {},
  89.  
  90.     QueryInterface: function (aIID) {
  91.         if (aIID.equals(Components.interfaces.calIEvent) ||
  92.             aIID.equals(Components.interfaces.calIInternalShallowCopy))
  93.             return this;
  94.  
  95.         if (aIID.equals(Components.interfaces.nsIClassInfo))
  96.             return calEventClassInfo;
  97.  
  98.         return this.__proto__.__proto__.QueryInterface.call(this, aIID);
  99.     },
  100.  
  101.     cloneShallow: function (aNewParent) {
  102.         var m = new calEvent();
  103.         this.cloneItemBaseInto(m, aNewParent);
  104.  
  105.         return m;
  106.     },
  107.  
  108.     clone: function () {
  109.         var m;
  110.  
  111.         if (this.mParentItem) {
  112.             var clonedParent = this.mParentItem.clone();
  113.             m = clonedParent.recurrenceInfo.getOccurrenceFor (this.recurrenceId);
  114.         } else {
  115.             m = this.cloneShallow(null);
  116.         }
  117.  
  118.         return m;
  119.     },
  120.  
  121.     createProxy: function () {
  122.         if (this.mIsProxy) {
  123.             calDebug("Tried to create a proxy for an existing proxy!\n");
  124.             throw Components.results.NS_ERROR_UNEXPECTED;
  125.         }
  126.  
  127.         var m = new calEvent();
  128.         m.initializeProxy(this);
  129.  
  130.         return m;
  131.     },
  132.  
  133.     makeImmutable: function () {
  134.         this.makeItemBaseImmutable();
  135.     },
  136.  
  137.     initEvent: function() {
  138.         this.startDate = new CalDateTime();
  139.         this.endDate = new CalDateTime();
  140.     },
  141.  
  142.     get duration() {
  143.         return this.endDate.subtractDate(this.startDate);
  144.     },
  145.  
  146.     get recurrenceStartDate() {
  147.         return this.startDate;
  148.     },
  149.  
  150.     icsEventPropMap: [
  151.     { cal: "DTSTART", ics: "startTime" },
  152.     { cal: "DTEND", ics: "endTime" }],
  153.  
  154.     set icalString(value) {
  155.         this.icalComponent = icalFromString(value);
  156.     },
  157.  
  158.     get icalString() {
  159.         const icssvc = Components.
  160.           classes["@mozilla.org/calendar/ics-service;1"].
  161.           getService(Components.interfaces.calIICSService);
  162.         var calcomp = icssvc.createIcalComponent("VCALENDAR");
  163.         calcomp.prodid = "-//Mozilla Calendar//NONSGML Sunbird//EN";
  164.         calcomp.version = "2.0";
  165.         calcomp.addSubcomponent(this.icalComponent);
  166.         return calcomp.serializeToICS();
  167.     },
  168.  
  169.     get icalComponent() {
  170.         const icssvc = Components.
  171.           classes["@mozilla.org/calendar/ics-service;1"].
  172.           getService(Components.interfaces.calIICSService);
  173.         var icalcomp = icssvc.createIcalComponent("VEVENT");
  174.         this.fillIcalComponentFromBase(icalcomp);
  175.         this.mapPropsToICS(icalcomp, this.icsEventPropMap);
  176.         
  177.         var bagenum = this.mProperties.enumerator;
  178.         while (bagenum.hasMoreElements()) {
  179.             var iprop = bagenum.getNext().
  180.                 QueryInterface(Components.interfaces.nsIProperty);
  181.             try {
  182.                 if (!this.eventPromotedProps[iprop.name]) {
  183.                     var icalprop = icssvc.createIcalProperty(iprop.name);
  184.                     icalprop.value = iprop.value;
  185.                     icalcomp.addProperty(icalprop);
  186.                 }
  187.             } catch (e) {
  188.                 dump("XXX failed to set " + iprop.name + " to " + iprop.value +
  189.                 ": " + e + "\n");
  190.             }
  191.         }
  192.         return icalcomp;
  193.     },
  194.  
  195.     eventPromotedProps: null,
  196.  
  197.     set icalComponent(event) {
  198.         this.modify();
  199.         if (event.componentType != "VEVENT") {
  200.             event = event.getFirstSubcomponent("VEVENT");
  201.             if (!event)
  202.                 throw Components.results.NS_ERROR_INVALID_ARG;
  203.         }
  204.  
  205.         this.setItemBaseFromICS(event);
  206.         this.mapPropsFromICS(event, this.icsEventPropMap);
  207.  
  208.         this.importUnpromotedProperties(event, this.eventPromotedProps);
  209.         
  210.         // If there is a duration set on the event, calculate the right
  211.         // end time.
  212.         // XXX This means that serializing later will loose the duration
  213.         //     information, to replace it with a dtend. bug 317786
  214.         if (event.duration) {
  215.             this.endDate = this.startDate.clone();
  216.             this.endDate.addDuration(event.duration);
  217.         }
  218.         
  219.         // If endDate is still invalid neither a end time nor a duration is set
  220.         // on the event. We have to set endDate ourselves.
  221.         // If the start time is a date-time the event ends on the same calendar
  222.         // date and time of day. If the start time is a date the events
  223.         // non-inclusive end is the end of the calendar date.
  224.         if (!this.endDate.isValid) {
  225.             this.endDate = this.startDate.clone();
  226.             if (this.startDate.isDate) {
  227.                 this.endDate.day += 1;
  228.                 this.endDate.normalize();
  229.             }
  230.         }
  231.         
  232.         // Importing didn't really change anything
  233.         this.mDirty = false;
  234.     },
  235.  
  236.     isPropertyPromoted: function (name) {
  237.         return (this.eventPromotedProps[name]);
  238.     },
  239.  
  240.     getOccurrencesBetween: function(aStartDate, aEndDate, aCount) {
  241.         if (this.recurrenceInfo) {
  242.             return this.recurrenceInfo.getOccurrences(aStartDate, aEndDate, 0, aCount);
  243.         }
  244.  
  245.         if ((this.startDate.compare(aStartDate) >= 0 && this.startDate.compare(aEndDate) <= 0) ||
  246.             (this.endDate.compare(aStartDate) >= 0 && this.endDate.compare(aEndDate) <= 0))
  247.         {
  248.             aCount.value = 1;
  249.             return ([ this ]);
  250.         }
  251.  
  252.         aCount.value = 0;
  253.         return null;
  254.     }
  255. };
  256.  
  257. // var to avoid spurious errors when loaded as component during autoreg
  258.  
  259. var makeMemberAttr;
  260. if (makeMemberAttr) {
  261.     makeMemberAttr(calEvent, "DTSTART", null, "startDate", true);
  262.     makeMemberAttr(calEvent, "DTEND", null, "endDate", true);
  263. }
  264.