not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / plasma / applets / lock_logout / lockout.cpp
blobb3da614003c5bae4fe80506b5ef23958828298e7
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/IconWidget>
25 // Qt
26 #include <QtGui/QWidget> // QWIDGETSIZE_MAX
27 #include <QtDBus/QDBusInterface>
28 #include <QtDBus/QDBusReply>
29 #include <QGraphicsLinearLayout>
31 // KDE
32 #include <KIcon>
33 #ifndef Q_OS_WIN
34 #include <kworkspace/kworkspace.h>
35 #include <screensaver_interface.h>
36 #endif
38 // Windows
39 #ifdef Q_OS_WIN
40 #define _WIN32_WINNT 0x0500 // require NT 5.0 (win 2k pro)
41 #include <windows.h>
42 #endif // Q_OS_WIN
44 static const int MINBUTTONSIZE = 8;
45 static const int MARGINSIZE = 2;
47 LockOut::LockOut(QObject *parent, const QVariantList &args)
48 : Plasma::Applet(parent, args)
50 resize(MINBUTTONSIZE, MINBUTTONSIZE * 2 + MARGINSIZE);
53 void LockOut::init()
55 m_layout = new QGraphicsLinearLayout(this);
56 m_layout->setContentsMargins(0,0,0,0);
57 m_layout->setSpacing(0);
59 Plasma::IconWidget *icon_lock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
60 m_layout->addItem(icon_lock);
61 connect(icon_lock, SIGNAL(clicked()), this, SLOT(clickLock()));
62 #ifndef Q_OS_WIN
63 Plasma::IconWidget *icon_logout = new Plasma::IconWidget(KIcon("system-shutdown"), "", this);
64 m_layout->addItem(icon_logout);
65 connect(icon_logout, SIGNAL(clicked()), this, SLOT(clickLogout()));
66 #endif
69 LockOut::~LockOut()
73 void LockOut::checkLayout()
75 Qt::Orientation direction;
76 qreal ratioToKeep = 2;
78 switch (formFactor()) {
79 case Plasma::Vertical:
80 if (geometry().width() >= MINBUTTONSIZE * 2 + MARGINSIZE) {
81 direction = Qt::Horizontal;
82 ratioToKeep = 2;
83 } else {
84 direction = Qt::Vertical;
85 ratioToKeep = 0.5;
87 break;
88 case Plasma::Horizontal:
89 if (geometry().height() >= MINBUTTONSIZE * 2 + MARGINSIZE) {
90 direction = Qt::Vertical;
91 ratioToKeep = 0.5;
92 } else {
93 direction = Qt::Horizontal;
94 ratioToKeep = 2;
96 break;
97 default:
98 direction = Qt::Vertical;
101 if (direction == Qt::Horizontal) {
102 setMinimumSize(MINBUTTONSIZE * 2 + MARGINSIZE, MINBUTTONSIZE);
103 } else {
104 setMinimumSize(MINBUTTONSIZE, MINBUTTONSIZE * 2 + MARGINSIZE);
107 if (direction != m_layout->orientation()) {
108 m_layout->setOrientation(direction);
111 if (formFactor() == Plasma::Horizontal) {
112 setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding));
113 qreal wsize = size().height() * ratioToKeep;
114 setMaximumSize(wsize, QWIDGETSIZE_MAX);
115 } else if (formFactor() == Plasma::Vertical) {
116 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
117 qreal hsize = size().width() / ratioToKeep;
118 setMaximumSize(QWIDGETSIZE_MAX, hsize);
119 } else {
120 setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
124 void LockOut::constraintsEvent(Plasma::Constraints constraints)
126 if (constraints & Plasma::FormFactorConstraint ||
127 constraints & Plasma::SizeConstraint) {
128 checkLayout();
132 void LockOut::clickLock()
134 kDebug()<<"LockOut:: lock clicked ";
136 #ifndef Q_OS_WIN
137 QString interface("org.freedesktop.ScreenSaver");
138 org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
139 QDBusConnection::sessionBus());
140 if (screensaver.isValid()) {
141 screensaver.Lock();
143 #else
144 LockWorkStation();
145 #endif // !Q_OS_WIN
148 void LockOut::clickLogout()
150 kDebug()<<"LockOut:: logout clicked ";
151 #ifndef Q_OS_WIN
152 KWorkSpace::requestShutDown( KWorkSpace::ShutdownConfirmDefault,
153 KWorkSpace::ShutdownTypeDefault,
154 KWorkSpace::ShutdownModeDefault);
155 #endif
159 #include "lockout.moc"