Bug 1626066 - Move calendar invitations manager code out of messenger-overlay-sidebar.js. r=darktrojan
authorPaul Morris <paul@thunderbird.net>
Thu, 02 Apr 2020 13:42:50 +0300
changeset 38669 4c4e4de548fab79aae78dbf69c3f3b0296300ec1
parent 38668 3659c628e59178dfd5e930f16e4d1336201e658e
child 38670 51af2ea087fc6b75847adbe79eb0fbb97431d3d6
push id400
push userclokep@gmail.com
push dateMon, 04 May 2020 18:56:09 +0000
reviewersdarktrojan
bugs1626066
Bug 1626066 - Move calendar invitations manager code out of messenger-overlay-sidebar.js. r=darktrojan
calendar/base/content/calendar-invitations-manager.js
calendar/lightning/content/messenger-overlay-sidebar.js
--- a/calendar/base/content/calendar-invitations-manager.js
+++ b/calendar/base/content/calendar-invitations-manager.js
@@ -1,16 +1,20 @@
 /* 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/. */
 
 var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 
-/* exported getInvitationsManager */
+/* exported getInvitationsManager, openInvitationsDialog, setUpInvitationsManager,
+ *          tearDownInvitationsManager
+ */
+
+/* globals setElementValue */
 
 /**
  * This object contains functions to take care of manipulating requests.
  */
 var gInvitationsRequestManager = {
   mRequestStatusList: {},
 
   /**
@@ -48,16 +52,90 @@ var gInvitationsManager = null;
  */
 function getInvitationsManager() {
   if (!gInvitationsManager) {
     gInvitationsManager = new InvitationsManager();
   }
   return gInvitationsManager;
 }
 
+// Listeners, observers, set up, tear down, opening dialog, etc. This code kept
+// separate from the InvitationsManager class itself for separation of concerns.
+
+// == invitations link
+const FIRST_DELAY_STARTUP = 100;
+const FIRST_DELAY_RESCHEDULE = 100;
+const FIRST_DELAY_REGISTER = 10000;
+const FIRST_DELAY_UNREGISTER = 0;
+
+var gInvitationsOperationListener = {
+  mCount: 0,
+  QueryInterface: ChromeUtils.generateQI([Ci.calIOperationListener]),
+
+  onOperationComplete(aCalendar, aStatus, aOperationType, aId, aDetail) {
+    let invitationsBox = document.getElementById("calendar-invitations-panel");
+    if (Components.isSuccessCode(aStatus)) {
+      let value = cal.l10n.getLtnString("invitationsLink.label", [this.mCount]);
+      document.getElementById("calendar-invitations-label").value = value;
+      setElementValue(invitationsBox, this.mCount < 1 && "true", "hidden");
+    } else {
+      invitationsBox.setAttribute("hidden", "true");
+    }
+    this.mCount = 0;
+  },
+
+  onGetResult(aCalendar, aStatus, aItemType, aDetail, aItems) {
+    if (Components.isSuccessCode(aStatus)) {
+      this.mCount += aItems.length;
+    }
+  },
+};
+
+var gInvitationsCalendarManagerObserver = {
+  mStoredThis: this,
+  QueryInterface: ChromeUtils.generateQI([Ci.calICalendarManagerObserver]),
+
+  onCalendarRegistered(aCalendar) {
+    this.mStoredThis.rescheduleInvitationsUpdate(FIRST_DELAY_REGISTER);
+  },
+
+  onCalendarUnregistering(aCalendar) {
+    this.mStoredThis.rescheduleInvitationsUpdate(FIRST_DELAY_UNREGISTER);
+  },
+
+  onCalendarDeleting(aCalendar) {},
+};
+
+function scheduleInvitationsUpdate(firstDelay) {
+  gInvitationsOperationListener.mCount = 0;
+  getInvitationsManager().scheduleInvitationsUpdate(firstDelay, gInvitationsOperationListener);
+}
+
+function rescheduleInvitationsUpdate(firstDelay) {
+  getInvitationsManager().cancelInvitationsUpdate();
+  scheduleInvitationsUpdate(firstDelay);
+}
+
+function openInvitationsDialog() {
+  getInvitationsManager().cancelInvitationsUpdate();
+  gInvitationsOperationListener.mCount = 0;
+  getInvitationsManager().openInvitationsDialog(gInvitationsOperationListener, () =>
+    scheduleInvitationsUpdate(FIRST_DELAY_RESCHEDULE)
+  );
+}
+
+function setUpInvitationsManager() {
+  scheduleInvitationsUpdate(FIRST_DELAY_STARTUP);
+  cal.getCalendarManager().addObserver(gInvitationsCalendarManagerObserver);
+}
+
+function tearDownInvitationsManager() {
+  cal.getCalendarManager().removeObserver(gInvitationsCalendarManagerObserver);
+}
+
 /**
  * The invitations manager class constructor
  *
  * XXX do we really need this to be an instance?
  *
  * @constructor
  */
 function InvitationsManager() {
--- a/calendar/lightning/content/messenger-overlay-sidebar.js
+++ b/calendar/lightning/content/messenger-overlay-sidebar.js
@@ -1,24 +1,26 @@
 /* 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/. */
 
 /* exported calendarOnToolbarsPopupShowing, customizeMailToolbarForTabType,
  *          gCurrentMode, InitViewCalendarPaneMenu, loadCalendarComponent,
  *          onToolbarsPopupShowingWithMode, openInvitationsDialog, refreshUIBits,
- *          rescheduleInvitationsUpdate, switchCalendarView
+ *          switchCalendarView,
  */
 
 /* import-globals-from ../../base/content/calendar-command-controller.js */
 /* import-globals-from ../../base/content/calendar-invitations-manager.js */
 /* import-globals-from ../../base/content/today-pane.js */
 /* import-globals-from lightning-item-panel.js */
 
-/* globals calSwitchToMode, changeMode, */
+/* globals calSwitchToMode, changeMode, setUpInvitationsManager,
+ *         tearDownInvitationsManager,
+ */
 
 var { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
 var { AddonManager } = ChromeUtils.import("resource://gre/modules/AddonManager.jsm");
 var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");
 
 /**
  * Loads the calendar component. Called once at startup as the application
  * window is loaded, before tabs are restored.
@@ -34,19 +36,17 @@ async function loadCalendarComponent() {
     .addEventListener("select", LtnObserveDisplayDeckChange, true);
 
   // Take care of common initialization
   await commonInitCalendar();
 
   // Add an unload function to the window so we don't leak any listeners
   window.addEventListener("unload", ltnFinish);
 
-  // Set up invitations manager
-  scheduleInvitationsUpdate(FIRST_DELAY_STARTUP);
-  cal.getCalendarManager().addObserver(gInvitationsCalendarManagerObserver);
+  setUpInvitationsManager();
 
   let filter = document.getElementById("task-tree-filtergroup");
   filter.value = filter.value || "all";
   changeMode();
 
   let mailContextPopup = document.getElementById("mailContext");
   if (mailContextPopup) {
     mailContextPopup.addEventListener("popupshowing", gCalSetupMailContext.popup);
@@ -192,91 +192,28 @@ function LtnObserveDisplayDeckChange(eve
     id != "calendar-view-box" &&
     id != "calendar-task-box"
   ) {
     calSwitchToMode("mail");
   }
 }
 
 function ltnFinish() {
-  cal.getCalendarManager().removeObserver(gInvitationsCalendarManagerObserver);
+  tearDownInvitationsManager();
 
   // Remove listener for mailContext.
   let mailContextPopup = document.getElementById("mailContext");
   if (mailContextPopup) {
     mailContextPopup.removeEventListener("popupshowing", gCalSetupMailContext.popup);
   }
 
   // Common finish steps
   commonFinishCalendar();
 }
 
-// == invitations link
-var FIRST_DELAY_STARTUP = 100;
-var FIRST_DELAY_RESCHEDULE = 100;
-var FIRST_DELAY_REGISTER = 10000;
-var FIRST_DELAY_UNREGISTER = 0;
-
-var gInvitationsOperationListener = {
-  mCount: 0,
-
-  QueryInterface: ChromeUtils.generateQI([Ci.calIOperationListener]),
-  onOperationComplete(aCalendar, aStatus, aOperationType, aId, aDetail) {
-    let invitationsBox = document.getElementById("calendar-invitations-panel");
-    if (Components.isSuccessCode(aStatus)) {
-      let value = cal.l10n.getLtnString("invitationsLink.label", [this.mCount]);
-      document.getElementById("calendar-invitations-label").value = value;
-      setElementValue(invitationsBox, this.mCount < 1 && "true", "hidden");
-    } else {
-      invitationsBox.setAttribute("hidden", "true");
-    }
-    this.mCount = 0;
-  },
-
-  onGetResult(aCalendar, aStatus, aItemType, aDetail, aItems) {
-    if (Components.isSuccessCode(aStatus)) {
-      this.mCount += aItems.length;
-    }
-  },
-};
-
-var gInvitationsCalendarManagerObserver = {
-  mSideBar: this,
-
-  QueryInterface: ChromeUtils.generateQI([Ci.calICalendarManagerObserver]),
-
-  onCalendarRegistered(aCalendar) {
-    this.mSideBar.rescheduleInvitationsUpdate(FIRST_DELAY_REGISTER);
-  },
-
-  onCalendarUnregistering(aCalendar) {
-    this.mSideBar.rescheduleInvitationsUpdate(FIRST_DELAY_UNREGISTER);
-  },
-
-  onCalendarDeleting(aCalendar) {},
-};
-
-function scheduleInvitationsUpdate(firstDelay) {
-  gInvitationsOperationListener.mCount = 0;
-  getInvitationsManager().scheduleInvitationsUpdate(firstDelay, gInvitationsOperationListener);
-}
-
-function rescheduleInvitationsUpdate(firstDelay) {
-  getInvitationsManager().cancelInvitationsUpdate();
-  scheduleInvitationsUpdate(firstDelay);
-}
-
-function openInvitationsDialog() {
-  getInvitationsManager().cancelInvitationsUpdate();
-  gInvitationsOperationListener.mCount = 0;
-  getInvitationsManager().openInvitationsDialog(gInvitationsOperationListener, () =>
-    scheduleInvitationsUpdate(FIRST_DELAY_RESCHEDULE)
-  );
-}
-
 var gCalSetupMailContext = {
   popup() {
     let hasSelection = gFolderDisplay.selectedMessage != null;
     // Disable the convert menu altogether.
     setElementValue("mailContext-calendar-convert-menu", !hasSelection && "true", "hidden");
   },
 };