summaryrefslogtreecommitdiffstats
path: root/kshutdown/main.cpp
blob: 44286d7831a06e946f807c2df6e850e829f8d775 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
	main.cpp - KShutDown
	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 "actions.h"
#include "configuration.h"
#include "extras.h"
#include "miscutils.h"
#include "mmainwindow.h"
#include "msystemtray.h"

#include <ntqfileinfo.h>

#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kuniqueapplication.h>

// 0.1.x, 0.3.x, etc - Unstable, Beta
// 0.2.x, 0.4.x, etc - Stable
TQString version = "1.0.4";

KAboutData aboutData(
	"kshutdown", // internal name
	"KShutDown", // full name
	version,
	I18N_NOOP("A Shut Down Utility for KDE"), // description
	KAboutData::License_GPL_V2, // license
	"(C) 2003-3000 Konrad Twardowski", // copyright
	"", // text
	"http://kshutdown.sourceforge.net/", // project page
	"kdtonline@poczta.onet.pl" // bugs
);

static KCmdLineOptions options[] =
{
	{ "s", 0, 0 },
	{ "shutdown", I18N_NOOP("Turn off computer"), 0 },
	{ "h", 0, 0 },
	{ "halt", I18N_NOOP("Turn off computer"), 0 },
	{ "r", 0, 0 },
	{ "reboot", I18N_NOOP("Restart computer"), 0 },
	{ "k", 0, 0 },
	{ "lock", I18N_NOOP("Lock session"), 0 },
	{ "l", 0, 0 },
	{ "logout", I18N_NOOP("End current session"), 0 },
	{ "e", 0, 0 },
	{ "extra <file>", I18N_NOOP("Execute \"Extras\" command (.desktop file)"), 0 },
	{ "confirm", I18N_NOOP("Confirm command line action"), 0 },
	{ "standard", I18N_NOOP("Show standard logout dialog"), 0 },
	{ "c", 0, 0 },
	{ "cancel", I18N_NOOP("Cancel an active action"), 0 },
	{ "init", I18N_NOOP("Don't show window at startup"), 0 },
	{ "test", I18N_NOOP("Enable test mode"), 0 },
	{ "default", I18N_NOOP("Disable test mode"), 0 },
	{ "+[time]", I18N_NOOP("Time; Examples: 01:30 - absolute time (HH:MM); " \
		"10 - number of minutes to wait from now"), 0 },
	KCmdLineLastOption
};

/** @short KShutDown application. */
class KShutDownApplication: public KUniqueApplication
{
public:
	/**
	 * Constructor.
	 */
	KShutDownApplication()
		: KUniqueApplication(),
		_confirm(false),
		isTimeArg(false),
		now(false),
		timeArgIsValid(false),
		timeArg()
	{
	}

	/**
	 * Initializes the main window, and checks command line arguments.
	 */
	virtual int newInstance();
private:
	enum Mode {
		Mode_Visible = -1, // show the main window at startup
		Mode_Hidden = -2, // hide the main window at startup
		Mode_Ok = 0, // success
		Mode_Error = 1 // misc. failure
	};
	bool
		_confirm,
		isTimeArg,
		now,
		timeArgIsValid;
	TQString timeArg;
	int doExecAction(const Action::Type action);
	int doProcessArgs(const KCmdLineArgs *args);
};

// public

int KShutDownApplication::newInstance()
{
	// autostart
	if (kapp->isRestored()) {
		KConfig *config = kapp->config();
		config->setGroup("KShutDown");
		if (!config->readBoolEntry("Autostart", false)) {
			quit();

			return 0;
		}
	}

	static bool doShow = false;
	bool doRaise = ks_main;

	// create main window (only one)
	if (!ks_main) {
		(void)new MMainWindow();
		kapp->setMainWidget(ks_main);
		connect(
			this, SIGNAL(aboutToQuit()),
			ks_main, SLOT(deleteLater()));
	}

	// check command line args
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
	int retval = doProcessArgs(args);
	args->clear();

	if (MSystemTray::mode() != MSystemTray::Always) {
		ks_main->show();
		if (doRaise)
			ks_main->makeVisible();
		doShow = true;

		return Mode_Ok;
	}

	// gui mode
	if ((retval == Mode_Visible) || (retval == Mode_Hidden)) {
		if ((retval == Mode_Visible) && (!kapp->isRestored() || doShow)) {
			ks_main->show();
			if (doRaise)
				ks_main->makeVisible();
		}
		doShow = true;

		return Mode_Ok;
	}

	return retval; // command line exit code
}

