kalarm

specialactions.cpp
1 /*
2  * specialactions.cpp - widget to specify special alarm actions
3  * Program: kalarm
4  * Copyright © 2004,2005,2007 by David Jarvie <software@astrojar.org.uk>
5  *
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.
10  *
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.
15  *
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.
19  */
20 
21 #include "kalarm.h"
22 
23 #include <tqlabel.h>
24 #include <tqlayout.h>
25 #include <tqwhatsthis.h>
26 
27 #include <klineedit.h>
28 #include <tdeapplication.h>
29 #include <tdeaboutdata.h>
30 #include <tdelocale.h>
31 #include <kdebug.h>
32 
33 #include "functions.h"
34 #include "shellprocess.h"
35 #include "specialactions.moc"
36 
37 
38 /*=============================================================================
39 = Class SpecialActionsButton
40 = Button to display the Special Alarm Actions dialogue.
41 =============================================================================*/
42 
43 SpecialActionsButton::SpecialActionsButton(const TQString& caption, TQWidget* parent, const char* name)
44  : TQPushButton(caption, parent, name),
45  mReadOnly(false)
46 {
47  setToggleButton(true);
48  setOn(false);
49  connect(this, TQ_SIGNAL(clicked()), TQ_SLOT(slotButtonPressed()));
50  TQWhatsThis::add(this,
51  i18n("Specify actions to execute before and after the alarm is displayed."));
52 }
53 
54 /******************************************************************************
55 * Set the pre- and post-alarm actions.
56 * The button's pressed state is set to reflect whether any actions are set.
57 */
58 void SpecialActionsButton::setActions(const TQString& pre, const TQString& post)
59 {
60  mPreAction = pre;
61  mPostAction = post;
62  setOn(!mPreAction.isEmpty() || !mPostAction.isEmpty());
63 }
64 
65 /******************************************************************************
66 * Called when the OK button is clicked.
67 * Display a font and colour selection dialog and get the selections.
68 */
69 void SpecialActionsButton::slotButtonPressed()
70 {
71  SpecialActionsDlg dlg(mPreAction, mPostAction,
72  i18n("Special Alarm Actions"), this, "actionsDlg");
73  dlg.setReadOnly(mReadOnly);
74  if (dlg.exec() == TQDialog::Accepted)
75  {
76  mPreAction = dlg.preAction();
77  mPostAction = dlg.postAction();
78  emit selected();
79  }
80  setOn(!mPreAction.isEmpty() || !mPostAction.isEmpty());
81 }
82 
83 
84 /*=============================================================================
85 = Class SpecialActionsDlg
86 = Pre- and post-alarm actions dialogue.
87 =============================================================================*/
88 
89 static const char SPEC_ACT_DIALOG_NAME[] = "SpecialActionsDialog";
90 
91 
92 SpecialActionsDlg::SpecialActionsDlg(const TQString& preAction, const TQString& postAction,
93  const TQString& caption, TQWidget* parent, const char* name)
94  : KDialogBase(parent, name, true, caption, Ok|Cancel, Ok, false)
95 {
96  TQWidget* page = new TQWidget(this);
97  setMainWidget(page);
98  TQVBoxLayout* layout = new TQVBoxLayout(page, 0, spacingHint());
99 
100  mActions = new SpecialActions(page);
101  mActions->setActions(preAction, postAction);
102  layout->addWidget(mActions);
103  layout->addSpacing(KDialog::spacingHint());
104 
105  TQSize s;
106  if (KAlarm::readConfigWindowSize(SPEC_ACT_DIALOG_NAME, s))
107  resize(s);
108 }
109 
110 /******************************************************************************
111 * Called when the OK button is clicked.
112 */
113 void SpecialActionsDlg::slotOk()
114 {
115  if (mActions->isReadOnly())
116  reject();
117  accept();
118 }
119 
120 /******************************************************************************
121 * Called when the dialog's size has changed.
122 * Records the new size in the config file.
123 */
124 void SpecialActionsDlg::resizeEvent(TQResizeEvent* re)
125 {
126  if (isVisible())
127  KAlarm::writeConfigWindowSize(SPEC_ACT_DIALOG_NAME, re->size());
128  KDialog::resizeEvent(re);
129 }
130 
131 
132 /*=============================================================================
133 = Class SpecialActions
134 = Pre- and post-alarm actions widget.
135 =============================================================================*/
136 
137 SpecialActions::SpecialActions(TQWidget* parent, const char* name)
138  : TQWidget(parent, name),
139  mReadOnly(false)
140 {
141  TQBoxLayout* topLayout = new TQVBoxLayout(this, 0, KDialog::spacingHint());
142 
143  // Pre-alarm action
144  TQLabel* label = new TQLabel(i18n("Pre-a&larm action:"), this);
145  label->setFixedSize(label->sizeHint());
146  topLayout->addWidget(label, 0, TQt::AlignAuto);
147 
148  mPreAction = new KLineEdit(this);
149  label->setBuddy(mPreAction);
150  TQWhatsThis::add(mPreAction,
151  i18n("Enter a shell command to execute before the alarm is displayed.\n"
152  "Note that it is executed only when the alarm proper is displayed, not when a reminder or deferred alarm is displayed.\n"
153  "N.B. KAlarm will wait for the command to complete before displaying the alarm."));
154  topLayout->addWidget(mPreAction);
155  topLayout->addSpacing(KDialog::spacingHint());
156 
157  // Post-alarm action
158  label = new TQLabel(i18n("Post-alar&m action:"), this);
159  label->setFixedSize(label->sizeHint());
160  topLayout->addWidget(label, 0, TQt::AlignAuto);
161 
162  mPostAction = new KLineEdit(this);
163  label->setBuddy(mPostAction);
164  TQWhatsThis::add(mPostAction,
165  i18n("Enter a shell command to execute after the alarm window is closed.\n"
166  "Note that it is not executed after closing a reminder window. If you defer "
167  "the alarm, it is not executed until the alarm is finally acknowledged or closed."));
168  topLayout->addWidget(mPostAction);
169 }
170 
171 void SpecialActions::setActions(const TQString& pre, const TQString& post)
172 {
173  mPreAction->setText(pre);
174  mPostAction->setText(post);
175 }
176 
177 TQString SpecialActions::preAction() const
178 {
179  return mPreAction->text();
180 }
181 
182 TQString SpecialActions::postAction() const
183 {
184  return mPostAction->text();
185 }
186 
187 void SpecialActions::setReadOnly(bool ro)
188 {
189  mReadOnly = ro;
190  mPreAction->setReadOnly(mReadOnly);
191  mPostAction->setReadOnly(mReadOnly);
192 }
miscellaneous functions