summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/modules/utils/calWindowUtils.jsm
blob: 21626d9ef003e9aec646e1586265353957a0b3f2 (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
172
173
174
175
176
177
178
179
180
181
182
/* 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/. */

/**
 * Calendar window helpers, e.g. to open our dialogs
 */

// NOTE: This module should not be loaded directly, it is available when
// including calUtils.jsm under the cal.window namespace.

const EXPORTED_SYMBOLS = ["calwindow"];

const { XPCOMUtils } = ChromeUtils.importESModule("resource://gre/modules/XPCOMUtils.sys.mjs");

const lazy = {};
XPCOMUtils.defineLazyGetter(
  lazy,
  "l10nDeletePrompt",
  () => new Localization(["calendar/calendar-delete-prompt.ftl"], true)
);

var calwindow = {
  /**
   * Opens the Create Calendar wizard
   *
   * @param aWindow    the window to open the dialog on, or null for the main calendar window
   * @param aCallback  a function to be performed after calendar creation
   */
  openCalendarWizard(aWindow, aCallback) {
    let window = aWindow || calwindow.getCalendarWindow();
    window.openDialog(
      "chrome://calendar/content/calendar-creation.xhtml",
      "caEditServer",
      "chrome,titlebar,resizable,centerscreen",
      aCallback
    );
  },

  /**
   * @typedef {object} OpenCalendarPropertiesArgs
   * @property {calICalendar} calendar - The calendar whose properties should be displayed.
   * @property {boolean} [canDisable=true] - Whether the user can disable the calendar.
   */

  /**
   * Opens the calendar properties window for aCalendar.
   *
   * @param {ChromeWindow | null} aWindow   The window to open the dialog on,
   *                                          or null for the main calendar window.
   * @param {OpenCalendarPropertiesArgs} args - Passed directly to the window.
   */
  openCalendarProperties(aWindow, args) {
    let window = aWindow || calwindow.getCalendarWindow();
    window.openDialog(
      "chrome://calendar/content/calendar-properties-dialog.xhtml",
      "CalendarPropertiesDialog",
      "chrome,titlebar,resizable,centerscreen",
      { canDisable: true, ...args }
    );
  },

  /**
   * Returns the most recent calendar window in an application independent way
   */
  getCalendarWindow() {
    return (
      Services.wm.getMostRecentWindow("calendarMainWindow") ||
      Services.wm.getMostRecentWindow("mail:3pane")
    );
  },

  /**
   * Open (or focus if already open) the calendar tab, even if the imip bar is
   * in a message window, and even if there is no main three pane Thunderbird
   * window open. Called when clicking the imip bar's calendar button.
   */
  goToCalendar() {
    let openCal = mainWindow => {
      mainWindow.focus();
      mainWindow.document.getElementById("tabmail").openTab("calendar");
    };

    let mainWindow = Services.wm.getMostRecentWindow("mail:3pane");

    if (mainWindow) {
      openCal(mainWindow);
    } else {
      mainWindow = Services.ww.openWindow(
        null,
        "chrome://messenger/content/messenger.xhtml",
        "_blank",
        "chrome,extrachrome,menubar,resizable,scrollbars,status,toolbar",
        null
      );

      // Wait until calendar is set up in the new window.
      let calStartupObserver = {
        observe(subject, topic, data) {
          openCal(mainWindow);
          Services.obs.removeObserver(calStartupObserver, "calendar-startup-done");
        },
      };
      Services.obs.addObserver(calStartupObserver, "calendar-startup-done");
    }
  },

  /**
   * Brings up a dialog prompting the user about the deletion of the passed
   * item(s).
   *
   * @param {calIItemBase|calItemBase[]} items - One or more items that will be deleted.
   * @param {boolean} byPassPref - If true the pref for this prompt will be ignored.
   *
   * @returns {boolean} True if the user confirms deletion, false if otherwise.
   */
  promptDeleteItems(items, byPassPref) {
    items = Array.isArray(items) ? items : [items];
    let pref = Services.prefs.getBoolPref("calendar.item.promptDelete", true);

    // Recurring events will be handled by the recurring event prompt.
    if ((!pref && !byPassPref) || items.some(item => item.parentItem != item)) {
      return true;
    }

    let deletingEvents;
    let deletingTodos;
    for (let item of items) {
      if (!deletingEvents) {
        deletingEvents = item.isEvent();
      }
      if (!deletingTodos) {
        deletingTodos = item.isTodo();
      }
    }

    let title;
    let message;
    let disableMessage;
    if (deletingEvents && !deletingTodos) {
      [title, message, disableMessage] = lazy.l10nDeletePrompt.formatValuesSync([
        { id: "calendar-delete-event-prompt-title", args: { count: items.length } },
        { id: "calendar-delete-event-prompt-message", args: { count: items.length } },
        "calendar-delete-prompt-disable-message",
      ]);
    } else if (!deletingEvents && deletingTodos) {
      [title, message, disableMessage] = lazy.l10nDeletePrompt.formatValuesSync([
        { id: "calendar-delete-task-prompt-title", args: { count: items.length } },
        { id: "calendar-delete-task-prompt-message", args: { count: items.length } },
        "calendar-delete-prompt-disable-message",
      ]);
    } else {
      [title, message, disableMessage] = lazy.l10nDeletePrompt.formatValuesSync([
        { id: "calendar-delete-items-prompt-title", args: { count: items.length } },
        { id: "calendar-delete-items-prompt-message", args: { count: items.length } },
        "calendar-delete-prompt-disable-message",
      ]);
    }

    if (byPassPref) {
      return Services.prompt.confirm(null, title, message);
    }

    let checkResult = { value: false };
    let result = Services.prompt.confirmEx(
      null,
      title,
      message,
      Services.prompt.STD_YES_NO_BUTTONS + Services.prompt.BUTTON_DELAY_ENABLE,
      null,
      null,
      null,
      disableMessage,
      checkResult
    );

    if (checkResult.value) {
      Services.prefs.setBoolPref("calendar.item.promptDelete", false);
    }
    return result != 1;
  },
};