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
|
/* 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/. */
"use strict";
var EXPORTED_SYMBOLS = [
"CalTransactionManager",
"CalTransaction",
"CalBatchTransaction",
"CalAddTransaction",
"CalModifyTransaction",
"CalDeleteTransaction",
];
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
const OP_ADD = Ci.calIOperationListener.ADD;
const OP_MODIFY = Ci.calIOperationListener.MODIFY;
const OP_DELETE = Ci.calIOperationListener.DELETE;
let transactionManager = null;
/**
* CalTransactionManager is used to track user initiated operations on calendar
* items. These transactions can be undone or repeated when appropriate.
*
* This implementation is used instead of nsITransactionManager because better
* support for async transactions and access to batch transactions is needed
* which nsITransactionManager does not provide.
*/
class CalTransactionManager {
/**
* Contains transactions executed by the transaction manager than can be
* undone.
*
* @type {CalTransaction}
*/
undoStack = [];
/**
* Contains transactions that have been undone by the transaction manager and
* can be redone again later if desired.
*
* @type {CalTransaction}
*/
redoStack = [];
/**
* Provides a singleton instance of the CalTransactionManager.
*
* @returns {CalTransactionManager}
*/
static getInstance() {
if (!transactionManager) {
transactionManager = new CalTransactionManager();
}
return transactionManager;
}
/**
* @typedef {object} ExtResponse
* @property {number} responseMode One of the calIItipItem.autoResponse values.
*/
/**
* @typedef {"add" | "modify" | "delete"} Action
*/
/**
* Adds a CalTransaction to the internal stack. The transaction will be
* executed and its resulting Promise returned.
*
* @param {CalTransaction} trn - The CalTransaction to add to the stack and
* execute.
*/
async commit(trn) {
this.undoStack.push(trn);
return trn.doTransaction();
}
/**
* Creates and pushes a new CalBatchTransaction onto the internal stack.
* The created transaction is returned and can be used to combine multiple
* transactions into one.
*
* @returns {CalBatchTrasaction}
*/
beginBatch() {
let trn = new CalBatchTransaction();
this.undoStack.push(trn);
return trn;
}
/**
* peekUndoStack provides the top transaction on the undo stack (if any)
* without modifying the stack.
*
* @returns {CalTransaction?}
*/
peekUndoStack() {
return this.undoStack.at(-1);
}
/**
* Undo the transaction at the top of the undo stack.
*
* @throws - NS_ERROR_FAILURE if the undo stack is empty.
*/
async undo() {
if (!this.undoStack.length) {
throw new Components.Exception(
"CalTransactionManager: undo stack is empty!",
Cr.NS_ERROR_FAILURE
);
}
let trn = this.undoStack.pop();
this.redoStack.push(trn);
return trn.undoTransaction();
}
/**
* Returns true if it is possible to undo the transaction at the top of the
* undo stack.
*
* @returns {boolean}
*/
canUndo() {
let trn = this.peekUndoStack();
return Boolean(trn?.canWrite());
}
/**
* peekRedoStack provides the top transaction on the redo stack (if any)
* without modifying the stack.
*
* @returns {CalTransaction?}
*/
peekRedoStack() {
return this.redoStack.at(-1);
}
/**
* Redo the transaction at the top of the redo stack.
*
* @throws - NS_ERROR_FAILURE if the redo stack is empty.
*/
async redo() {
if (!this.redoStack.length) {
throw new Components.Exception(
"CalTransactionManager: redo stack is empty!",
Cr.NS_ERROR_FAILURE
);
}
let trn = this.redoStack.pop();
this.undoStack.push(trn);
return trn.doTransaction();
}
/**
* Returns true if it is possible to redo the transaction at the top of the
* redo stack.
*
* @returns {boolean}
*/
canRedo() {
let trn = this.peekRedoStack();
return Boolean(trn?.canWrite());
}
}
/**
* CalTransaction represents a single, atomic user operation on one or more
* calendar items.
*/
class CalTransaction {
/**
* Indicates whether the calendar of the transaction's target item(s) can be
* written to.
*
* @returns {boolean}
*/
canWrite() {
return false;
}
/**
* Executes the transaction.
*/
async doTransaction() {}
/**
* Executes the "undo" action of the transaction.
*/
async undoTransaction() {}
}
/**
* CalBatchTransaction is used for batch transactions where multiple transactions
* treated as one is desired. For example; where the user selects and deletes
* more than one event.
*/
class CalBatchTransaction extends CalTransaction {
/**
* Stores the transactions that belong to the batch.
*
* @type {CalTransaction[]}
*/
transactions = [];
/**
* Similar to the CalTransactionManager method except the transaction will be
* added to the batch.
*/
async commit(trn) {
this.transactions.push(trn);
return trn.doTransaction();
}
canWrite() {
return Boolean(this.transactions.length && this.transactions.every(trn => trn.canWrite()));
}
async doTransaction() {
for (let trn of this.transactions) {
await trn.doTransaction();
}
}
async undoTransaction() {
for (let trn of this.transactions.slice().reverse()) {
await trn.undoTransaction();
}
}
}
/**
* CalBaseTransaction serves as the base for add/modify/delete operations.
*/
class CalBaseTransaction extends CalTransaction {
/**
* @type {calICalendar}
*/
calendar = null;
/**
* @type {calIItemBase}
*/
item = null;
/**
* @type {calIItemBase}
*/
oldItem = null;
/**
* @type {calICalendar}
*/
oldCalendar = null;
/**
* @type {ExtResponse}
*/
extResponse = null;
/**
* @private
* @param {calIItemBase} item
* @param {calICalendar} calendar
* @param {calIItemBase?} oldItem
* @param {object?} extResponse
*/
constructor(item, calendar, oldItem, extResponse) {
super();
this.item = item;
this.calendar = calendar;
this.oldItem = oldItem;
this.extResponse = extResponse;
}
_dispatch(opType, item, oldItem) {
cal.itip.checkAndSend(opType, item, oldItem, this.extResponse);
}
canWrite() {
if (itemWritable(this.item)) {
return this instanceof CalModifyTransaction ? itemWritable(this.oldItem) : true;
}
return false;
}
}
/**
* CalAddTransaction handles additions.
*/
class CalAddTransaction extends CalBaseTransaction {
async doTransaction() {
let item = await this.calendar.addItem(this.item);
this._dispatch(OP_ADD, item, this.oldItem);
this.item = item;
}
async undoTransaction() {
await this.calendar.deleteItem(this.item);
this._dispatch(OP_DELETE, this.item, this.item);
this.oldItem = this.item;
}
}
/**
* CalModifyTransaction handles modifications.
*/
class CalModifyTransaction extends CalBaseTransaction {
async doTransaction() {
let item;
if (this.item.calendar.id == this.oldItem.calendar.id) {
item = await this.calendar.modifyItem(
cal.itip.prepareSequence(this.item, this.oldItem),
this.oldItem
);
this._dispatch(OP_MODIFY, item, this.oldItem);
} else {
this.oldCalendar = this.oldItem.calendar;
item = await this.calendar.addItem(this.item);
this._dispatch(OP_ADD, item, this.oldItem);
await this.oldItem.calendar.deleteItem(this.oldItem);
this._dispatch(OP_DELETE, this.oldItem, this.oldItem);
}
this.item = item;
}
async undoTransaction() {
if (this.oldItem.calendar.id == this.item.calendar.id) {
await this.calendar.modifyItem(cal.itip.prepareSequence(this.oldItem, this.item), this.item);
this._dispatch(OP_MODIFY, this.oldItem, this.oldItem);
} else {
await this.calendar.deleteItem(this.item);
this._dispatch(OP_DELETE, this.item, this.item);
await this.oldCalendar.addItem(this.oldItem);
this._dispatch(OP_ADD, this.oldItem, this.item);
}
}
}
/**
* CalDeleteTransaction handles deletions.
*/
class CalDeleteTransaction extends CalBaseTransaction {
async doTransaction() {
await this.calendar.deleteItem(this.item);
this._dispatch(OP_DELETE, this.item, this.oldItem);
}
async undoTransaction() {
await this.calendar.addItem(this.item);
this._dispatch(OP_ADD, this.item, this.item);
}
}
/**
* Checks whether an item's calendar can be written to.
*
* @param {calIItemBase} item
*/
function itemWritable(item) {
return (
item &&
item.calendar &&
cal.acl.isCalendarWritable(item.calendar) &&
cal.acl.userCanAddItemsToCalendar(item.calendar)
);
}
|