summaryrefslogtreecommitdiffstats
path: root/kshutdown/links.cpp
blob: f12816921c14fa2f1ff34a9af40397958ac89cd1 (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
/*
	links.cpp - A link creator/remover
	Copyright (C) 2004  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 "links.h"
#include "miscutils.h"

#include <tqcombobox.h>
#include <tqfile.h>
#include <tqlabel.h>

#include <tdeapplication.h>
#include <kdesktopfile.h>
#include <tdeglobalsettings.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <tdemessagebox.h>
#include <kpushbutton.h>
#include <kstandarddirs.h>

// public

Links::Links(TQWidget *parent)
	: TQVBox(parent, "Links")
{
	setSpacing(5);
	int alignment = AlignVCenter;
	alignment |= kapp->reverseLayout() ? AlignLeft : AlignRight;


	// location
	TQHBox *w_location = new TQHBox(this);
	w_location->setSpacing(5);
	TQLabel *l_location = new TQLabel(i18n("Location where to create the link:"), w_location);
	l_location->setAlignment(alignment);
	cb_location = new TQComboBox(w_location, "TQComboBox::cb_location");
	cb_location->setFocusPolicy(StrongFocus);
	cb_location->insertItem(SmallIcon("desktop"), i18n("Desktop"));
	cb_location->insertItem(SmallIcon("kmenu"), i18n("K Menu"));
	l_location->setBuddy(cb_location);
	connect(cb_location, TQ_SIGNAL(activated(int)), TQ_SLOT(slotActivated(int)));

	// type
	TQHBox *w_type = new TQHBox(this);
	w_type->setSpacing(5);
	TQLabel *l_type = new TQLabel(i18n("Type of the link:"), w_type);
	l_type->setAlignment(alignment);
	cb_type = new TQComboBox(w_type, "TQComboBox::cb_type");
	cb_type->setFocusPolicy(StrongFocus);
	cb_type->insertItem(kapp->miniIcon(), "KShutDown");

	// NOTE: slotAddRemoveLink()
	cb_type->insertItem(SmallIcon("system-log-out"), i18n("Standard Logout Dialog"));

	cb_type->insertItem(ks_actions->getIcon(Action::ShutDown), ks_actions->getName(Action::ShutDown));
	cb_type->insertItem(ks_actions->getIcon(Action::Reboot), ks_actions->getName(Action::Reboot));
	cb_type->insertItem(ks_actions->getIcon(Action::LockScreen), ks_actions->getName(Action::LockScreen));
	cb_type->insertItem(ks_actions->getIcon(Action::Logout), ks_actions->getName(Action::Logout));
	l_type->setBuddy(cb_type);
	connect(cb_type, TQ_SIGNAL(activated(int)), TQ_SLOT(slotActivated(int)));

	// add/remove link
	b_addRemoveLink = new KPushButton(this, "KPushButton::b_addRemoveLink");
	connect(b_addRemoveLink, TQ_SIGNAL(clicked()), TQ_SLOT(slotAddRemoveLink()));

	updateAddRemoveButton();
}

void Links::createLink(const TQString &path, const TQString &command, const TQString &icon, const TQString &name, const TQString &comment)
{
	if (path.isNull())
		return;

	KDesktopFile *df = new KDesktopFile(path);
	df->setGroup("Desktop Entry");
	df->writeEntry("Comment", comment);
	df->writeEntry("Encoding", "UTF-8");
	df->writeEntry("Exec", command);
	df->writeEntry("GenericName", i18n("System Shut Down Utility"));
	df->writeEntry("Icon", icon);
	df->writeEntry("Name", name);
	df->writeEntry("StartupNotify", "false");
	df->writeEntry("Type", "Application");
	delete df;

	if (!TQFile::exists(path))
	{
		KMessageBox::error(
			0,
			MiscUtils::HTML(i18n("Could not create file <b>%1</b>!").arg(path))
		);
	}
}

void Links::removeLink(const TQString &path)
{
	if (TQFile::exists(path) && !TQFile::remove(path))
	{
		KMessageBox::error(
			0,
			MiscUtils::HTML(i18n("Could not remove file <b>%1</b>!").arg(path))
		);
	}
}

// private

TQString Links::getCurrentLocationPath() const
{
	TQString path;
	switch (cb_location->currentItem())
	{
		case 0:
			path = TDEGlobalSettings::desktopPath() + "/";
			break;
		case 1:
			path = locateLocal("apps", "") + "/";
			break;
		default:
			return TQString::null;
	}
	switch (cb_type->currentItem())
	{
		case 0: return path += "kshutdown-classic.desktop";
		case 1: return path += "kshutdown-standard.desktop";
		case 2: return path += "kshutdown-shutdown.desktop";
		case 3: return path += "kshutdown-reboot.desktop";
		case 4: return path += "kshutdown-lock.desktop";
		case 5: return path += "kshutdown-logout.desktop";
		default: return TQString::null;
	}
}

TQString Links::getCurrentTypeCommand() const
{
	switch (cb_type->currentItem())
	{
		case 0: return "kshutdown";
		case 1: return "kshutdown --standard";
		case 2: return "kshutdown --confirm --shutdown";
		case 3: return "kshutdown --confirm --reboot";
		case 4: return "kshutdown --confirm --lock";
		case 5: return "kshutdown --confirm --logout";
		default: return TQString::null;
	}
}

TQString Links::getCurrentTypeIcon() const
{
	switch (cb_type->currentItem())
	{
		case 0: return "kshutdown";
		case 1: return "system-log-out";
		// sync. with Action::getIcon
		case 2: return "system-log-out";
		case 3: return "reload";
		case 4: return "system-lock-screen";
		case 5: return "edit-undo";
		default: return TQString::null;
	}
}

void Links::updateAddRemoveButton() {
	if (TQFile::exists(getCurrentLocationPath())) {
		b_addRemoveLink->setIconSet(SmallIcon("edit-delete"));
		b_addRemoveLink->setText(i18n("Remove Link"));
	}
	else {
		b_addRemoveLink->setIconSet(SmallIcon("document-new"));
		b_addRemoveLink->setText(i18n("Add Link"));
	}
}

// private slots

void Links::slotActivated(int/* index*/) {
	updateAddRemoveButton();
}

void Links::slotAddRemoveLink() {
	if (TQFile::exists(getCurrentLocationPath())) {
		removeLink(getCurrentLocationPath());
	}
	else {
		createLink(
			getCurrentLocationPath(),
			getCurrentTypeCommand(),
			getCurrentTypeIcon(),
			(cb_type->currentItem() == 1) ? i18n("Logout") : cb_type->currentText(),
			cb_type->currentText()
		);
	}
	updateAddRemoveButton();
}
#include "links.moc"