summaryrefslogtreecommitdiffstats
path: root/kshutdown/actions.h
blob: 4f386087e933b0b2a99b8ee938bea0007c03de48 (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
/*
	actions.h - Actions
	Copyright (C) 2005  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
*/

#ifndef __ACTIONS_H__
#define __ACTIONS_H__

#include <tdeapplication.h>

// default external commands
const TQString
	DEFAULT_LOCK_SCREEN_COMMAND("xscreensaver-command -lock"),
// TODO: 2.0: logout command for GNOME (gnome-session-save --kill?)
	DEFAULT_LOGOUT_COMMAND(""),
	DEFAULT_REBOOT_COMMAND("/sbin/reboot"),
	DEFAULT_SHUT_DOWN_COMMAND("/sbin/poweroff");

#define ks_actions Action::getInstance()

class TQPixmap;

/** @short An action manager. */
class Action: public TQObject
{
	Q_OBJECT
public:

	/**
	 * Methods. Don't change these values!
	 */
	enum Method {
		Method_TDE = 0, /**< Standard KDE/TDM method. (default) */
		Method_DefaultCommand = 1, /**< Default external command. (e.g. /sbin/reboot) */
		Method_UserCommand = 2 /**< User command. Any command that can be executed by @c KRun::run. */
	};

	/**
	 * Actions.
	 */
	enum Type {
		Nothing = 0, /**< Nothing. */
		ShutDown = 1, /**< System shut down. */
		Reboot = 2, /**< System reboot. */
		LockScreen = 3, /**< Screen lock. */
		Logout = 4, /**< End current session. */
		Extras = 5 /**< Extras. Execute an item selected from the "Extras..." menu. */
	};

	/** Destructor. */
	virtual ~Action();

	/**
	 * Converts @p action value to the configuration group name.
	 */
	TQString actionToConfigGroup(const Type action) const;

	/**
	 * Returns @c true if the current action is active.
	 */
	inline bool active() const { return _active; }

	/**
	 * Activates the current action if @p yes is @c true.
	 */
	inline void setActive(const bool yes) { _active = yes; }

	/** Returns the currently selected action. */
	inline Type current() const { return _current; }

	/**
	 * Sets current action to @p action.
	 */
	inline void setCurrent(const Type action) { _current = action; }

	/**
	 * Ends current session.
	 * This function is a wrapper for the @c kapp->requestShutDown().
	 * @param type A shut down type
	 * @param action An action to execute
	 * @return @c true if successful; otherwise @c false
	 */
	bool endSession(const TDEApplication::ShutdownType type, const Type action);

	/**
	 * Executes an action.
	 * @param action An action to execute
	 * @param stopTimer If @c true, the current active action and its timer are stopped
	 * @return @c true if successful; otherwise @c false
	 */
	bool exec(const Type action, const bool stopTimer = true);

	/**
	 * Executes an action.
	 * Displays confirmation message before execution.
	 * @param action An action to execute
	 * @param delay A selected delay
	 */
	bool execConfirm(const Type action, const TQString &delay = TQString::null);

	/**
	 * Executes the current action without confirmation.
	 */
	bool execCurrent();

	/**
	 * Returns the current action name as text.
	 */
	TQString getCurrentName() const;

	/**
	 * Returns an icon for @p action.
	 */
	TQPixmap getIcon(const Type action) const;

	/**
	 * Returns an icon name for @p action.
	 */
	TQString getIconName(const Type action) const;

	/**
	 * Initializes and return singleton instance of the @c Action class.
	 */
	inline static Action *getInstance()
	{
		if (!_instance)
			_instance = new Action();

		return _instance;
	}

	/**
	 * Reads method for action from the configuration file.
	 * @param action An action
	 * @retval method A method to use
	 * @retval command A command to run
	 * @return A default command
	 */
	TQString getMethod(
		const Type action,
		Method &method,
		TQString &command
	);

	/**
	 * Changes the action method.
	 * @param group A configuration group (in @b kshutdownrc file)
	 * @param method A new method
	 * @param command A new command
	 */
	void setMethod(const TQString &group, const Method method, const TQString &command) const;

	/**
	 * Returns an action name as text.
	 * @param action An action
	 */
	TQString getName(const Type action) const;

	/** Returns @c true if @p action is @b not disabled by KIOSK. */
	bool isEnabled(const Type action);

	/** Returns @c true if test mode is enabled. */
	inline bool testMode() const { return _testMode; }

	/**
	 * Enables/disables the <b>test mode</b>.
	 * @param yes @c true to enable test mode
	 */
	inline void setTestMode(const bool yes) { _testMode = yes; }

	/**
	 * Executes the current action,
	 * and displays an error message if action failed.
	 */
	void totalExec();

	/**
	 * Returns @c true if application/system is shutting down.
	 */
	inline bool totalExit() const { return _totalExit; }

	/**
	 * Sets <b>total exit</b> info to @p yes.
	 */
	inline void setTotalExit(const bool yes) { _totalExit = yes; }
private:
	static Action *_instance;
	bool
		_active,
		_testMode,
		_totalExit;
	TQMap<Type, TQPixmap> *iconCache;
	Type _current;
	Action();
	bool lockScreen();
public slots:
	void slotLockScreen();
	void slotLogout();
	void slotReboot();
	void slotShutDown();
};

#endif // __ACTIONS_H__