1
/***************************************************************************
2
* Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com> *
3
* Copyright (C) 2009 by Frederik Gladhorn <gladhorn@kde.org> *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 2 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, write to the *
17
* Free Software Foundation, Inc., *
18
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
19
***************************************************************************/
24
#include <Plasma/IconWidget>
25
#include <Plasma/ToolTipManager>
28
#include <QtGui/QWidget> // QWIDGETSIZE_MAX
29
#include <QtDBus/QDBusInterface>
30
#include <QtDBus/QDBusReply>
31
#include <QtDBus/QDBusConnectionInterface>
32
#include <QGraphicsLinearLayout>
38
#include <KAuthorized>
40
#include <KConfigDialog>
41
#include <KSharedConfig>
42
#include <kworkspace/kworkspace.h>
43
#include <krunner_interface.h>
44
#include <screensaver_interface.h>
52
static const int MINBUTTONSIZE = 16;
53
static const int MARGINSIZE = 1;
55
LockOut::LockOut(QObject *parent, const QVariantList &args)
56
: Plasma::Applet(parent, args),
65
setHasConfigurationInterface(true);
67
setAspectRatioMode(Plasma::IgnoreAspectRatio);
72
m_layout = new QGraphicsLinearLayout(this);
73
m_layout->setContentsMargins(0,0,0,0);
74
m_layout->setSpacing(0);
79
void LockOut::configChanged()
82
KConfigGroup cg = config();
83
m_showLockButton = cg.readEntry("showLockButton", true);
84
m_showSwitchUserButton = cg.readEntry("showSwitchUserButton", false);
85
m_showLogoutButton = cg.readEntry("showLogoutButton", true);
86
m_showSleepButton = cg.readEntry("showSleepButton", false);
87
m_showHibernateButton = cg.readEntry("showHibernateButton", false);
91
if (m_visibleButtons == 0 ) {
92
m_showLockButton = true;
93
m_showSwitchUserButton = false;
94
m_showLogoutButton = true;
95
m_showSleepButton = false;
96
m_showHibernateButton = false;
101
void LockOut::buttonChanged()
104
if (m_showLockButton != ui.checkBox_lock->isChecked()) {
105
m_showLockButton = !m_showLockButton;
109
if (m_showSwitchUserButton != ui.checkBox_switchUser->isChecked()) {
110
m_showSwitchUserButton = !m_showSwitchUserButton;
114
if (m_showLogoutButton != ui.checkBox_logout->isChecked()) {
115
m_showLogoutButton = !m_showLogoutButton;
119
if (m_showSleepButton != ui.checkBox_sleep->isChecked()) {
120
m_showSleepButton = !m_showSleepButton;
124
if (m_showHibernateButton != ui.checkBox_hibernate->isChecked()) {
125
m_showHibernateButton = !m_showHibernateButton;
131
emit configUiChanged();
134
void LockOut::setCheckable()
138
if (m_visibleButtons == 1) {
139
if (ui.checkBox_lock->isChecked()) {
140
ui.checkBox_lock->setEnabled(false);
142
if (ui.checkBox_switchUser->isChecked()) {
143
ui.checkBox_switchUser->setEnabled(false);
145
if (ui.checkBox_logout->isChecked()) {
146
ui.checkBox_logout->setEnabled(false);
148
if (ui.checkBox_sleep->isChecked()) {
149
ui.checkBox_sleep->setEnabled(false);
151
if(ui.checkBox_hibernate->isChecked()) {
152
ui.checkBox_hibernate->setEnabled(false);
155
ui.checkBox_lock->setEnabled(true);
156
ui.checkBox_switchUser->setEnabled(true);
157
ui.checkBox_logout->setEnabled(true);
158
ui.checkBox_sleep->setEnabled(true);
159
ui.checkBox_hibernate->setEnabled(true);
164
void LockOut::countButtons()
166
m_visibleButtons = 0;
168
if (m_showLockButton) {
172
if (m_showSwitchUserButton) {
176
if (m_showLogoutButton) {
180
if (m_showSleepButton) {
184
if (m_showHibernateButton) {
193
void LockOut::checkLayout()
195
qreal left,top, right, bottom;
196
getContentsMargins(&left,&top, &right, &bottom);
197
int width = geometry().width() - left - right;
198
int height = geometry().height() - top - bottom;
200
Qt::Orientation direction = Qt::Vertical;
204
switch (formFactor()) {
205
case Plasma::Vertical:
206
if (width >= (MINBUTTONSIZE + MARGINSIZE) * m_visibleButtons) {
207
direction = Qt::Horizontal;
208
height = qMax(width / m_visibleButtons, MINBUTTONSIZE);
209
minHeight = MINBUTTONSIZE;
211
minHeight = MINBUTTONSIZE * m_visibleButtons + top + bottom;
215
case Plasma::Horizontal:
216
if (height < (MINBUTTONSIZE + MARGINSIZE) * m_visibleButtons) {
217
direction = Qt::Horizontal;
218
minWidth = MINBUTTONSIZE * m_visibleButtons + left + right;
220
width = qMax(height / m_visibleButtons, MINBUTTONSIZE);
221
minWidth = MINBUTTONSIZE;
226
if (width > height) {
227
direction = Qt::Horizontal;
228
minWidth = MINBUTTONSIZE * m_visibleButtons + left + right;
229
minHeight = MINBUTTONSIZE + top + bottom;
231
minWidth = MINBUTTONSIZE + left + right;
232
minHeight = MINBUTTONSIZE * m_visibleButtons + top + bottom;
237
m_layout->setOrientation(direction);
238
setMinimumSize(minWidth, minHeight);
239
if (direction == Qt::Horizontal) {
240
setPreferredSize(height * m_visibleButtons + left + right, height + top + bottom);
242
setPreferredSize(width + left + right, width * m_visibleButtons + top + bottom);
246
void LockOut::constraintsEvent(Plasma::Constraints constraints)
248
if (constraints & Plasma::FormFactorConstraint ||
249
constraints & Plasma::SizeConstraint) {
254
void LockOut::clickLock()
256
kDebug()<<"LockOut:: lock clicked ";
259
QString interface("org.freedesktop.ScreenSaver");
260
org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
261
QDBusConnection::sessionBus());
262
if (screensaver.isValid()) {
270
void LockOut::clickLogout()
272
if (!KAuthorized::authorizeKAction("logout")) {
276
kDebug()<<"LockOut:: logout clicked ";
278
KWorkSpace::requestShutDown( KWorkSpace::ShutdownConfirmDefault,
279
KWorkSpace::ShutdownTypeDefault,
280
KWorkSpace::ShutdownModeDefault);
284
void LockOut::clickSwitchUser()
287
// Taken from kickoff/core/itemhandlers.cpp
288
QString interface("org.kde.krunner");
289
org::kde::krunner::App krunner(interface, "/App", QDBusConnection::sessionBus());
290
krunner.switchUser();
294
#include <KMessageBox>
296
void LockOut::clickSleep()
298
if (KMessageBox::questionYesNo(0,
299
i18n("Do you want to suspend to RAM (sleep)?"),
301
!= KMessageBox::Yes) {
304
// Check if KDE Power Management System is running, and use its methods to suspend if available
305
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
306
kDebug() << "Using KDE Power Management System to suspend";
307
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
308
"/org/kde/Solid/PowerManagement",
309
"org.kde.Solid.PowerManagement",
311
QDBusConnection::sessionBus().asyncCall(call);
313
kDebug() << "KDE Power Management System not available, suspend failed";
317
void LockOut::clickHibernate()
319
if (KMessageBox::questionYesNo(0,
320
i18n("Do you want to suspend to disk (hibernate)?"),
322
!= KMessageBox::Yes) {
325
// Check if KDE Power Management System is running, and use its methods to hibernate if available
326
if (QDBusConnection::sessionBus().interface()->isServiceRegistered("org.kde.Solid.PowerManagement")) {
327
kDebug() << "Using KDE Power Management System to hibernate";
328
QDBusMessage call = QDBusMessage::createMethodCall("org.kde.Solid.PowerManagement",
329
"/org/kde/Solid/PowerManagement",
330
"org.kde.Solid.PowerManagement",
332
QDBusConnection::sessionBus().asyncCall(call);
334
kDebug() << "KDE Power Management System not available, hibernate failed";
338
void LockOut::configAccepted()
341
KConfigGroup cg = config();
343
int oldButtonCount = m_visibleButtons;
345
if(m_visibleButtons == 0) {
346
configChanged(); // if no button was selected, reject configuration and reload previous
350
cg.writeEntry("showHibernateButton", m_showHibernateButton);
351
cg.writeEntry("showSleepButton", m_showSleepButton);
352
cg.writeEntry("showLogoutButton", m_showLogoutButton);
353
cg.writeEntry("showSwitchUserButton", m_showSwitchUserButton);
354
cg.writeEntry("showLockButton", m_showLockButton);
357
if (formFactor() != Plasma::Horizontal && formFactor() != Plasma::Vertical) {
358
resize(size().width(), (size().height() / oldButtonCount) * m_visibleButtons);
360
emit configNeedsSaving();
365
void LockOut::createConfigurationInterface(KConfigDialog *parent)
368
QWidget *widget = new QWidget(parent);
370
parent->addPage(widget, i18n("Please select one or more items on the list below"), Applet::icon());
371
connect(parent, SIGNAL(applyClicked()), this, SLOT(configAccepted()));
372
connect(parent, SIGNAL(okClicked()), this, SLOT(configAccepted()));
375
if (m_visibleButtons == 1) {
376
ui.checkBox_lock->setEnabled(!m_showLockButton);
377
ui.checkBox_switchUser->setEnabled(!m_showSwitchUserButton);
378
ui.checkBox_logout->setEnabled(!m_showLogoutButton);
379
ui.checkBox_sleep->setEnabled(!m_showSleepButton);
380
ui.checkBox_hibernate->setEnabled(!m_showHibernateButton);
383
ui.checkBox_lock->setChecked(m_showLockButton);
384
connect(ui.checkBox_lock, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
385
ui.checkBox_switchUser->setChecked(m_showSwitchUserButton);
386
connect(ui.checkBox_switchUser, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
387
ui.checkBox_logout->setChecked(m_showLogoutButton);
388
connect(ui.checkBox_logout, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
389
ui.checkBox_sleep->setChecked(m_showSleepButton);
390
connect(ui.checkBox_sleep, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
391
ui.checkBox_hibernate->setChecked(m_showHibernateButton);
392
connect(ui.checkBox_hibernate, SIGNAL(toggled(bool)), this, SLOT(buttonChanged()));
394
connect(this, SIGNAL(configUiChanged()), parent, SLOT(settingsModified()));
395
connect(ui.checkBox_logout, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
396
connect(ui.checkBox_lock, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
397
connect(ui.checkBox_switchUser, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
398
connect(ui.checkBox_hibernate, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
399
connect(ui.checkBox_sleep, SIGNAL(toggled(bool)), parent, SLOT(settingsModified()));
403
void LockOut::showButtons()
405
while(m_layout->count() > 0) {
406
m_layout->removeAt(0);
409
// can this be done more beautiful?
412
delete m_iconSwitchUser;
413
m_iconSwitchUser = 0;
418
delete m_iconHibernate;
422
//Tooltip strings maybe should be different (eg. "Leave..."->"Logout")?
423
m_iconLock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
424
connect(m_iconLock, SIGNAL(clicked()), this, SLOT(clickLock()));
425
Plasma::ToolTipContent lockToolTip(i18n("Lock"),i18n("Lock the screen"),m_iconLock->icon());
426
Plasma::ToolTipManager::self()->setContent(m_iconLock, lockToolTip);
427
m_layout->addItem(m_iconLock);
430
if (m_showLockButton) {
431
//Tooltip strings maybe should be different (eg. "Leave..."->"Logout")?
432
m_iconLock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
433
connect(m_iconLock, SIGNAL(clicked()), this, SLOT(clickLock()));
434
Plasma::ToolTipContent lockToolTip(i18n("Lock"),i18n("Lock the screen"),m_iconLock->icon());
435
Plasma::ToolTipManager::self()->setContent(m_iconLock, lockToolTip);
436
m_layout->addItem(m_iconLock);
439
if (m_showSwitchUserButton) {
440
m_iconSwitchUser = new Plasma::IconWidget(KIcon("system-switch-user"), "", this);
441
connect(m_iconSwitchUser, SIGNAL(clicked()), this, SLOT(clickSwitchUser()));
442
Plasma::ToolTipContent switchUserToolTip(i18n("Switch user"),i18n("Start a parallel session as a different user"),m_iconSwitchUser->icon());
443
Plasma::ToolTipManager::self()->setContent(m_iconSwitchUser, switchUserToolTip);
444
m_layout->addItem(m_iconSwitchUser);
447
if (m_showLogoutButton) {
448
m_iconLogout = new Plasma::IconWidget(KIcon("system-shutdown"), "", this);
449
connect(m_iconLogout, SIGNAL(clicked()), this, SLOT(clickLogout()));
450
Plasma::ToolTipContent logoutToolTip(i18n("Leave..."),i18n("Logout, turn off or restart the computer"),m_iconLogout->icon());
451
Plasma::ToolTipManager::self()->setContent(m_iconLogout, logoutToolTip);
452
m_layout->addItem(m_iconLogout);
455
if (m_showSleepButton) {
456
m_iconSleep = new Plasma::IconWidget(KIcon("system-suspend"), "", this);
457
connect(m_iconSleep, SIGNAL(clicked()), this, SLOT(clickSleep()));
458
Plasma::ToolTipContent sleepToolTip(i18n("Suspend"),i18n("Sleep (suspend to RAM)"),m_iconSleep->icon());
459
Plasma::ToolTipManager::self()->setContent(m_iconSleep, sleepToolTip);
460
m_layout->addItem(m_iconSleep);
463
if (m_showHibernateButton) {
464
m_iconHibernate = new Plasma::IconWidget(KIcon("system-suspend-hibernate"), "", this);
465
connect(m_iconHibernate, SIGNAL(clicked()), this, SLOT(clickHibernate()));
466
Plasma::ToolTipContent hibernateToolTip(i18n("Hibernate"),i18n("Hibernate (suspend to disk)"),m_iconHibernate->icon());
467
Plasma::ToolTipManager::self()->setContent(m_iconHibernate, hibernateToolTip);
468
m_layout->addItem(m_iconHibernate);
471
setConfigurationRequired(!m_showLockButton && !m_showSwitchUserButton && !m_showLogoutButton && !m_showSleepButton && !m_showHibernateButton);
476
#include "lockout.moc"