~ubuntu-branches/ubuntu/karmic/kdepim/karmic-backports

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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
 *  specialactions.cpp  -  widget to specify special alarm actions
 *  Program:  kalarm
 *  Copyright © 2004-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 <QLabel>
#include <QGroupBox>
#include <QVBoxLayout>
#include <QResizeEvent>

#include <klineedit.h>
#include <khbox.h>
#include <kapplication.h>
#include <kaboutdata.h>
#include <klocale.h>
#include <kdebug.h>

#include "checkbox.h"
#include "functions.h"
#include "shellprocess.h"
#include "specialactions.moc"


/*=============================================================================
= Class SpecialActionsButton
= Button to display the Special Alarm Actions dialog.
=============================================================================*/

SpecialActionsButton::SpecialActionsButton(bool enableCancelOnError, QWidget* parent)
	: QPushButton(i18nc("@action:button", "Special Actions..."), parent),
	  mEnableCancel(enableCancelOnError),
	  mReadOnly(false)
{
	setCheckable(true);
	setChecked(false);
	connect(this, SIGNAL(clicked()), SLOT(slotButtonPressed()));
	setWhatsThis(i18nc("@info:whatsthis", "Specify actions to execute before and after the alarm is displayed."));
}

/******************************************************************************
*  Set the pre- and post-alarm actions.
*  The button's pressed state is set to reflect whether any actions are set.
*/
void SpecialActionsButton::setActions(const QString& pre, const QString& post, bool cancelOnError)
{
	mPreAction     = pre;
	mPostAction    = post;
	mCancelOnError = cancelOnError;
	setChecked(!mPreAction.isEmpty() || !mPostAction.isEmpty());
}

/******************************************************************************
*  Called when the OK button is clicked.
*  Display a font and colour selection dialog and get the selections.
*/
void SpecialActionsButton::slotButtonPressed()
{
	SpecialActionsDlg dlg(mPreAction, mPostAction, mCancelOnError, mEnableCancel, this);
	dlg.setReadOnly(mReadOnly);
	if (dlg.exec() == QDialog::Accepted)
	{
		mPreAction     = dlg.preAction();
		mPostAction    = dlg.postAction();
		mCancelOnError = dlg.cancelOnError();
		emit selected();
	}
	setChecked(!mPreAction.isEmpty() || !mPostAction.isEmpty());
}


/*=============================================================================
= Class SpecialActionsDlg
= Pre- and post-alarm actions dialog.
=============================================================================*/

static const char SPEC_ACT_DIALOG_NAME[] = "SpecialActionsDialog";


SpecialActionsDlg::SpecialActionsDlg(const QString& preAction, const QString& postAction,
                                     bool cancelOnError, bool enableCancelOnError, QWidget* parent)
	: KDialog(parent)
{
#ifdef __GNUC__
#warning Dialogue appears below edit dialogue when Edit button in alarm message window is clicked
#endif
	setCaption(i18nc("@title:window", "Special Alarm Actions"));
	setButtons(Ok|Cancel);
	setDefaultButton(Ok);
	connect(this, SIGNAL(okClicked()), SLOT(slotOk()));

	QWidget* page = new QWidget(this);
	setMainWidget(page);
	QVBoxLayout* layout = new QVBoxLayout(page);
	layout->setMargin(0);
	layout->setSpacing(spacingHint());

	mActions = new SpecialActions(enableCancelOnError, page);
	mActions->setActions(preAction, postAction, cancelOnError);
	layout->addWidget(mActions);
	layout->addSpacing(spacingHint());

	QSize s;
	if (KAlarm::readConfigWindowSize(SPEC_ACT_DIALOG_NAME, s))
		resize(s);
}

/******************************************************************************
*  Called when the OK button is clicked.
*/
void SpecialActionsDlg::slotOk()
{
	if (mActions->isReadOnly())
		reject();
	accept();
}

/******************************************************************************
*  Called when the dialog's size has changed.
*  Records the new size in the config file.
*/
void SpecialActionsDlg::resizeEvent(QResizeEvent* re)
{
	if (isVisible())
		KAlarm::writeConfigWindowSize(SPEC_ACT_DIALOG_NAME, re->size());
	KDialog::resizeEvent(re);
}


