Bug 827078 - Items deleted server-side still appear in cached CalDAV calendars - part 2: ensure meta-data at load time. r=redDragon, a=mmecca
authorMatthew Mecca <matthew.mecca@gmail.com>
Tue, 05 Feb 2013 00:31:38 -0500
changeset 13618 b1d4f46d856f7ef0209914a8b82e9b63ab92598b
parent 13617 3c20b0923d6db5ad01e0a8d49b522d527ff7f0fd
child 13619 cdeed7b9449b7eef0293064823bfcf3a22a97dac
push id33
push usermatthew.mecca@gmail.com
push dateFri, 08 Feb 2013 22:30:17 +0000
reviewersredDragon, mmecca
bugs827078
Bug 827078 - Items deleted server-side still appear in cached CalDAV calendars - part 2: ensure meta-data at load time. r=redDragon, a=mmecca
calendar/providers/caldav/calDavCalendar.js
--- a/calendar/providers/caldav/calDavCalendar.js
+++ b/calendar/providers/caldav/calDavCalendar.js
@@ -247,57 +247,93 @@ calDavCalendar.prototype = {
                 this.mOfflineStorage.setMetaData(id, dataString);
             } else {
                 cal.LOG("CalDAV: cannot store meta data without an id");
             }
         } else {
             cal.ERROR("CalDAV: calendar storage does not support meta data");
         }
     },
+
+    /**
+     * Ensure that cached items have associated meta data, otherwise server side
+     * changes may not be reflected
+     */
+    ensureMetaData: function caldav_ensureMetaData() {
+        let self = this;
+        let refreshNeeded = false;
+        let getMetaListener = {
+            onGetResult: function meta_onGetResult(aCalendar, aStatus, aItemType, aDetail, aCount, aItems) {
+                for each (let item in aItems) {
+                    if (!(item.id in self.mItemInfoCache)) {
+                        let path = self.getItemLocationPath(item);
+                        cal.LOG("Adding meta-data for cached item " + item.id);
+                        self.mItemInfoCache[item.id] = { etag: null,
+                                                         isNew: false,
+                                                         locationPath: path,
+                                                         isInboxItem: false};
+                        self.mHrefIndex[self.mLocationPath + path] = item.id;
+                        refreshNeeded = true;
+                    }
+                }
+            },
+            onOperationComplete: function meta_onOperationComplete(aCalendar, aStatus, aOpType, aId, aDetail) {
+                if (refreshNeeded) {
+                    // reseting the cached ctag forces an item refresh when
+                    // safeRefresh is called later
+                    self.mCtag = null;
+                }
+            }
+        };
+        this.mOfflineStorage.getItems(calICalendar.ITEM_FILTER_ALL_ITEMS,
+                                      0, null, null, getMetaListener);
+    },
+
     fetchCachedMetaData: function caldav_fetchCachedMetaData() {
         cal.LOG("CalDAV: Retrieving server info from cache for " + this.name);
-        var cacheIds = {};
-        var cacheValues = {};
+        let cacheIds = {};
+        let cacheValues = {};
         this.mOfflineStorage.getAllMetaData({}, cacheIds, cacheValues);
         cacheIds = cacheIds.value;
         cacheValues = cacheValues.value;
-        let needsResave = false;
-        let calendarProperties = null;
-        for (var count = 0; count < cacheIds.length; count++) {
-            var itemId = cacheIds[count];
-            var itemData = cacheValues[count];
+
+        for (let count = 0; count < cacheIds.length; count++) {
+            let itemId = cacheIds[count];
+            let itemData = cacheValues[count];
             if (itemId == "ctag") {
                 this.mCtag = itemData;
                 this.mOfflineStorage.deleteMetaData("ctag");
             } else if (itemId == "webdav-sync-token") {
                 this.mWebdavSyncToken = itemData;
                 this.mOfflineStorage.deleteMetaData("sync-token");
             } else if (itemId == "calendar-properties") {
                 this.restoreCalendarProperties(itemData);
                 this.setProperty("currentStatus", Components.results.NS_OK);
                 if (this.mHaveScheduling || this.hasAutoScheduling || this.hasFreeBusy) {
                     cal.getFreeBusyService().addProvider(this);
                 }
             } else {
-                var itemDataArray = itemData.split("\u001A");
-                var etag = itemDataArray[0];
-                var resourcePath = itemDataArray[1];
-                var isInboxItem = itemDataArray[2];
+                let itemDataArray = itemData.split("\u001A");
+                let etag = itemDataArray[0];
+                let resourcePath = itemDataArray[1];
+                let isInboxItem = itemDataArray[2];
                 if (itemDataArray.length == 3) {
                     this.mHrefIndex[resourcePath] = itemId;
-                    var locationPath = resourcePath
+                    let locationPath = resourcePath
                         .substr(this.mLocationPath.length);
-                    var item = { etag: etag,
+                    let item = { etag: etag,
                                  isNew: false,
                                  locationPath: locationPath,
                                  isInboxItem: (isInboxItem == "true")};
                     this.mItemInfoCache[itemId] = item;
                 }
             }
         }
+
+        this.ensureMetaData();
     },
 
     //
     // calICalendar interface
     //
 
     // readonly attribute AUTF8String type;
     get type() { return "caldav"; },