Fix bug 662718 - Empty "Calendar Alarm" popup at every startup. pending r=mmecca
authorPhilipp Kewisch <mozilla@kewis.ch>
Mon, 27 Jun 2011 09:49:36 +0200
changeset 7981 deefec7bd03ca280cbdc0066b1b64502d3e563e4
parent 7980 184049e2fddf87e629207c4255a6d256d95a4d33
child 7982 518c0fd4d729f688f24b1c6ece7492074737a1ea
push id50
push usermozilla@kewis.ch
push dateTue, 19 Jul 2011 20:04:29 +0000
treeherdercomm-beta@aee08b4d6cf3 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmmecca
bugs662718
Fix bug 662718 - Empty "Calendar Alarm" popup at every startup. pending r=mmecca
calendar/base/content/dialogs/calendar-alarm-dialog.js
calendar/base/src/calAlarmMonitor.js
--- a/calendar/base/content/dialogs/calendar-alarm-dialog.js
+++ b/calendar/base/content/dialogs/calendar-alarm-dialog.js
@@ -267,52 +267,61 @@ function addWidgetFor(aItem, aAlarm) {
  *
  * @param aItem       The calendar item to remove the alarm widget for.
  * @param aAlarm      The alarm to remove the widget for.
  */
 function removeWidgetFor(aItem, aAlarm) {
     let hashId = aItem.hashId;
     let alarmRichlist = document.getElementById("alarm-richlist");
     let nodes = alarmRichlist.childNodes;
-    for (let i = nodes.length - 1; i >= 0; --i) {
+    let notfound = true;
+    for (let i = nodes.length - 1; notfound && i >= 0; --i) {
         let widget = nodes[i];
         if (widget.item && widget.item.hashId == hashId &&
             widget.alarm && widget.alarm.icalString == aAlarm.icalString) {
 
             if (widget.selected) {
                 // Advance selection if needed
                 widget.control.selectedItem = widget.previousSibling ||
                                               widget.nextSibling;
             }
 
             widget.removeEventListener("snooze", onSnoozeAlarm, false);
             widget.removeEventListener("dismiss", onDismissAlarm, false);
             widget.removeEventListener("itemdetails", onItemDetails, false);
+
             alarmRichlist.removeChild(widget);
-
-            if (!alarmRichlist.hasChildNodes()) {
-                // check again next round since this removeWidgetFor call may be
-                // followed by an addWidgetFor call (e.g. when refreshing), and
-                // we don't want to close and open the window in that case.
-                function closer() {
-                    if (!alarmRichlist.hasChildNodes()) {
-                        window.close();
-                    }
-                }
-                setTimeout(closer, 0);
-            }
-            break;
+            closeIfEmpty();
+            notfound = false;
         }
     }
 
     // Update the title
     setupTitle();
 }
 
 /**
+ * Close the alarm dialog if there are no further alarm widgets
+ */
+function closeIfEmpty() {
+    let alarmRichlist = document.getElementById("alarm-richlist");
+    if (!alarmRichlist.hasChildNodes()) {
+        // check again next round since this removeWidgetFor call may be
+        // followed by an addWidgetFor call (e.g. when refreshing), and
+        // we don't want to close and open the window in that case.
+        function closer() {
+            if (!alarmRichlist.hasChildNodes()) {
+                window.close();
+            }
+        }
+        setTimeout(closer, 0);
+    }
+}
+
+/**
  * Handler function called when an alarm entry in the richlistbox is selected
  *
  * @param event         The DOM event from the click action
  */
 function onSelectAlarm(event) {
     let richList = document.getElementById("alarm-richlist")
     if (richList == event.target) {
         richList.ensureElementIsVisible(richList.getSelectedItem(0));
--- a/calendar/base/src/calAlarmMonitor.js
+++ b/calendar/base/src/calAlarmMonitor.js
@@ -174,18 +174,24 @@ calAlarmMonitor.prototype = {
         if (!this.mWindowOpening) {
             calAlarmWindow.addWidgetFor(aItem, aAlarm);
         }
     },
 
     window_onLoad: function cAM_window_onLoad() {
         let calAlarmWindow = this.mWindowOpening;
         this.mWindowOpening = null;
-        for each (let [item, alarm] in this.mAlarms) {
-            calAlarmWindow.addWidgetFor(item, alarm);
+        if (this.mAlarms.length > 0) {
+            for each (let [item, alarm] in this.mAlarms) {
+                calAlarmWindow.addWidgetFor(item, alarm);
+            }
+        } else {
+            // Uh oh, it seems the alarms were removed even before the window
+            // finished loading. Looks like we can close it again
+            calAlarmWindow.closeIfEmpty();
         }
     },
 
     onRemoveAlarmsByItem: function cAM_onRemoveAlarmsByItem(aItem) {
         let calAlarmWindow = peekAlarmWindow();
         this.mAlarms = this.mAlarms.filter(function(itemAlarm) {
             let [thisItem, alarm] = itemAlarm;
             let ret = (aItem.hashId != thisItem.parentItem.hashId);