/*=============================================================================
= Class SpecialActions
= Pre- and post-alarm actions widget.
=============================================================================*/

SpecialActions::SpecialActions(bool enableCancelOnError, QWidget* parent)
	: QWidget(parent),
	  mEnableCancel(enableCancelOnError),
	  mReadOnly(false)
{
	QVBoxLayout* topLayout = new QVBoxLayout(this);
	topLayout->setMargin(0);
	topLayout->setSpacing(KDialog::spacingHint());

	// Pre-alarm action
	QGroupBox* group = new QGroupBox(i18nc("@title:group", "Pre-Alarm Action"), this);
	topLayout->addWidget(group);
	QVBoxLayout* vlayout = new QVBoxLayout(group);
	vlayout->setMargin(KDialog::marginHint());
	vlayout->setSpacing(KDialog::spacingHint());

	KHBox* box = new KHBox(group);   // this is to control the QWhatsThis text display area
	box->setMargin(0);
	box->setSpacing(KDialog::spacingHint());
	vlayout->addWidget(box);
	QLabel* label = new QLabel(i18nc("@label:textbox", "Command:"), box);
	mPreAction = new KLineEdit(box);
	label->setBuddy(mPreAction);
	connect(mPreAction, SIGNAL(textChanged(const QString&)), SLOT(slotPreActionChanged(const QString&)));
	box->setWhatsThis(i18nc("@info:whatsthis",
	                        "<para>Enter a shell command to execute before the alarm is displayed.</para>"
	                        "<para>Note that it is executed only when the alarm proper is displayed, not when a reminder or deferred alarm is displayed.</para>"
	                        "<para><note>KAlarm will wait for the command to complete before displaying the alarm.</note></para>"));
	box->setStretchFactor(mPreAction, 1);

	// Cancel if error in pre-alarm action
	mCancelOnError = new CheckBox(i18nc("@option:check", "Cancel alarm on error"), group);
	mCancelOnError->setWhatsThis(i18nc("@info:whatsthis", "Cancel the alarm if the pre-alarm command fails, i.e. do not display the alarm or execute any post-alarm action command."));
	vlayout->addWidget(mCancelOnError, 0, Qt::AlignLeft);

	// Post-alarm action
	group = new QGroupBox(i18nc("@title:group", "Post-Alarm Action"), this);
	topLayout->addWidget(group);
	vlayout = new QVBoxLayout(group);
	vlayout->setMargin(KDialog::marginHint());
	vlayout->setSpacing(KDialog::spacingHint());

	box = new KHBox(group);   // this is to control the QWhatsThis text display area
	box->setMargin(0);
	box->setSpacing(KDialog::spacingHint());
	vlayout->addWidget(box);
	label = new QLabel(i18nc("@label:textbox", "Command:"), box);
	mPostAction = new KLineEdit(box);
	label->setBuddy(mPostAction);
	box->setWhatsThis(i18nc("@info:whatsthis",
	                        "<para>Enter a shell command to execute after the alarm window is closed.</para>"
	                        "<para>Note that it is not executed after closing a reminder window. If you defer "
	                        "the alarm, it is not executed until the alarm is finally acknowledged or closed.</para>"));
	box->setStretchFactor(mPostAction, 1);

	mCancelOnError->setEnabled(enableCancelOnError);
}

void SpecialActions::setActions(const QString& pre, const QString& post, bool cancelOnError)
{
	mPreAction->setText(pre);
	mPostAction->setText(post);
	mCancelOnError->setChecked(cancelOnError);
}

QString SpecialActions::preAction() const
{
	return mPreAction->text();
}

QString SpecialActions::postAction() const
{
	return mPostAction->text();
}

bool SpecialActions::cancelOnError() const
{
	return mCancelOnError->isChecked();
}

void SpecialActions::setReadOnly(bool ro)
{
	mReadOnly = ro;
	mPreAction->setReadOnly(mReadOnly);
	mPostAction->setReadOnly(mReadOnly);
	mCancelOnError->setReadOnly(mReadOnly);
}

void SpecialActions::slotPreActionChanged(const QString& text)
{
	if (!mEnableCancel)
		mCancelOnError->setEnabled(!text.isEmpty());
}