1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
var EXPORTED_SYMBOLS = ["CalIcalProperty", "CalICSService"];
const { ICAL, unwrapSetter, unwrapSingle, wrapGetter } = ChromeUtils.import(
"resource:///modules/calendar/Ical.jsm"
);
const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
const lazy = {};
ChromeUtils.defineModuleGetter(lazy, "CalDateTime", "resource:///modules/CalDateTime.jsm");
ChromeUtils.defineModuleGetter(lazy, "CalDuration", "resource:///modules/CalDuration.jsm");
ChromeUtils.defineModuleGetter(lazy, "CalTimezone", "resource:///modules/CalTimezone.jsm");
function CalIcalProperty(innerObject) {
this.innerObject = innerObject || new ICAL.Property();
this.wrappedJSObject = this;
}
CalIcalProperty.prototype = {
QueryInterface: ChromeUtils.generateQI(["calIIcalProperty"]),
classID: Components.ID("{423ac3f0-f612-48b3-953f-47f7f8fd705b}"),
get icalString() {
return this.innerObject.toICALString() + ICAL.newLineChar;
},
get icalProperty() {
return this.innerObject;
},
set icalProperty(val) {
this.innerObject = val;
},
get parent() {
return this.innerObject.parent;
},
toString() {
return this.innerObject.toICAL();
},
get value() {
// Unescaped value for properties of TEXT, escaped otherwise.
if (this.innerObject.type == "text") {
return this.innerObject.getValues().join(",");
}
return this.valueAsIcalString;
},
set value(val) {
// Unescaped value for properties of TEXT, escaped otherwise.
if (this.innerObject.type == "text") {
this.innerObject.setValue(val);
return;
}
this.valueAsIcalString = val;
},
get valueAsIcalString() {
let propertyStr = this.innerObject.toICALString();
if (propertyStr.match(/:/g).length == 1) {
// For property containing only one colon, e.g. `GEO:latitude;longitude`,
// the left hand side must be the property name, the right hand side must
// be property value.
return propertyStr.slice(propertyStr.indexOf(":") + 1);
}
// For property containing many or no colons, retrieve the property value
// according to its type. An example is
// `ATTENDEE;MEMBER="mailto:foo@example.com": mailto:bar@example.com`
let type = this.innerObject.type;
return this.innerObject
.getValues()
.map(val => {
if (type == "text") {
return ICAL.stringify.value(val, type, ICAL.design.icalendar);
} else if (typeof val == "number" || typeof val == "string") {
return val;
} else if ("toICALString" in val) {
return val.toICALString();
}
return val.toString();
})
.join(",");
},
set valueAsIcalString(val) {
let mockLine = this.propertyName + ":" + val;
let prop = ICAL.Property.fromString(mockLine, ICAL.design.icalendar);
if (this.innerObject.isMultiValue) {
this.innerObject.setValues(prop.getValues());
} else {
this.innerObject.setValue(prop.getFirstValue());
}
},
get valueAsDatetime() {
let val = this.innerObject.getFirstValue();
let isIcalTime =
val && typeof val == "object" && "icalclass" in val && val.icalclass == "icaltime";
return isIcalTime ? new lazy.CalDateTime(val) : null;
},
set valueAsDatetime(rawval) {
unwrapSetter(
ICAL.Time,
rawval,
function (val) {
if (
val &&
val.zone &&
val.zone != ICAL.Timezone.utcTimezone &&
val.zone != ICAL.Timezone.localTimezone
) {
this.innerObject.setParameter("TZID", val.zone.tzid);
if (this.parent) {
let tzref = wrapGetter(lazy.CalTimezone, val.zone);
this.parent.addTimezoneReference(tzref);
}
} else {
this.innerObject.removeParameter("TZID");
}
this.innerObject.setValue(val);
},
this
);
},
get propertyName() {
return this.innerObject.name.toUpperCase();
},
getParameter(name) {
// Unfortunately getting the "VALUE" parameter won't work, since in
// jCal it has been translated to the value type id.
if (name == "VALUE") {
let defaultType = this.innerObject.getDefaultType();
if (this.innerObject.type != defaultType) {
// Default type doesn't match object type, so we have a VALUE
// parameter
return this.innerObject.type.toUpperCase();
}
}
return this.innerObject.getParameter(name.toLowerCase());
},
setParameter(name, value) {
// Similar problems for setting the value parameter. Calendar code
// expects setting the value parameter to just change the value type
// and attempt to use the previous value as the new one. To do this in
// ICAL.js we need to save the value, reset the type and then try to
// set the value again.
if (name == "VALUE") {
let oldValues;
let type = this.innerObject.type;
let designSet = this.innerObject._designSet;
let wasMultiValue = this.innerObject.isMultiValue;
if (wasMultiValue) {
oldValues = this.innerObject.getValues();
} else {
let oldValue = this.innerObject.getFirstValue();
oldValues = oldValue ? [oldValue] : [];
}
this.innerObject.resetType(value.toLowerCase());
try {
oldValues = oldValues.map(oldValue => {
let strvalue = ICAL.stringify.value(oldValue.toString(), type, designSet);
return ICAL.parse._parseValue(strvalue, value, designSet);
});
} catch (e) {
// If there was an error reparsing the value, then just keep it
// empty.
oldValues = null;
}
if (oldValues && oldValues.length) {
if (wasMultiValue && this.innerObject.isMultiValue) {
this.innerObject.setValues(oldValues);
} else {
this.innerObject.setValue(oldValues.join(","));
}
}
} else {
this.innerObject.setParameter(name.toLowerCase(), value);
}
},
removeParameter(name) {
// Again, VALUE needs special handling. Removing the value parameter is
// kind of like resetting it to the default type. So find out the
// default type and then set the value parameter to it.
if (name == "VALUE") {
let propname = this.innerObject.name.toLowerCase();
if (propname in ICAL.design.icalendar.property) {
let details = ICAL.design.icalendar.property[propname];
if ("defaultType" in details) {
this.setParameter("VALUE", details.defaultType);
}
}
} else {
this.innerObject.removeParameter(name.toLowerCase());
}
},
clearXParameters() {
cal.WARN(
"calIICSService::clearXParameters is no longer implemented, please use removeParameter"
);
},
paramIterator: null,
getFirstParameterName() {
let innerObject = this.innerObject;
this.paramIterator = (function* () {
let defaultType = innerObject.getDefaultType();
if (defaultType != innerObject.type) {
yield "VALUE";
}
let paramNames = Object.keys(innerObject.jCal[1] || {});
for (let name of paramNames) {
yield name.toUpperCase();
}
})();
return this.getNextParameterName();
},
getNextParameterName() {
if (this.paramIterator) {
let next = this.paramIterator.next();
if (next.done) {
this.paramIterator = null;
}
return next.value;
}
return this.getFirstParameterName();
},
};
function calIcalComponent(innerObject) {
this.innerObject = innerObject || new ICAL.Component();
this.wrappedJSObject = this;
this.mReferencedZones = {};
}
calIcalComponent.prototype = {
QueryInterface: ChromeUtils.generateQI(["calIIcalComponent"]),
classID: Components.ID("{51ac96fd-1279-4439-a85b-6947b37f4cea}"),
clone() {
return new calIcalComponent(new ICAL.Component(this.innerObject.toJSON()));
},
get parent() {
return wrapGetter(calIcalComponent, this.innerObject.parent);
},
get icalTimezone() {
return this.innerObject.name == "vtimezone" ? this.innerObject : null;
},
get icalComponent() {
return this.innerObject;
},
set icalComponent(val) {
this.innerObject = val;
},
componentIterator: null,
getFirstSubcomponent(kind) {
if (kind == "ANY") {
kind = null;
} else if (kind) {
kind = kind.toLowerCase();
}
let innerObject = this.innerObject;
this.componentIterator = (function* () {
let comps = innerObject.getAllSubcomponents(kind);
if (comps) {
for (let comp of comps) {
yield new calIcalComponent(comp);
}
}
})();
return this.getNextSubcomponent(kind);
},
getNextSubcomponent(kind) {
if (this.componentIterator) {
let next = this.componentIterator.next();
if (next.done) {
this.componentIterator = null;
}
return next.value;
}
return this.getFirstSubcomponent(kind);
},
get componentType() {
return this.innerObject.name.toUpperCase();
},
get uid() {
return this.innerObject.getFirstPropertyValue("uid");
},
set uid(val) {
this.innerObject.updatePropertyWithValue("uid", val);
},
get prodid() {
return this.innerObject.getFirstPropertyValue("prodid");
},
set prodid(val) {
this.innerObject.updatePropertyWithValue("prodid", val);
},
get version() {
return this.innerObject.getFirstPropertyValue("version");
},
set version(val) {
this.innerObject.updatePropertyWithValue("version", val);
},
get method() {
return this.innerObject.getFirstPropertyValue("method");
},
set method(val) {
this.innerObject.updatePropertyWithValue("method", val);
},
get status() {
return this.innerObject.getFirstPropertyValue("status");
},
set status(val) {
this.innerObject.updatePropertyWithValue("status", val);
},
get summary() {
return this.innerObject.getFirstPropertyValue("summary");
},
set summary(val) {
this.innerObject.updatePropertyWithValue("summary", val);
},
get description() {
return this.innerObject.getFirstPropertyValue("description");
},
set description(val) {
this.innerObject.updatePropertyWithValue("description", val);
},
get location() {
return this.innerObject.getFirstPropertyValue("location");
},
set location(val) {
this.innerObject.updatePropertyWithValue("location", val);
},
get categories() {
return this.innerObject.getFirstPropertyValue("categories");
},
set categories(val) {
this.innerObject.updatePropertyWithValue("categories", val);
},
get URL() {
return this.innerObject.getFirstPropertyValue("url");
},
set URL(val) {
this.innerObject.updatePropertyWithValue("url", val);
},
get priority() {
// If there is no value for this integer property, then we must return
// the designated INVALID_VALUE.
const INVALID_VALUE = Ci.calIIcalComponent.INVALID_VALUE;
let prop = this.innerObject.getFirstProperty("priority");
let val = prop ? prop.getFirstValue() : null;
return val === null ? INVALID_VALUE : val;
},
set priority(val) {
this.innerObject.updatePropertyWithValue("priority", val);
},
_setTimeAttr(propName, val) {
let prop = this.innerObject.updatePropertyWithValue(propName, val);
if (
val &&
val.zone &&
val.zone != ICAL.Timezone.utcTimezone &&
val.zone != ICAL.Timezone.localTimezone
) {
prop.setParameter("TZID", val.zone.tzid);
this.addTimezoneReference(wrapGetter(lazy.CalTimezone, val.zone));
} else {
prop.removeParameter("TZID");
}
},
get startTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("dtstart"));
},
set startTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "dtstart"), this);
},
get endTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("dtend"));
},
set endTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "dtend"), this);
},
get duration() {
return wrapGetter(lazy.CalDuration, this.innerObject.getFirstPropertyValue("duration"));
},
get dueTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("due"));
},
set dueTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "due"), this);
},
get stampTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("dtstamp"));
},
set stampTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "dtstamp"), this);
},
get createdTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("created"));
},
set createdTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "created"), this);
},
get completedTime() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("completed"));
},
set completedTime(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "completed"), this);
},
get lastModified() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("last-modified"));
},
set lastModified(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "last-modified"), this);
},
get recurrenceId() {
return wrapGetter(lazy.CalDateTime, this.innerObject.getFirstPropertyValue("recurrence-id"));
},
set recurrenceId(val) {
unwrapSetter(ICAL.Time, val, this._setTimeAttr.bind(this, "recurrence-id"), this);
},
serializeToICS() {
return this.innerObject.toString() + ICAL.newLineChar;
},
toString() {
return this.innerObject.toString();
},
addSubcomponent(comp) {
comp.getReferencedTimezones().forEach(this.addTimezoneReference, this);
let jscomp = unwrapSingle(ICAL.Component, comp);
this.innerObject.addSubcomponent(jscomp);
},
propertyIterator: null,
getFirstProperty(kind) {
if (kind == "ANY") {
kind = null;
} else if (kind) {
kind = kind.toLowerCase();
}
let innerObject = this.innerObject;
this.propertyIterator = (function* () {
let props = innerObject.getAllProperties(kind);
if (!props) {
return;
}
for (let prop of props) {
let hell = prop.getValues();
if (hell.length > 1) {
// Uh oh, multiple property values. Our code expects each as one
// property. I hate API incompatibility!
for (let devil of hell) {
let thisprop = new ICAL.Property(prop.toJSON(), prop.parent);
thisprop.removeAllValues();
thisprop.setValue(devil);
yield new CalIcalProperty(thisprop);
}
} else {
yield new CalIcalProperty(prop);
}
}
})();
return this.getNextProperty(kind);
},
getNextProperty(kind) {
if (this.propertyIterator) {
let next = this.propertyIterator.next();
if (next.done) {
this.propertyIterator = null;
}
return next.value;
}
return this.getFirstProperty(kind);
},
_getNextParentVCalendar() {
let vcalendar = this; // eslint-disable-line consistent-this
while (vcalendar && vcalendar.componentType != "VCALENDAR") {
vcalendar = vcalendar.parent;
}
return vcalendar || this;
},
addProperty(prop) {
try {
let datetime = prop.valueAsDatetime;
if (datetime && datetime.timezone) {
this._getNextParentVCalendar().addTimezoneReference(datetime.timezone);
}
} catch (e) {
// If there is an issue adding the timezone reference, don't make
// that break adding the property.
}
let jsprop = unwrapSingle(ICAL.Property, prop);
this.innerObject.addProperty(jsprop);
},
addTimezoneReference(timezone) {
if (timezone) {
if (!(timezone.tzid in this.mReferencedZones) && this.componentType == "VCALENDAR") {
let comp = timezone.icalComponent;
if (comp) {
this.addSubcomponent(comp);
}
}
this.mReferencedZones[timezone.tzid] = timezone;
}
},
getReferencedTimezones(aCount) {
return Object.keys(this.mReferencedZones).map(timezone => this.mReferencedZones[timezone]);
},
serializeToICSStream() {
let data = this.innerObject.toString();
let stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
Ci.nsIStringInputStream
);
stream.setUTF8Data(data, data.length);
return stream;
},
};
function CalICSService() {
this.wrappedJSObject = this;
}
CalICSService.prototype = {
QueryInterface: ChromeUtils.generateQI(["calIICSService"]),
classID: Components.ID("{c61cb903-4408-41b3-bc22-da0b27efdfe1}"),
parseICS(serialized) {
let comp = ICAL.parse(serialized);
return new calIcalComponent(new ICAL.Component(comp));
},
parseICSAsync(serialized, listener) {
let worker = new ChromeWorker("resource:///components/calICSService-worker.js");
worker.onmessage = function (event) {
let icalComp = new calIcalComponent(new ICAL.Component(event.data));
listener.onParsingComplete(Cr.OK, icalComp);
};
worker.onerror = function (event) {
cal.ERROR(`Parsing failed; ${event.message}. ICS data:\n${serialized}`);
listener.onParsingComplete(Cr.NS_ERROR_FAILURE, null);
};
worker.postMessage(serialized);
},
createIcalComponent(kind) {
return new calIcalComponent(new ICAL.Component(kind.toLowerCase()));
},
createIcalProperty(kind) {
return new CalIcalProperty(new ICAL.Property(kind.toLowerCase()));
},
createIcalPropertyFromString(str) {
return new CalIcalProperty(ICAL.Property.fromString(str.trim(), ICAL.design.icalendar));
},
};
|