/usr/lib/thunderbird-addons/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/chrome/calendar/content/calendar/import-export.js is in xul-ext-lightning 1:24.4.0+build1-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* 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/. */
// File constants copied from file-utils.js
const MODE_RDONLY = 0x01;
const MODE_WRONLY = 0x02;
const MODE_RDWR = 0x04;
const MODE_CREATE = 0x08;
const MODE_APPEND = 0x10;
const MODE_TRUNCATE = 0x20;
const MODE_SYNC = 0x40;
const MODE_EXCL = 0x80;
/**
* Shows a file dialog, reads the selected file(s) and tries to parse events from it.
*
* @param aCalendar (optional) If specified, the items will be imported directly
* into the calendar
*/
function loadEventsFromFile(aCalendar) {
const nsIFilePicker = Components.interfaces.nsIFilePicker;
let fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window,
calGetString("calendar", "filepickerTitleImport"),
nsIFilePicker.modeOpen);
fp.defaultExtension = "ics";
// Get a list of importers
let contractids = new Array();
let catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
let catenum = catman.enumerateCategory('cal-importers');
let currentListLength = 0;
let defaultCIDIndex = 0;
while (catenum.hasMoreElements()) {
let entry = catenum.getNext();
entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
let contractid = catman.getCategoryEntry('cal-importers', entry);
let importer;
try {
importer = Components.classes[contractid]
.getService(Components.interfaces.calIImporter);
} catch (e) {
cal.WARN("Could not initialize importer: " + contractid + "\nError: " + e);
continue;
}
let types = importer.getFileTypes({});
for each (let type in types) {
fp.appendFilter(type.description, type.extensionFilter);
if (type.extensionFilter=="*." + fp.defaultExtension) {
fp.filterIndex = currentListLength;
defaultCIDIndex = currentListLength;
}
contractids.push(contractid);
currentListLength++;
}
}
let rv = fp.show();
if (rv != nsIFilePicker.returnCancel &&
fp.file && fp.file.path && fp.file.path.length > 0) {
let filterIndex = fp.filterIndex;
if (fp.filterIndex < 0 || fp.filterIndex > contractids.length) {
// For some reason the wrong filter was selected, assume default extension
filterIndex = defaultCIDIndex;
}
let filePath = fp.file.path;
let importer = Components.classes[contractids[filterIndex]]
.getService(Components.interfaces.calIImporter);
const nsIFileInputStream = Components.interfaces.nsIFileInputStream;
const nsIScriptableInputStream = Components.interfaces.nsIScriptableInputStream;
let inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(nsIFileInputStream);
let items = [];
try {
inputStream.init( fp.file, MODE_RDONLY, parseInt("0444", 8), {});
items = importer.importFromStream(inputStream, {});
} catch(ex) {
switch (ex.result) {
case Components.interfaces.calIErrors.INVALID_TIMEZONE:
showError(calGetString("calendar", "timezoneError", [filePath]));
break;
default:
showError(calGetString("calendar", "unableToRead") + filePath + "\n"+ ex);
}
} finally {
inputStream.close();
}
if (aCalendar) {
putItemsIntoCal(aCalendar, items);
return;
}
let calendars = cal.getCalendarManager().getCalendars({});
calendars = calendars.filter(isCalendarWritable);
if (calendars.length < 1) {
// XXX alert something?
return;
} else if (calendars.length == 1) {
// There's only one calendar, so it's silly to ask what calendar
// the user wants to import into.
putItemsIntoCal(calendars[0], items, filePath);
} else {
// Ask what calendar to import into
let args = new Object();
args.onOk = function putItems(aCal) { putItemsIntoCal(aCal, items, filePath); };
args.calendars = calendars;
args.promptText = calGetString("calendar", "importPrompt");
openDialog("chrome://calendar/content/chooseCalendarDialog.xul",
"_blank", "chrome,titlebar,modal,resizable", args);
}
}
}
/**
* Put items into a certain calendar, catching errors and showing them to the
* user.
*
* @param destCal The destination calendar.
* @param aItems An array of items to put into the calendar.
* @param aFilePath The original file path, for error messages.
*/
function putItemsIntoCal(destCal, aItems, aFilePath) {
// Set batch for the undo/redo transaction manager
startBatchTransaction();
// And set batch mode on the calendar, to tell the views to not
// redraw until all items are imported
destCal.startBatch();
// This listener is needed to find out when the last addItem really
// finished. Using a counter to find the last item (which might not
// be the last item added)
var count = 0;
var failedCount = 0;
var duplicateCount = 0;
// Used to store the last error. Only the last error, because we don't
// wan't to bomb the user with thousands of error messages in case
// something went really wrong.
// (example of something very wrong: importing the same file twice.
// quite easy to trigger, so we really should do this)
var lastError;
var listener = {
onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail) {
count++;
if (!Components.isSuccessCode(aStatus)) {
if (aStatus == Components.interfaces.calIErrors.DUPLICATE_ID) {
duplicateCount++;
} else {
failedCount++;
lastError = aStatus;
}
}
// See if it is time to end the calendar's batch.
if (count == aItems.length) {
destCal.endBatch();
if (!failedCount && duplicateCount) {
showError(calGetString("calendar", "duplicateError", [duplicateCount, aFilePath]));
} else if (failedCount) {
showError(calGetString("calendar", "importItemsFailed", [failedCount, lastError.toString()]));
}
}
}
}
for each (let item in aItems) {
// XXX prompt when finding a duplicate.
try {
destCal.addItem(item, listener);
} catch(e) {
failedCount++;
lastError = e;
// Call the listener's operationComplete, to increase the
// counter and not miss failed items. Otherwise, endBatch might
// never be called.
listener.onOperationComplete(null, null, null, null, null);
Components.utils.reportError("Import error: "+e);
}
}
// End transmgr batch
endBatchTransaction();
}
/**
* Save data to a file. Create the file or overwrite an existing file.
*
* @param calendarEventArray (required) Array of calendar events that should
* be saved to file.
* @param aDefaultFileName (optional) Initial filename shown in SaveAs dialog.
*/
function saveEventsToFile(calendarEventArray, aDefaultFileName) {
if (!calendarEventArray || !calendarEventArray.length) {
return;
}
// Show the 'Save As' dialog and ask for a filename to save to
const nsIFilePicker = Components.interfaces.nsIFilePicker;
let fp = Components.classes["@mozilla.org/filepicker;1"]
.createInstance(nsIFilePicker);
fp.init(window,
calGetString("calendar", "filepickerTitleExport"),
nsIFilePicker.modeSave);
if (aDefaultFileName && aDefaultFileName.length && aDefaultFileName.length > 0) {
fp.defaultString = aDefaultFileName;
} else if (calendarEventArray.length == 1 && calendarEventArray[0].title) {
fp.defaultString = calendarEventArray[0].title;
} else {
fp.defaultString = calGetString("calendar", "defaultFileName");
}
fp.defaultExtension = "ics";
// Get a list of exporters
let contractids = new Array();
let catman = Components.classes["@mozilla.org/categorymanager;1"]
.getService(Components.interfaces.nsICategoryManager);
let catenum = catman.enumerateCategory('cal-exporters');
let currentListLength = 0;
let defaultCIDIndex = 0;
while (catenum.hasMoreElements()) {
let entry = catenum.getNext();
entry = entry.QueryInterface(Components.interfaces.nsISupportsCString);
let contractid = catman.getCategoryEntry('cal-exporters', entry);
let exporter;
try {
exporter = Components.classes[contractid]
.getService(Components.interfaces.calIExporter);
} catch (e) {
cal.WARN("Could not initialize exporter: " + contractid + "\nError: " + e);
continue;
}
let types = exporter.getFileTypes({});
for each (let type in types) {
fp.appendFilter(type.description, type.extensionFilter);
if (type.extensionFilter=="*." + fp.defaultExtension) {
fp.filterIndex = currentListLength;
defaultCIDIndex = currentListLength;
}
contractids.push(contractid);
currentListLength++;
}
}
let rv = fp.show();
// Now find out as what to save, convert the events and save to file.
if (rv != nsIFilePicker.returnCancel &&
fp.file && fp.file.path.length > 0) {
const UTF8 = "UTF-8";
let aDataStream;
let extension;
let charset;
let filterIndex = fp.filterIndex;
if (fp.filterIndex < 0 || fp.filterIndex > contractids.length) {
// For some reason the wrong filter was selected, assume default extension
filterIndex = defaultCIDIndex;
}
let exporter = Components.classes[contractids[filterIndex]]
.getService(Components.interfaces.calIExporter);
let filePath = fp.file.path;
if (filePath.indexOf(".") == -1) {
filePath += "."+exporter.getFileTypes({})[0].defaultExtension;
}
const nsILocalFile = Components.interfaces.nsILocalFile;
const nsIFileOutputStream = Components.interfaces.nsIFileOutputStream;
let outputStream;
let localFileInstance = Components.classes["@mozilla.org/file/local;1"]
.createInstance(nsILocalFile);
localFileInstance.initWithPath(filePath);
outputStream = Components.classes["@mozilla.org/network/file-output-stream;1"]
.createInstance(nsIFileOutputStream);
try {
outputStream.init(localFileInstance,
MODE_WRONLY | MODE_CREATE | MODE_TRUNCATE,
parseInt("0664", 8),
0);
// XXX Do the right thing with unicode and stuff. Or, again, should the
// exporter handle that?
exporter.exportToStream(outputStream,
calendarEventArray.length,
calendarEventArray,
null);
outputStream.close();
} catch(ex) {
showError(calGetString("calendar", "unableToWrite") + filePath);
}
}
}
/**
* Exports all the events and tasks in a calendar. If aCalendar is not specified,
* the user will be prompted with a list of calendars to choose which one to export.
*
* @param aCalendar (optional) A specific calendar to export
*/
function exportEntireCalendar(aCalendar) {
var itemArray = [];
var getListener = {
onOperationComplete: function(aCalendar, aStatus, aOperationType, aId, aDetail)
{
saveEventsToFile(itemArray, aCalendar.name);
},
onGetResult: function(aCalendar, aStatus, aItemType, aDetail, aCount, aItems)
{
for each (let item in aItems) {
itemArray.push(item);
}
}
};
function getItemsFromCal(aCal) {
aCal.getItems(Components.interfaces.calICalendar.ITEM_FILTER_ALL_ITEMS,
0, null, null, getListener);
}
if (!aCalendar) {
var count = new Object();
var calendars = getCalendarManager().getCalendars(count);
if (count.value == 1) {
// There's only one calendar, so it's silly to ask what calendar
// the user wants to import into.
getItemsFromCal(calendars[0]);
} else {
// Ask what calendar to import into
var args = new Object();
args.onOk = getItemsFromCal;
args.promptText = calGetString("calendar", "exportPrompt");
openDialog("chrome://calendar/content/chooseCalendarDialog.xul",
"_blank", "chrome,titlebar,modal,resizable", args);
}
} else {
getItemsFromCal(aCalendar);
}
}
|