summaryrefslogtreecommitdiffstats
path: root/kshutdown/mactioneditdialog.cpp
blob: f826dcef3a8bdf4dea99608cfcbcf6e0c17c2fb6 (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
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
/*
	mactioneditsdialog.cpp - An action editor
	Copyright (C) 2003  Konrad Twardowski <kdtonline@poczta.onet.pl>

	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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "configuration.h"
#include "mactioneditdialog.h"
#include "miscutils.h"
#include "mtip.h"

#include <ntqcheckbox.h>
#include <ntqcombobox.h>
#include <ntqhbox.h>
#include <ntqlabel.h>
#include <ntqlayout.h>
#include <ntqvbox.h>
#include <ntqvgroupbox.h>
#include <ntqwhatsthis.h>

#include <klineedit.h>
#include <tdelocale.h>
#include <knuminput.h>
#include <kpushbutton.h>

MActionEditDialog::MActionEditDialog(TQWidget *parent, Action::Type action)
	: KDialogBase(
		parent,
		"MActionEditDialog",
		true,
		"",
		Default | Ok | Cancel,
		Ok, // default button
		true
	),
	_action(action),
	_userCommand("")
{
	TQVBox *main = new TQVBox(this);
	setMainWidget(main);

	// title
	l_title = new TQLabel(main);
	TQFont f = TQFont(l_title->font());
	f.setPointSize(16);
	l_title->setFont(f);
	l_title->setMargin(10);

	TQHBox *gb_south = new TQHBox(main);
	gb_south->setSpacing(5);

	TQVBox *gb_widgets = new TQVBox(gb_south);

	TQVGroupBox *gb_method = new TQVGroupBox(i18n("Method"), gb_widgets);

	// method label
	TQLabel *l_method = new TQLabel(i18n("Select a method:"), gb_method);

	// method combo box
	_methods = new TQComboBox(gb_method, "TQComboBox::_methods");
	_methods->setFocusPolicy(StrongFocus);
	_methods->insertItem(i18n("TDE (default)"));
	l_method->setBuddy(_methods);
	connect(_methods, SIGNAL(activated(int)), SLOT(slotMethodChange(int)));

	// command label
	l_command = new TQLabel(i18n("Enter a custom command:"), gb_method);

	// command edit line
	in_command = new KLineEdit(gb_method, "KLineEdit::in_command");
	l_command->setBuddy(in_command);

	TQVGroupBox *gb_commandBeforeAction = new TQVGroupBox(i18n("Command before action"), gb_widgets);
	c_runCommandBeforeAction = new TQCheckBox(i18n("Run command"), gb_commandBeforeAction);
	i_commandBeforeAction = new KLineEdit(gb_commandBeforeAction, "KLineEdit::i_commandBeforeAction");
	i_commandBeforeActionPause = new KIntNumInput(gb_commandBeforeAction, "KIntNumInput::i_commandBeforeActionPause");
	i_commandBeforeActionPause->setLabel(i18n("Pause after run command:"));
	i_commandBeforeActionPause->setRange(0, 300, 1, true);
	i_commandBeforeActionPause->setSpecialValueText(i18n("No pause"));
	i_commandBeforeActionPause->setSuffix(" " + i18n("second(s)"));
	b_testCommandBeforeAction = new KPushButton(KStdGuiItem::test(), gb_commandBeforeAction, "KPushButton::b_testCommandBeforeAction");
	connect(b_testCommandBeforeAction, SIGNAL(clicked()), SLOT(slotTestCommandBeforeAction()));
	connect(
		c_runCommandBeforeAction, SIGNAL(toggled(bool)),
		i_commandBeforeAction, SLOT(setEnabled(bool)));
	connect(
		c_runCommandBeforeAction, SIGNAL(toggled(bool)),
		i_commandBeforeActionPause, SLOT(setEnabled(bool)));
	connect(
		c_runCommandBeforeAction, SIGNAL(toggled(bool)),
		b_testCommandBeforeAction, SLOT(setEnabled(bool)));

	MTip *t_progs = new MTip(MTip::Warning, gb_south);
	t_progs->setTipText(
		MiscUtils::HTML(
			i18n("In most cases you need privileges to shut down system (e.g. run /sbin/shutdown)") + "<br>" \
			"<ul>" \
				"<li>" + i18n("If you are using <b>TDE</b> and <b>TDM</b> (TDE Display Manager), then set all methods to <i>TDE</i>") + "</li>" \
				"<li>" + i18n("If you are using <b>TDE</b> and display manager different than <b>TDM</b>, then set <i>Turn Off Computer</i> and <i>Restart Computer</i> methods to <i>/sbin/...</i>") + "</li>" \
			"</ul>" +
			i18n("Manuals:") +
			"<ul>" \
				"<li><a href=\"man:/halt\">halt, poweroff, reboot</a></li>" \
				"<li><a href=\"man:/shutdown\">shutdown</a></li>" \
				"<li><a href=\"man:/xlock\">xlock</a></li>" \
				"<li><a href=\"man:/xscreensaver-command\">xscreensaver-command</a></li>" \
			"</ul>" \
		)
	);

	connect(this, SIGNAL(okClicked()), SLOT(slotOKClicked()));

	Action::Method m = Action::Method_TDE;
	TQString c;
	TQString group = ks_actions->actionToConfigGroup(_action);
	_methods->insertItem(ks_actions->getMethod(_action, m, c));

	TQString s = ks_actions->getName(_action);
	l_title->setText(s);
	setCaption(s);

	_methods->insertItem(i18n("User Command"));

	// method
	_userCommand = c;
	setMethod(m);

	// command before action
	TDEConfig *conf = kshutdownrc->config();
	conf->setGroup(group);
	c_runCommandBeforeAction->setChecked(conf->readBoolEntry("RunCommandBeforeAction", false));
	i_commandBeforeAction->setText(conf->readEntry("CommandBeforeAction", ""));
	i_commandBeforeActionPause->setValue(conf->readNumEntry("CommandBeforeActionPause", 10));

	bool runCommand = c_runCommandBeforeAction->isChecked();
	i_commandBeforeAction->setEnabled(runCommand);
	i_commandBeforeActionPause->setEnabled(runCommand);
	b_testCommandBeforeAction->setEnabled(runCommand);
	_methods->setFocus();

	adjustSize();
	disableResize();
}

MActionEditDialog::~MActionEditDialog()
{
}

void MActionEditDialog::setMethod(const Action::Method method) const
{
	_methods->setCurrentItem(method);
	l_command->setEnabled(method == Action::Method_UserCommand);
	in_command->setEnabled(method == Action::Method_UserCommand);
	switch (method)
	{
		case Action::Method_TDE:
			in_command->setText("");
			break;
		case Action::Method_DefaultCommand:
			in_command->setText(_methods->text(Action::Method_DefaultCommand));
			break;
		case Action::Method_UserCommand:
			in_command->setText(_userCommand);
			break;
	}
}

void MActionEditDialog::slotDefault()
{
	setMethod(Action::Method_TDE);
	c_runCommandBeforeAction->setChecked(false);
	i_commandBeforeAction->clear();
	i_commandBeforeActionPause->setValue(10);
}

void MActionEditDialog::slotMethodChange(int index)
{
	if (in_command->isEnabled())
		_userCommand = in_command->text();
	setMethod((Action::Method)index);
}

void MActionEditDialog::slotOKClicked()
{
	Action::Method m;
	TQString c;
	TQString group = ks_actions->actionToConfigGroup(_action);

	// method
	m = (Action::Method)_methods->currentItem();
	if (m == Action::Method_UserCommand)
		c = in_command->text();
	else
		c = "";
	ks_actions->setMethod(group, m, c);

	// command before action
	TDEConfig *conf = kshutdownrc->config();
	conf->setGroup(group);
	conf->writeEntry("RunCommandBeforeAction", c_runCommandBeforeAction->isChecked());
	conf->writeEntry("CommandBeforeAction", i_commandBeforeAction->text());
	conf->writeEntry("CommandBeforeActionPause", i_commandBeforeActionPause->value());
}

void MActionEditDialog::slotTestCommandBeforeAction()
{
	MiscUtils::runShellCommand(
		i_commandBeforeAction->text(),
		TDEProcess::DontCare,
		i_commandBeforeActionPause->value()
	);
}