summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/modules/utils/calAlarmUtils.jsm
blob: d4792652f7fe215ec4af1efd4186c320b82bf362 (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
/* 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/. */

/**
 * Helpers for manipulating calendar alarms
 */

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

const EXPORTED_SYMBOLS = ["calalarms"];

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

const lazy = {};

ChromeUtils.defineModuleGetter(lazy, "cal", "resource:///modules/calendar/calUtils.jsm");
XPCOMUtils.defineLazyModuleGetters(lazy, {
  CalAlarm: "resource:///modules/CalAlarm.jsm",
});

var calalarms = {
  /**
   * Read default alarm settings from user preferences and apply them to the
   * event/todo passed in. The item's calendar should be set to ensure the
   * correct alarm type is set.
   *
   * @param aItem     The item to apply the default alarm values to.
   */
  setDefaultValues(aItem) {
    let type = aItem.isEvent() ? "event" : "todo";
    if (Services.prefs.getIntPref("calendar.alarms.onfor" + type + "s", 0) == 1) {
      let alarmOffset = lazy.cal.createDuration();
      let alarm = new lazy.CalAlarm();
      let units = Services.prefs.getStringPref("calendar.alarms." + type + "alarmunit", "minutes");

      // Make sure the alarm pref is valid, default to minutes otherwise
      if (!["weeks", "days", "hours", "minutes", "seconds"].includes(units)) {
        units = "minutes";
      }

      alarmOffset[units] = Services.prefs.getIntPref("calendar.alarms." + type + "alarmlen", 0);
      alarmOffset.normalize();
      alarmOffset.isNegative = true;
      if (type == "todo" && !aItem.entryDate) {
        // You can't have an alarm if the entryDate doesn't exist.
        aItem.entryDate = lazy.cal.dtz.now();
      }
      alarm.related = Ci.calIAlarm.ALARM_RELATED_START;
      alarm.offset = alarmOffset;

      // Default to a display alarm, unless the calendar doesn't support
      // it or we have no calendar yet. (Man this is hard to wrap)
      let actionValues = (aItem.calendar &&
        aItem.calendar.getProperty("capabilities.alarms.actionValues")) || ["DISPLAY"];

      alarm.action = actionValues.includes("DISPLAY") ? "DISPLAY" : actionValues[0];
      aItem.addAlarm(alarm);
    }
  },

  /**
   * Calculate the alarm date for a calIAlarm.
   *
   * @param aItem     The item used to calculate the alarm date.
   * @param aAlarm    The alarm to calculate the date for.
   * @returns The alarm date.
   */
  calculateAlarmDate(aItem, aAlarm) {
    if (aAlarm.related == Ci.calIAlarm.ALARM_RELATED_ABSOLUTE) {
      return aAlarm.alarmDate;
    }
    let returnDate;
    if (aAlarm.related == Ci.calIAlarm.ALARM_RELATED_START) {
      returnDate = aItem[lazy.cal.dtz.startDateProp(aItem)];
    } else if (aAlarm.related == Ci.calIAlarm.ALARM_RELATED_END) {
      returnDate = aItem[lazy.cal.dtz.endDateProp(aItem)];
    }

    if (returnDate && aAlarm.offset) {
      // Handle all day events.  This is kinda weird, because they don't
      // have a well defined startTime.  We just consider the start/end
      // to be midnight in the user's timezone.
      if (returnDate.isDate) {
        let timezone = lazy.cal.dtz.defaultTimezone;
        // This returns a copy, so no extra cloning needed.
        returnDate = returnDate.getInTimezone(timezone);
        returnDate.isDate = false;
      } else if (returnDate.timezone.tzid == "floating") {
        let timezone = lazy.cal.dtz.defaultTimezone;
        returnDate = returnDate.getInTimezone(timezone);
      } else {
        // Clone the date to correctly add the duration.
        returnDate = returnDate.clone();
      }

      returnDate.addDuration(aAlarm.offset);
      return returnDate;
    }

    return null;
  },

  /**
   * Removes previous children and adds reminder images to a given container,
   * making sure only one icon per alarm action is added.
   *
   * @param {Element} container - The element to add the images to.
   * @param {CalAlarm[]} reminderSet - The set of reminders to add images for.
   */
  addReminderImages(container, reminderSet) {
    while (container.lastChild) {
      container.lastChild.remove();
    }

    let document = container.ownerDocument;
    let suppressed = container.hasAttribute("suppressed");
    let actionSet = [];
    for (let reminder of reminderSet) {
      // Up to one icon per action
      if (actionSet.includes(reminder.action)) {
        continue;
      }
      actionSet.push(reminder.action);

      let src;
      let l10nId;
      switch (reminder.action) {
        case "DISPLAY":
          if (suppressed) {
            src = "chrome://messenger/skin/icons/new/bell-disabled.svg";
            l10nId = "calendar-editable-item-reminder-icon-suppressed-alarm";
          } else {
            src = "chrome://messenger/skin/icons/new/bell.svg";
            l10nId = "calendar-editable-item-reminder-icon-alarm";
          }
          break;
        case "EMAIL":
          src = "chrome://messenger/skin/icons/new/mail-sm.svg";
          l10nId = "calendar-editable-item-reminder-icon-email";
          break;
        case "AUDIO":
          src = "chrome://messenger/skin/icons/new/bell-ring.svg";
          l10nId = "calendar-editable-item-reminder-icon-audio";
          break;
        default:
          // Never create icons for actions we don't handle.
          continue;
      }

      let image = document.createElement("img");
      image.setAttribute("class", "reminder-icon");
      image.setAttribute("value", reminder.action);
      image.setAttribute("src", src);
      // Set alt.
      document.l10n.setAttributes(image, l10nId);
      container.appendChild(image);
    }
  },
};