calendar/base/modules/calCalendarDeactivator.jsm
author Geoff Lankow <geoff@darktrojan.net>
Tue, 17 Mar 2020 21:37:55 +1300
changeset 38502 6b7f4b4acf12c581adaa6588006cfd1d2b33c6bc
parent 38499 7b3549479ead955071d25aa89e17e0ae50b95082
child 38563 fb7372b0b4f73436f03d02908dbc954e9a48bc77
permissions -rw-r--r--
Backed out changeset 7b3549479ead (bug 1589005) for breaking the calendar tests. rs=bustage-fix I guess this wasn't supposed to be checked in yet

/* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

this.EXPORTED_SYMBOLS = ["calendarDeactivator"];

/**
 * Handles deactivation of calendar UI and background processes/services (such
 * as the alarms service) when users do not want to use calendar functionality.
 * Also handles re-activation when users change their mind.
 *
 * If all of a user's calendars are disabled (e.g. calendar > properties >
 * "turn this calendar on") then full calendar functionality is deactivated.
 * If one or more calendars are enabled then full calendar functionality is
 * activated.
 *
 * Note we use "disabled"/"enabled" for a user's individual calendars and
 * "deactivated"/"activated" for the calendar component as a whole.
 *
 * @implements {calICalendarManagerObserver}
 * @implements {calIObserver}
 */
var calendarDeactivator = {
  windows: new Set(),
  calendars: null,
  isCalendarActivated: null,
  QueryInterface: cal.generateQI([Ci.calICalendarManagerObserver, Ci.calIObserver]),

  initializeDeactivator() {
    let manager = cal.getCalendarManager();
    this.calendars = new Set(manager.getCalendars());
    manager.addObserver(this);
    manager.addCalendarObserver(this);
    this.isCalendarActivated = this.checkCalendarsEnabled();
  },

  /**
   * Register a window to allow future modifications, and set up the window's
   * deactivated/activated state. Deregistration is not required.
   *
   * @param {ChromeWindow} window - A ChromeWindow object.
   */
  registerWindow(window) {
    if (this.calendars === null) {
      this.initializeDeactivator();
    }
    this.windows.add(window);
    window.addEventListener("unload", () => this.windows.delete(window));

    if (!this.isCalendarActivated) {
      window.document.documentElement.setAttribute("calendar-deactivated", "");
    }
  },

  /**
   * Check the enabled state of all of the user's calendars.
   *
   * @return {boolean} True if any calendars are enabled, false if all are disabled.
   */
  checkCalendarsEnabled() {
    for (let calendar of this.calendars) {
      if (!calendar.getProperty("disabled")) {
        return true;
      }
    }
    return false;
  },

  /**
   * If needed, change the calendar activated/deactivated state and update the
   * UI and background processes/services accordingly.
   */
  refreshDeactivatedState() {
    let someCalsEnabled = this.checkCalendarsEnabled();

    if (someCalsEnabled == this.isCalendarActivated) {
      return;
    }

    for (let window of this.windows) {
      if (someCalsEnabled) {
        window.document.documentElement.removeAttribute("calendar-deactivated");
      } else {
        window.document.documentElement.setAttribute("calendar-deactivated", "");
      }
    }

    if (someCalsEnabled) {
      Services.prefs.setBoolPref("calendar.itip.showImipBar", true);
    }

    this.isCalendarActivated = someCalsEnabled;
  },

  // calICalendarManagerObserver methods
  onCalendarRegistered(calendar) {
    this.calendars.add(calendar);

    if (!this.isCalendarActivated && !calendar.getProperty("disabled")) {
      this.refreshDeactivatedState();
    }
  },

  onCalendarUnregistering(calendar) {
    this.calendars.delete(calendar);

    if (!calendar.getProperty("disabled")) {
      this.refreshDeactivatedState();
    }
  },
  onCalendarDeleting(calendar) {},

  // calIObserver methods
  onStartBatch() {},
  onEndBatch() {},
  onLoad() {},
  onAddItem(item) {},
  onModifyItem(newItem, oldItem) {},
  onDeleteItem(deletedItem) {},
  onError(calendar, errNo, message) {},

  onPropertyChanged(calendar, name, value, oldValue) {
    if (name == "disabled") {
      this.refreshDeactivatedState();
    }
  },

  onPropertyDeleting(calendar, name) {},
};