Incidenceeditor

alarmpresets.cpp
1/*
2 SPDX-FileCopyrightText: 2010 Bertjan Broeksema <broeksema@kde.org>
3 SPDX-FileCopyrightText: 2010 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "alarmpresets.h"
9
10#include <CalendarSupport/KCalPrefs>
11
12#include <KLocalizedString>
13
14#include "incidenceeditor_debug.h"
15
16using namespace CalendarSupport;
17using namespace KCalendarCore;
18
19namespace IncidenceEditorNG
20{
21namespace AlarmPresets
22{
23// Don't use a map, because order matters
24Q_GLOBAL_STATIC(QStringList, sBeforeStartPresetNames)
25Q_GLOBAL_STATIC(QStringList, sBeforeEndPresetNames)
26Q_GLOBAL_STATIC(QList<KCalendarCore::Alarm::Ptr>, sBeforeStartPresets)
27Q_GLOBAL_STATIC(QList<KCalendarCore::Alarm::Ptr>, sBeforeEndPresets)
28
29static int sDefaultPresetIndex = 0;
30static int sDefaultAlarmOffset = 0; // We must save it, so we can detect that config changed.
31
32int configuredReminderTimeInMinutes()
33{
34 QList<int> units;
35 units << 1 << 60 << (24 * 60);
36
37 const int configuredUnits = KCalPrefs::instance()->reminderTimeUnits();
38 const int unitsToUse = configuredUnits >= 0 && configuredUnits <= 2 ? configuredUnits : 0;
39
40 const int configuredReminderTime = KCalPrefs::instance()->reminderTime();
41 const int reminderTimeToUse = configuredReminderTime >= 0 ? configuredReminderTime : DEFAULT_REMINDER_OFFSET;
42
43 return reminderTimeToUse * units[unitsToUse];
44}
45
46void initPresets(AlarmPresets::When when)
47{
48 QList<int> hardcodedPresets;
49 hardcodedPresets << 0 // at start/due
50 << 5 // 5 minutes
51 << 10 << 15 << 30 << 45 << 60 // 1 hour
52 << 2 * 60 // 2 hours
53 << 24 * 60 // 1 day
54 << 2 * 24 * 60 // 2 days
55 << 5 * 24 * 60; // 5 days
56
57 sDefaultAlarmOffset = configuredReminderTimeInMinutes();
58
59 if (!hardcodedPresets.contains(sDefaultAlarmOffset)) {
60 // Lets insert the user's favorite preset (and keep the list sorted):
61 int index;
62 for (index = 0; index < hardcodedPresets.count(); ++index) {
63 if (hardcodedPresets[index] > sDefaultAlarmOffset) {
64 break;
65 }
66 }
67 hardcodedPresets.insert(index, sDefaultAlarmOffset);
68 sDefaultPresetIndex = index;
69 } else {
70 sDefaultPresetIndex = hardcodedPresets.indexOf(sDefaultAlarmOffset);
71 }
72
73 switch (when) {
74 case AlarmPresets::BeforeStart:
75
76 for (int i = 0; i < hardcodedPresets.count(); ++i) {
78 alarm->setType(KCalendarCore::Alarm::Display);
79 const int minutes = hardcodedPresets[i];
80 alarm->setStartOffset(-minutes * 60);
81 alarm->setEnabled(true);
82 if (minutes == 0) {
83 sBeforeStartPresetNames->append(i18nc("@item:inlistbox", "At start"));
84 } else if (minutes < 60) {
85 sBeforeStartPresetNames->append(i18ncp("@item:inlistbox", "%1 minute before start", "%1 minutes before start", minutes));
86 } else if (minutes < 24 * 60) {
87 sBeforeStartPresetNames->append(i18ncp("@item:inlistbox", "%1 hour before start", "%1 hours before start", minutes / 60));
88 } else {
89 sBeforeStartPresetNames->append(i18ncp("@item:inlistbox", "%1 day before start", "%1 days before start", minutes / (24 * 60)));
90 }
91 sBeforeStartPresets->append(alarm);
92 }
93 break;
94
95 case AlarmPresets::BeforeEnd:
96 for (int i = 0; i < hardcodedPresets.count(); ++i) {
98 alarm->setType(KCalendarCore::Alarm::Display);
99 const int minutes = hardcodedPresets[i];
100 alarm->setEndOffset(-minutes * 60);
101 alarm->setEnabled(true);
102 if (minutes == 0) {
103 sBeforeEndPresetNames->append(i18nc("@item:inlistbox", "When due"));
104 } else if (minutes < 60) {
105 sBeforeEndPresetNames->append(i18ncp("@item:inlistbox", "%1 minute before due", "%1 minutes before due", minutes));
106 } else if (minutes < 24 * 60) {
107 sBeforeEndPresetNames->append(i18ncp("@item:inlistbox", "%1 hour before due", "%1 hours before due", minutes / 60));
108 } else {
109 sBeforeEndPresetNames->append(i18ncp("@item:inlistbox", "%1 day before due", "%1 days before due", minutes / (24 * 60)));
110 }
111 sBeforeEndPresets->append(alarm);
112 }
113 break;
114 }
115}
116
117void checkInitNeeded(When when)
118{
119 const int currentAlarmOffset = configuredReminderTimeInMinutes();
120 const bool configChanged = currentAlarmOffset != sDefaultAlarmOffset;
121
122 switch (when) {
123 case AlarmPresets::BeforeStart:
124 if (sBeforeStartPresetNames->isEmpty() || configChanged) {
125 sBeforeStartPresetNames->clear();
126 sBeforeStartPresets->clear();
127 initPresets(when);
128 }
129 break;
130 case AlarmPresets::BeforeEnd:
131 if (sBeforeEndPresetNames->isEmpty() || configChanged) {
132 sBeforeEndPresetNames->clear();
133 sBeforeEndPresets->clear();
134 initPresets(when);
135 }
136 break;
137 default:
138 Q_ASSERT_X(false, "checkInitNeeded", "Unknown preset type");
139 }
140}
141
142QStringList availablePresets(AlarmPresets::When when)
143{
144 checkInitNeeded(when);
145
146 switch (when) {
147 case AlarmPresets::BeforeStart:
148 return *sBeforeStartPresetNames;
149 case AlarmPresets::BeforeEnd:
150 return *sBeforeEndPresetNames;
151 }
152 return {};
153}
154
155KCalendarCore::Alarm::Ptr preset(When when, const QString &name)
156{
157 checkInitNeeded(when);
158
159 switch (when) {
160 case AlarmPresets::BeforeStart:
161 // The name should exists and only once
162 if (sBeforeStartPresetNames->count(name) != 1) {
163 // print some debug info before crashing
164 qCDebug(INCIDENCEEDITOR_LOG) << " name = " << name << "; when = " << when << "; count for name = " << sBeforeStartPresetNames->count(name)
165 << "; global count = " << sBeforeStartPresetNames->count();
166 Q_ASSERT_X(false, "preset", "Number of presets should be one");
167 }
168
169 return KCalendarCore::Alarm::Ptr(new KCalendarCore::Alarm(*sBeforeStartPresets->at(sBeforeStartPresetNames->indexOf(name))));
170 case AlarmPresets::BeforeEnd:
171 Q_ASSERT(sBeforeEndPresetNames->count(name) == 1); // The name should exists and only once
172
173 return KCalendarCore::Alarm::Ptr(new KCalendarCore::Alarm(*sBeforeEndPresets->at(sBeforeEndPresetNames->indexOf(name))));
174 }
175 return {};
176}
177
178KCalendarCore::Alarm::Ptr defaultAlarm(When when)
179{
180 checkInitNeeded(when);
181
182 switch (when) {
183 case AlarmPresets::BeforeStart:
184 return Alarm::Ptr(new Alarm(*sBeforeStartPresets->at(sDefaultPresetIndex)));
185 case AlarmPresets::BeforeEnd:
186 return Alarm::Ptr(new Alarm(*sBeforeEndPresets->at(sDefaultPresetIndex)));
187 }
188 return {};
189}
190
191int presetIndex(When when, const KCalendarCore::Alarm::Ptr &alarm)
192{
193 checkInitNeeded(when);
194 const QStringList presets = availablePresets(when);
195
196 for (int i = 0; i < presets.size(); ++i) {
197 KCalendarCore::Alarm::Ptr presetAlarm(preset(when, presets.at(i)));
198 if (presetAlarm == alarm) {
199 return i;
200 }
201 }
202
203 return -1;
204}
205
206int defaultPresetIndex()
207{
208 // BeforeEnd would do too, index is the same.
209 checkInitNeeded(AlarmPresets::BeforeStart);
210 return sDefaultPresetIndex;
211}
212} // AlarmPresets
213} // IncidenceEditorNG
QSharedPointer< Alarm > Ptr
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18ncp(const char *context, const char *singular, const char *plural, const TYPE &arg...)
QString name(StandardAction id)
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
bool contains(const AT &value) const const
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
iterator insert(const_iterator before, parameter_type value)
qsizetype size() const const
qsizetype count() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.