// private

int KShutDownApplication::doExecAction(const Action::Type action) {
	if (!ks_main)
		return Mode_Error;

	// use time arg.
	if (timeArgIsValid) {
		ks_main->cancel(); // cancel action (if any)
		if (!now) {
			ks_main->setAction(action);
			ks_main->start(_confirm);

			return Mode_Ok;
		}
	}

	if (_confirm) {
		if (ks_actions->execConfirm(action))
			return Mode_Ok;
	}
	else {
		if (ks_actions->exec(action, action != Action::LockScreen))
			return Mode_Ok;
	}
	MSystemTray::setMode((MSystemTray::Mode)kshutdownrc->systemTray);

	return Mode_Error;
}

int KShutDownApplication::doProcessArgs(const KCmdLineArgs *args) {
	if (!ks_main)
		return Mode_Error;

	_confirm = args->isSet("confirm");
	isTimeArg = false;
	now = false;
	timeArg = "";
	timeArgIsValid = false;

	// read time arg.
	bool absoluteTime = false;
	int minutes = 0;
	TQDateTime dt = TQDateTime::currentDateTime();
	if (args->count()) {
		timeArg = args->arg(0); // first arg. is a "time" arg.
		if (!timeArg.isEmpty()) {
			isTimeArg = true;

			if (timeArg.upper() == "NOW") {
				now = true;
				timeArgIsValid = true;
			}
			else {
				bool ok = false;
				minutes = timeArg.toInt(&ok); // MM?
				if (ok) {
					if (minutes == 0) {
						now = true;
						timeArgIsValid = true;
					}
				}
				else {
					absoluteTime = true;
// FIXME: 2.0: AM/PM
					TQTime t = TQTime::fromString(timeArg);
					if (t.isValid() && !t.isNull()) { // HH:MM[:SS]?
						ok = true;
						dt.setTime(t);
					}
				}
				if (!ok || !dt.isValid() || dt.isNull()) {
					KMessageBox::error(
						0,
						MiscUtils::HTML(i18n("Invalid time: <b>%1</b>").arg(timeArg))
					);
				}
				else {
					timeArgIsValid = true;
				}
			}
		}
	}

	// test mode
	if (args->isSet("test"))
		ks_main->setTestMode(true);

	// normal mode
	if (args->isSet("default"))
		ks_main->setTestMode(false);

	// cancel
	if (args->isSet("cancel")) {
		ks_main->cancel();

		return Mode_Ok;
	}

	// standard logout dialog
	if (args->isSet("standard")) {
		kapp->requestShutDown(
			KApplication::ShutdownConfirmYes,
			KApplication::ShutdownTypeDefault,
			KApplication::ShutdownModeDefault
		);

		return Mode_Ok;
	}

	// do nothing ..
	if (isTimeArg && !timeArgIsValid)
		return Mode_Error;

	// setup main window
	if (timeArgIsValid) {
		if (!now) {
			if (MiscUtils::isRestricted("tab_time"))
				return Mode_Error;

			ks_main->cancel(); // cancel action before "setDelayType"
			if (absoluteTime)
			{
				ks_main->setDelayType(MMainWindow::DelayType_DateTime);
				ks_main->setDate(dt.date());
				ks_main->setTime(dt.time());
			}
			else
			{
				ks_main->setDelayType(MMainWindow::DelayType_TimeFromNow);
				ks_main->setTime(TQTime().addSecs(minutes * 60));
			}
		}
		ks_main->makeVisible();

		// exec action below ..
	}

	// extra
	if (args->isSet("extra")) {
		TQString extrasCommand = args->getOption("extra");
		if (!extrasCommand.isEmpty()) {
			TQFileInfo extra(extrasCommand);
			if (extra.exists()) {
				ks_extras->setAction(extra.filePath(), extra.baseName());
				
				return doExecAction(Action::Extras);
			}
			else {
				tqWarning("ERROR: File \"%s\" does not exist", extrasCommand.latin1());
			}
		}
	
		return Mode_Error;
	}

	// halt
	if (args->isSet("shutdown") || args->isSet("halt"))
		return doExecAction(Action::ShutDown);

	// reboot
	if (args->isSet("reboot"))
		return doExecAction(Action::Reboot);

	// lock
	if (args->isSet("lock"))
		return doExecAction(Action::LockScreen);

	// logout
	if (args->isSet("logout"))
		return doExecAction(Action::Logout);

	if (args->isSet("init"))
		return Mode_Hidden; // init window, but don't show it

	return Mode_Visible; // gui mode
}

