/usr/lib/thunderbird-addons/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/chrome/calendar/content/calendar/calendar-dnd-listener.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 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 605 606 607 608 609 610 611 612 | /* 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/. */
Components.utils.import("resource://calendar/modules/calUtils.jsm");
Components.utils.import("resource://calendar/modules/calAlarmUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
var itemConversion = {
/**
* Converts an email message to a calendar item.
*
* @param aItem The target calIItemBase.
* @param aMessage The nsIMsgHdr to convert from.
*/
calendarItemFromMessage: function iC_calendarItemFromMessage(aItem, aMsgHdr) {
let msgFolder = aMsgHdr.folder;
let msgUri = msgFolder.getUriForMsg(aMsgHdr);
aItem.calendar = getSelectedCalendar();
aItem.title = aMsgHdr.mime2DecodedSubject;
cal.setDefaultStartEndHour(aItem);
cal.alarms.setDefaultValues(aItem);
let messenger = Components.classes["@mozilla.org/messenger;1"]
.createInstance(Components.interfaces.nsIMessenger);
let streamListener = Components.classes["@mozilla.org/network/sync-stream-listener;1"]
.createInstance(Components.interfaces.nsISyncStreamListener);
messenger.messageServiceFromURI(msgUri).streamMessage(msgUri,
streamListener,
null,
null,
false,
"",
false);
let plainTextMessage = "";
plainTextMessage = msgFolder.getMsgTextFromStream(streamListener.inputStream,
aMsgHdr.Charset,
65536,
32768,
false,
true,
{});
aItem.setProperty("DESCRIPTION", plainTextMessage);
},
/**
* Copy base item properties from aItem to aTarget. This includes properties
* like title, location, description, priority, transparency,
* attendees, categories, calendar, recurrence and possibly more.
*
* @param aItem The item to copy from.
* @param aTarget the item to copy to.
*/
copyItemBase: function iC_copyItemBase(aItem, aTarget) {
const copyProps = ["SUMMARY", "LOCATION", "DESCRIPTION",
"URL", "CLASS", "PRIORITY"];
for each (var prop in copyProps) {
aTarget.setProperty(prop, aItem.getProperty(prop));
}
// Attendees
var attendees = aItem.getAttendees({});
for each (var attendee in attendees) {
aTarget.addAttendee(attendee.clone());
}
// Categories
var categories = aItem.getCategories({});
aTarget.setCategories(categories.length, categories);
// Organizer
aTarget.organizer = (aItem.organizer ? aItem.organizer.clone() : null);
// Calendar
aTarget.calendar = getSelectedCalendar();
// Recurrence
if (aItem.recurrenceInfo) {
aTarget.recurrenceInfo = aItem.recurrenceInfo.clone();
aTarget.recurrenceInfo.item = aTarget;
}
},
/**
* Creates a task from the passed event. This function copies the base item
* and a few event specific properties (dates, alarms, ...).
*
* @param aEvent The event to copy from.
* @return The resulting task.
*/
taskFromEvent: function iC_taskFromEvent(aEvent) {
let item = cal.createTodo();
this.copyItemBase(aEvent, item);
// Dates and alarms
if (!aEvent.startDate.isDate && !aEvent.endDate.isDate) {
// Dates
item.entryDate = aEvent.startDate.clone();
item.dueDate = aEvent.endDate.clone();
// Alarms
for each (let alarm in aEvent.getAlarms({})) {
item.addAlarm(alarm.clone());
}
item.alarmLastAck = (aEvent.alarmLastAck ?
aEvent.alarmLastAck.clone() :
null);
}
// Map Status values
let statusMap = {
"TENTATIVE": "NEEDS-ACTION",
"CONFIRMED": "IN-PROCESS",
"CANCELLED": "CANCELLED"
};
if (aEvent.getProperty("STATUS") in statusMap) {
item.setProperty("STATUS", statusMap[aEvent.getProperty("STATUS")]);
}
return item;
},
/**
* Creates an event from the passed task. This function copies the base item
* and a few task specific properties (dates, alarms, ...). If the task has
* no due date, the default event length is used.
*
* @param aTask The task to copy from.
* @return The resulting event.
*/
eventFromTask: function iC_eventFromTask(aTask) {
let item = cal.createEvent();
this.copyItemBase(aTask, item);
// Dates and alarms
item.startDate = aTask.entryDate;
if (!item.startDate) {
if (aTask.dueDate) {
item.startDate = aTask.dueDate.clone();
item.startDate.minute -= getPrefSafe("calendar.event.defaultlength", 60);
} else {
item.startDate = cal.getDefaultStartDate();
}
}
item.endDate = aTask.dueDate;
if (!item.endDate) {
// Make the event be the default event length if no due date was
// specified.
item.endDate = item.startDate.clone();
item.endDate.minute += getPrefSafe("calendar.event.defaultlength", 60);
}
// Alarms
for each (let alarm in aTask.getAlarms({})) {
item.addAlarm(alarm.clone());
}
item.alarmLastAck = (aTask.alarmLastAck ?
aTask.alarmLastAck.clone() :
null);
// Map Status values
let statusMap = {
"NEEDS-ACTION": "TENTATIVE",
"COMPLETED": "CONFIRMED",
"IN-PROCESS": "CONFIRMED",
"CANCELLED": "CANCELLED"
};
if (aTask.getProperty("STATUS") in statusMap) {
item.setProperty("STATUS", statusMap[aTask.getProperty("STATUS")]);
}
return item;
}
};
/**
* A base class for drag and drop observers
* @class calDNDBaseObserver
*/
function calDNDBaseObserver() {
ASSERT(false, "Inheriting objects call calDNDBaseObserver!");
}
calDNDBaseObserver.prototype = {
// initialize this class's members
initBase: function calDNDInitBase() {
},
getSupportedFlavours: function calDNDGetFlavors() {
var flavourSet = new FlavourSet();
flavourSet.appendFlavour("text/calendar");
flavourSet.appendFlavour("text/x-moz-url");
flavourSet.appendFlavour("text/x-moz-message");
flavourSet.appendFlavour("text/unicode");
flavourSet.appendFlavour("application/x-moz-file");
return flavourSet;
},
/**
* Action to take when dropping the event.
*/
onDrop: function calDNDDrop(aEvent, aTransferData, aDragSession) {
var transferable = Components.classes["@mozilla.org/widget/transferable;1"]
.createInstance(Components.interfaces.nsITransferable);
transferable.init(null);
transferable.addDataFlavor("text/calendar");
transferable.addDataFlavor("text/x-moz-url");
transferable.addDataFlavor("text/x-moz-message");
transferable.addDataFlavor("text/unicode");
transferable.addDataFlavor("application/x-moz-file");
aDragSession.getData(transferable, 0);
var data = new Object();
var bestFlavor = new Object();
var length = new Object();
transferable.getAnyTransferData(bestFlavor, data, length);
try {
data = data.value.QueryInterface(Components.interfaces.nsISupportsString);
} catch (exc) {
// we currently only supports strings:
return;
}
// Treat unicode data with VEVENT in it as text/calendar
if (bestFlavor.value == "text/unicode" && data.toString().indexOf("VEVENT") != -1) {
bestFlavor.value = "text/calendar";
}
var destCal = getSelectedCalendar();
switch (bestFlavor.value) {
case "text/calendar":
//@line 246 "/build/buildd/thunderbird-24.4.0+build1/calendar/base/content/calendar-dnd-listener.js"
var parser = Components.classes["@mozilla.org/calendar/ics-parser;1"]
.createInstance(Components.interfaces.calIIcsParser);
parser.parseString(data);
this.onDropItems(parser.getItems({}).concat(parser.getParentlessItems({})));
break;
case "text/unicode":
var droppedUrl = this.retrieveURLFromData(data, bestFlavor.value);
if (!droppedUrl)
return;
var url = makeURL(droppedUrl);
var localFileInstance = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
localFileInstance.initWithPath(url.path);
var inputStream = Components.classes["@mozilla.org/network/file-input-stream;1"]
.createInstance(Components.interfaces.nsIFileInputStream);
inputStream.init(localFileInstance,
MODE_RDONLY,
parseInt("0444", 8),
{});
try {
//XXX support csv
var importer = Components.classes["@mozilla.org/calendar/import;1?type=ics"]
.getService(Components.interfaces.calIImporter);
var items = importer.importFromStream(inputStream, {});
this.onDropItems(items);
}
finally {
inputStream.close();
}
break;
case "application/x-moz-file-promise":
case "text/x-moz-url":
var uri = Services.io.newURI(data.toString(), null, null);
var loader = Components.classes["@mozilla.org/network/unichar-stream-loader;1"]
.createInstance(Components.interfaces.nsIUnicharStreamLoader);
var channel = Services.io.newChannelFromURI(uri);
channel.loadFlags |= Components.interfaces.nsIRequest.LOAD_BYPASS_CACHE;
var self = this;
var listener = {
// nsIUnicharStreamLoaderObserver:
onDetermineCharset: function(loader, context, firstSegment, length) {
var charset = null;
if (loader && loader.channel) {
charset = channel.contentCharset;
}
if (!charset || charset.length == 0) {
charset = "UTF-8";
}
return charset;
},
onStreamComplete: function(loader, context, status, unicharString) {
var parser = Components.classes["@mozilla.org/calendar/ics-parser;1"]
.createInstance(Components.interfaces.calIIcsParser);
parser.parseString(unicharString);
self.onDropItems(parser.getItems({}).concat(parser.getParentlessItems({})));
}
};
try {
loader.init(listener, Components.interfaces.nsIUnicharStreamLoader.DEFAULT_SEGMENT_SIZE);
channel.asyncOpen(loader, null);
} catch(e) {
Components.utils.reportError(e)
}
break;
case "text/x-moz-message":
this.onDropMessage(messenger.msgHdrFromURI(data));
break;
default:
ASSERT(false, "unknown data flavour:" + bestFlavor.value+'\n');
break;
}
},
onDragStart: function calDNDStart(aEvent, aTransferData, aDragAction) {},
onDragOver: function calDNDOver(aEvent, aFlavor, aDragSession) {},
onDragExit: function calDNDExit(aEvent, aDragSession) {},
onDropItems: function calDNDDropItems(aItems) {},
onDropMessage: function calDNDDropMessage(aMessage) {},
retrieveURLFromData: function calDNDRetrieveURL(aData, aFlavor) {
var data;
switch (aFlavor) {
case "text/unicode":
data = aData.toString();
var separator = data.indexOf("\n");
if (separator != -1)
data = data.substr(0, separator);
return data;
case "application/x-moz-file":
return aData.URL;
default:
return null;
}
}
};
/**
* calViewDNDObserver::calViewDNDObserver
*
* Drag'n'drop handler for the calendar views. This handler is
* derived from the base handler and just implements specific actions.
*/
function calViewDNDObserver() {
this.wrappedJSObject = this;
this.initBase();
}
calViewDNDObserver.prototype = {
__proto__: calDNDBaseObserver.prototype,
/**
* calViewDNDObserver::onDropItems
*
* Gets called in case we're dropping an array of items
* on one of the calendar views. In this case we just
* try to add these items to the currently selected calendar.
*/
onDropItems: function(aItems) {
var destCal = getSelectedCalendar();
startBatchTransaction();
try {
for each (var item in aItems) {
doTransaction('add', item, destCal, null, null);
}
}
finally {
endBatchTransaction();
}
}
};
/**
* calMailButtonDNDObserver::calMailButtonDNDObserver
*
* Drag'n'drop handler for the 'mail mode'-button. This handler is
* derived from the base handler and just implements specific actions.
*/
function calMailButtonDNDObserver() {
this.wrappedJSObject = this;
this.initBase();
}
calMailButtonDNDObserver.prototype = {
__proto__: calDNDBaseObserver.prototype,
/**
* calMailButtonDNDObserver::onDropItems
*
* Gets called in case we're dropping an array of items
* on the 'mail mode'-button.
*
* @param aItems An array of items to handle.
*/
onDropItems: function(aItems) {
if (aItems && aItems.length > 0) {
var item = aItems[0];
var recipients = "";
var attendees = item.getAttendees({});
for each (var attendee in attendees) {
if (attendee.id && attendee.id.length) {
var email = attendee.id;
var re = new RegExp("^mailto:(.*)", "i");
if (email && email.length) {
if (re.test(email)) {
email = RegExp.$1;
} else {
email = email;
}
}
// Prevent trailing commas.
if (recipients.length > 0) {
recipients += ",";
}
// Add this recipient id to the list.
recipients += email;
}
}
sendMailTo(recipients, item.title, item.getProperty("DESCRIPTION"));
}
},
/**
* calMailButtonDNDObserver::onDropMessage
*
* Gets called in case we're dropping a message
* on the 'mail mode'-button.
*
* @param aMessage The message to handle.
*/
onDropMessage: function(aMessage) {
}
};
/**
* calCalendarButtonDNDObserver::calCalendarButtonDNDObserver
*
* Drag'n'drop handler for the 'calendar mode'-button. This handler is
* derived from the base handler and just implements specific actions.
*/
function calCalendarButtonDNDObserver() {
this.wrappedJSObject = this;
this.initBase();
}
calCalendarButtonDNDObserver.prototype = {
__proto__: calDNDBaseObserver.prototype,
/**
* calCalendarButtonDNDObserver::onDropItems
*
* Gets called in case we're dropping an array of items
* on the 'calendar mode'-button.
*
* @param aItems An array of items to handle.
*/
onDropItems: function(aItems) {
for each (var item in aItems) {
var newItem = item;
if (isToDo(item)) {
newItem = itemConversion.eventFromTask(item);
}
createEventWithDialog(null, null, null, null, newItem);
}
},
/**
* calCalendarButtonDNDObserver::onDropMessage
*
* Gets called in case we're dropping a message on the
* 'calendar mode'-button. In this case we create a new
* event from the mail. We open the default event dialog
* and just use the subject of the message as the event title.
*
* @param aMessage The message to handle.
*/
onDropMessage: function(aMessage) {
var newItem = createEvent();
itemConversion.calendarItemFromMessage(newItem, aMessage);
createEventWithDialog(null, null, null, null, newItem);
}
};
/**
* calTaskButtonDNDObserver::calTaskButtonDNDObserver
*
* Drag'n'drop handler for the 'task mode'-button. This handler is
* derived from the base handler and just implements specific actions.
*/
function calTaskButtonDNDObserver() {
this.wrappedJSObject = this;
this.initBase();
}
calTaskButtonDNDObserver.prototype = {
__proto__: calDNDBaseObserver.prototype,
/**
* calTaskButtonDNDObserver::onDropItems
*
* Gets called in case we're dropping an array of items
* on the 'task mode'-button.
*
* @param aItems An array of items to handle.
*/
onDropItems: function(aItems) {
for each (var item in aItems) {
var newItem = item;
if (isEvent(item)) {
newItem = itemConversion.taskFromEvent(item);
}
createTodoWithDialog(null, null, null, newItem);
}
},
/**
* calTaskButtonDNDObserver::onDropMessage
*
* Gets called in case we're dropping a message
* on the 'task mode'-button.
*
* @param aMessage The message to handle.
*/
onDropMessage: function(aMessage) {
var todo = createTodo();
itemConversion.calendarItemFromMessage(todo, aMessage);
createTodoWithDialog(null, null, null, todo);
}
};
/**
* Invoke a drag session for the passed item. The passed box will be used as a
* source.
*
* @param aItem The item to drag.
* @param aXULBox The XUL box to invoke the drag session from.
*/
function invokeEventDragSession(aItem, aXULBox) {
let transfer = Components.classes["@mozilla.org/widget/transferable;1"]
.createInstance(Components.interfaces.nsITransferable);
transfer.init(null);
transfer.addDataFlavor("text/calendar");
let flavourProvider = {
QueryInterface: XPCOMUtils.generateQI([Components.interfaces.nsIFlavorDataProvider]),
item: aItem,
getFlavorData: function(aInTransferable, aInFlavor, aOutData, aOutDataLen) {
if ((aInFlavor == "application/vnd.x-moz-cal-event") ||
(aInFlavor == "application/vnd.x-moz-cal-task")) {
aOutData.value = aItem;
aOutDataLen.value = 1;
} else {
ASSERT(false, "error:" + aInFlavor);
}
}
};
if (isEvent(aItem)) {
transfer.addDataFlavor("application/vnd.x-moz-cal-event");
transfer.setTransferData("application/vnd.x-moz-cal-event", flavourProvider, 0);
} else if (isToDo(aItem)) {
transfer.addDataFlavor("application/vnd.x-moz-cal-task");
transfer.setTransferData("application/vnd.x-moz-cal-task", flavourProvider, 0);
}
// Also set some normal data-types, in case we drag into another app
let serializer = Components.classes["@mozilla.org/calendar/ics-serializer;1"]
.createInstance(Components.interfaces.calIIcsSerializer);
serializer.addItems([aItem], 1);
let supportsString = Components.classes["@mozilla.org/supports-string;1"]
.createInstance(Components.interfaces.nsISupportsString);
supportsString.data = serializer.serializeToString();
transfer.setTransferData("text/calendar", supportsString, supportsString.data.length * 2);
transfer.setTransferData("text/unicode", supportsString, supportsString.data.length * 2);
let action = Components.interfaces.nsIDragService.DRAGDROP_ACTION_MOVE;
let supArray = Components.classes["@mozilla.org/supports-array;1"]
.createInstance(Components.interfaces.nsISupportsArray);
supArray.AppendElement(transfer);
aXULBox.sourceObject = aItem;
try {
cal.getDragService().invokeDragSession(aXULBox, supArray, null, action);
} catch (error) {
// Nothing done here because we only have to catch an exception that occurs when dragging
// is cancelled with ESC. This is an odd behaviour of the nativeDragService which we have
// have to cover.
// Therefore the DND API for calendar should be changed to the new DOM driven DND-API
// sometime.
}
}
var calendarViewDNDObserver = new calViewDNDObserver();
var calendarMailButtonDNDObserver = new calMailButtonDNDObserver();
var calendarCalendarButtonDNDObserver = new calCalendarButtonDNDObserver();
var calendarTaskButtonDNDObserver = new calTaskButtonDNDObserver();
|