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.
22 #include "deferdlg.moc"
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>
37 #include <QVBoxLayout>
40 DeferAlarmDlg::DeferAlarmDlg(const DateTime
& initialDT
, bool anyTimeOption
, bool cancelButton
, QWidget
* parent
)
43 setWindowModality(Qt::WindowModal
);
44 setCaption(i18nc("@title:window", "Defer Alarm"));
45 setButtons(Ok
| Cancel
| User1
);
46 setButtonGuiItem(User1
, KGuiItem(i18nc("@action:button", "Cancel Deferral")));
48 showButton(User1
, false);
49 connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
50 connect(this, SIGNAL(user1Clicked()), SLOT(slotCancelDeferral()));
52 QWidget
* page
= new QWidget(this);
54 QVBoxLayout
* layout
= new QVBoxLayout(page
);
56 layout
->setSpacing(spacingHint());
58 mTimeWidget
= new AlarmTimeWidget((anyTimeOption
? AlarmTimeWidget::DEFER_ANY_TIME
: AlarmTimeWidget::DEFER_TIME
), page
);
59 mTimeWidget
->setDateTime(initialDT
);
60 mTimeWidget
->setMinDateTimeIsCurrent();
61 connect(mTimeWidget
, SIGNAL(pastMax()), SLOT(slotPastLimit()));
62 layout
->addWidget(mTimeWidget
);
63 layout
->addSpacing(spacingHint());
65 setButtonWhatsThis(Ok
, i18nc("@info:whatsthis", "Defer the alarm until the specified time."));
66 setButtonWhatsThis(User1
, i18nc("@info:whatsthis", "Cancel the deferred alarm. This does not affect future recurrences."));
70 /******************************************************************************
71 * Called when the OK button is clicked.
73 void DeferAlarmDlg::slotOk()
75 mAlarmDateTime
= mTimeWidget
->getDateTime(&mDeferMinutes
);
76 if (!mAlarmDateTime
.isValid())
78 KAEvent::DeferLimitType limitType
= KAEvent::LIMIT_NONE
;
80 if (!mLimitEventId
.isEmpty())
82 // Get the event being deferred
83 const KAEvent
* event
= AlarmCalendar::getEvent(mLimitEventId
);
85 endTime
= event
->deferralLimit(&limitType
);
89 endTime
= mLimitDateTime
;
90 limitType
= mLimitDateTime
.isValid() ? KAEvent::LIMIT_MAIN
: KAEvent::LIMIT_NONE
;
92 if (endTime
.isValid() && mAlarmDateTime
> endTime
)
97 case KAEvent::LIMIT_REPETITION
:
98 text
= i18nc("@info", "Cannot defer past the alarm's next sub-repetition (currently %1)",
99 endTime
.formatLocale());
101 case KAEvent::LIMIT_RECURRENCE
:
102 text
= i18nc("@info", "Cannot defer past the alarm's next recurrence (currently %1)",
103 endTime
.formatLocale());
105 case KAEvent::LIMIT_REMINDER
:
106 text
= i18nc("@info", "Cannot defer past the alarm's next reminder (currently %1)",
107 endTime
.formatLocale());
109 case KAEvent::LIMIT_MAIN
:
110 text
= i18nc("@info", "Cannot defer reminder past the main alarm time (%1)",
111 endTime
.formatLocale());
113 case KAEvent::LIMIT_NONE
:
114 break; // can't happen with a valid endTime
116 KAMessageBox::sorry(this, text
);
122 /******************************************************************************
123 * Select the 'Time from now' radio button and preset its value.
125 void DeferAlarmDlg::setDeferMinutes(int minutes
)
127 mTimeWidget
->selectTimeFromNow(minutes
);
130 /******************************************************************************
131 * Called the maximum date/time for the date/time edit widget has been passed.
133 void DeferAlarmDlg::slotPastLimit()
135 enableButtonOk(false);
138 /******************************************************************************
139 * Set the time limit for deferral based on the next occurrence of the alarm
140 * with the specified ID.
142 void DeferAlarmDlg::setLimit(const DateTime
& limit
)
144 mLimitEventId
.clear();
145 mLimitDateTime
= limit
;
146 mTimeWidget
->setMaxDateTime(mLimitDateTime
);
149 /******************************************************************************
150 * Set the time limit for deferral based on the next occurrence of the alarm
151 * with the specified ID.
153 DateTime
DeferAlarmDlg::setLimit(const KAEvent
& event
)
156 Q_ASSERT(event
.collectionId() >= 0);
157 mLimitEventId
= EventId(event
);
159 mLimitEventId
= event
.id();
161 const KAEvent
* evnt
= AlarmCalendar::getEvent(mLimitEventId
);
162 mLimitDateTime
= evnt
? evnt
->deferralLimit() : DateTime();
163 mTimeWidget
->setMaxDateTime(mLimitDateTime
);
164 return mLimitDateTime
;
167 /******************************************************************************
168 * Called when the Cancel Deferral button is clicked.
170 void DeferAlarmDlg::slotCancelDeferral()
172 mAlarmDateTime
= DateTime();