Commit 1ee1e599 authored by Alexander Volkov's avatar Alexander Volkov
Browse files

Port PowerManagement data engine and PowerDevilRunner away from kdelibs4support

parent b8e48356
......@@ -20,7 +20,6 @@ target_link_libraries(plasma_engine_powermanagement
KF5::Service
Qt5::DBus
PW::KWorkspace
KF5::KDELibs4Support
)
kcoreaddons_desktop_to_json(plasma_engine_powermanagement plasma-dataengine-powermanagement.desktop)
......
......@@ -21,11 +21,13 @@
#include "powermanagementengine.h"
// kde-workspace/libs
#include <sessionmanagement.h>
//solid specific includes
#include <solid/devicenotifier.h>
#include <solid/device.h>
#include <solid/deviceinterface.h>
#include <solid/powermanagement.h>
#include <klocalizedstring.h>
#include <KAuthorized>
......@@ -51,6 +53,7 @@ Q_DECLARE_METATYPE(InhibitionInfo)
PowermanagementEngine::PowermanagementEngine(QObject* parent, const QVariantList& args)
: Plasma::DataEngine(parent, args)
, m_sources(basicSourceNames())
, m_session(new SessionManagement(this))
{
Q_UNUSED(args)
qDBusRegisterMetaType<QList<InhibitionInfo>>();
......@@ -229,17 +232,25 @@ bool PowermanagementEngine::sourceRequestEvent(const QString &name)
m_sources = basicSourceNames() + batterySources;
} else if (name == QLatin1String("AC Adapter")) {
connect(Solid::PowerManagement::notifier(), &Solid::PowerManagement::Notifier::appShouldConserveResourcesChanged,
this, &PowermanagementEngine::updateAcPlugState);
updateAcPlugState(Solid::PowerManagement::appShouldConserveResources());
QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.PowerManagement"),
QStringLiteral("/org/freedesktop/PowerManagement"),
QStringLiteral("org.freedesktop.PowerManagement"),
QStringLiteral("PowerSaveStatusChanged"),
this, SLOT(updateAcPlugState(bool)));
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement"),
QStringLiteral("/org/freedesktop/PowerManagement"),
QStringLiteral("org.freedesktop.PowerManagement"),
QStringLiteral("GetPowerSaveStatus"));
QDBusReply<bool> reply = QDBusConnection::sessionBus().call(msg);
updateAcPlugState(reply.isValid() ? reply.value() : false);
} else if (name == QLatin1String("Sleep States")) {
const QSet<Solid::PowerManagement::SleepState> sleepstates = Solid::PowerManagement::supportedSleepStates();
setData(QStringLiteral("Sleep States"), QStringLiteral("Standby"), sleepstates.contains(Solid::PowerManagement::StandbyState));
setData(QStringLiteral("Sleep States"), QStringLiteral("Suspend"), sleepstates.contains(Solid::PowerManagement::SuspendState));
setData(QStringLiteral("Sleep States"), QStringLiteral("Hibernate"), sleepstates.contains(Solid::PowerManagement::HibernateState));
setData(QStringLiteral("Sleep States"), QStringLiteral("HybridSuspend"), sleepstates.contains(Solid::PowerManagement::HybridSuspendState));
setData(QStringLiteral("Sleep States"), QStringLiteral("LockScreen"), KAuthorized::authorizeAction(QStringLiteral("lock_screen")));
setData(QStringLiteral("Sleep States"), QStringLiteral("Logout"), KAuthorized::authorize(QStringLiteral("logout")));
setData(QStringLiteral("Sleep States"), QStringLiteral("Standby"), m_session->canSuspend());
setData(QStringLiteral("Sleep States"), QStringLiteral("Suspend"), m_session->canSuspend());
setData(QStringLiteral("Sleep States"), QStringLiteral("Hibernate"), m_session->canHibernate());
setData(QStringLiteral("Sleep States"), QStringLiteral("HybridSuspend"), m_session->canHybridSuspend());
setData(QStringLiteral("Sleep States"), QStringLiteral("LockScreen"), m_session->canLock());
setData(QStringLiteral("Sleep States"), QStringLiteral("Logout"), m_session->canLogout());
} else if (name == QLatin1String("PowerDevil")) {
QDBusMessage screenMsg = QDBusMessage::createMethodCall(SOLID_POWERMANAGEMENT_SERVICE,
QStringLiteral("/org/kde/Solid/PowerManagement/Actions/BrightnessControl"),
......
......@@ -29,6 +29,8 @@
#include <QHash>
#include <QPair>
class SessionManagement;
using InhibitionInfo = QPair<QString, QString>;
......@@ -81,6 +83,8 @@ private:
QHash<QString, QString> m_batterySources; // <udi, Battery0>
QHash<QString, QPair<QString, QString>> m_applicationInfo; // <appname, <pretty name, icon>>
SessionManagement *m_session;
};
......
......@@ -30,8 +30,6 @@
#include "powermanagementjob.h"
#include <Solid/PowerManagement>
PowerManagementJob::PowerManagementJob(const QString &operation, QMap<QString, QVariant> &parameters, QObject *parent)
: ServiceJob(parent->objectName(), operation, parameters, parent)
, m_session(new SessionManagement(this))
......@@ -106,16 +104,40 @@ void PowerManagementJob::start()
setResult(false);
return;
} else if (operation == QLatin1String("beginSuppressingSleep")) {
setResult(Solid::PowerManagement::beginSuppressingSleep(parameters().value(QStringLiteral("reason")).toString()));
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("Inhibit"));
msg << QCoreApplication::applicationName() << parameters().value(QStringLiteral("reason")).toString();
QDBusReply<uint> reply = QDBusConnection::sessionBus().call(msg);
setResult(reply.isValid() ? reply.value() : -1);
return;
} else if (operation == QLatin1String("stopSuppressingSleep")) {
setResult(Solid::PowerManagement::stopSuppressingSleep(parameters().value(QStringLiteral("cookie")).toInt()));
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("UnInhibit"));
msg << parameters().value(QStringLiteral("cookie")).toInt();
QDBusReply<void> reply = QDBusConnection::sessionBus().call(msg);
setResult(reply.isValid());
return;
} else if (operation == QLatin1String("beginSuppressingScreenPowerManagement")) {
setResult(Solid::PowerManagement::beginSuppressingScreenPowerManagement(parameters().value(QStringLiteral("reason")).toString()));
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
QStringLiteral("/ScreenSaver"),
QStringLiteral("org.freedesktop.ScreenSaver"),
QStringLiteral("Inhibit"));
msg << QCoreApplication::applicationName() << parameters().value(QStringLiteral("reason")).toString();
QDBusReply<uint> reply = QDBusConnection::sessionBus().call(msg);
setResult(reply.isValid() ? reply.value() : -1);
return;
} else if (operation == QLatin1String("stopSuppressingScreenPowerManagement")) {
setResult(Solid::PowerManagement::stopSuppressingScreenPowerManagement(parameters().value(QStringLiteral("cookie")).toInt()));
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.ScreenSaver"),
QStringLiteral("/ScreenSaver"),
QStringLiteral("org.freedesktop.ScreenSaver"),
QStringLiteral("UnInhibit"));
msg << parameters().value(QStringLiteral("cookie")).toInt();
QDBusReply<uint> reply = QDBusConnection::sessionBus().call(msg);
setResult(reply.isValid());
return;
} else if (operation == QLatin1String("setBrightness")) {
auto pending = setScreenBrightness(parameters().value(QStringLiteral("brightness")).toInt(), parameters().value(QStringLiteral("silent")).toBool());
......
......@@ -10,6 +10,6 @@ set(krunner_powerdevil_SRCS
add_library(krunner_powerdevil MODULE ${krunner_powerdevil_SRCS})
kcoreaddons_desktop_to_json(krunner_powerdevil plasma-runner-powerdevil.desktop)
target_link_libraries(krunner_powerdevil Qt5::DBus KF5::ConfigCore KF5::I18n KF5::Plasma KF5::Runner KF5::KDELibs4Support)
target_link_libraries(krunner_powerdevil Qt5::DBus KF5::ConfigCore KF5::I18n KF5::Plasma KF5::Runner PW::KWorkspace)
install(TARGETS krunner_powerdevil DESTINATION "${KDE_INSTALL_PLUGINDIR}/kf5/krunner")
......@@ -20,6 +20,9 @@
#include "PowerDevilRunner.h"
// kde-workspace/libs
#include <sessionmanagement.h>
#include <QDBusInterface>
#include <QDBusReply>
#include <QDBusConnectionInterface>
......@@ -29,12 +32,11 @@
#include <KSharedConfig>
#include <KLocalizedString>
#include <Solid/PowerManagement>
K_EXPORT_PLASMA_RUNNER_WITH_JSON(PowerDevilRunner, "plasma-runner-powerdevil.json")
PowerDevilRunner::PowerDevilRunner(QObject *parent, const KPluginMetaData &metaData, const QVariantList &args)
: Plasma::AbstractRunner(parent, metaData, args)
, m_session(new SessionManagement(this))
{
setObjectName(QStringLiteral("PowerDevil"));
updateStatus();
......@@ -58,16 +60,14 @@ void PowerDevilRunner::updateSyntaxes()
i18n("Lists system suspend (e.g. sleep, hibernate) options "
"and allows them to be activated")));
QSet< Solid::PowerManagement::SleepState > states = Solid::PowerManagement::supportedSleepStates();
if (states.contains(Solid::PowerManagement::SuspendState)) {
if (m_session->canSuspend()) {
Plasma::RunnerSyntax sleepSyntax(i18nc("Note this is a KRunner keyword", "sleep"),
i18n("Suspends the system to RAM"));
sleepSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "to ram"));
syntaxes.append(sleepSyntax);
}
if (states.contains(Solid::PowerManagement::HibernateState)) {
if (m_session->canHibernate()) {
Plasma::RunnerSyntax hibernateSyntax(i18nc("Note this is a KRunner keyword", "hibernate"),
i18n("Suspends the system to disk"));
hibernateSyntax.addExampleQuery(i18nc("Note this is a KRunner keyword", "to disk"));
......@@ -117,6 +117,13 @@ bool PowerDevilRunner::parseQuery(const QString& query, const QList<QRegExp>& rx
return false;
}
enum SleepState {
StandbyState = 1,
SuspendState = 2,
HibernateState = 4,
HybridSuspendState = 8
};
void PowerDevilRunner::match(Plasma::RunnerContext &context)
{
const QString term = context.query();
......@@ -162,21 +169,19 @@ void PowerDevilRunner::match(Plasma::RunnerContext &context)
matches.append(match2);
}
} else if (term.compare(i18nc("Note this is a KRunner keyword", "sleep"), Qt::CaseInsensitive) == 0) {
QSet< Solid::PowerManagement::SleepState > states = Solid::PowerManagement::supportedSleepStates();
if (states.contains(Solid::PowerManagement::SuspendState)) {
addSuspendMatch(Solid::PowerManagement::SuspendState, matches);
if (m_session->canSuspend()) {
addSuspendMatch(SuspendState, matches);
}
if (states.contains(Solid::PowerManagement::HibernateState)) {
addSuspendMatch(Solid::PowerManagement::HibernateState, matches);
if (m_session->canHibernate()) {
addSuspendMatch(HibernateState, matches);
}
} else if (term.compare(i18nc("Note this is a KRunner keyword", "suspend"), Qt::CaseInsensitive) == 0 ||
term.compare(i18nc("Note this is a KRunner keyword", "to ram"), Qt::CaseInsensitive) == 0) {
addSuspendMatch(Solid::PowerManagement::SuspendState, matches);
addSuspendMatch(SuspendState, matches);
} else if (term.compare(i18nc("Note this is a KRunner keyword", "hibernate"), Qt::CaseInsensitive) == 0 ||
term.compare(i18nc("Note this is a KRunner keyword", "to disk"), Qt::CaseInsensitive) == 0) {
addSuspendMatch(Solid::PowerManagement::HibernateState, matches);
addSuspendMatch(HibernateState, matches);
}
context.addMatches(matches);
......@@ -187,15 +192,15 @@ void PowerDevilRunner::addSuspendMatch(int value, QList<Plasma::QueryMatch> &mat
Plasma::QueryMatch match(this);
match.setType(Plasma::QueryMatch::ExactMatch);
switch ((Solid::PowerManagement::SleepState)value) {
case Solid::PowerManagement::SuspendState:
case Solid::PowerManagement::StandbyState:
switch ((SleepState)value) {
case SuspendState:
case StandbyState:
match.setIconName(QStringLiteral("system-suspend"));
match.setText(i18nc("Suspend to RAM", "Sleep"));
match.setSubtext(i18n("Suspend to RAM"));
match.setRelevance(1);
break;
case Solid::PowerManagement::HibernateState:
case HibernateState:
match.setIconName(QStringLiteral("system-suspend-hibernate"));
match.setText(i18nc("Suspend to disk", "Hibernate"));
match.setSubtext(i18n("Suspend to disk"));
......@@ -228,13 +233,13 @@ void PowerDevilRunner::run(const Plasma::RunnerContext &context, const Plasma::Q
QDBusReply<int> brightness = brightnessIface.asyncCall(QStringLiteral("brightness"));
brightnessIface.asyncCall(QStringLiteral("setBrightness"), static_cast<int>(brightness / 2));
} else if (match.id().startsWith(QLatin1String("PowerDevil_Sleep"))) {
switch ((Solid::PowerManagement::SleepState)match.data().toInt()) {
case Solid::PowerManagement::SuspendState:
case Solid::PowerManagement::StandbyState:
Solid::PowerManagement::requestSleep(Solid::PowerManagement::SuspendState, nullptr, nullptr);
switch ((SleepState)match.data().toInt()) {
case SuspendState:
case StandbyState:
m_session->suspend();
break;
case Solid::PowerManagement::HibernateState:
Solid::PowerManagement::requestSleep(Solid::PowerManagement::HibernateState, nullptr, nullptr);
case HibernateState:
m_session->hibernate();
break;
}
}
......
......@@ -23,6 +23,8 @@
#include <KRunner/AbstractRunner>
#include <QDBusConnection>
class SessionManagement;
class PowerDevilRunner : public Plasma::AbstractRunner
{
Q_OBJECT
......@@ -41,6 +43,8 @@ class PowerDevilRunner : public Plasma::AbstractRunner
void updateSyntaxes();
void addSuspendMatch(int value, QList<Plasma::QueryMatch> &matches);
bool parseQuery(const QString& query, const QList<QRegExp>& rxList, QString& parameter) const;
SessionManagement *m_session;
};
#endif
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