summaryrefslogtreecommitdiffstats
path: root/kalarm/deferdlg.cpp
blob: 5e0e039c464c59c83886e36980074dd70c756ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
 *  deferdlg.cpp  -  dialogue to defer an alarm
 *  Program:  kalarm
 *  Copyright © 2002-2006,2008 by David Jarvie <djarvie@kde.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "kalarm.h"

#include <qlayout.h>

#include <kglobal.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kdebug.h>

#include <libkcal/event.h>
#include <libkcal/recurrence.h>

#include "alarmcalendar.h"
#include "alarmevent.h"
#include "alarmtimewidget.h"
#include "datetime.h"
#include "functions.h"
#include "kalarmapp.h"
#include "deferdlg.moc"


DeferAlarmDlg::DeferAlarmDlg(const QString& caption, const DateTime& initialDT,
                             bool cancelButton, QWidget* parent, const char* name)
	: KDialogBase(parent, name, true, caption, Ok|Cancel|User1, Ok, false, i18n("Cancel &Deferral"))
{
	if (!cancelButton)
		showButton(User1, false);

	QWidget* page = new QWidget(this);
	setMainWidget(page);
	QVBoxLayout* layout = new QVBoxLayout(page, 0, spacingHint());

	mTimeWidget = new AlarmTimeWidget(AlarmTimeWidget::DEFER_TIME, page, "timeGroup");
	mTimeWidget->setDateTime(initialDT);
	mTimeWidget->setMinDateTimeIsCurrent();
	connect(mTimeWidget, SIGNAL(pastMax()), SLOT(slotPastLimit()));
	layout->addWidget(mTimeWidget);
	layout->addSpacing(spacingHint());

	setButtonWhatsThis(Ok, i18n("Defer the alarm until the specified time."));
	setButtonWhatsThis(User1, i18n("Cancel the deferred alarm. This does not affect future recurrences."));
}


/******************************************************************************
* Called when the OK button is clicked.
*/
void DeferAlarmDlg::slotOk()
{
	mAlarmDateTime = mTimeWidget->getDateTime(&mDeferMinutes);
	if (!mAlarmDateTime.isValid())
		return;
	KAEvent::DeferLimitType limitType;
	DateTime endTime;
	if (!mLimitEventID.isEmpty())
	{
		// Get the event being deferred
		const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mLimitEventID);
		if (kcalEvent)
		{
			KAEvent event(*kcalEvent);
			endTime = event.deferralLimit(&limitType);
		}
	}
	else
	{
		endTime = mLimitDateTime;
		limitType = mLimitDateTime.isValid() ? KAEvent::LIMIT_MAIN : KAEvent::LIMIT_NONE;
	}
	if (endTime.isValid()  &&  mAlarmDateTime > endTime)
	{
		QString text;
		switch (limitType)
		{
			case KAEvent::LIMIT_REPETITION:
				text = i18n("Cannot defer past the alarm's next sub-repetition (currently %1)");
				break;
			case KAEvent::LIMIT_RECURRENCE:
				text = i18n("Cannot defer past the alarm's next recurrence (currently %1)");
				break;
			case KAEvent::LIMIT_REMINDER:
				text = i18n("Cannot defer past the alarm's next reminder (currently %1)");
				break;
			case KAEvent::LIMIT_MAIN:
				text = i18n("Cannot defer reminder past the main alarm time (%1)");
				break;
			case KAEvent::LIMIT_NONE:
				break;   // can't happen with a valid endTime
		}
		KMessageBox::sorry(this, text.arg(endTime.formatLocale()));
	}
	else
		accept();
}

/******************************************************************************
* Select the 'Time from now' radio button and preset its value.
*/
void DeferAlarmDlg::setDeferMinutes(int minutes)
{
	mTimeWidget->selectTimeFromNow(minutes);
}

/******************************************************************************
* Called the maximum date/time for the date/time edit widget has been passed.
*/
void DeferAlarmDlg::slotPastLimit()
{
	enableButtonOK(false);
}

/******************************************************************************
* Set the time limit for deferral based on the next occurrence of the alarm
* with the specified ID.
*/
void DeferAlarmDlg::setLimit(const DateTime& limit)
{
	mLimitEventID  = QString::null;
	mLimitDateTime = limit;
	mTimeWidget->setMaxDateTime(mLimitDateTime);
}

/******************************************************************************
* Set the time limit for deferral based on the next occurrence of the alarm
* with the specified ID.
*/
DateTime DeferAlarmDlg::setLimit(const QString& eventID)
{
	mLimitEventID = eventID;
	const KCal::Event* kcalEvent = AlarmCalendar::getEvent(mLimitEventID);
	if (kcalEvent)
	{
		KAEvent event(*kcalEvent);
		mLimitDateTime = event.deferralLimit();
	}
	else
		mLimitDateTime = DateTime();
	mTimeWidget->setMaxDateTime(mLimitDateTime);
	return mLimitDateTime;
}

/******************************************************************************
* Called when the Cancel Deferral button is clicked.
*/
void DeferAlarmDlg::slotUser1()
{
	mAlarmDateTime = DateTime();
	accept();
}

/******************************************************************************
* Called when the Cancel button is clicked.
*/
void DeferAlarmDlg::slotCancel()
{
	reject();
}