Applets
lockout.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "lockout.h"
00021
00022
00023 #include <Plasma/IconWidget>
00024
00025
00026 #include <QtGui/QWidget>
00027 #include <QtDBus/QDBusInterface>
00028 #include <QtDBus/QDBusReply>
00029 #include <QGraphicsLinearLayout>
00030
00031
00032 #include <KIcon>
00033 #ifndef Q_OS_WIN
00034 #include <kworkspace/kworkspace.h>
00035 #include <screensaver_interface.h>
00036 #endif
00037
00038
00039 #ifdef Q_OS_WIN
00040 #define _WIN32_WINNT 0x0500 // require NT 5.0 (win 2k pro)
00041 #include <windows.h>
00042 #endif // Q_OS_WIN
00043
00044 static const int MINBUTTONSIZE = 8;
00045 static const int MARGINSIZE = 2;
00046
00047 LockOut::LockOut(QObject *parent, const QVariantList &args)
00048 : Plasma::Applet(parent, args)
00049 {
00050 resize(MINBUTTONSIZE, MINBUTTONSIZE * 2 + MARGINSIZE);
00051 }
00052
00053 void LockOut::init()
00054 {
00055 m_layout = new QGraphicsLinearLayout(this);
00056 m_layout->setContentsMargins(0,0,0,0);
00057 m_layout->setSpacing(0);
00058
00059 Plasma::IconWidget *icon_lock = new Plasma::IconWidget(KIcon("system-lock-screen"), "", this);
00060 m_layout->addItem(icon_lock);
00061 connect(icon_lock, SIGNAL(clicked()), this, SLOT(clickLock()));
00062 #ifndef Q_OS_WIN
00063 Plasma::IconWidget *icon_logout = new Plasma::IconWidget(KIcon("system-shutdown"), "", this);
00064 m_layout->addItem(icon_logout);
00065 connect(icon_logout, SIGNAL(clicked()), this, SLOT(clickLogout()));
00066 #endif
00067 }
00068
00069 LockOut::~LockOut()
00070 {
00071 }
00072
00073 void LockOut::checkLayout()
00074 {
00075 Qt::Orientation direction;
00076 qreal ratioToKeep = 2;
00077
00078 switch (formFactor()) {
00079 case Plasma::Vertical:
00080 if (geometry().width() >= MINBUTTONSIZE * 2 + MARGINSIZE) {
00081 direction = Qt::Horizontal;
00082 ratioToKeep = 2;
00083 } else {
00084 direction = Qt::Vertical;
00085 ratioToKeep = 0.5;
00086 }
00087 break;
00088 case Plasma::Horizontal:
00089 if (geometry().height() >= MINBUTTONSIZE * 2 + MARGINSIZE) {
00090 direction = Qt::Vertical;
00091 ratioToKeep = 0.5;
00092 } else {
00093 direction = Qt::Horizontal;
00094 ratioToKeep = 2;
00095 }
00096 break;
00097 default:
00098 direction = Qt::Vertical;
00099 }
00100
00101 if (direction == Qt::Horizontal) {
00102 setMinimumSize(MINBUTTONSIZE * 2 + MARGINSIZE, MINBUTTONSIZE);
00103 } else {
00104 setMinimumSize(MINBUTTONSIZE, MINBUTTONSIZE * 2 + MARGINSIZE);
00105 }
00106
00107 if (direction != m_layout->orientation()) {
00108 m_layout->setOrientation(direction);
00109 }
00110
00111 if (formFactor() == Plasma::Horizontal) {
00112 setSizePolicy(QSizePolicy(QSizePolicy::Maximum, QSizePolicy::Expanding));
00113 qreal wsize = size().height() * ratioToKeep;
00114 setMaximumSize(wsize, QWIDGETSIZE_MAX);
00115 } else if (formFactor() == Plasma::Vertical) {
00116 setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::Maximum));
00117 qreal hsize = size().width() / ratioToKeep;
00118 setMaximumSize(QWIDGETSIZE_MAX, hsize);
00119 } else {
00120 setMaximumSize(QWIDGETSIZE_MAX, QWIDGETSIZE_MAX);
00121 }
00122 }
00123
00124 void LockOut::constraintsEvent(Plasma::Constraints constraints)
00125 {
00126 if (constraints & Plasma::FormFactorConstraint ||
00127 constraints & Plasma::SizeConstraint) {
00128 checkLayout();
00129 }
00130 }
00131
00132 void LockOut::clickLock()
00133 {
00134 kDebug()<<"LockOut:: lock clicked ";
00135
00136 #ifndef Q_OS_WIN
00137 QString interface("org.freedesktop.ScreenSaver");
00138 org::freedesktop::ScreenSaver screensaver(interface, "/ScreenSaver",
00139 QDBusConnection::sessionBus());
00140 if (screensaver.isValid()) {
00141 screensaver.Lock();
00142 }
00143 #else
00144 LockWorkStation();
00145 #endif // !Q_OS_WIN
00146 }
00147
00148 void LockOut::clickLogout()
00149 {
00150 kDebug()<<"LockOut:: logout clicked ";
00151 #ifndef Q_OS_WIN
00152 KWorkSpace::requestShutDown( KWorkSpace::ShutdownConfirmDefault,
00153 KWorkSpace::ShutdownTypeDefault,
00154 KWorkSpace::ShutdownModeDefault);
00155 #endif
00156 }
00157
00158
00159 #include "lockout.moc"