1 // preferences.cpp - Preferences Dialog
2 // Copyright (C) 2007  Konrad Twardowski
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License along
15 // with this program; if not, write to the Free Software Foundation, Inc.,
16 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 
18 #include "preferences.h"
19 
20 #include "config.h"
21 #include "mainwindow.h"
22 #include "password.h"
23 #include "progressbar.h"
24 #include "utils.h"
25 
26 #include <QPushButton>
27 
28 #ifdef KS_KF5
29 	#include <KStandardGuiItem>
30 #endif // KS_KF5
31 
32 // public
33 
Preferences(QWidget * parent)34 Preferences::Preferences(QWidget *parent) :
35 	UDialog(parent, i18n("Preferences"), false) {
36 
37 	#ifdef Q_OS_WIN32
38 	rootLayout()->setMargin(5_px);
39 	rootLayout()->setSpacing(5_px);
40 	#endif // Q_OS_WIN32
41 
42 	ProgressBar *progressBar = MainWindow::self()->progressBar();
43 	m_oldProgressBarVisible = progressBar->isVisible();
44 
45 	m_tabs = new QTabWidget();
46 	m_tabs->addTab(createGeneralWidget(), i18n("General"));
47 	m_tabs->addTab(createSystemTrayWidget(), i18n("System Tray"));
48 
49 	m_passwordPreferences = new PasswordPreferences(this);
50 	m_tabs->addTab(m_passwordPreferences, i18n("Password"));
51 
52 // TODO: "Tweaks/Advanced" tab
53 
54 // TODO: actions/triggers config.
55 	//tabs->addTab(createActionsWidget(), i18n("Actions"));
56 	//tabs->addTab(createTriggersWidget(), i18n("Triggers"));
57 
58 	#ifdef KS_KF5
59 	if (Utils::isKDE()) {
60 		int iconSize = QApplication::style()->pixelMetric(QStyle::PM_LargeIconSize);
61 		m_tabs->setIconSize(QSize(iconSize, iconSize));
62 		m_tabs->setTabIcon(0, QIcon::fromTheme(KStandardGuiItem::configure().iconName())); // General
63 		m_tabs->setTabIcon(1, QIcon::fromTheme("user-desktop")); // System Tray
64 		m_tabs->setTabIcon(2, QIcon::fromTheme("dialog-password")); // Password
65 	}
66 	#endif // KS_KF5
67 
68 	mainLayout()->addWidget(m_tabs);
69 	m_tabs->setFocus();
70 
71 	// show recently used tab
72 	Config *config = Config::user();
73 	config->beginGroup("Preferences");
74 	int currentTabIndex = qBound(0, config->read("Current Tab Index", 0).toInt(), m_tabs->count() - 1);
75 	config->endGroup();
76 	m_tabs->setCurrentIndex(currentTabIndex);
77 
78 	connect(this, SIGNAL(finished(int)), SLOT(onFinish(int)));
79 }
80 
apply()81 void Preferences::apply() {
82 	Config *config = Config::user();
83 
84 	Config::blackAndWhiteSystemTrayIconVar.write(m_bwTrayIcon);
85 	Config::confirmActionVar.write(m_confirmAction);
86 	Config::lockScreenBeforeHibernateVar.write(m_lockScreenBeforeHibernate);
87 
88 	Config::minimizeToSystemTrayIconVar.setBool(!m_noMinimizeToSystemTrayIcon->isChecked());
89 	Config::minimizeToSystemTrayIconVar.write();
90 
91 	Config::progressBarEnabledVar.write(m_progressBarEnabled);
92 	Config::systemTrayIconEnabledVar.write(m_systemTrayIconEnabled);
93 	#ifndef KS_KF5
94 	Config::write("General", "Use Theme Icon In System Tray", m_useThemeIconInSystemTray->isChecked());
95 	#endif // !KS_KF5
96 
97 	m_passwordPreferences->apply();
98 
99 	#ifdef Q_OS_LINUX
100 	config->beginGroup("KShutdown Action lock");
101 	config->write("Custom Command", m_lockCommand->text());
102 	config->endGroup();
103 	#endif // Q_OS_LINUX
104 
105 	Config::cancelDefaultVar.write(m_cancelDefault);
106 	Config::oldActionNamesVar.write(m_oldActionNames);
107 
108 	config->sync();
109 }
110 
111 // private
112 
113 /*
114 QWidget *Preferences::createActionsWidget() {
115 	QWidget *w = createContainerWidget();
116 
117 	return w;
118 }
119 */
120 
createGeneralWidget()121 QWidget *Preferences::createGeneralWidget() {
122 	QWidget *w = new QWidget();
123 	auto *l = new QVBoxLayout(w);
124 	l->setMargin(10);
125 
126 	m_confirmAction = Config::confirmActionVar.newCheckBox(i18n("Confirm Action"));
127 	l->addWidget(m_confirmAction);
128 
129 	m_cancelDefault = Config::cancelDefaultVar.newCheckBox(i18n("Select \"Cancel\" button by default"));
130 	QString attr = QApplication::isRightToLeft() ? "margin-right" : "margin-left";
131 	m_cancelDefault->setStyleSheet("QCheckBox { " + attr + ": 20px; }");
132 	l->addWidget(m_cancelDefault);
133 
134 	l->addSpacing(20_px);
135 
136 	m_oldActionNames = Config::oldActionNamesVar.newCheckBox(i18n("Use old action names"));
137 	m_oldActionNames->setToolTip(
138 		i18n("Old: %0").arg(i18n("Turn Off Computer") + ", " + i18n("Suspend Computer") + ", ...") + "\n" +
139 		i18n("New: %0").arg(i18n("Shut Down") + ", " + i18n("Sleep") + ", ...")
140 	);
141 
142 // TODO: InfoWidget - smaller margin; more compact size in dialogs
143 	InfoWidget *infoWidget = new InfoWidget(nullptr);
144 
145 	connect(m_oldActionNames, &QCheckBox::toggled, [infoWidget](const bool selected) {
146 		if (selected != Config::oldActionNamesVar.getBool())
147 			infoWidget->setText(i18n("Restart application to apply changes"));
148 		else
149 			infoWidget->setText("");
150 	});
151 
152 	l->addWidget(m_oldActionNames);
153 	l->addWidget(infoWidget);
154 
155 	m_progressBarEnabled = Config::progressBarEnabledVar.newCheckBox(i18n("Show progress bar on top/bottom of the screen"));
156 // TODO: connect(m_progressBarEnabled, SIGNAL(toggled(bool)), SLOT(onProgressBarEnabled(bool)));
157 	l->addWidget(m_progressBarEnabled);
158 
159 	l->addSpacing(20_px);
160 	l->addStretch();
161 
162 	m_lockScreenBeforeHibernate = Config::lockScreenBeforeHibernateVar.newCheckBox(i18n("Lock Screen Before Hibernate/Sleep"));
163 	l->addWidget(m_lockScreenBeforeHibernate);
164 #if defined(Q_OS_WIN32) || defined(Q_OS_HAIKU)
165 	m_lockScreenBeforeHibernate->hide();
166 #endif // Q_OS_WIN32
167 
168 	#ifdef Q_OS_LINUX
169 	if (m_lockScreenBeforeHibernate->isVisible())
170 		l->addSpacing(10_px);
171 
172 	m_lockCommand = new QLineEdit();
173 	m_lockCommand->setClearButtonEnabled(true);
174 
175 	Config *config = Config::user();
176 	config->beginGroup("KShutdown Action lock");
177 	m_lockCommand->setText(config->read("Custom Command", "").toString());
178 	config->endGroup();
179 
180 // TODO: "Test/Presets" button
181 	QLabel *lockCommandLabel = new QLabel(i18n("Custom Lock Screen Command:"));
182 	lockCommandLabel->setBuddy(m_lockCommand);
183 	l->addSpacing(10_px);
184 	l->addWidget(lockCommandLabel);
185 	l->addWidget(m_lockCommand);
186 	#endif // Q_OS_LINUX
187 
188 	return w;
189 }
190 
createSystemTrayWidget()191 QWidget *Preferences::createSystemTrayWidget() {
192 	QWidget *w = new QWidget();
193 	auto *l = new QVBoxLayout(w);
194 	l->setMargin(10);
195 
196 // TODO: show info if not supported
197 
198 	#ifdef Q_OS_LINUX
199 	auto *systemTrayWarning = new InfoWidget(this);
200 	systemTrayWarning->setText(
201 		i18n("May not work correctly with some Desktop Environments"),
202 		InfoWidget::Type::Warning
203 	);
204 	l->addWidget(systemTrayWarning);
205 	l->addSpacing(10_px);
206 	#endif // Q_OS_LINUX
207 
208 	m_systemTrayIconEnabled = Config::systemTrayIconEnabledVar.newCheckBox(i18n("Enable System Tray Icon"));
209 	l->addWidget(m_systemTrayIconEnabled);
210 
211 	m_noMinimizeToSystemTrayIcon = new QCheckBox(i18n("Quit instead of minimizing to System Tray Icon"));
212 	m_noMinimizeToSystemTrayIcon->setChecked(!Config::minimizeToSystemTrayIconVar);
213 	l->addWidget(m_noMinimizeToSystemTrayIcon);
214 
215 	m_noMinimizeToSystemTrayIcon->setEnabled(m_systemTrayIconEnabled->isChecked());
216 	connect(m_systemTrayIconEnabled, SIGNAL(toggled(bool)), m_noMinimizeToSystemTrayIcon, SLOT(setEnabled(bool)));
217 
218 	l->addSpacing(10_px);
219 
220 	#ifndef KS_KF5
221 	m_useThemeIconInSystemTray = new QCheckBox(i18n("Use System Icon Theme"));
222 	m_useThemeIconInSystemTray->setChecked(Config::readBool("General", "Use Theme Icon In System Tray", true));
223 	connect(m_useThemeIconInSystemTray, &QCheckBox::toggled, [this](const bool selected) {
224 		m_bwTrayIcon->setEnabled(!selected);
225 	});
226 	l->addWidget(m_useThemeIconInSystemTray);
227 	#endif // !KS_KF5
228 
229 	m_bwTrayIcon = Config::blackAndWhiteSystemTrayIconVar.newCheckBox(i18n("Black and White System Tray Icon"));
230 	#ifdef KS_KF5
231 // TODO: redesign this option
232 	m_bwTrayIcon->hide();
233 	#else
234 		#ifndef Q_OS_WIN32
235 		m_bwTrayIcon->setEnabled(!m_useThemeIconInSystemTray->isChecked());
236 		#endif // !Q_OS_WIN32
237 	#endif // KS_KF5
238 	l->addWidget(m_bwTrayIcon);
239 
240 	#ifdef Q_OS_WIN32
241 	m_useThemeIconInSystemTray->hide();
242 	#endif // Q_OS_WIN32
243 
244 // TODO: system tray icon preview
245 
246 	l->addStretch();
247 
248 	return w;
249 }
250 
251 /*
252 QWidget *Preferences::createTriggersWidget() {
253 	QWidget *w = createContainerWidget();
254 
255 	return w;
256 }
257 */
258 
259 // private slots
260 
onFinish(int result)261 void Preferences::onFinish(int result) {
262 	Q_UNUSED(result)
263 
264 /*
265 	ProgressBar *progressBar = MainWindow::self()->progressBar();
266 	progressBar->setDemo(false);
267 	progressBar->setVisible(m_oldProgressBarVisible);
268 */
269 
270 	// save recently used tab
271 	Config *config = Config::user();
272 	config->beginGroup("Preferences");
273 	config->write("Current Tab Index", m_tabs->currentIndex());
274 	config->endGroup();
275 }
276 
onProgressBarEnabled(bool enabled)277 void Preferences::onProgressBarEnabled(bool enabled) {
278 	ProgressBar *progressBar = MainWindow::self()->progressBar();
279 	progressBar->setDemo(enabled);
280 	progressBar->setVisible(enabled || m_oldProgressBarVisible);
281 }
282