2 * specialactions.cpp - widget to specify special alarm actions
4 * Copyright © 2004-2009,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 "specialactions.moc"
24 #include "autoqpointer.h"
26 #include "functions.h"
27 #include "shellprocess.h"
29 #include <klineedit.h>
31 #include <kapplication.h>
32 #include <kaboutdata.h>
38 #include <QVBoxLayout>
39 #include <QResizeEvent>
42 /*=============================================================================
43 = Class SpecialActionsButton
44 = Button to display the Special Alarm Actions dialog.
45 =============================================================================*/
47 SpecialActionsButton::SpecialActionsButton(bool enableCheckboxes
, QWidget
* parent
)
48 : QPushButton(i18nc("@action:button", "Special Actions..."), parent
),
50 mEnableCheckboxes(enableCheckboxes
),
55 connect(this, SIGNAL(clicked()), SLOT(slotButtonPressed()));
56 setWhatsThis(i18nc("@info:whatsthis", "Specify actions to execute before and after the alarm is displayed."));
59 /******************************************************************************
60 * Set the pre- and post-alarm actions.
61 * The button's pressed state is set to reflect whether any actions are set.
63 void SpecialActionsButton::setActions(const QString
& pre
, const QString
& post
, KAEvent::ExtraActionOptions options
)
68 setChecked(!mPreAction
.isEmpty() || !mPostAction
.isEmpty());
71 /******************************************************************************
72 * Called when the OK button is clicked.
73 * Display a font and colour selection dialog and get the selections.
75 void SpecialActionsButton::slotButtonPressed()
77 // Use AutoQPointer to guard against crash on application exit while
78 // the dialogue is still open. It prevents double deletion (both on
79 // deletion of SpecialActionsButton, and on return from this function).
80 AutoQPointer
<SpecialActionsDlg
> dlg
= new SpecialActionsDlg(mPreAction
, mPostAction
, mOptions
, mEnableCheckboxes
, this);
81 dlg
->setReadOnly(mReadOnly
);
82 if (dlg
->exec() == QDialog::Accepted
)
84 mPreAction
= dlg
->preAction();
85 mPostAction
= dlg
->postAction();
86 mOptions
= dlg
->options();
90 setChecked(!mPreAction
.isEmpty() || !mPostAction
.isEmpty());
94 /*=============================================================================
95 = Class SpecialActionsDlg
96 = Pre- and post-alarm actions dialog.
97 =============================================================================*/
99 static const char SPEC_ACT_DIALOG_NAME
[] = "SpecialActionsDialog";
102 SpecialActionsDlg::SpecialActionsDlg(const QString
& preAction
, const QString
& postAction
,
103 KAEvent::ExtraActionOptions options
, bool enableCheckboxes
,
107 setCaption(i18nc("@title:window", "Special Alarm Actions"));
108 setButtons(Ok
|Cancel
);
109 setDefaultButton(Ok
);
110 connect(this, SIGNAL(okClicked()), SLOT(slotOk()));
112 QWidget
* page
= new QWidget(this);
114 QVBoxLayout
* layout
= new QVBoxLayout(page
);
115 layout
->setMargin(0);
116 layout
->setSpacing(spacingHint());
118 mActions
= new SpecialActions(enableCheckboxes
, page
);
119 mActions
->setActions(preAction
, postAction
, options
);
120 layout
->addWidget(mActions
);
121 layout
->addSpacing(spacingHint());
124 if (KAlarm::readConfigWindowSize(SPEC_ACT_DIALOG_NAME
, s
))
128 /******************************************************************************
129 * Called when the OK button is clicked.
131 void SpecialActionsDlg::slotOk()
133 if (mActions
->isReadOnly())
138 /******************************************************************************
139 * Called when the dialog's size has changed.
140 * Records the new size in the config file.
142 void SpecialActionsDlg::resizeEvent(QResizeEvent
* re
)
145 KAlarm::writeConfigWindowSize(SPEC_ACT_DIALOG_NAME
, re
->size());
146 KDialog::resizeEvent(re
);
150 /*=============================================================================
151 = Class SpecialActions
152 = Pre- and post-alarm actions widget.
153 =============================================================================*/
155 SpecialActions::SpecialActions(bool enableCheckboxes
, QWidget
* parent
)
157 mEnableCheckboxes(enableCheckboxes
),
160 QVBoxLayout
* topLayout
= new QVBoxLayout(this);
161 topLayout
->setMargin(0);
162 topLayout
->setSpacing(KDialog::spacingHint());
165 QGroupBox
* group
= new QGroupBox(i18nc("@title:group", "Pre-Alarm Action"), this);
166 topLayout
->addWidget(group
);
167 QVBoxLayout
* vlayout
= new QVBoxLayout(group
);
168 vlayout
->setMargin(KDialog::marginHint());
169 vlayout
->setSpacing(KDialog::spacingHint());
171 KHBox
* box
= new KHBox(group
); // this is to control the QWhatsThis text display area
173 box
->setSpacing(KDialog::spacingHint());
174 vlayout
->addWidget(box
);
175 QLabel
* label
= new QLabel(i18nc("@label:textbox", "Command:"), box
);
176 mPreAction
= new KLineEdit(box
);
177 label
->setBuddy(mPreAction
);
178 connect(mPreAction
, SIGNAL(textChanged(QString
)), SLOT(slotPreActionChanged(QString
)));
179 box
->setWhatsThis(i18nc("@info:whatsthis",
180 "<para>Enter a shell command to execute before the alarm is displayed.</para>"
181 "<para>Note that it is executed only when the alarm proper is displayed, not when a reminder or deferred alarm is displayed.</para>"
182 "<para><note>KAlarm will wait for the command to complete before displaying the alarm.</note></para>"));
183 box
->setStretchFactor(mPreAction
, 1);
185 // Cancel if error in pre-alarm action
186 mExecOnDeferral
= new CheckBox(i18nc("@option:check", "Execute for deferred alarms"), group
);
187 mExecOnDeferral
->setWhatsThis(i18nc("@info:whatsthis", "<para>If unchecked, the command is only executed before the alarm proper is displayed.</para>"
188 "<para>If checked, the pre-alarm command is also executed before a deferred alarm is displayed.</para>"));
189 vlayout
->addWidget(mExecOnDeferral
, 0, Qt::AlignLeft
);
191 mCancelOnError
= new CheckBox(i18nc("@option:check", "Cancel alarm on error"), group
);
192 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."));
193 vlayout
->addWidget(mCancelOnError
, 0, Qt::AlignLeft
);
195 mDontShowError
= new CheckBox(i18nc("@option:check", "Do not notify errors"), group
);
196 mDontShowError
->setWhatsThis(i18nc("@info:whatsthis", "Do not show error status or error message if the pre-alarm command fails."));
197 vlayout
->addWidget(mDontShowError
, 0, Qt::AlignLeft
);
200 group
= new QGroupBox(i18nc("@title:group", "Post-Alarm Action"), this);
201 topLayout
->addWidget(group
);
202 vlayout
= new QVBoxLayout(group
);
203 vlayout
->setMargin(KDialog::marginHint());
204 vlayout
->setSpacing(KDialog::spacingHint());
206 box
= new KHBox(group
); // this is to control the QWhatsThis text display area
208 box
->setSpacing(KDialog::spacingHint());
209 vlayout
->addWidget(box
);
210 label
= new QLabel(i18nc("@label:textbox", "Command:"), box
);
211 mPostAction
= new KLineEdit(box
);
212 label
->setBuddy(mPostAction
);
213 box
->setWhatsThis(i18nc("@info:whatsthis",
214 "<para>Enter a shell command to execute after the alarm window is closed.</para>"
215 "<para>Note that it is not executed after closing a reminder window. If you defer "
216 "the alarm, it is not executed until the alarm is finally acknowledged or closed.</para>"));
217 box
->setStretchFactor(mPostAction
, 1);
219 mExecOnDeferral
->setEnabled(enableCheckboxes
);
220 mCancelOnError
->setEnabled(enableCheckboxes
);
221 mDontShowError
->setEnabled(enableCheckboxes
);
224 void SpecialActions::setActions(const QString
& pre
, const QString
& post
, KAEvent::ExtraActionOptions options
)
226 mPreAction
->setText(pre
);
227 mPostAction
->setText(post
);
228 mExecOnDeferral
->setChecked(options
& KAEvent::ExecPreActOnDeferral
);
229 mCancelOnError
->setChecked(options
& KAEvent::CancelOnPreActError
);
230 mDontShowError
->setChecked(options
& KAEvent::DontShowPreActError
);
233 QString
SpecialActions::preAction() const
235 return mPreAction
->text();
238 QString
SpecialActions::postAction() const
240 return mPostAction
->text();
243 KAEvent::ExtraActionOptions
SpecialActions::options() const
245 KAEvent::ExtraActionOptions opts
= 0;
246 if (mExecOnDeferral
->isChecked()) opts
|= KAEvent::ExecPreActOnDeferral
;
247 if (mCancelOnError
->isChecked()) opts
|= KAEvent::CancelOnPreActError
;
248 if (mDontShowError
->isChecked()) opts
|= KAEvent::DontShowPreActError
;
252 void SpecialActions::setReadOnly(bool ro
)
255 mPreAction
->setReadOnly(mReadOnly
);
256 mPostAction
->setReadOnly(mReadOnly
);
257 mExecOnDeferral
->setReadOnly(mReadOnly
);
258 mCancelOnError
->setReadOnly(mReadOnly
);
259 mDontShowError
->setReadOnly(mReadOnly
);
262 void SpecialActions::slotPreActionChanged(const QString
& text
)
264 if (!mEnableCheckboxes
)
266 bool textValid
= !text
.isEmpty();
267 mExecOnDeferral
->setEnabled(textValid
);
268 mCancelOnError
->setEnabled(textValid
);
269 mDontShowError
->setEnabled(textValid
);