Commit 26a217fa authored by David Edmundson's avatar David Edmundson

Port all plasma workspace to new libkworkspace API

Summary:
ContainmentActions had some old windows code was dropped as that's
clearly unused.

logout and leave actions now enable/disable at runtime, so in future can
respond to config changes when that's added to libkworkspace.

DataEngines and the krunner code should have no visible changes

Test Plan:
Left from the desktop and from krunner
Changed the default action in ksmserver's kcm
Confirmed that changes the default leave action changed

Reviewers: #plasma, broulik

Reviewed By: #plasma, broulik

Subscribers: broulik, plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D21764
parent 80618725
......@@ -35,13 +35,8 @@
#include <Plasma/Corona>
#include "kworkspace.h"
#include <sessionmanagement.h>
#include "krunner_interface.h"
#include "screensaver_interface.h"
#ifdef Q_OS_WIN
#define _WIN32_WINNT 0x0500 // require NT 5.0 (win 2k pro)
#include <windows.h>
#endif // Q_OS_WIN
ContextMenu::ContextMenu(QObject *parent, const QVariantList &args)
: Plasma::ContainmentActions(parent, args),
......@@ -51,7 +46,8 @@ ContextMenu::ContextMenu(QObject *parent, const QVariantList &args)
m_separator1(nullptr),
m_separator2(nullptr),
m_separator3(nullptr),
m_buttons(nullptr)
m_buttons(nullptr),
m_session(new SessionManagement(this))
{
}
......@@ -101,11 +97,20 @@ void ContextMenu::restore(const KConfigGroup &config)
m_lockScreenAction = new QAction(i18nc("plasma_containmentactions_contextmenu", "Lock Screen"), this);
m_lockScreenAction->setIcon(QIcon::fromTheme(QStringLiteral("system-lock-screen")));
m_lockScreenAction->setShortcut(KGlobalAccel::self()->globalShortcut(QStringLiteral("ksmserver"), QStringLiteral("Lock Session")).value(0));
connect(m_lockScreenAction, &QAction::triggered, this, &ContextMenu::lockScreen);
m_lockScreenAction->setEnabled(m_session->canLock());
connect(m_session, &SessionManagement::canLockChanged, this, [this]() {
m_lockScreenAction->setEnabled(m_session->canLock());
});
connect(m_lockScreenAction, &QAction::triggered, m_session, &SessionManagement::lock);
m_logoutAction = new QAction(i18nc("plasma_containmentactions_contextmenu", "Leave..."), this);
m_logoutAction->setIcon(QIcon::fromTheme(QStringLiteral("system-log-out")));
m_logoutAction->setShortcut(KGlobalAccel::self()->globalShortcut(QStringLiteral("ksmserver"), QStringLiteral("Log Out")).value(0));
m_logoutAction->setEnabled(m_session->canLogout());
connect(m_session, &SessionManagement::canLogoutChanged, this, [this]() {
m_logoutAction->setEnabled(m_session->canLogout());
});
connect(m_logoutAction, &QAction::triggered, this, &ContextMenu::startLogout);
m_separator1 = new QAction(this);
......@@ -208,47 +213,21 @@ void ContextMenu::runCommand()
krunner.display();
}
void ContextMenu::lockScreen()
{
if (!KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
return;
}
#ifndef Q_OS_WIN
QString interface(QStringLiteral("org.freedesktop.ScreenSaver"));
org::freedesktop::ScreenSaver screensaver(interface, QStringLiteral("/ScreenSaver"),
QDBusConnection::sessionBus());
if (screensaver.isValid()) {
screensaver.Lock();
}
#else
LockWorkStation();
#endif // !Q_OS_WIN
}
void ContextMenu::startLogout()
{
// this short delay is due to two issues:
// a) KWorkSpace's DBus alls are all synchronous
// b) the destruction of the menu that this action is in is delayed
//
// (a) leads to the menu hanging out where everyone can see it because
// the even loop doesn't get returned to allowing it to close.
//
// (b) leads to a 0ms timer not working since a 0ms timer just appends to
// the event queue, and then the menu closing event gets appended to that.
//
// ergo a timer with small timeout
QTimer::singleShot(10, this, &ContextMenu::logout);
}
void ContextMenu::logout()
{
if (!KAuthorized::authorizeAction(QStringLiteral("logout"))) {
return;
KConfig config(QStringLiteral("ksmserverrc"));
const auto group = config.group("General");
switch (group.readEntry("shutdownType", int(KWorkSpace::ShutdownTypeNone))) {
case int(KWorkSpace::ShutdownTypeHalt):
m_session->requestShutdown();
break;
case int(KWorkSpace::ShutdownTypeReboot):
m_session->requestReboot();
break;
default:
m_session->requestLogout();
break;
}
KWorkSpace::requestShutDown();
}
QWidget* ContextMenu::createConfigurationInterface(QWidget* parent)
......
......@@ -23,6 +23,8 @@
#include <QButtonGroup>
#include <plasma/containmentactions.h>
class SessionManagement;
class ContextMenu : public Plasma::ContainmentActions
{
Q_OBJECT
......@@ -41,9 +43,7 @@ public:
public Q_SLOTS:
void runCommand();
void lockScreen();
void startLogout();
void logout();
private:
QAction *m_runCommandAction;
......@@ -57,6 +57,7 @@ private:
QHash<QString, bool> m_actions;
QStringList m_actionOrder;
QButtonGroup *m_buttons;
SessionManagement *m_session;
};
#endif
......@@ -24,7 +24,7 @@
#include <KAuthorized>
// kde-workspace/libs
#include <kworkspace.h>
#include <sessionmanagement.h>
#include <krunner_interface.h>
......@@ -32,8 +32,9 @@
#include <Solid/PowerManagement>
PowerManagementJob::PowerManagementJob(const QString &operation, QMap<QString, QVariant> &parameters, QObject *parent) :
ServiceJob(parent->objectName(), operation, parameters, parent)
PowerManagementJob::PowerManagementJob(const QString &operation, QMap<QString, QVariant> &parameters, QObject *parent)
: ServiceJob(parent->objectName(), operation, parameters, parent)
, m_session(new SessionManagement(this))
{
}
......@@ -57,10 +58,8 @@ void PowerManagementJob::start()
//qDebug() << "starting operation ... " << operation;
if (operation == QLatin1String("lockScreen")) {
if (KAuthorized::authorizeAction(QStringLiteral("lock_screen"))) {
const QString interface(QStringLiteral("org.freedesktop.ScreenSaver"));
QDBusInterface screensaver(interface, QStringLiteral("/ScreenSaver"));
screensaver.asyncCall(QStringLiteral("Lock"));
if (m_session->canLock()) {
m_session->lock();
setResult(true);
return;
}
......@@ -68,26 +67,43 @@ void PowerManagementJob::start()
setResult(false);
return;
} else if (operation == QLatin1String("suspend") || operation == QLatin1String("suspendToRam")) {
Solid::PowerManagement::requestSleep(Solid::PowerManagement::SuspendState, nullptr, nullptr);
setResult(Solid::PowerManagement::supportedSleepStates().contains(Solid::PowerManagement::SuspendState));
if (m_session->canSuspend()) {
m_session->suspend();
setResult(true);
} else {
setResult(false);
}
return;
} else if (operation == QLatin1String("suspendToDisk")) {
Solid::PowerManagement::requestSleep(Solid::PowerManagement::HibernateState, nullptr, nullptr);
setResult(Solid::PowerManagement::supportedSleepStates().contains(Solid::PowerManagement::HibernateState));
if (m_session->canHibernate()) {
m_session->hibernate();
setResult(true);
} else {
setResult(false);
}
return;
} else if (operation == QLatin1String("suspendHybrid")) {
Solid::PowerManagement::requestSleep(Solid::PowerManagement::HybridSuspendState, nullptr, nullptr);
setResult(Solid::PowerManagement::supportedSleepStates().contains(Solid::PowerManagement::HybridSuspendState));
if (m_session->canHybridSuspend()) {
m_session->hybridSuspend();
setResult(true);
} else {
setResult(false);
}
return;
} else if (operation == QLatin1String("requestShutDown")) {
requestShutDown();
setResult(true);
if (m_session->canShutdown()) {
m_session->requestShutdown();
setResult(true);
} else {
setResult(false);
}
return;
} else if (operation == QLatin1String("switchUser")) {
// Taken from kickoff/core/itemhandlers.cpp
org::kde::krunner::App krunner(QStringLiteral("org.kde.krunner"), QStringLiteral("/App"), QDBusConnection::sessionBus());
krunner.switchUser();
setResult(true);
if (m_session->canSwitchUser()) {
m_session->switchUser();
setResult(true);
}
setResult(false);
return;
} else if (operation == QLatin1String("beginSuppressingSleep")) {
setResult(Solid::PowerManagement::beginSuppressingSleep(parameters().value(QStringLiteral("reason")).toString()));
......@@ -134,9 +150,3 @@ QDBusPendingCall PowerManagementJob::setKeyboardBrightness(int value, bool silen
msg << value;
return QDBusConnection::sessionBus().asyncCall(msg);
}
void PowerManagementJob::requestShutDown()
{
KWorkSpace::requestShutDown();
}
......@@ -22,6 +22,7 @@
// plasma
#include <Plasma/ServiceJob>
class SessionManagement;
class QDBusPendingCall;
class PowerManagementJob : public Plasma::ServiceJob
......@@ -37,9 +38,9 @@ class PowerManagementJob : public Plasma::ServiceJob
void start() override;
private:
void requestShutDown();
QDBusPendingCall setScreenBrightness(int value, bool silent);
QDBusPendingCall setKeyboardBrightness(int value, bool silent);
SessionManagement *m_session;
};
#endif // POWERMANAGEMENTJOB_H
......@@ -18,7 +18,6 @@
#include "sessionrunner.h"
#include <KAuthorized>
#include <QDebug>
#include <KLocalizedString>
......@@ -213,29 +212,21 @@ void SessionRunner::run(const Plasma::RunnerContext &context, const Plasma::Quer
{
Q_UNUSED(context);
if (match.data().type() == QVariant::Int) {
KWorkSpace::ShutdownType type = KWorkSpace::ShutdownTypeDefault;
switch (match.data().toInt()) {
case LogoutAction:
type = KWorkSpace::ShutdownTypeNone;
m_session.requestLogout();
break;
case RestartAction:
type = KWorkSpace::ShutdownTypeReboot;
m_session.requestReboot();
break;
case ShutdownAction:
type = KWorkSpace::ShutdownTypeHalt;
m_session.requestShutdown();
break;
case LockAction:
lock();
return;
m_session.lock();
break;
}
if (type != KWorkSpace::ShutdownTypeDefault) {
KWorkSpace::ShutdownConfirm confirm = KWorkSpace::ShutdownConfirmDefault;
KWorkSpace::requestShutDown(confirm, type);
return;
}
return;
}
if (!match.data().toString().isEmpty()) {
......@@ -262,19 +253,9 @@ void SessionRunner::run(const Plasma::RunnerContext &context, const Plasma::Quer
if (result == QMessageBox::Cancel) {
return;
}
m_session.lock();
lock();
dm.startReserve();
}
void SessionRunner::lock()
{
QString interface(QStringLiteral("org.freedesktop.ScreenSaver"));
org::freedesktop::ScreenSaver screensaver(interface, QStringLiteral("/ScreenSaver"),
QDBusConnection::sessionBus());
if (screensaver.isValid()) {
screensaver.Lock();
}
}
#include "sessionrunner.moc"
......@@ -22,6 +22,7 @@
#include <kdisplaymanager.h>
#include <krunner/abstractrunner.h>
#include <sessionmanagement.h>
/**
* This class provides matches for running sessions as well as
......@@ -41,11 +42,11 @@ class SessionRunner : public Plasma::AbstractRunner
enum { LogoutAction = 1, ShutdownAction, RestartAction, LockAction };
private:
void lock();
void matchCommands(QList<Plasma::QueryMatch> &matches, const QString& term);
QString m_triggerWord;
KDisplayManager dm;
SessionManagement m_session;
bool m_canLogout;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment