home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2006 June / PCpro_2006_06.ISO / files / firefox / calendar_windows_latest.xpi / components / calTodo.js < prev    next >
Encoding:
JavaScript  |  2005-11-23  |  8.3 KB  |  263 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.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. //
  40. // calTodo.js
  41. //
  42.  
  43. //
  44. // constructor
  45. //
  46. function calTodo() {
  47.     this.wrappedJSObject = this;
  48.     this.initItemBase();
  49.     this.initTodo();
  50.  
  51.     this.todoPromotedProps = {
  52.         "DTSTART": true,
  53.         "DTEND": true,
  54.         "DTSTAMP": true,
  55.         "DUE": true,
  56.         "COMPLETED": true,
  57.         __proto__: this.itemBasePromotedProps
  58.     };
  59. }
  60.  
  61. // var trickery to suppress lib-as-component errors from loader
  62. var calItemBase;
  63.  
  64. var calTodoClassInfo = {
  65.     getInterfaces: function (count) {
  66.         var ifaces = [
  67.             Components.interfaces.nsISupports,
  68.             Components.interfaces.calIItemBase,
  69.             Components.interfaces.calITodo,
  70.             Components.interfaces.calIInternalShallowCopy,
  71.             Components.interfaces.nsIClassInfo
  72.         ];
  73.         count.value = ifaces.length;
  74.         return ifaces;
  75.     },
  76.  
  77.     getHelperForLanguage: function (language) {
  78.         return null;
  79.     },
  80.  
  81.     contractID: "@mozilla.org/calendar/todo;1",
  82.     classDescription: "Calendar Todo",
  83.     classID: Components.ID("{7af51168-6abe-4a31-984d-6f8a3989212d}"),
  84.     implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
  85.     flags: 0
  86. };
  87.  
  88. calTodo.prototype = {
  89.     __proto__: calItemBase ? (new calItemBase()) : {},
  90.  
  91.     QueryInterface: function (aIID) {
  92.         if (aIID.equals(Components.interfaces.calITodo) ||
  93.             aIID.equals(Components.interfaces.calIInternalShallowCopy))
  94.             return this;
  95.  
  96.         if (aIID.equals(Components.interfaces.nsIClassInfo))
  97.             return calTodoClassInfo;
  98.  
  99.         return this.__proto__.__proto__.QueryInterface.call(this, aIID);
  100.     },
  101.  
  102.     initTodo: function () {
  103.         // todos by default don't have any of the dates set, or status, or
  104.         // percentComplete
  105.     },
  106.  
  107.     cloneShallow: function (aNewParent) {
  108.         var m = new calTodo();
  109.         this.cloneItemBaseInto(m, aNewParent);
  110.  
  111.         return m;
  112.     },
  113.  
  114.     clone: function () {
  115.         var m;
  116.  
  117.         if (this.parentItem != this) {
  118.             var clonedParent = this.mParentItem.clone();
  119.             m = clonedParent.recurrenceInfo.getExceptionFor (this.recurrenceId, true);
  120.         } else {
  121.             m = this.cloneShallow(null);
  122.         }
  123.  
  124.         return m;
  125.     },
  126.  
  127.     createProxy: function () {
  128.         if (this.mIsProxy) {
  129.             calDebug("Tried to create a proxy for an existing proxy!\n");
  130.             throw Components.results.NS_ERROR_UNEXPECTED;
  131.         }
  132.  
  133.         var m = new calTodo();
  134.         m.initializeProxy(this);
  135.  
  136.         return m;
  137.     },
  138.  
  139.     makeImmutable: function () {
  140.         this.makeItemBaseImmutable();
  141.     },
  142.  
  143.     get isCompleted() {
  144.         return this.completedDate != null ||
  145.                this.percentComplete == 100 ||
  146.                this.status == "COMPLETED";
  147.     },
  148.     
  149.     set isCompleted(v) {
  150.         if (v) {
  151.             if (!this.completedDate)
  152.                 this.completedDate = NewCalDateTime(new Date);
  153.             this.status = "COMPLETED";
  154.             this.percentComplete = 100;
  155.         } else {
  156.             this.deleteProperty("COMPLETED");
  157.             this.deleteProperty("STATUS");
  158.             this.deleteProperty("PERCENT-COMPLETE");
  159.         }
  160.     },
  161.  
  162.     get recurrenceStartDate() {
  163.         return this.entryDate;
  164.     },
  165.  
  166.     icsEventPropMap: [
  167.     { cal: "DTSTART", ics: "startTime" },
  168.     { cal: "DUE", ics: "dueTime" },
  169.     { cal: "COMPLETED", ics: "completedTime" }],
  170.  
  171.     set icalString(value) {
  172.         this.icalComponent = icalFromString(value);
  173.     },
  174.  
  175.     get icalString() {
  176.         const icssvc = Components.
  177.           classes["@mozilla.org/calendar/ics-service;1"].
  178.           getService(Components.interfaces.calIICSService);
  179.         var calcomp = icssvc.createIcalComponent("VCALENDAR");
  180.         calcomp.prodid = "-//Mozilla Calendar//NONSGML Sunbird//EN";
  181.         calcomp.version = "2.0";
  182.         calcomp.addSubcomponent(this.icalComponent);
  183.         return calcomp.serializeToICS();
  184.     },
  185.  
  186.     get icalComponent() {
  187.         const icssvc = Components.
  188.           classes["@mozilla.org/calendar/ics-service;1"].
  189.           getService(Components.interfaces.calIICSService);
  190.         var icalcomp = icssvc.createIcalComponent("VTODO");
  191.         this.fillIcalComponentFromBase(icalcomp);
  192.         this.mapPropsToICS(icalcomp, this.icsEventPropMap);
  193.  
  194.         var bagenum = this.mProperties.enumerator;
  195.         while (bagenum.hasMoreElements()) {
  196.             var iprop = bagenum.getNext().
  197.                 QueryInterface(Components.interfaces.nsIProperty);
  198.             try {
  199.                 if (!this.todoPromotedProps[iprop.name]) {
  200.                     var icalprop = icssvc.createIcalProperty(iprop.name);
  201.                     icalprop.value = iprop.value;
  202.                     icalcomp.addProperty(icalprop);
  203.                 }
  204.             } catch (e) {
  205.                 // dump("failed to set " + iprop.name + " to " + iprop.value +
  206.                 // ": " + e + "\n");
  207.             }
  208.         }
  209.         return icalcomp;
  210.     },
  211.  
  212.     todoPromotedProps: null,
  213.  
  214.     set icalComponent(todo) {
  215.         this.modify();
  216.         if (todo.componentType != "VTODO") {
  217.             todo = todo.getFirstSubcomponent("VTODO");
  218.             if (!todo)
  219.                 throw Components.results.NS_ERROR_INVALID_ARG;
  220.         }
  221.  
  222.         this.setItemBaseFromICS(todo);
  223.         this.mapPropsFromICS(todo, this.icsEventPropMap);
  224.         this.mIsAllDay = this.mStartDate && this.mStartDate.isDate;
  225.  
  226.         this.importUnpromotedProperties(todo, this.todoPromotedProps);
  227.         // Importing didn't really change anything
  228.         this.mDirty = false;
  229.     },
  230.  
  231.     isPropertyPromoted: function (name) {
  232.         return (this.todoPromotedProps[name]);
  233.     },
  234.  
  235.     getOccurrencesBetween: function(aStartDate, aEndDate, aCount) {
  236.         if (this.recurrenceInfo) {
  237.             return this.recurrenceInfo.getOccurrences(aStartDate, aEndDate, 0, aCount);
  238.         }
  239.         if (!this.entryDate && !this.dueDate)
  240.             return null;
  241.  
  242.         if ((this.entryDate && this.entryDate.compare(aStartDate) >= 0 && this.entryDate.compare(aEndDate) <= 0) ||
  243.             (this.dueDate && this.dueDate.compare(aStartDate) >= 0 && this.dueDate.compare(aEndDate) <= 0))
  244.         {
  245.             aCount.value = 1;
  246.             return ([ this ]);
  247.         }
  248.  
  249.         aCount.value = 0;
  250.         return null;
  251.     }
  252. };
  253.         
  254. // var decl to prevent spurious error messages when loaded as component
  255.  
  256. var makeMemberAttr;
  257. if (makeMemberAttr) {
  258.     makeMemberAttr(calTodo, "DTSTART", null, "entryDate", true);
  259.     makeMemberAttr(calTodo, "DUE", null, "dueDate", true);
  260.     makeMemberAttr(calTodo, "COMPLETED", null, "completedDate", true);
  261.     makeMemberAttr(calTodo, "PERCENT-COMPLETE", 0, "percentComplete", true);
  262. }
  263.