Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / plasma / applets / lock_logout / lockout.cpp
blob704e168179418f2ed66967c14da7606df32e99c4
1 /***************************************************************************
2 * Copyright (C) 2007 by Alexis Ménard <darktears31@gmail.com> *
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 *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
18 ***************************************************************************/
20 #include "lockout.h"
22 // Plasma
23 #include <plasma/layouts/vboxlayout.h>
24 #include <plasma/widgets/icon.h>
26 // Qt
27 #include <QtDBus/QDBusInterface>
28 #include <QtDBus/QDBusReply>
30 // KDE
31 #include <KIcon>
32 #include <kworkspace/kworkspace.h>
33 #include <ksmserver_interface.h>
34 #include <screensaver_interface.h>
36 #define MINSIZE 48
38 LockOut::LockOut(QObject *parent, const QVariantList &args)
39 : Plasma::Applet(parent, args)
43 void LockOut::init()
45 m_layout = new Plasma::BoxLayout(Plasma::BoxLayout::TopToBottom, this);
46 m_layout->setMargin(0);
47 m_layout->setSpacing(0);
49 Plasma::Icon *icon_lock = new Plasma::Icon(KIcon("system-lock-screen"), "", this);
50 m_layout->addItem(icon_lock);
51 connect(icon_lock, SIGNAL(clicked()), this, SLOT(clickLock()));
53 Plasma::Icon *icon_logout = new Plasma::Icon(KIcon("system-log-out"), "", this);
54 m_layout->addItem(icon_logout);
55 connect(icon_logout, SIGNAL(clicked()), this, SLOT(clickLogout()));
57 //It seems the layout geometry must be calculated by hand for the first time
58 m_layout->setGeometry(QRectF(0,0, contentSize().width(), contentSize().height()));
59 checkLayout();
62 LockOut::~LockOut()
66 void LockOut::checkLayout()
68 Plasma::BoxLayout::Direction direction;
70 switch (formFactor()) {
71 case Plasma::Vertical:
72 if (contentSize().width() >= MINSIZE) {
73 direction = Plasma::BoxLayout::LeftToRight;
74 } else {
75 direction = Plasma::BoxLayout::TopToBottom;
77 break;
78 case Plasma::Horizontal:
79 if (contentSize().height() >= MINSIZE) {
80 direction = Plasma::BoxLayout::TopToBottom;
81 } else {
82 direction = Plasma::BoxLayout::LeftToRight;
84 break;
85 default:
86 direction = Plasma::BoxLayout::TopToBottom;
88 if (direction != m_layout->direction()) {
89 m_layout->setDirection(direction);
90 updateGeometry();
94 void LockOut::constraintsUpdated(Plasma::Constraints constraints)
96 if (constraints & Plasma::FormFactorConstraint ||
97 constraints & Plasma::SizeConstraint) {
98 checkLayout();
102 QSizeF LockOut::contentSizeHint() const
104 QSizeF sizeHint = contentSize();
105 switch (formFactor()) {
106 case Plasma::Vertical:
107 if (sizeHint.width() >= MINSIZE) {
108 sizeHint.setHeight(sizeHint.width() / 2);
109 } else {
110 sizeHint.setHeight(sizeHint.width() * 2);
112 break;
113 case Plasma::Horizontal:
114 if (sizeHint.height() >= MINSIZE) {
115 sizeHint.setHeight(sizeHint.height() / 2);
116 } else {
117 sizeHint.setHeight(sizeHint.height() * 2);
119 break;
120 default:
121 //totally arbitrary size
122 if (sizeHint.width() < 10) {
123 sizeHint = QSizeF(48,96);
125 break;
127 return sizeHint;
130 Qt::Orientations LockOut::expandingDirections() const
132 return 0;
135 void LockOut::clickLock()
137 kDebug()<<"LockOut:: lock clicked ";
139 QString interface("org.freedesktop.ScreenSaver");
140 org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
141 QDBusConnection::sessionBus());
142 if (screensaver.isValid()) {
143 screensaver.Lock();
147 void LockOut::clickLogout()
149 kDebug()<<"LockOut:: logout clicked ";
150 QString interface("org.kde.ksmserver");
151 org::kde::KSMServerInterface smserver(interface, "/KSMServer",
152 QDBusConnection::sessionBus());
153 if (smserver.isValid()) {
154 smserver.logout(KWorkSpace::ShutdownConfirmDefault,
155 KWorkSpace::ShutdownTypeDefault,
156 KWorkSpace::ShutdownModeDefault);
161 #include "lockout.moc"