8#include "incidencealarm.h"
11#include "alarmdialog.h"
12#include "alarmpresets.h"
13#include "incidencedatetime.h"
14#include "ui_dialogdesktop.h"
16#include <CalendarSupport/KCalPrefs>
18using namespace IncidenceEditorNG;
19using namespace CalendarSupport;
21IncidenceAlarm::IncidenceAlarm(IncidenceDateTime *dateTime, Ui::EventOrTodoDesktop *ui)
25 setObjectName(
"IncidenceAlarm"_L1);
27 mUi->mAlarmPresetCombo->insertItems(0, AlarmPresets::availablePresets());
28 mUi->mAlarmPresetCombo->setCurrentIndex(AlarmPresets::defaultPresetIndex());
31 connect(mDateTime, &IncidenceDateTime::startDateTimeToggled,
this, &IncidenceAlarm::handleDateTimeToggle);
32 connect(mDateTime, &IncidenceDateTime::endDateTimeToggled,
this, &IncidenceAlarm::handleDateTimeToggle);
51 const auto lstAlarms =
incidence->alarms();
58 mUi->mAlarmPresetCombo->clear();
59 mUi->mAlarmPresetCombo->addItems(AlarmPresets::availablePresets(AlarmPresets::BeforeEnd));
61 mUi->mAlarmPresetCombo->clear();
62 mUi->mAlarmPresetCombo->addItems(AlarmPresets::availablePresets(AlarmPresets::BeforeStart));
64 mUi->mAlarmPresetCombo->setCurrentIndex(AlarmPresets::defaultPresetIndex());
66 handleDateTimeToggle();
80 Q_ASSERT(*al == *(*it));
85bool IncidenceAlarm::isDirty()
const
87 if (mLoadedIncidence->alarms().count() != mAlarms.
count()) {
91 if (!mLoadedIncidence->alarms().isEmpty()) {
94 if (initialAlarms.
count() != mAlarms.
count()) {
106 if (*alarm == *initialAlarm) {
123void IncidenceAlarm::editCurrentAlarm()
128 dialog->load(currentAlarm);
130 dialog->setAllowBeginReminders(mDateTime->startDateTimeEnabled());
131 dialog->setAllowEndReminders(mDateTime->endDateTimeEnabled());
134 dialog->save(currentAlarm);
141void IncidenceAlarm::handleDateTimeToggle()
145 parent->setEnabled(mDateTime->startDateTimeEnabled() || mDateTime->endDateTimeEnabled());
148 mUi->mAlarmPresetCombo->setEnabled(mDateTime->endDateTimeEnabled());
149 mUi->mAlarmAddPresetButton->setEnabled(mDateTime->endDateTimeEnabled());
151 mUi->mQuickAddReminderLabel->setEnabled(mDateTime->endDateTimeEnabled());
154void IncidenceAlarm::newAlarm()
157 const int reminderOffset = KCalPrefs::instance()->reminderTime();
159 if (reminderOffset >= 0) {
160 dialog->setOffset(reminderOffset);
162 dialog->setOffset(DEFAULT_REMINDER_OFFSET);
164 dialog->setUnit(AlarmDialog::Minutes);
165 if (mIsTodo && mDateTime->endDateTimeEnabled()) {
166 dialog->setWhen(AlarmDialog::BeforeEnd);
168 dialog->setWhen(AlarmDialog::BeforeStart);
171 dialog->setAllowBeginReminders(mDateTime->startDateTimeEnabled());
172 dialog->setAllowEndReminders(mDateTime->endDateTimeEnabled());
176 dialog->save(newAlarm);
177 newAlarm->setEnabled(
true);
185void IncidenceAlarm::newAlarmFromPreset()
188 mAlarms.
append(AlarmPresets::preset(AlarmPresets::BeforeEnd, mUi->mAlarmPresetCombo->currentText()));
190 mAlarms.
append(AlarmPresets::preset(AlarmPresets::BeforeStart, mUi->mAlarmPresetCombo->currentText()));
197void IncidenceAlarm::removeCurrentAlarm()
199 Q_ASSERT(mUi->mAlarmList->selectedItems().size() == 1);
200 const int curAlarmIndex = mUi->mAlarmList->currentRow();
201 delete mUi->mAlarmList->takeItem(curAlarmIndex);
202 mAlarms.
remove(curAlarmIndex);
209void IncidenceAlarm::toggleCurrentAlarm()
211 Q_ASSERT(mUi->mAlarmList->selectedItems().size() == 1);
212 const int curAlarmIndex = mUi->mAlarmList->currentRow();
214 alarm->setEnabled(!alarm->enabled());
221void IncidenceAlarm::updateAlarmList()
223 const int prevEnabledAlarmCount = mEnabledAlarmCount;
224 mEnabledAlarmCount = 0;
226 const QModelIndex currentIndex = mUi->mAlarmList->currentIndex();
227 mUi->mAlarmList->clear();
229 mUi->mAlarmList->addItem(stringForAlarm(alarm));
230 if (alarm->enabled()) {
231 ++mEnabledAlarmCount;
235 mUi->mAlarmList->setCurrentIndex(currentIndex);
236 if (prevEnabledAlarmCount != mEnabledAlarmCount) {
237 Q_EMIT alarmCountChanged(mEnabledAlarmCount);
241void IncidenceAlarm::updateButtons()
243 if (mUi->mAlarmList->count() > 0 && !mUi->mAlarmList->selectedItems().isEmpty()) {
244 mUi->mAlarmConfigureButton->setEnabled(
true);
245 mUi->mAlarmRemoveButton->setEnabled(
true);
246 mUi->mAlarmToggleButton->setEnabled(
true);
248 if (mUi->mAlarmList->currentIndex().isValid()) {
249 selAlarm = mAlarms.
at(mUi->mAlarmList->currentIndex().row());
251 if (selAlarm && selAlarm->enabled()) {
252 mUi->mAlarmToggleButton->setText(
i18nc(
"Disable currently selected reminder",
"Disable"));
254 mUi->mAlarmToggleButton->setText(
i18nc(
"Enable currently selected reminder",
"Enable"));
257 mUi->mAlarmConfigureButton->setEnabled(
false);
258 mUi->mAlarmRemoveButton->setEnabled(
false);
259 mUi->mAlarmToggleButton->setEnabled(
false);
268 switch (alarm->type()) {
272 action =
i18nc(
"Alarm action",
"Display a dialog");
275 action =
i18nc(
"Alarm action",
"Play an audio file");
278 action =
i18nc(
"Alarm action",
"Invalid Reminder.");
282 const int offset = alarm->hasStartOffset() ? alarm->startOffset().asSeconds() / 60 : alarm->endOffset().asSeconds() / 60;
284 QString offsetUnitTranslated =
i18ncp(
"The reminder is set to X minutes before/after the event",
"1 minute",
"%1 minutes", qAbs(offset));
286 int useoffset = offset;
287 if (offset % (24 * 60) == 0 && offset != 0) {
288 useoffset = offset / 60 / 24;
289 offsetUnitTranslated =
i18ncp(
"The reminder is set to X days before/after the event",
"1 day",
"%1 days", qAbs(useoffset));
290 }
else if (offset % 60 == 0 && offset != 0) {
291 useoffset = offset / 60;
292 offsetUnitTranslated =
i18ncp(
"The reminder is set to X hours before/after the event",
"1 hour",
"%1 hours", qAbs(useoffset));
296 if (alarm->repeatCount() > 0) {
297 repeatStr =
i18nc(
"The reminder is configured to repeat after snooze",
"(Repeats)");
300 if (alarm->enabled()) {
301 if (useoffset > 0 && alarm->hasStartOffset()) {
307 return i18n(
"%1 %2 after the to-do started %3", action, offsetUnitTranslated, repeatStr);
309 return i18n(
"%1 %2 after the event started %3", action, offsetUnitTranslated, repeatStr);
311 }
else if (useoffset < 0 && alarm->hasStartOffset()) {
313 return i18n(
"%1 %2 before the to-do starts %3", action, offsetUnitTranslated, repeatStr);
315 return i18n(
"%1 %2 before the event starts %3", action, offsetUnitTranslated, repeatStr);
317 }
else if (useoffset > 0 && alarm->hasEndOffset()) {
319 return i18n(
"%1 %2 after the to-do is due %3", action, offsetUnitTranslated, repeatStr);
321 return i18n(
"%1 %2 after the event ends %3", action, offsetUnitTranslated, repeatStr);
323 }
else if (useoffset < 0 && alarm->hasEndOffset()) {
325 return i18n(
"%1 %2 before the to-do is due %3", action, offsetUnitTranslated, repeatStr);
327 return i18n(
"%1 %2 before the event ends %3", action, offsetUnitTranslated, repeatStr);
331 if (useoffset > 0 && alarm->hasStartOffset()) {
333 return i18n(
"%1 %2 after the to-do started %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
335 return i18n(
"%1 %2 after the event started %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
337 }
else if (useoffset < 0 && alarm->hasStartOffset()) {
339 return i18n(
"%1 %2 before the to-do starts %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
341 return i18n(
"%1 %2 before the event starts %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
343 }
else if (useoffset > 0 && alarm->hasEndOffset()) {
345 return i18n(
"%1 %2 after the to-do is due %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
347 return i18n(
"%1 %2 after the event ends %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
349 }
else if (useoffset < 0 && alarm->hasEndOffset()) {
351 return i18n(
"%1 %2 before the to-do is due %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
353 return i18n(
"%1 %2 before the event ends %3 (Disabled)", action, offsetUnitTranslated, repeatStr);
359 if (alarm->enabled()) {
360 if (mIsTodo && alarm->hasStartOffset()) {
361 return i18n(
"%1 when the to-do starts", action);
362 }
else if (alarm->hasStartOffset()) {
363 return i18n(
"%1 when the event starts", action);
364 }
else if (mIsTodo && alarm->hasEndOffset()) {
365 return i18n(
"%1 when the to-do is due", action);
367 return i18n(
"%1 when the event ends", action);
370 if (mIsTodo && alarm->hasStartOffset()) {
371 return i18n(
"%1 when the to-do starts (Disabled)", action);
372 }
else if (alarm->hasStartOffset()) {
373 return i18n(
"%1 when the event starts (Disabled)", action);
374 }
else if (mIsTodo && alarm->hasEndOffset()) {
375 return i18n(
"%1 when the to-do is due (Disabled)", action);
377 return i18n(
"%1 when the event ends (Disabled)", action);
382#include "moc_incidencealarm.cpp"
void checkDirtyStatus()
Checks if the dirty status has changed until last check and emits the dirtyStatusChanged signal if ne...
QSharedPointer< IncidenceT > incidence() const
Convenience method to get a pointer for a specific const Incidence Type.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString i18ncp(const char *context, const char *singular, const char *plural, const TYPE &arg...)
const QList< QKeySequence > & end()
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
const_iterator constBegin() const const
const_iterator constEnd() const const
qsizetype count() const const
void remove(qsizetype i, qsizetype n)
QObject * parent() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)