8#include "alarmpresets.h"
10#include <CalendarSupport/KCalPrefs>
12#include <KLocalizedString>
14#include "incidenceeditor_debug.h"
16using namespace CalendarSupport;
19namespace IncidenceEditorNG
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)
29static int sDefaultPresetIndex = 0;
30static int sDefaultAlarmOffset = 0;
32int configuredReminderTimeInMinutes()
35 units << 1 << 60 << (24 * 60);
37 const int configuredUnits = KCalPrefs::instance()->reminderTimeUnits();
38 const int unitsToUse = configuredUnits >= 0 && configuredUnits <= 2 ? configuredUnits : 0;
40 const int configuredReminderTime = KCalPrefs::instance()->reminderTime();
41 const int reminderTimeToUse = configuredReminderTime >= 0 ? configuredReminderTime : DEFAULT_REMINDER_OFFSET;
43 return reminderTimeToUse * units[unitsToUse];
46void initPresets(AlarmPresets::When when)
48 QList<int> hardcodedPresets;
51 << 10 << 15 << 30 << 45 << 60
57 sDefaultAlarmOffset = configuredReminderTimeInMinutes();
59 if (!hardcodedPresets.
contains(sDefaultAlarmOffset)) {
62 for (index = 0; index < hardcodedPresets.
count(); ++index) {
63 if (hardcodedPresets[index] > sDefaultAlarmOffset) {
67 hardcodedPresets.
insert(index, sDefaultAlarmOffset);
68 sDefaultPresetIndex = index;
70 sDefaultPresetIndex = hardcodedPresets.
indexOf(sDefaultAlarmOffset);
74 case AlarmPresets::BeforeStart:
76 for (
int i = 0; i < hardcodedPresets.
count(); ++i) {
79 const int minutes = hardcodedPresets[i];
80 alarm->setStartOffset(-minutes * 60);
81 alarm->setEnabled(
true);
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));
89 sBeforeStartPresetNames->append(
i18ncp(
"@item:inlistbox",
"%1 day before start",
"%1 days before start", minutes / (24 * 60)));
91 sBeforeStartPresets->
append(alarm);
95 case AlarmPresets::BeforeEnd:
96 for (
int i = 0; i < hardcodedPresets.
count(); ++i) {
99 const int minutes = hardcodedPresets[i];
100 alarm->setEndOffset(-minutes * 60);
101 alarm->setEnabled(
true);
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));
109 sBeforeEndPresetNames->append(
i18ncp(
"@item:inlistbox",
"%1 day before due",
"%1 days before due", minutes / (24 * 60)));
111 sBeforeEndPresets->
append(alarm);
117void checkInitNeeded(When when)
119 const int currentAlarmOffset = configuredReminderTimeInMinutes();
120 const bool configChanged = currentAlarmOffset != sDefaultAlarmOffset;
123 case AlarmPresets::BeforeStart:
124 if (sBeforeStartPresetNames->isEmpty() || configChanged) {
125 sBeforeStartPresetNames->clear();
126 sBeforeStartPresets->clear();
130 case AlarmPresets::BeforeEnd:
131 if (sBeforeEndPresetNames->isEmpty() || configChanged) {
132 sBeforeEndPresetNames->clear();
133 sBeforeEndPresets->clear();
138 Q_ASSERT_X(
false,
"checkInitNeeded",
"Unknown preset type");
142QStringList availablePresets(AlarmPresets::When when)
144 checkInitNeeded(when);
147 case AlarmPresets::BeforeStart:
148 return *sBeforeStartPresetNames;
149 case AlarmPresets::BeforeEnd:
150 return *sBeforeEndPresetNames;
157 checkInitNeeded(when);
160 case AlarmPresets::BeforeStart:
162 if (sBeforeStartPresetNames->count(name) != 1) {
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");
169 return KCalendarCore::Alarm::Ptr(
new KCalendarCore::Alarm(*sBeforeStartPresets->at(sBeforeStartPresetNames->indexOf(name))));
170 case AlarmPresets::BeforeEnd:
171 Q_ASSERT(sBeforeEndPresetNames->count(name) == 1);
173 return KCalendarCore::Alarm::Ptr(
new KCalendarCore::Alarm(*sBeforeEndPresets->at(sBeforeEndPresetNames->indexOf(name))));
180 checkInitNeeded(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)));
193 checkInitNeeded(when);
194 const QStringList presets = availablePresets(when);
196 for (
int i = 0; i < presets.
size(); ++i) {
198 if (presetAlarm == alarm) {
206int defaultPresetIndex()
209 checkInitNeeded(AlarmPresets::BeforeStart);
210 return sDefaultPresetIndex;
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