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 Lightning code.
16 *
17 * The Initial Developer of the Original Code is Simdesk Technologies Inc.
18 * Portions created by the Initial Developer are Copyright (C) 2006
19 * the Initial Developer. All Rights Reserved.
20 *
21 * Contributor(s):
22 * Clint Talbert <ctalbert.moz@gmail.com>
23 * Matthew Willis <lilmatt@mozilla.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 * Constructor of calItipItem object
41 */
calItipItem
42 function calItipItem() {
43 this.wrappedJSObject = this;
44 this.mIsInitialized = false;
45 this.mCurrentItemIndex = 0;
46 }
47
48 calItipItem.prototype = {
ciiGI
49 getInterfaces: function ciiGI(count) {
50 var ifaces = [
51 Components.interfaces.nsIClassInfo,
52 Components.interfaces.nsISupports,
53 Components.interfaces.calIItipItem
54 ];
55 count.value = ifaces.length;
56 return ifaces;
57 },
58
ciiGHFL
59 getHelperForLanguage: function ciiGHFL(aLanguage) {
60 return null;
61 },
62
63 contractID: "@mozilla.org/calendar/itip-item;1",
64 classDescription: "Calendar iTIP item",
65 classID: Components.ID("{f41392ab-dcad-4bad-818f-b3d1631c4d93}"),
66 implementationLanguage: Components.interfaces.nsIProgrammingLanguage.JAVASCRIPT,
67 flags: 0,
68
ciiQI
69 QueryInterface: function ciiQI(aIid) {
70 if (!aIid.equals(Components.interfaces.nsISupports) &&
71 !aIid.equals(Components.interfaces.calIItipItem)) {
72 throw Components.results.NS_ERROR_NO_INTERFACE;
73 }
74
75 return this;
76 },
77
78 mIsSend: false,
get_isSend
79 get isSend() {
80 return this.mIsSend;
81 },
set_isSend
82 set isSend(aValue) {
83 return (this.mIsSend = aValue);
84 },
85
86 mReceivedMethod: null,
get_receivedMethod
87 get receivedMethod() {
88 return this.mReceivedMethod;
89 },
set_receivedMethod
90 set receivedMethod(aMethod) {
91 return (this.mReceivedMethod = aMethod.toUpperCase());
92 },
93
94 mResponseMethod: null,
get_responseMethod
95 get responseMethod() {
96 if (this.mIsInitialized) {
97 var method = null;
98 for each (var prop in this.mPropertiesList) {
99 if (prop.propertyName == "METHOD") {
100 method = prop.value;
101 break;
102 }
103 }
104 return method;
105 } else {
106 throw Components.results.NS_ERROR_NOT_INITIALIZED;
107 }
108 },
set_responseMethod
109 set responseMethod(aMethod) {
110 this.mResponseMethod = aMethod.toUpperCase();
111 // Setting this also sets the global method attribute inside the
112 // encapsulated VCALENDAR.
113 if (this.mIsInitialized) {
114 var methodExists = false;
115 for each (var prop in this.mPropertiesList) {
116 if (prop.propertyName == "METHOD") {
117 methodExists = true;
118 prop.value = this.mResponseMethod;
119 }
120 }
121
122 if (!methodExists) {
123 var newProp = { propertyName: "METHOD",
124 value: this.mResponseMethod };
125 this.mPropertiesList.push(newProp);
126 }
127 } else {
128 throw Components.results.NS_ERROR_NOT_INITIALIZED;
129 }
130 return this.mResponseMethod;
131 },
132
133 mAutoResponse: null,
get_autoResponse
134 get autoResponse() {
135 return this.mAutoResponse;
136 },
set_autoResponse
137 set autoResponse(aValue) {
138 return (this.mAutoResponse = aValue);
139 },
140
141 mTargetCalendar: null,
get_targetCalendar
142 get targetCalendar() {
143 return this.mTargetCalendar;
144 },
set_targetCalendar
145 set targetCalendar(aValue) {
146 return (this.mTargetCalendar = aValue);
147 },
148
149 mLocalStatus: null,
get_localStatus
150 get localStatus() {
151 return this.mLocalStatus;
152 },
set_localStatus
153 set localStatus(aValue) {
154 return (this.mLocalStatus = aValue);
155 },
156
ciiMI
157 modifyItem: function ciiMI(item) {
158 // Bug 348666: This will be used when we support delegation and COUNTER.
159 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
160 },
161
162 mItemList: null,
163 mPropertiesList: null,
164
ciiI
165 init: function ciiI(aIcalString) {
166 var parser = Components.classes["@mozilla.org/calendar/ics-parser;1"].
167 createInstance(Components.interfaces.calIIcsParser);
168 parser.parseString(aIcalString, null);
169 this.mItemList = parser.getItems({});
170 this.mPropertiesList = parser.getProperties({});
171
172 // We set both methods now for safety's sake. It's the ItipProcessor's
173 // responsibility to properly ascertain what the correct response
174 // method is (using user feedback, prefs, etc.) for the given
175 // receivedMethod. The RFC tells us to treat items without a METHOD
176 // as if they were METHOD:REQUEST.
177 var method;
178 for each (var prop in this.mPropertiesList) {
179 if (prop.propertyName == "METHOD") {
180 method = prop.value;
181 }
182 }
183 this.mReceivedMethod = method;
184 this.mResponseMethod = method;
185
186 this.mIsInitialized = true;
187 },
188
ciiC
189 clone: function ciiC() {
190 // Iterate through all the calItems in the original calItipItem to
191 // concatenate all the calItems' icalStrings.
192 var icalString = "";
193
194 var itemList = this.getItemList({ });
195 for (var i = 0; i < itemList.length; i++) {
196 icalString += itemList[i].icalString;
197 }
198
199 // Create a new calItipItem and initialize it using the icalString
200 // from above.
201 var newItem = Components.classes["@mozilla.org/calendar/itip-item;1"].
202 createInstance(Components.interfaces.calIItipItem);
203 newItem.init(icalString);
204
205 // Copy over the exposed attributes.
206 newItem.receivedMethod = this.receivedMethod;
207 newItem.responseMethod = this.responseMethod;
208 newItem.autoResponse = this.autoResponse;
209 newItem.targetCalendar = this.targetCalendar;
210 newItem.localStatus = this.localStatus;
211 newItem.isSend = this.isSend;
212
213 return newItem;
214 },
215
216 /**
217 * This returns both the array and the number of items. An easy way to
218 * call it is: var itemArray = itipItem.getItemList({ });
219 */
ciiGIL
220 getItemList: function ciiGIL(itemCountRef) {
221 if (!this.mIsInitialized || (this.mItemList.length == 0)) {
222 throw Components.results.NS_ERROR_NOT_INITIALIZED;
223 }
224 itemCountRef.value = this.mItemList.length;
225 return this.mItemList;
226 },
227
228 /*getFirstItem: function ciiGFI() {
229 if (!this.mIsInitialized || (this.mItemList.length == 0)) {
230 throw Components.results.NS_ERROR_NOT_INITIALIZED;
231 }
232 this.mCurrentItemIndex = 0;
233 return this.mItemList[0];
234 },
235
236 getNextItem: function ciiGNI() {
237 if (!this.mIsInitialized || (this.mItemList.length == 0)) {
238 throw Components.results.NS_ERROR_NOT_INITIALIZED;
239 }
240 ++this.mCurrentItemIndex;
241 if (this.mCurrentItemIndex < this.mItemList.length) {
242 return this.mItemList[this.mCurrentItemIndex];
243 } else {
244 return null;
245 }
246 },*/
247
248 /**
249 * Note that this code forces the user to respond to all items in the same
250 * way, which is a current limitation of the spec.
251 */
ciiSAS
252 setAttendeeStatus: function ciiSAS(aAttendeeId, aStatus) {
253 // Append "mailto:" to the attendee if it is missing it.
254 var attId = aAttendeeId.toLowerCase();
255 if (!attId.match(/mailto:/i)) {
256 attId = "mailto:" + attId;
257 }
258
259 for each (var item in this.mItemList) {
260 var attendee = item.getAttendeeById(attId);
261 if (attendee) {
262 // XXX BUG 351589: workaround for updating an attendee
263 item.removeAttendee(attendee);
264
265 // Replies should not have the RSVP property.
266 // Since attendee.deleteProperty("RSVP") doesn't work, we must
267 // create a new attendee from scratch WITHOUT the RSVP
268 // property and copy in the other relevant data.
269 // XXX use deleteProperty after bug 358498 is fixed.
270 newAttendee = Components.classes["@mozilla.org/calendar/attendee;1"].
271 createInstance(Components.interfaces.calIAttendee);
272 if (attendee.commonName) {
273 newAttendee.commonName = attendee.commonName;
274 }
275 if (attendee.role) {
276 newAttendee.role = attendee.role;
277 }
278 if (attendee.userType) {
279 newAttendee.userType = attendee.userType;
280 }
281 newAttendee.id = attendee.id;
282 newAttendee.participationStatus = aStatus;
283 item.addAttendee(newAttendee);
284 }
285 }
286 }
287 };