summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/modules/calCalendarDeactivator.jsm
blob: c987d1772831aa0eeac0929fa23582de4b78b8d1 (plain)
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
/* 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/. */

const EXPORTED_SYMBOLS = ["calendarDeactivator"];

const { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

/**
 * 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: ChromeUtils.generateQI(["calICalendarManagerObserver", "calIObserver"]),

  initializeDeactivator() {
    this.calendars = new Set(cal.manager.getCalendars());
    cal.manager.addObserver(this);
    cal.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.removeAttribute("calendar-deactivated");
    } else {
      this.refreshNotificationBoxes(window, false);
    }
  },

  /**
   * Check the enabled state of all of the user's calendars.
   *
   * @returns {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", "");
      }
      this.refreshNotificationBoxes(window, someCalsEnabled);
    }

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

    this.isCalendarActivated = someCalsEnabled;
  },

  /**
   * Show or hide the notification boxes that appear at the top of the calendar
   * and tasks tabs when calendar functionality is deactivated.
   *
   * @param {ChromeWindow} window - A ChromeWindow object.
   * @param {boolean} isEnabled - Whether any calendars are enabled.
   */
  refreshNotificationBoxes(window, isEnabled) {
    let notificationboxes = [
      [
        window.calendarTabType.modes.calendar.notificationbox,
        "calendar-deactivated-notification-events",
      ],
      [
        window.calendarTabType.modes.tasks.notificationbox,
        "calendar-deactivated-notification-tasks",
      ],
    ];

    let value = "calendarDeactivated";
    for (let [notificationbox, l10nId] of notificationboxes) {
      let existingNotification = notificationbox.getNotificationWithValue(value);

      if (isEnabled) {
        notificationbox.removeNotification(existingNotification);
      } else if (!existingNotification) {
        notificationbox.appendNotification(
          value,
          {
            label: { "l10n-id": l10nId },
            priority: notificationbox.PRIORITY_WARNING_MEDIUM,
          },
          null
        );
      }
    }
  },

  // 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) {},
};