2 * deferdlg.cpp - dialog to defer an alarm
4 * Copyright © 2002-2012 by David Jarvie <djarvie@kde.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #include "alarmcalendar.h"
25 #include "alarmtimewidget.h"
26 #include "functions.h"
27 #include "kalarmapp.h"
28 #include "messagebox.h"
30 #include <kalarmcal/datetime.h>
31 #include <kalarmcal/kaevent.h>
33 #include <KLocalizedString>
35 #include <QVBoxLayout>
37 #include <QDialogButtonBox>
38 #include <QPushButton>
39 #include "kalarm_debug.h"
42 DeferAlarmDlg::DeferAlarmDlg(const DateTime
& initialDT
, bool anyTimeOption
, bool cancelButton
, QWidget
* parent
)
45 setWindowModality(Qt::WindowModal
);
46 setWindowTitle(i18nc("@title:window", "Defer Alarm"));
48 QVBoxLayout
* layout
= new QVBoxLayout(this);
50 layout
->setSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
52 mTimeWidget
= new AlarmTimeWidget((anyTimeOption
? AlarmTimeWidget::DEFER_ANY_TIME
: AlarmTimeWidget::DEFER_TIME
), this);
53 mTimeWidget
->setDateTime(initialDT
);
54 mTimeWidget
->setMinDateTimeIsCurrent();
55 connect(mTimeWidget
, &AlarmTimeWidget::pastMax
, this, &DeferAlarmDlg::slotPastLimit
);
56 layout
->addWidget(mTimeWidget
);
57 layout
->addSpacing(style()->pixelMetric(QStyle::PM_DefaultLayoutSpacing
));
59 mButtonBox
= new QDialogButtonBox(this);
60 layout
->addWidget(mButtonBox
);
61 QPushButton
*okButton
= mButtonBox
->addButton(QDialogButtonBox::Ok
);
62 okButton
->setWhatsThis(i18nc("@info:whatsthis", "Defer the alarm until the specified time."));
64 mButtonBox
->addButton(QDialogButtonBox::Cancel
);
66 QPushButton
*deferButton
=mButtonBox
->addButton(i18nc("@action:button", "Cancel Deferral"), QDialogButtonBox::ActionRole
);
67 deferButton
->setWhatsThis(i18nc("@info:whatsthis", "Cancel the deferred alarm. This does not affect future recurrences."));
68 connect(mButtonBox
, &QDialogButtonBox::accepted
,
69 this, &DeferAlarmDlg::slotOk
);
70 connect(mButtonBox
, &QDialogButtonBox::rejected
,
71 this, &QDialog::reject
);
72 connect(mButtonBox
, &QDialogButtonBox::clicked
,
73 [this, deferButton
](QAbstractButton
*btn
) {
74 if (btn
== deferButton
) {
82 /******************************************************************************
83 * Called when the OK button is clicked.
85 void DeferAlarmDlg::slotOk()
87 mAlarmDateTime
= mTimeWidget
->getDateTime(&mDeferMinutes
);
88 if (!mAlarmDateTime
.isValid())
90 KAEvent::DeferLimitType limitType
= KAEvent::LIMIT_NONE
;
92 if (!mLimitEventId
.isEmpty())
94 // Get the event being deferred
95 const KAEvent
* event
= AlarmCalendar::getEvent(mLimitEventId
);
97 endTime
= event
->deferralLimit(&limitType
);
101 endTime
= mLimitDateTime
;
102 limitType
= mLimitDateTime
.isValid() ? KAEvent::LIMIT_MAIN
: KAEvent::LIMIT_NONE
;
104 if (endTime
.isValid() && mAlarmDateTime
> endTime
)
109 case KAEvent::LIMIT_REPETITION
:
110 text
= i18nc("@info", "Cannot defer past the alarm's next sub-repetition (currently %1)",
111 endTime
.formatLocale());
113 case KAEvent::LIMIT_RECURRENCE
:
114 text
= i18nc("@info", "Cannot defer past the alarm's next recurrence (currently %1)",
115 endTime
.formatLocale());
117 case KAEvent::LIMIT_REMINDER
:
118 text
= i18nc("@info", "Cannot defer past the alarm's next reminder (currently %1)",
119 endTime
.formatLocale());
121 case KAEvent::LIMIT_MAIN
:
122 text
= i18nc("@info", "Cannot defer reminder past the main alarm time (%1)",
123 endTime
.formatLocale());
125 case KAEvent::LIMIT_NONE
:
126 break; // can't happen with a valid endTime
128 KAMessageBox::sorry(this, text
);
134 /******************************************************************************
135 * Select the 'Time from now' radio button and preset its value.
137 void DeferAlarmDlg::setDeferMinutes(int minutes
)
139 mTimeWidget
->selectTimeFromNow(minutes
);
142 /******************************************************************************
143 * Called the maximum date/time for the date/time edit widget has been passed.
145 void DeferAlarmDlg::slotPastLimit()
147 mButtonBox
->button(QDialogButtonBox::Ok
)->setEnabled(false);
150 /******************************************************************************
151 * Set the time limit for deferral based on the next occurrence of the alarm
152 * with the specified ID.
154 void DeferAlarmDlg::setLimit(const DateTime
& limit
)
156 mLimitEventId
.clear();
157 mLimitDateTime
= limit
;
158 mTimeWidget
->setMaxDateTime(mLimitDateTime
);
161 /******************************************************************************
162 * Set the time limit for deferral based on the next occurrence of the alarm
163 * with the specified ID.
165 DateTime
DeferAlarmDlg::setLimit(const KAEvent
& event
)
167 Q_ASSERT(event
.collectionId() >= 0);
168 mLimitEventId
= EventId(event
);
169 const KAEvent
* evnt
= AlarmCalendar::getEvent(mLimitEventId
);
170 mLimitDateTime
= evnt
? evnt
->deferralLimit() : DateTime();
171 mTimeWidget
->setMaxDateTime(mLimitDateTime
);
172 return mLimitDateTime
;
175 /******************************************************************************
176 * Called when the Cancel Deferral button is clicked.
178 void DeferAlarmDlg::slotCancelDeferral()
180 mAlarmDateTime
= DateTime();