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
|
/* 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/. */
/* exported publishCalendarData, publishCalendarDataDialogResponse,
* publishEntireCalendar, publishEntireCalendarDialogResponse
*/
/* import-globals-from ../../base/content/calendar-views-utils.js */
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
/**
* Show publish dialog, ask for URL and publish all selected items.
*/
function publishCalendarData() {
let args = {};
args.onOk = self.publishCalendarDataDialogResponse;
openDialog(
"chrome://calendar/content/publishDialog.xhtml",
"caPublishEvents",
"chrome,titlebar,modal,resizable",
args
);
}
/**
* Callback method for publishCalendarData() that is called when the user
* presses the OK button in the publish dialog.
*/
function publishCalendarDataDialogResponse(CalendarPublishObject, aProgressDialog) {
publishItemArray(
currentView().getSelectedItems(),
CalendarPublishObject.remotePath,
aProgressDialog
);
}
/**
* Show publish dialog, ask for URL and publish all items from the calendar.
*
* @param {?calICalendar} aCalendar - The calendar that will be published.
* If not specified, the user will be prompted to select a calendar.
*/
function publishEntireCalendar(aCalendar) {
if (!aCalendar) {
let calendars = cal.manager.getCalendars();
if (calendars.length == 1) {
// Do not ask user for calendar if only one calendar exists
aCalendar = calendars[0];
} else {
// Ask user to select the calendar that should be published.
// publishEntireCalendar() will be called again if OK is pressed
// in the dialog and the selected calendar will be passed in.
// Therefore return after openDialog().
let args = {};
args.onOk = publishEntireCalendar;
args.promptText = cal.l10n.getCalString("publishPrompt");
openDialog(
"chrome://calendar/content/chooseCalendarDialog.xhtml",
"_blank",
"chrome,titlebar,modal,resizable",
args
);
return;
}
}
let args = {};
let publishObject = {};
args.onOk = self.publishEntireCalendarDialogResponse;
publishObject.calendar = aCalendar;
// restore the remote ics path preference from the calendar passed in
let remotePath = aCalendar.getProperty("remote-ics-path");
if (remotePath) {
publishObject.remotePath = remotePath;
}
args.publishObject = publishObject;
openDialog(
"chrome://calendar/content/publishDialog.xhtml",
"caPublishEvents",
"chrome,titlebar,modal,resizable",
args
);
}
/**
* Callback method for publishEntireCalendar() that is called when the user
* presses the OK button in the publish dialog.
*/
async function publishEntireCalendarDialogResponse(CalendarPublishObject, aProgressDialog) {
// store the selected remote ics path as a calendar preference
CalendarPublishObject.calendar.setProperty("remote-ics-path", CalendarPublishObject.remotePath);
aProgressDialog.onStartUpload();
let oldCalendar = CalendarPublishObject.calendar;
let items = await oldCalendar.getItemsAsArray(
Ci.calICalendar.ITEM_FILTER_ALL_ITEMS,
0,
null,
null
);
publishItemArray(items, CalendarPublishObject.remotePath, aProgressDialog);
}
function publishItemArray(aItemArray, aPath, aProgressDialog) {
let outputStream;
let inputStream;
let storageStream;
let icsURL = Services.io.newURI(aPath);
let channel = Services.io.newChannelFromURI(
icsURL,
null,
Services.scriptSecurityManager.getSystemPrincipal(),
null,
Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
Ci.nsIContentPolicy.TYPE_OTHER
);
if (icsURL.schemeIs("webcal")) {
icsURL.scheme = "http";
}
if (icsURL.schemeIs("webcals")) {
icsURL.scheme = "https";
}
switch (icsURL.scheme) {
case "http":
case "https":
channel = channel.QueryInterface(Ci.nsIHttpChannel);
break;
case "file":
channel = channel.QueryInterface(Ci.nsIFileChannel);
break;
default:
dump("No such scheme\n");
return;
}
let uploadChannel = channel.QueryInterface(Ci.nsIUploadChannel);
uploadChannel.notificationCallbacks = notificationCallbacks;
storageStream = Cc["@mozilla.org/storagestream;1"].createInstance(Ci.nsIStorageStream);
storageStream.init(32768, 0xffffffff, null);
outputStream = storageStream.getOutputStream(0);
let serializer = Cc["@mozilla.org/calendar/ics-serializer;1"].createInstance(
Ci.calIIcsSerializer
);
serializer.addItems(aItemArray);
// Outlook requires METHOD:PUBLISH property:
let methodProp = cal.icsService.createIcalProperty("METHOD");
methodProp.value = "PUBLISH";
serializer.addProperty(methodProp);
serializer.serializeToStream(outputStream);
outputStream.close();
inputStream = storageStream.newInputStream(0);
uploadChannel.setUploadStream(inputStream, "text/calendar", -1);
try {
channel.asyncOpen(new PublishingListener(aProgressDialog));
} catch (e) {
Services.prompt.alert(
null,
cal.l10n.getCalString("genericErrorTitle"),
cal.l10n.getCalString("otherPutError", [e.message])
);
}
}
/** @implements {nsIInterfaceRequestor} */
var notificationCallbacks = {
getInterface(iid, instance) {
if (iid.equals(Ci.nsIAuthPrompt2)) {
if (!this.calAuthPrompt) {
return new cal.auth.Prompt();
}
}
if (iid.equals(Ci.nsIAuthPrompt)) {
// use the window watcher service to get a nsIAuthPrompt impl
return Services.ww.getNewAuthPrompter(null);
}
throw Components.Exception(`${iid} not implemented`, Cr.NS_ERROR_NO_INTERFACE);
},
};
/**
* Listener object to pass to `channel.asyncOpen()`. A reference to the current dialog window
* passed to the constructor provides access to the dialog once the request is done.
*
* @implements {nsIStreamListener}
*/
class PublishingListener {
QueryInterface = ChromeUtils.generateQI(["nsIStreamListener"]);
constructor(progressDialog) {
this.progressDialog = progressDialog;
}
onStartRequest(request) {}
onStopRequest(request, status) {
let channel;
let requestSucceeded;
try {
channel = request.QueryInterface(Ci.nsIHttpChannel);
requestSucceeded = channel.requestSucceeded;
} catch (e) {
// Don't fail if it is not an http channel, will be handled below.
}
if (channel && !requestSucceeded) {
this.progressDialog.wrappedJSObject.onStopUpload(0);
let body = cal.l10n.getCalString("httpPutError", [
channel.responseStatus,
channel.responseStatusText,
]);
Services.prompt.alert(null, cal.l10n.getCalString("genericErrorTitle"), body);
} else if (!channel && !Components.isSuccessCode(request.status)) {
this.progressDialog.wrappedJSObject.onStopUpload(0);
// XXX this should be made human-readable.
let body = cal.l10n.getCalString("otherPutError", [request.status.toString(16)]);
Services.prompt.alert(null, cal.l10n.getCalString("genericErrorTitle"), body);
} else {
this.progressDialog.wrappedJSObject.onStopUpload(100);
}
}
onDataAvailable(request, inStream, sourceOffset, count) {}
}
|