/usr/lib/thunderbird-addons/extensions/{e2fda1a4-762b-4020-b5ad-a41df1933103}/calendar-js/calCachedCalendar.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 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 | /* 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/calProviderUtils.jsm");
Components.utils.import("resource://gre/modules/Services.jsm");
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
const calICalendar = Components.interfaces.calICalendar;
const cICL = Components.interfaces.calIChangeLog;
const cIOL = Components.interfaces.calIOperationListener;
let gNoOpListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {
},
onOperationComplete: function(calendar, status, opType, id, detail) {
}
};
/**
* Returns true if the exception passed is one that should cause the cache
* layer to retry the operation. This is usually a network error or other
* temporary error.
*
* @param result The result code to check.
* @return True, if the result code means server unavailability.
*/
function isUnavailableCode(result) {
// Stolen from nserror.h
const NS_ERROR_MODULE_NETWORK = 6;
function NS_ERROR_GET_MODULE(code) {
return (((code >> 16) - 0x45) & 0x1fff);
}
if (NS_ERROR_GET_MODULE(result) == NS_ERROR_MODULE_NETWORK &&
!Components.isSuccessCode(result)) {
// This is a network error, which most likely means we should
// retry it some time.
return true;
}
// Other potential errors we want to retry with
switch (result) {
case Components.results.NS_ERROR_NOT_AVAILABLE:
return true;
default:
return false;
}
}
function calCachedCalendarObserverHelper(home, isCachedObserver) {
this.home = home;
this.isCachedObserver = isCachedObserver;
}
calCachedCalendarObserverHelper.prototype = {
isCachedObserver: false,
onStartBatch: function() {
this.home.mObservers.notify("onStartBatch");
},
onEndBatch: function() {
this.home.mObservers.notify("onEndBatch");
},
onLoad: function(calendar) {
if (this.isCachedObserver) {
this.home.mObservers.notify("onLoad", [this.home]);
} else {
// start sync action after uncached calendar has been loaded.
// xxx todo, think about:
// although onAddItem et al have been called, we need to fire
// an additional onLoad completing the refresh call (->composite)
var home = this.home;
home.synchronize(function(status) {
home.mObservers.notify("onLoad", [home]);
});
}
},
onAddItem: function(aItem) {
if (this.isCachedObserver) {
this.home.mObservers.notify("onAddItem", arguments);
}
},
onModifyItem: function(aNewItem, aOldItem) {
if (this.isCachedObserver) {
this.home.mObservers.notify("onModifyItem", arguments);
}
},
onDeleteItem: function(aItem) {
if (this.isCachedObserver) {
this.home.mObservers.notify("onDeleteItem", arguments);
}
},
onError: function(aCalendar, aErrNo, aMessage) {
this.home.mObservers.notify("onError", arguments);
},
onPropertyChanged: function(aCalendar, aName, aValue, aOldValue) {
if (!this.isCachedObserver) {
this.home.mObservers.notify("onPropertyChanged", [this.home, aName, aValue, aOldValue]);
}
},
onPropertyDeleting: function(aCalendar, aName) {
if (!this.isCachedObserver) {
this.home.mObservers.notify("onPropertyDeleting", [this.home, aName]);
}
}
};
function calCachedCalendar(uncachedCalendar) {
this.wrappedJSObject = this;
this.mSyncQueue = [];
this.mObservers = new cal.ObserverBag(Components.interfaces.calIObserver);
uncachedCalendar.superCalendar = this;
uncachedCalendar.addObserver(new calCachedCalendarObserverHelper(this, false));
this.mUncachedCalendar = uncachedCalendar;
this.setupCachedCalendar();
if (this.supportsChangeLog) {
uncachedCalendar.offlineStorage = this.mCachedCalendar;
}
this.offlineCachedItems = {};
this.offlineCachedItemFlags = {};
}
calCachedCalendar.prototype = {
QueryInterface: function cCC_QueryInterface(aIID) {
if (aIID.equals(Components.interfaces.calISchedulingSupport) &&
this.mUncachedCalendar.QueryInterface(aIID)) {
// check whether uncached calendar supports it:
return this;
} else if (aIID.equals(Components.interfaces.calICalendar) ||
aIID.equals(Components.interfaces.nsISupports)) {
return this;
} else {
throw Components.results.NS_ERROR_NO_INTERFACE;
}
},
mCachedCalendar: null,
mCachedObserver: null,
mUncachedCalendar: null,
mObservers: null,
mSuperCalendar: null,
offlineCachedItems: null,
offlineCachedItemFlags: null,
onCalendarUnregistering: function() {
if (this.mCachedCalendar) {
this.mCachedCalendar.removeObserver(this.mCachedObserver);
// Although this doesn't really follow the spec, we know the
// storage calendar's deleteCalendar method is synchronous.
// TODO put changes into a different calendar and delete
// afterwards.
this.mCachedCalendar.QueryInterface(Components.interfaces.calICalendarProvider)
.deleteCalendar(this.mCachedCalendar, null);
this.mCachedCalendar = null;
}
},
setupCachedCalendar: function cCC_setupCachedCalendar() {
try {
if (this.mCachedCalendar) { // this is actually a resetupCachedCalendar:
// Although this doesn't really follow the spec, we know the
// storage calendar's deleteCalendar method is synchronous.
// TODO put changes into a different calendar and delete
// afterwards.
this.mCachedCalendar.QueryInterface(Components.interfaces.calICalendarProvider)
.deleteCalendar(this.mCachedCalendar, null);
if (this.supportsChangeLog) {
// start with full sync:
this.mUncachedCalendar.resetLog();
}
} else {
let calType = getPrefSafe("calendar.cache.type", "storage");
// While technically, the above deleteCalendar should delete the
// whole calendar, this is nothing more than deleting all events
// todos and properties. Therefore the initialization can be
// skipped.
let cachedCalendar = Components.classes["@mozilla.org/calendar/calendar;1?type=" + calType]
.createInstance(Components.interfaces.calICalendar);
switch (calType) {
case "memory":
if (this.supportsChangeLog) {
// start with full sync:
this.mUncachedCalendar.resetLog();
}
break;
case "storage":
let file = getCalendarDirectory();
file.append("cache.sqlite");
cachedCalendar.uri = Services.io.newFileURI(file);
cachedCalendar.id = this.id;
break;
default:
throw new Error("unsupported cache calendar type: " + calType);
}
cachedCalendar.transientProperties = true;
cachedCalendar.setProperty("relaxedMode", true);
cachedCalendar.superCalendar = this;
if (!this.mCachedObserver) {
this.mCachedObserver = new calCachedCalendarObserverHelper(this, true);
}
cachedCalendar.addObserver(this.mCachedObserver);
this.mCachedCalendar = cachedCalendar;
}
} catch (exc) {
Components.utils.reportError(exc);
}
},
getOfflineAddedItems: function cCC_getOfflineAddedItems(callbackFunc) {
let this_ = this;
this_.offlineCachedItems = {};
let getListener = {
onGetResult: function cCC_oOC_cL_onGetResult(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
for each (let item in aItems) {
this_.offlineCachedItems[item.hashId] = item;
this_.offlineCachedItemFlags[item.hashId] = cICL.OFFLINE_FLAG_CREATED_RECORD;
}
},
onOperationComplete: function cCC_oOC_cL_onOperationComplete(aCalendar, aStatus, aOpType, aId, aDetail) {
this_.getOfflineModifiedItems(callbackFunc);
}
};
this.mCachedCalendar.getItems(calICalendar.ITEM_FILTER_ALL_ITEMS | calICalendar.ITEM_FILTER_OFFLINE_CREATED,
0, null, null, getListener);
},
getOfflineModifiedItems: function cCC_getOfflineModifiedItems(callbackFunc) {
let this_ = this;
let getListener = {
onGetResult: function cCC_oOC_cL_onGetResult(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
for each (let item in aItems) {
this_.offlineCachedItems[item.hashId] = item;
this_.offlineCachedItemFlags[item.hashId] = cICL.OFFLINE_FLAG_MODIFIED_RECORD;
}
},
onOperationComplete: function cCC_oOC_cL_onOperationComplete(aCalendar, aStatus, aOpType, aId, aDetail) {
this_.getOfflineDeletedItems(callbackFunc);
}
};
this.mCachedCalendar.getItems(calICalendar.ITEM_FILTER_OFFLINE_MODIFIED | calICalendar.ITEM_FILTER_ALL_ITEMS,
0, null, null, getListener);
},
getOfflineDeletedItems: function cCC_getOfflineDeletedItems(callbackFunc) {
let this_ = this;
let getListener = {
onGetResult: function cCC_oOC_cL_onGetResult(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
for each (let item in aItems) {
this_.offlineCachedItems[item.hashId] = item;
this_.offlineCachedItemFlags[item.hashId] = cICL.OFFLINE_FLAG_DELETED_RECORD;
}
},
onOperationComplete: function cCC_oOC_cL_onOperationComplete(aCalendar, aStatus, aOpType, aId, aDetail) {
if (callbackFunc) {
callbackFunc();
}
}
};
this.mCachedCalendar.getItems(calICalendar.ITEM_FILTER_OFFLINE_DELETED | calICalendar.ITEM_FILTER_ALL_ITEMS,
0, null, null, getListener);
},
mPendingSync: null,
mSyncQueue: null,
synchronize: function cCC_synchronize(respFunc) {
var this_ = this;
if (this.getProperty("disabled")) {
return emptyQueue(Components.results.NS_OK);
}
this.mSyncQueue.push(respFunc);
if (this.mSyncQueue.length > 1) { // don't use mPendingSync here
cal.LOG("[calCachedCalendar] sync in action/pending.");
return this.mPendingSync;
}
function emptyQueue(status) {
var queue = this_.mSyncQueue;
this_.mSyncQueue = [];
function execResponseFunc(func) {
try {
func(status);
} catch (exc) {
cal.ASSERT(false, exc);
}
}
queue.forEach(execResponseFunc);
cal.LOG("[calCachedCalendar] sync queue empty.");
var op = this_.mPendingSync;
this_.mPendingSync = null;
return op;
}
if (this.offline) {
return emptyQueue(Components.results.NS_OK);
}
if (this.supportsChangeLog) {
cal.LOG("[calCachedCalendar] Doing changelog based sync for calendar " + this.uri.spec);
var opListener = {
onResult: function(op, result) {
if (!op || !op.isPending) {
var status = (op ? op.status : Components.results.NS_OK);
if (!Components.isSuccessCode(status)) {
cal.ERROR("[calCachedCalendar] replay action failed: " +
(op ? op.id : "<unknown>")+", uri=" +
this_.uri.spec + ", result=" +
result + ", op=" + op);
}
cal.LOG("[calCachedCalendar] replayChangesOn finished.");
emptyQueue(status);
}
}
};
this.mPendingSync = this.mUncachedCalendar.replayChangesOn(opListener);
return this.mPendingSync;
}
cal.LOG("[calCachedCalendar] Doing full sync for calendar " + this.uri.spec);
let completeListener = {
modifiedTimes: {},
hasRenewedCalendar: false,
getsCompleted: 0,
getsReceived: 0,
opCompleted: false,
onGetResult: function cCC_oOC_cL_onGetResult(aCalendar,
aStatus,
aItemType,
aDetail,
aCount,
aItems) {
if (Components.isSuccessCode(aStatus)) {
if (!this.hasRenewedCalendar) {
// TODO instead of deleting the calendar and creating a new
// one, maybe we want to do a "real" sync between the
// existing local calendar and the remote calendar.
this_.setupCachedCalendar();
this.hasRenewedCalendar = true;
}
this.getsReceived++;
cal.forEach(aItems, function(item) {
// Adding items recd from the Memory Calendar
// These may be different than what the cache has
completeListener.modifiedTimes[item.id] = item.lastModifiedTime;
this_.mCachedCalendar.addItem(item, null);
}, function completed() {
completeListener.getsCompleted++;
if (completeListener.opCompleted) {
// onOperationComplete was called, but we were not ready yet. call it now.
completeListener.onOperationComplete.apply(completeListener, completeListener.opCompleted);
completeListener.opCompleted = false;
}
});
}
},
onOperationComplete: function cCC_oOC_cL_onOperationComplete(aCalendar,
aStatus,
aOpType,
aId,
aDetail) {
if (this.getsCompleted < this.getsReceived) {
// If not all of our gets have been processed, then save the
// arguments and finish processing later.
this.opCompleted = Array.slice(arguments);
return;
}
if (Components.isSuccessCode(aStatus)) {
cal.forEach(this_.offlineCachedItems, function(item) {
switch (this_.offlineCachedItemFlags[item.hashId]) {
case cICL.OFFLINE_FLAG_CREATED_RECORD:
// Created items are not present on the server, so its safe to adopt them
this_.adoptOfflineItem(item.clone(), null);
break;
case cICL.OFFLINE_FLAG_MODIFIED_RECORD:
// Two Cases Here:
if (item.id in completeListener.modifiedTimes) {
// The item is still on the server, we just retrieved it in the listener above.
if (item.lastModifiedTime.compare(completeListener.modifiedTimes[item.id]) < 0) {
// The item on the server has been modified, ask to overwrite
cal.WARN("[calCachedCalendar] Item '" + item.title + "' at the server seems to be modified recently.");
this_.promptOverwrite("modify", item, null, null);
} else {
// Our item is newer, just modify the item
this_.modifyOfflineItem(item, null, null);
}
} else {
// The item has been deleted from the server, ask if it should be added again
cal.WARN("[calCachedCalendar] Item '" + item.title + "' has been deleted from the server");
if (cal.promptOverwrite("modify", item, null, null)) {
this_.adoptOfflineItem(item.clone(), null);
}
}
break;
case cICL.OFFLINE_FLAG_DELETED_RECORD:
if (item.id in completeListener.modifiedTimes) {
// The item seems to exist on the server...
if (item.lastModifiedTime.compare(completeListener.modifiedTimes[item.id]) < 0) {
// ...and has been modified on the server. Ask to overwrite
cal.WARN("[calCachedCalendar] Item '" + item.title + "' at the server seems to be modified recently.");
this_.promptOverwrite("delete", item, null, null);
} else {
// ...and has not been modified. Delete it now.
this_.deleteOfflineItem(item, null);
}
} else {
// Item has already been deleted from the server, no need to change anything.
}
break;
}
},
function completed() {
this_.offlineCachedItems = {};
this_.offlineCachedItemFlags = {};
this_.playbackOfflineItems(function() emptyQueue(aStatus));
});
} else {
this_.playbackOfflineItems(function() {this_.mCachedObserver.onLoad(this_.mCachedCalendar);});
emptyQueue(aStatus);
}
}
};
this.getOfflineAddedItems(function(){
this_.mPendingSync = this_.mUncachedCalendar.getItems(Components.interfaces.calICalendar.ITEM_FILTER_ALL_ITEMS,
0, null, null, completeListener);
});
return this.mPendingSync;
},
onOfflineStatusChanged: function cCC_onOfflineStatusChanged(aNewState) {
if (aNewState) {
// Going offline: (XXX get items before going offline?) => we may ask the user to stay online a bit longer
} else {
// Going online (start replaying changes to the remote calendar)
this.refresh();
}
},
//aOldItem is already in the cache
promptOverwrite: function cCC_promptOverwrite(aMethod, aItem, aListener, aOldItem) {
let overwrite = cal.promptOverwrite(aMethod, aItem, aListener, aOldItem);
if (overwrite) {
if (aMethod == "modify") {
this.modifyOfflineItem(aItem, aOldItem, aListener);
} else {
this.deleteOfflineItem(aItem, aListener);
}
}
},
/*
* Asynchronously performs playback operations of items added, modified, or deleted offline
*
* @param aCallback (optional) The function to be callled when playback is complete.
* @param aPlaybackType (optional) The starting operation type. This function will be
* called recursively through playback operations in the order of
* add, modify, delete. By default playback will start with the add
* operation. Valid values for this parameter are defined as
* OFFLINE_FLAG_XXX constants in the calIChangeLog interface.
*/
playbackOfflineItems: function cCC_playbackOfflineItems(aCallback, aPlaybackType) {
let this_ = this;
let storage = this.mCachedCalendar.QueryInterface(Components.interfaces.calIOfflineStorage);
let resetListener = gNoOpListener;
let itemQueue = [];
let debugOp;
let nextCallback;
let uncachedOp;
let listenerOp;
let filter;
aPlaybackType = aPlaybackType || cICL.OFFLINE_FLAG_CREATED_RECORD;
switch (aPlaybackType) {
case cICL.OFFLINE_FLAG_CREATED_RECORD:
debugOp = "add";
nextCallback = this.playbackOfflineItems.bind(this, aCallback, cICL.OFFLINE_FLAG_MODIFIED_RECORD);
uncachedOp = this.mUncachedCalendar.addItem.bind(this.mUncachedCalendar);
listenerOp = cIOL.ADD;
filter = calICalendar.ITEM_FILTER_OFFLINE_CREATED;
break;
case cICL.OFFLINE_FLAG_MODIFIED_RECORD:
debugOp = "modify";
nextCallback = this.playbackOfflineItems.bind(this, aCallback, cICL.OFFLINE_FLAG_DELETED_RECORD);
uncachedOp = function(item, listener) { this_.mUncachedCalendar.modifyItem(item, item, listener); };
listenerOp = cIOL.MODIFY;
filter = calICalendar.ITEM_FILTER_OFFLINE_MODIFIED;
break;
case cICL.OFFLINE_FLAG_DELETED_RECORD:
debugOp = "delete";
nextCallback = aCallback;
uncachedOp = this.mUncachedCalendar.deleteItem.bind(this.mUncachedCalendar);
listenerOp = cIOL.MODIFY;
filter = calICalendar.ITEM_FILTER_OFFLINE_DELETED;
break;
default:
cal.ERROR("[calCachedCalendar] Invalid playback type: " + aPlaybackType);
return;
}
let opListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (Components.isSuccessCode(status)) {
if (aPlaybackType == cICL.OFFLINE_FLAG_DELETED_RECORD) {
this_.mCachedCalendar.deleteItem(detail, resetListener);
} else {
storage.resetItemOfflineFlag(detail, resetListener);
}
} else {
// If the playback action could not be performed, then there
// is no need for further action. The item still has the
// offline flag, so it will be taken care of next time.
cal.WARN("[calCachedCalendar] Unable to perform playback action " + debugOp +
" to the server, will try again next time (" + id + "," + detail + ")");
}
// move on to the next item in the queue
popItemQueue();
}
};
function popItemQueue() {
if (!itemQueue || itemQueue.length == 0) {
// no items left in the queue, move on to the next operation
if (nextCallback) {
nextCallback();
}
} else {
// perform operation on the next offline item in the queue
let item = itemQueue.pop();
try {
uncachedOp(item, opListener);
} catch (e) {
cal.ERROR("[calCachedCalendar] Could not perform playback operation " + debugOp +
" for item " + (item.title || " (none) ") + ": " + e);
opListener.onOperationComplete(this_, e.result, listenerOp, item.id, e.message);
}
}
}
let getListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {
itemQueue = itemQueue.concat(items);
},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (this_.offline) {
cal.LOG("[calCachedCalendar] back to offline mode, reconciliation aborted");
if (aCallback) {
aCallback();
}
} else {
cal.LOG("[calCachedCalendar] Performing playback operation " + debugOp +
" on " + itemQueue.length + " items to " + this_.name);
// start the first operation
popItemQueue();
}
}
};
this.mCachedCalendar.getItems(calICalendar.ITEM_FILTER_ALL_ITEMS | filter,
0, null, null, getListener);
},
get superCalendar() {
return this.mSuperCalendar && this.mSuperCalendar.superCalendar || this;
},
set superCalendar(val) {
return (this.mSuperCalendar = val);
},
get offline() {
return Services.io.offline;
},
get supportsChangeLog() {
return (cal.wrapInstance(this.mUncachedCalendar, Components.interfaces.calIChangeLog) != null);
},
get canRefresh() { // enable triggering sync using the reload button
return true;
},
getProperty: function(aName) {
switch (aName) {
case "cache.enabled":
if (this.mUncachedCalendar.getProperty("cache.always")) {
return true;
}
break;
}
return this.mUncachedCalendar.getProperty(aName);
},
refresh: function() {
if (this.offline) {
this.downstreamRefresh();
} else {
/* we first ensure that any remaining offline items are reconciled with the calendar server */
if (this.supportsChangeLog) {
this.playbackOfflineItems(this.downstreamRefresh.bind(this));
} else {
this.downstreamRefresh();
}
}
},
downstreamRefresh: function() {
if (this.mUncachedCalendar.canRefresh && !this.offline) {
return this.mUncachedCalendar.refresh(); // will trigger synchronize once the calendar is loaded
} else {
var this_ = this;
return this.synchronize(
function(status) { // fire completing onLoad for this refresh call
this_.mCachedObserver.onLoad(this_.mCachedCalendar);
});
}
},
addObserver: function(aObserver) {
this.mObservers.add(aObserver);
},
removeObserver: function(aObserver) {
this.mObservers.remove(aObserver);
},
addItem: function(item, listener) {
return this.adoptItem(item.clone(), listener);
},
adoptItem: function(item, listener) {
// Forwarding add/modify/delete to the cached calendar using the calIObserver
// callbacks would be advantageous, because the uncached provider could implement
// a true push mechanism firing without being triggered from within the program.
// But this would mean the uncached provider fires on the passed
// calIOperationListener, e.g. *before* it fires on calIObservers
// (because that order is undefined). Firing onOperationComplete before onAddItem et al
// would result in this facade firing onOperationComplete even though the modification
// hasn't yet been performed on the cached calendar (which happens in onAddItem et al).
// Result is that we currently stick to firing onOperationComplete if the cached calendar
// has performed the modification, see below:
let self = this;
let cacheListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {
cal.ASSERT(false, "unexpected!");
},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (isUnavailableCode(status)) {
// The item couldn't be added to the (remote) location,
// this is like being offline. Add the item to the cached
// calendar instead.
cal.LOG("[calCachedCalendar] Calendar " + calendar.name + " is unavailable, adding item offline");
self.adoptOfflineItem(item, listener);
} else if (Components.isSuccessCode(status)) {
// On success, add the item to the cache.
self.mCachedCalendar.addItem(detail, listener);
} else if (listener) {
// Either an error occurred or this is a successful add
// to a cached calendar. Forward the call to the listener
listener.onOperationComplete(self, status, opType, id, detail);
}
}
}
if (this.offline) {
// If we are offline, don't even try to add the item
this.adoptOfflineItem(item, listener);
} else {
// Otherwise ask the provider to add the item now.
this.mUncachedCalendar.adoptItem(item, cacheListener);
}
},
adoptOfflineItem: function(item, listener) {
var this_ = this;
var opListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {
cal.ASSERT(false, "unexpected!");
},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (Components.isSuccessCode(status)) {
var storage = this_.mCachedCalendar.QueryInterface(Components.interfaces.calIOfflineStorage);
storage.addOfflineItem(detail, listener);
} else if (listener) {
listener.onOperationComplete(this_, status, opType, id, detail);
}
}
};
this.mCachedCalendar.adoptItem(item, opListener);
},
modifyItem: function(newItem, oldItem, listener) {
let self = this;
// First of all, we should find out if the item to modify is
// already an offline item or not.
let flagListener = {
onGetResult: function() {},
onOperationComplete: function(c, s, o, i, offline_flag) {
if (offline_flag == cICL.OFFLINE_FLAG_CREATED_RECORD ||
offline_flag == cICL.OFFLINE_FLAG_MODIFIED_RECORD) {
// The item is already offline, just modify it in the cache
self.modifyOfflineItem(newItem, oldItem, listener);
} else {
// Not an offline item, attempt to modify using provider
self.mUncachedCalendar.modifyItem(newItem, oldItem, cacheListener);
}
}
};
/* Forwarding add/modify/delete to the cached calendar using the calIObserver
* callbacks would be advantageous, because the uncached provider could implement
* a true push mechanism firing without being triggered from within the program.
* But this would mean the uncached provider fires on the passed
* calIOperationListener, e.g. *before* it fires on calIObservers
* (because that order is undefined). Firing onOperationComplete before onAddItem et al
* would result in this facade firing onOperationComplete even though the modification
* hasn't yet been performed on the cached calendar (which happens in onAddItem et al).
* Result is that we currently stick to firing onOperationComplete if the cached calendar
* has performed the modification, see below: */
let cacheListener = {
onGetResult: function() {},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (isUnavailableCode(status)) {
// The item couldn't be modified at the (remote) location,
// this is like being offline. Add the item to the cache
// instead.
cal.LOG("[calCachedCalendar] Calendar " + calendar.name + " is unavailable, modifying item offline");
self.modifyOfflineItem(newItem, oldItem, listener);
} else if (Components.isSuccessCode(status)) {
// On success, modify the item in the cache
self.mCachedCalendar.modifyItem(detail, oldItem, listener);
} else if (listener) {
// This happens on error, forward the error through the listener
listener.onOperationComplete(self, status, opType, id, detail);
}
}
};
if (this.offline) {
// If we are offline, don't even try to modify the item
this.modifyOfflineItem(newItem, oldItem, listener);
} else {
// Otherwise, get the item flags, the listener will further
// process the item.
this.mCachedCalendar.getItemOfflineFlag(oldItem, flagListener);
}
},
modifyOfflineItem: function(newItem, oldItem, listener) {
var this_ = this;
var opListener = {
onGetResult: function(calendar, status, itemType, detail, count, items) {
cal.ASSERT(false, "unexpected!");
},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (Components.isSuccessCode(status)) {
// Modify the offline item in the storage, passing the
// listener will make sure its notified
var storage = this_.mCachedCalendar.QueryInterface(Components.interfaces.calIOfflineStorage);
storage.modifyOfflineItem(detail, listener);
} else if (listener) {
// If there was not a success, then we need to notify the
// listener ourselves
listener.onOperationComplete(this_, status, opType, id, detail);
}
}
};
this.mCachedCalendar.modifyItem(newItem, oldItem, opListener);
},
deleteItem: function(item, listener) {
let self = this;
// First of all, we should find out if the item to delete is
// already an offline item or not.
let flagListener = {
onGetResult: function() {},
onOperationComplete: function(c, s, o, i, offline_flag) {
if (offline_flag == cICL.OFFLINE_FLAG_CREATED_RECORD ||
offline_flag == cICL.OFFLINE_FLAG_MODIFIED_RECORD) {
// The item is already offline, just mark it deleted it in
// the cache
self.deleteOfflineItem(item, listener);
} else {
// Not an offline item, attempt to delete using provider
self.mUncachedCalendar.deleteItem(item, cacheListener);
}
}
};
// Forwarding add/modify/delete to the cached calendar using the calIObserver
// callbacks would be advantageous, because the uncached provider could implement
// a true push mechanism firing without being triggered from within the program.
// But this would mean the uncached provider fires on the passed
// calIOperationListener, e.g. *before* it fires on calIObservers
// (because that order is undefined). Firing onOperationComplete before onAddItem et al
// would result in this facade firing onOperationComplete even though the modification
// hasn't yet been performed on the cached calendar (which happens in onAddItem et al).
// Result is that we currently stick to firing onOperationComplete if the cached calendar
// has performed the modification, see below:
var cacheListener = {
onGetResult: function() {},
onOperationComplete: function(calendar, status, opType, id, detail) {
if (isUnavailableCode(status)) {
// The item couldn't be deleted at the (remote) location,
// this is like being offline. Mark the item deleted in the
// cache instead.
cal.LOG("[calCachedCalendar] Calendar " + calendar.name + " is unavailable, deleting item offline");
self.deleteOfflineItem(item, listener);
} else if (Components.isSuccessCode(status)) {
// On success, delete the item from the cache
self.mCachedCalendar.deleteItem(item, listener);
// Also, remove any meta data associated with the item
try {
let storage = self.mCachedCalendar.QueryInterface(Components.interfaces.calISyncWriteCalendar);
storage.deleteMetaData(item.id);
} catch (e) {
cal.LOG("[calCachedCalendar] Offline storage doesn't support metadata");
}
} else if (listener) {
// This happens on error, forward the error through the listener
listener.onOperationComplete(self, status, opType, id, detail);
}
}
};
if (this.offline) {
// If we are offline, don't even try to delete the item
this.deleteOfflineItem(item, listener);
} else {
// Otherwise, get the item flags, the listener will further
// process the item.
this.mCachedCalendar.getItemOfflineFlag(item, flagListener);
}
},
deleteOfflineItem: function(item, listener) {
/* We do not delete the item from the cache, as we will need it when reconciling the cache content and the server content. */
var storage = this.mCachedCalendar.QueryInterface(Components.interfaces.calIOfflineStorage);
storage.deleteOfflineItem(item, listener);
}
};
(function() {
function defineForwards(proto, targetName, functions, getters, gettersAndSetters) {
function defineForwardGetter(attr) {
proto.__defineGetter__(attr, function() { return this[targetName][attr]; });
}
function defineForwardGetterAndSetter(attr) {
defineForwardGetter(attr);
proto.__defineSetter__(attr, function(value) { return (this[targetName][attr] = value); });
}
function defineForwardFunction(funcName) {
proto[funcName] = function() {
var obj = this[targetName];
return obj[funcName].apply(obj, arguments);
};
}
functions.forEach(defineForwardFunction);
getters.forEach(defineForwardGetter);
gettersAndSetters.forEach(defineForwardGetterAndSetter);
}
defineForwards(calCachedCalendar.prototype, "mUncachedCalendar",
["setProperty", "deleteProperty",
"isInvitation", "getInvitedAttendee", "canNotify"],
["type", "aclManager", "aclEntry"],
["id", "name", "uri", "readOnly"]);
defineForwards(calCachedCalendar.prototype, "mCachedCalendar",
["getItem", "getItems", "startBatch", "endBatch"], [], []);
})();
|