// main

int main(int argc, char **argv)
{
// TODO: 2.0: init on demand?
	aboutData.addCredit("Konrad Twardowski", "Author, Maintainer", "kdtonline@poczta.onet.pl", "http://www.kdtonline.prv.pl/");
	aboutData.addCredit("Caryn \"Hellchick\" Law", "Female Quake III Arena Announcer Voice Files", "hellchick ( at ) planetquake.com", "http://www.planetquake.com/voxfeminae/");
	aboutData.addCredit("Arend van Beelen jr.", "Ideas", "", "http://www.liacs.nl/~dvbeelen");
	aboutData.addCredit("Bram Schoenmakers", "Dutch translation", "bram_s ( at ) softhome.net");
	aboutData.addCredit("Charles Barcza", "Hungarian translation, blackPanther-Linux RPM", "kbarcza ( at ) blackpanther.hu", "http://www.blackpanther.hu/");
	aboutData.addCredit("Daniel Nylander", "Swedish translation");
	aboutData.addCredit("Elias Probst", "Gentoo ebuilds, German translation", "elias.probst ( at ) gmx.de");
	aboutData.addCredit("Giovanni Venturi", "Italian translation", "jumpyj ( at ) tiscali.it");
	aboutData.addCredit("Gregorio Guidi", "Patches");
	aboutData.addCredit("Guido Tack", "Ideas", "", "http://www.ps.uni-sb.de/~tack");
	aboutData.addCredit("Jozef Riha", "Slovak translation", "zefo ( at ) seznam.cz");
	aboutData.addCredit("Karol Adamczyk [rampage]", "Gentoo ebuild");
	aboutData.addCredit("Matrix", "SuSE RPM", "matrix ( at ) ehelp.pl");
	aboutData.addCredit("Michael Goettsche", "Bug reports");
	aboutData.addCredit("Paulo Zambon", "Portuguese Brazil translation", "pzambon ( at ) astornet.com.br");
	aboutData.addCredit("Philipp Weissenbacher");
	aboutData.addCredit("Piotr Budny");
	aboutData.addCredit("Quique", "Spanish translation", "tquique ( at ) sindominio.net");
	aboutData.addCredit("Robert Kratky", "Czech translation", "kratky ( at ) rob.cz");
	aboutData.addCredit("Romain Beauxis", "Debian Package", "", "http://www.cti.ecp.fr/~beauxir5/debian/");
	aboutData.addCredit("Spider (ALT Linux)", "Russian translation");
	aboutData.addCredit("Stephen Ellwood", "Panel Applet, User Interface");
	aboutData.addCredit("Zdenko Podobny", "Slovak translation, Mandrake RPM", "zdpo ( at ) mailbox.sk");
	aboutData.addCredit("KDE-APPS.org", "", "", "http://www.kde-apps.org/");
	aboutData.addCredit("Lock/Logout Applet Team");
	aboutData.addCredit("SourceForge.net", "", "", "http://www.sourceforge.net/");

	// init command line
	KCmdLineArgs::init(argc, argv, &aboutData);
	KCmdLineArgs::addCmdLineOptions(options);

	if (!KUniqueApplication::start())
		return 0;

	// init application
	KShutDownApplication software;

	// main loop
	return software.exec();
}