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
|
/***************************************************************************
* Copyright (C) 2010 by Flavio Castelli <flavio@castelli.name> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . *
***************************************************************************/
#include "sessionwidget.h"
#include "common.h"
#include <QtCore/QList>
#include <QtCore/QSignalMapper>
#include <QtCore/QVariant>
#include <QtDBus/QDBusInterface>
#include <QtGui/QLabel>
#include <QtGui/QGraphicsLinearLayout>
#include <QtGui/QShowEvent>
#include <kiconloader.h>
#include <kuser.h>
#include <kworkspace/kdisplaymanager.h>
#include <plasma/widgets/iconwidget.h>
bool operator <(const Plasma::IconWidget& a, const Plasma::IconWidget& b)
{
return (a.text() < b.text());
}
static Plasma::IconWidget *createButton(QGraphicsWidget *parent)
{
Plasma::IconWidget *button = new Plasma::IconWidget(parent);
button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);
button->setOrientation(Qt::Horizontal);
button->setDrawBackground(true);
button->setTextBackgroundColor(QColor(Qt::transparent));
return button;
}
SessionWidget::SessionWidget( QGraphicsItem * parent, Qt::WindowFlags wFlags)
: QGraphicsWidget ( parent, wFlags)
{
this->setFocusPolicy(Qt::ClickFocus);
m_signalMapper = new QSignalMapper(this);
QGraphicsLinearLayout* mainLayout = new QGraphicsLinearLayout(Qt::Vertical);
mainLayout->setSpacing(0);
mainLayout->addStretch();
this->setLayout(mainLayout);
connect( m_signalMapper, SIGNAL(mapped(int)),
this, SLOT(slotSwitchSession(int)));
Q_ASSERT(mainLayout->count() == 0);
//Cache the icon pixmaps
QSize iconSize = QSize(KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium);
SessList sessions;
KDisplayManager manager;
manager.localSessions(sessions);
QList<Plasma::IconWidget*> entries;
foreach(SessEnt session, sessions) {
QPixmap pixmap;
KUser user (session.user);
if (!user.isValid())
continue;
if (session.tty)
continue;
Plasma::IconWidget* entry = createButton(this);
pixmap = getUserIcon(user);
entry->setIcon(pixmap);
QString username = getUsername(false, user);
entry->setText(username);
connect(entry, SIGNAL(clicked()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(entry, session.vt);
if (session.self)
entry->setEnabled(false);
entries << entry;
}
// sort the entries alphabetically
qSort(entries);
// add "Login window"
Plasma::IconWidget* entry = createButton(this);
entry->setText(i18n("Open login window"));
connect(entry, SIGNAL(clicked()), m_signalMapper, SLOT(map()));
m_signalMapper->setMapping(entry, -1);
entries << entry;
foreach(Plasma::IconWidget* entry, entries) {
mainLayout->addItem(entry);
}
}
void SessionWidget::slotSwitchSession(int vt)
{
emit switching();
KDisplayManager manager;
if (vt == -1) {
// lock screen
QDBusInterface screensaver("org.freedesktop.ScreenSaver",
"/ScreenSaver", "org.freedesktop.ScreenSaver");
screensaver.call( "Lock" );
manager.startReserve();
} else
manager.lockSwitchVT(vt);
}
|