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
|
/*
SPDX-FileCopyrightText: 2015 Kai Uwe Broulik <kde@privat.broulik.de>
SPDX-License-Identifier: MIT
*/
#include "sessionsmodel.h"
#include <utility>
#include <KAuthorized>
#include <KLocalizedString>
#include <KUser>
#include "kscreensaversettings.h"
#include "screensaver_interface.h"
SessionsModel::SessionsModel(QObject *parent)
: QAbstractListModel(parent)
, m_screensaverInterface(
new org::freedesktop::ScreenSaver(QStringLiteral("org.freedesktop.ScreenSaver"), QStringLiteral("/ScreenSaver"), QDBusConnection::sessionBus(), this))
{
reload();
// wait for the screen locker to be ready before actually switching
connect(m_screensaverInterface, &org::freedesktop::ScreenSaver::ActiveChanged, this, [this](bool active) {
if (active) {
if (m_pendingVt) {
m_displayManager.switchVT(m_pendingVt);
Q_EMIT switchedUser(m_pendingVt);
} else if (m_pendingReserve) {
m_displayManager.startReserve();
Q_EMIT startedNewSession();
}
m_pendingVt = 0;
m_pendingReserve = false;
}
});
}
bool SessionsModel::canSwitchUser() const
{
return const_cast<SessionsModel *>(this)->m_displayManager.isSwitchable() && KAuthorized::authorizeAction(QStringLiteral("switch_user"));
}
bool SessionsModel::canStartNewSession() const
{
return const_cast<SessionsModel *>(this)->m_displayManager.numReserve() > 0 && KAuthorized::authorizeAction(QStringLiteral("start_new_session"));
}
bool SessionsModel::shouldLock() const
{
return m_shouldLock;
}
bool SessionsModel::includeUnusedSessions() const
{
return m_includeUnusedSessions;
}
void SessionsModel::setIncludeUnusedSessions(bool includeUnusedSessions)
{
if (m_includeUnusedSessions != includeUnusedSessions) {
m_includeUnusedSessions = includeUnusedSessions;
reload();
Q_EMIT includeUnusedSessionsChanged();
}
}
void SessionsModel::switchUser(int vt, bool shouldLock)
{
if (vt < 0) {
startNewSession(shouldLock);
return;
}
if (!canSwitchUser()) {
return;
}
if (!shouldLock) {
m_displayManager.switchVT(vt);
Q_EMIT switchedUser(vt);
return;
}
checkScreenLocked([this, vt](bool locked) {
if (locked) {
// already locked, switch right away
m_displayManager.switchVT(vt);
Q_EMIT switchedUser(vt);
} else {
m_pendingReserve = false;
m_pendingVt = vt;
Q_EMIT aboutToLockScreen();
m_screensaverInterface->Lock();
}
});
}
void SessionsModel::startNewSession(bool shouldLock)
{
if (!canStartNewSession()) {
return;
}
if (!shouldLock) {
m_displayManager.startReserve();
Q_EMIT startedNewSession();
return;
}
checkScreenLocked([this](bool locked) {
if (locked) {
// already locked, switch right away
m_displayManager.startReserve();
Q_EMIT startedNewSession();
} else {
m_pendingReserve = true;
m_pendingVt = 0;
Q_EMIT aboutToLockScreen();
m_screensaverInterface->Lock();
}
});
}
void SessionsModel::reload()
{
static QHash<QString, KUser> kusers;
const bool oldShouldLock = m_shouldLock;
m_shouldLock = KAuthorized::authorizeAction(QStringLiteral("lock_screen")) && KScreenSaverSettings::autolock();
if (m_shouldLock != oldShouldLock) {
Q_EMIT shouldLockChanged();
}
SessList sessions;
m_displayManager.localSessions(sessions);
const int oldCount = m_data.count();
beginResetModel();
m_data.clear();
m_data.reserve(sessions.count());
for (const SessEnt &session : std::as_const(sessions)) {
if (!session.vt || session.self) {
continue;
}
if (!m_includeUnusedSessions && session.session.isEmpty()) {
continue;
}
SessionEntry entry;
entry.name = session.user;
entry.displayNumber = session.display;
entry.vtNumber = session.vt;
entry.session = session.session;
entry.isTty = session.tty;
auto it = kusers.constFind(session.user);
if (it != kusers.constEnd()) {
entry.realName = it->property(KUser::FullName).toString();
entry.icon = it->faceIconPath();
} else {
KUser user(session.user);
entry.realName = user.property(KUser::FullName).toString();
entry.icon = user.faceIconPath();
kusers.insert(session.user, user);
}
m_data.append(entry);
}
endResetModel();
if (oldCount != m_data.count()) {
Q_EMIT countChanged();
}
}
void SessionsModel::checkScreenLocked(const std::function<void(bool)> &cb)
{
auto reply = m_screensaverInterface->GetActive();
QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [cb](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<bool> reply = *watcher;
if (!reply.isError()) {
cb(reply.value());
}
watcher->deleteLater();
});
}
void SessionsModel::setShowNewSessionEntry(bool showNewSessionEntry)
{
if (!canStartNewSession()) {
return;
}
if (showNewSessionEntry == m_showNewSessionEntry) {
return;
}
int row = m_data.size();
if (showNewSessionEntry) {
beginInsertRows(QModelIndex(), row, row);
m_showNewSessionEntry = showNewSessionEntry;
endInsertRows();
} else {
beginRemoveRows(QModelIndex(), row, row);
m_showNewSessionEntry = showNewSessionEntry;
endRemoveRows();
}
Q_EMIT countChanged();
}
QVariant SessionsModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() > rowCount(QModelIndex())) {
return QVariant();
}
if (index.row() == m_data.count()) {
switch (role) {
case RealNameRole:
return i18n("New Session");
case IconNameRole:
return QStringLiteral("system-switch-user");
case NameRole:
return i18n("New Session");
case DisplayNumberRole:
return 0; // NA
case VtNumberRole:
return -1; // an invalid VtNumber - which we'll use to indicate it's to start a new session
case SessionRole:
return 0; // NA
case IsTtyRole:
return false; // NA
default:
return QVariant();
}
}
const SessionEntry &item = m_data.at(index.row());
switch (role) {
case RealNameRole:
return item.realName;
case IconRole:
return item.icon;
case NameRole:
return item.name;
case DisplayNumberRole:
return item.displayNumber;
case VtNumberRole:
return item.vtNumber;
case SessionRole:
return item.session;
case IsTtyRole:
return item.isTty;
default:
return QVariant();
}
}
int SessionsModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return m_data.count() + (m_showNewSessionEntry ? 1 : 0);
}
QHash<int, QByteArray> SessionsModel::roleNames() const
{
QHash<int, QByteArray> roleNames;
roleNames[NameRole] = QByteArrayLiteral("name");
roleNames[RealNameRole] = QByteArrayLiteral("realName");
roleNames[IconRole] = QByteArrayLiteral("icon");
roleNames[IconNameRole] = QByteArrayLiteral("iconName");
roleNames[DisplayNumberRole] = QByteArrayLiteral("displayNumber");
roleNames[VtNumberRole] = QByteArrayLiteral("vtNumber");
roleNames[SessionRole] = QByteArrayLiteral("session");
roleNames[IsTtyRole] = QByteArrayLiteral("isTty");
return roleNames;
}
|