1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
|
/*****************************************************************
Copyright (c) 2001 Carsten Pfeiffer <pfeiffer@kde.org>
2001 Matthias Elter <elter@kde.org>
2001 Martijn Klingens <mklingens@yahoo.com>
KShutDown Branch:
2005 Stephen Ellwood
2005 Konrad Twardowski <kdtonline@poczta.onet.pl>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************/
// TODO: 0.9: update handbook
#include "lockout.h"
#include <qlayout.h>
#include <qtoolbutton.h>
#include <qtooltip.h>
#include <dcopclient.h>
#include <kapplication.h>
#include <kglobal.h>
#include <kiconloader.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <kprocess.h>
// TODO: 0.9: more buttons (reboot, logout, etc)
// plugin
extern "C"
{
KDE_EXPORT KPanelApplet *init(QWidget *parent, const QString& configFile)
{
// share i18n with KShutDown
KGlobal::locale()->insertCatalogue("kshutdown");
return new Lockout(configFile, parent);
}
}
// public
Lockout::Lockout(const QString& configFile, QWidget *parent)
: KPanelApplet(
configFile,
KPanelApplet::Normal,
0, // no standard actions
parent,
"kshutdownlockout"
)
{
// read configuration
KConfig *conf = config();
conf->setGroup("kshutdownlockout");
_showLockButton = conf->readBoolEntry("ShowLockButton", false);
_transparent = conf->readBoolEntry("Transparent", true);
// init widget
setBackgroundOrigin(AncestorOrigin);
setFrameStyle(Panel | Sunken);
// init layout
_layout = new QBoxLayout(
this,
(orientation() == Horizontal)
? QBoxLayout::TopToBottom
: QBoxLayout::LeftToRight
);
_layout->setAutoAdd(true);
_layout->setMargin(0);
_layout->setSpacing(0);
// init buttons
_logoutButton = addButton("kshutdown", i18n("Run KShutDown"));
connect(_logoutButton, SIGNAL(clicked()), SLOT(slotLogout()));
_lockButton = addButton("lock", i18n("Lock Session"));
connect(_lockButton, SIGNAL(clicked()), SLOT(slotLock()));
if (_showLockButton)
_lockButton->show();
else
_lockButton->hide();
// TODO: 0.9: Kiosk
if (!kapp->dcopClient()->isAttached())
kapp->dcopClient()->attach();
connect(kapp, SIGNAL(iconChanged(int)), SLOT(slotIconChanged()));
}
Lockout::~Lockout()
{
KGlobal::locale()->removeCatalogue("kshutdown");
}
int Lockout::heightForWidth(int width) const
{
checkLayout(width);
return sizeHint().height();
}
int Lockout::widthForHeight(int height) const
{
checkLayout(height);
return sizeHint().width();
}
// protected
bool Lockout::eventFilter(QObject */* o */, QEvent *e)
{
if (e->type() != QEvent::MouseButtonPress)
return false;
QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e);
if (mouseEvent->button() != QMouseEvent::RightButton)
return false;
if (!kapp->authorizeKAction("kicker_rmb"))
return false;
KConfig *conf = config();
conf->setGroup("kshutdownlockout");
KPopupMenu *popup = new KPopupMenu();
popup->insertTitle("KShutDown");
// run kshutdown
popup->insertItem(
SmallIcon("kshutdown"), i18n("&Run KShutDown"),
this, SLOT(slotLogout())
);
// lock session
popup->insertItem(
SmallIcon("lock"), i18n("&Lock Session"),
this, SLOT(slotLock())
);
// -
popup->insertSeparator();
// transparent
int transparentItem = popup->insertItem(i18n("&Transparent"));
popup->connectItem(transparentItem, this, SLOT(slotTransparent()));
popup->setItemChecked(transparentItem, _transparent);
// show lock button
int showLockButtonItem = popup->insertItem(i18n("&Show Lock Button"));
popup->connectItem(showLockButtonItem, this, SLOT(slotShowLockButton()));
popup->setItemChecked(showLockButtonItem, _showLockButton);
// configure kshutdown
popup->insertItem(
SmallIcon("configure"), i18n("&Configure: KShutDown..."),
this, SLOT(slotConfigure())
);
popup->exec(mouseEvent->globalPos());
delete popup;
return true;
}
void Lockout::mouseDoubleClickEvent(QMouseEvent* e)
{
propagateMouseEvent(e);
}
void Lockout::mouseMoveEvent(QMouseEvent* e)
{
propagateMouseEvent(e);
}
void Lockout::mousePressEvent(QMouseEvent* e)
{
propagateMouseEvent(e);
}
void Lockout::mouseReleaseEvent(QMouseEvent* e)
{
propagateMouseEvent(e);
}
// private
QToolButton *Lockout::addButton(const QString &icon, const QString &text)
{
QToolButton *b = new QToolButton(this, icon); // icon = widget name
b->installEventFilter(this);
b->setAutoRaise(_transparent);
b->setBackgroundMode(X11ParentRelative);
b->setPixmap(SmallIcon(icon));
b->setMinimumSize(b->pixmap()->size());
b->setSizePolicy(QSizePolicy(QSizePolicy::MinimumExpanding, QSizePolicy::MinimumExpanding));
QToolTip::add(b, text);
return b;
}
void Lockout::checkLayout(int height) const
{
// the -2 is due to kicker allowing us a width/height of 42 and the buttons
// having a size of 44. So we rather cut those 2 pixels instead of changing
// direction and wasting a lot of space.
QSize hint = minimumSizeHint();
QBoxLayout::Direction direction = _layout->direction();
if (
(direction == QBoxLayout::LeftToRight) &&
(
((orientation() == Vertical) && (hint.width() - 2 >= height)) ||
((orientation() == Horizontal) && (hint.width() - 2 < height))
)
)
{
_layout->setDirection(QBoxLayout::TopToBottom);
}
else if (
(direction == QBoxLayout::TopToBottom) &&
(
((orientation() == Vertical) && (hint.height() - 2 < height)) ||
((orientation() == Horizontal) && (hint.height() - 2 >= height))
)
)
{
_layout->setDirection(QBoxLayout::LeftToRight);
}
}
void Lockout::propagateMouseEvent(const QMouseEvent* e) const
{
if (isTopLevel())
return;
QMouseEvent mouseEvent(
e->type(),
mapTo(topLevelWidget(), e->pos()),
e->globalPos(),
e->button(),
e->state()
);
QApplication::sendEvent(topLevelWidget(), &mouseEvent);
}
void Lockout::runKShutDown() const
{
KProcess *p = new KProcess();
if (!p)
{
KMessageBox::error(0, i18n("Could not run KShutDown!"));
return;
}
*p << "kshutdown";
*p << "--init";
if (!p->start(KProcess::Block))
KMessageBox::error(0, i18n("Could not run KShutDown!"));
delete p;
}
// private slots
void Lockout::slotConfigure()
{
DCOPClient *client = kapp->dcopClient();
if (!client->isApplicationRegistered("kshutdown"))
runKShutDown();
client->send("kshutdown", "KShutdownIface", "configure()", "");
}
void Lockout::slotIconChanged()
{
_lockButton->setPixmap(SmallIcon("lock"));
_logoutButton->setPixmap(SmallIcon("kshutdown"));
}
void Lockout::slotLock()
{
DCOPClient *client = kapp->dcopClient();
if (!client->isApplicationRegistered("kshutdown"))
runKShutDown();
client->send("kshutdown", "KShutdownIface", "lockScreen()", "");
}
void Lockout::slotLogout()
{
DCOPClient *client = kapp->dcopClient();
if (!client->isApplicationRegistered("kshutdown"))
runKShutDown();
client->send("kshutdown", "KShutdownIface", "makeVisible()", "");
}
void Lockout::slotShowLockButton()
{
_showLockButton = !_showLockButton;
if (_showLockButton)
_lockButton->show();
else
_lockButton->hide();
// save config
KConfig* conf = config();
conf->setGroup("kshutdownlockout");
conf->writeEntry("ShowLockButton", _showLockButton);
conf->sync();
}
void Lockout::slotTransparent()
{
_transparent = !_transparent;
_lockButton->setAutoRaise(_transparent);
_logoutButton->setAutoRaise(_transparent);
// save config
KConfig* conf = config();
conf->setGroup("kshutdownlockout");
conf->writeEntry("Transparent", _transparent);
conf->sync();
}
|