Commit db089e0d authored by Aleix Pol Gonzalez's avatar Aleix Pol Gonzalez 🐧
Browse files

Request inhibiting sleep while transactions are on

BUG: 401304
parent ce8f38b3
Pipeline #221935 passed with stage
in 1 minute and 16 seconds
......@@ -21,6 +21,7 @@ add_executable(plasma-discover ${plasma_discover_SRCS}
PaginateModel.cpp
UnityLauncher.cpp
ReadFile.cpp
PowerManagementInterface.cpp
DiscoverObject.h
DiscoverDeclarativePlugin.h
......
......@@ -63,6 +63,7 @@
#ifdef WITH_FEEDBACK
#include "plasmauserfeedback.h"
#endif
#include "PowerManagementInterface.h"
#include "discoversettings.h"
class CachedNetworkAccessManagerFactory : public QQmlNetworkAccessManagerFactory
......@@ -104,6 +105,7 @@ DiscoverObject::DiscoverObject(CompactMode mode, const QVariantMap &initialPrope
qmlRegisterType<UnityLauncher>("org.kde.discover.app", 1, 0, "UnityLauncher");
qmlRegisterType<PaginateModel>("org.kde.discover.app", 1, 0, "PaginateModel");
qmlRegisterType<FeaturedModel>("org.kde.discover.app", 1, 0, "FeaturedModel");
qmlRegisterType<PowerManagementInterface>("org.kde.discover.app", 1, 0, "PowerManagementInterface");
qmlRegisterType<OurSortFilterProxyModel>("org.kde.discover.app", 1, 0, "QSortFilterProxyModel");
#ifdef WITH_FEEDBACK
qmlRegisterSingletonType<PlasmaUserFeedback>("org.kde.discover.app", 1, 0, "UserFeedbackSettings", [](QQmlEngine *, QJSEngine *) -> QObject * {
......
/*
SPDX-FileCopyrightText: 2019 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
SPDX-License-Identifier: LGPL-3.0-or-later
*/
#include "PowerManagementInterface.h"
#include <KLocalizedString>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDBusUnixFileDescriptor>
#include <QString>
#include <QGuiApplication>
#include <QLoggingCategory>
class PowerManagementInterfacePrivate
{
public:
bool mPreventSleep = false;
bool mInhibitedSleep = false;
uint mInhibitSleepCookie = 0;
QString m_reason;
};
PowerManagementInterface::PowerManagementInterface(QObject *parent) : QObject(parent), d(std::make_unique<PowerManagementInterfacePrivate>())
{
auto sessionBus = QDBusConnection::sessionBus();
sessionBus.connect(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("HasInhibitChanged"), this, SLOT(hostSleepInhibitChanged()));
}
PowerManagementInterface::~PowerManagementInterface() = default;
bool PowerManagementInterface::preventSleep() const
{
return d->mPreventSleep;
}
bool PowerManagementInterface::sleepInhibited() const
{
return d->mInhibitedSleep;
}
void PowerManagementInterface::setPreventSleep(bool value)
{
if (d->mPreventSleep == value) {
return;
}
if (value) {
inhibitSleep();
d->mPreventSleep = true;
} else {
uninhibitSleep();
d->mPreventSleep = false;
}
Q_EMIT preventSleepChanged();
}
void PowerManagementInterface::retryInhibitingSleep()
{
if (d->mPreventSleep && !d->mInhibitedSleep) {
inhibitSleep();
}
}
void PowerManagementInterface::hostSleepInhibitChanged()
{
}
void PowerManagementInterface::inhibitDBusCallFinished(QDBusPendingCallWatcher *aWatcher)
{
QDBusPendingReply<uint> reply = *aWatcher;
if (reply.isError()) {
} else {
d->mInhibitSleepCookie = reply.argumentAt<0>();
d->mInhibitedSleep = true;
Q_EMIT sleepInhibitedChanged();
}
aWatcher->deleteLater();
}
void PowerManagementInterface::uninhibitDBusCallFinished(QDBusPendingCallWatcher *aWatcher)
{
QDBusPendingReply<> reply = *aWatcher;
if (reply.isError()) {
qDebug() << "PowerManagementInterface::uninhibitDBusCallFinished" << reply.error();
} else {
d->mInhibitedSleep = false;
Q_EMIT sleepInhibitedChanged();
}
aWatcher->deleteLater();
}
void PowerManagementInterface::inhibitSleep()
{
auto sessionBus = QDBusConnection::sessionBus();
auto inhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("Inhibit"));
inhibitCall.setArguments({{QGuiApplication::desktopFileName()}, {d->m_reason}});
auto asyncReply = sessionBus.asyncCall(inhibitCall);
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished,
this, &PowerManagementInterface::inhibitDBusCallFinished);
}
void PowerManagementInterface::uninhibitSleep()
{
auto sessionBus = QDBusConnection::sessionBus();
auto uninhibitCall = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("/org/freedesktop/PowerManagement/Inhibit"),
QStringLiteral("org.freedesktop.PowerManagement.Inhibit"),
QStringLiteral("UnInhibit"));
uninhibitCall.setArguments({{d->mInhibitSleepCookie}});
auto asyncReply = sessionBus.asyncCall(uninhibitCall);
auto replyWatcher = new QDBusPendingCallWatcher(asyncReply, this);
QObject::connect(replyWatcher, &QDBusPendingCallWatcher::finished,
this, &PowerManagementInterface::uninhibitDBusCallFinished);
}
QString PowerManagementInterface::reason() const
{
return d->m_reason;
}
void PowerManagementInterface::setReason(const QString &reason)
{
d->m_reason = reason;
}
/*
SPDX-FileCopyrightText: 2019 (c) Matthieu Gallien <matthieu_gallien@yahoo.fr>
SPDX-License-Identifier: LGPL-3.0-or-later
*/
#ifndef POWERMANAGEMENTINTERFACE_H
#define POWERMANAGEMENTINTERFACE_H
#include <QObject>
#include <memory>
class QDBusPendingCallWatcher;
class PowerManagementInterfacePrivate;
class PowerManagementInterface : public QObject
{
Q_OBJECT
Q_PROPERTY(QString reason READ reason WRITE setReason)
Q_PROPERTY(bool preventSleep
READ preventSleep
WRITE setPreventSleep
NOTIFY preventSleepChanged)
Q_PROPERTY(bool sleepInhibited
READ sleepInhibited
NOTIFY sleepInhibitedChanged)
public:
explicit PowerManagementInterface(QObject *parent = nullptr);
~PowerManagementInterface() override;
[[nodiscard]] bool preventSleep() const;
[[nodiscard]] bool sleepInhibited() const;
QString reason() const;
void setReason(const QString &reason);
Q_SIGNALS:
void preventSleepChanged();
void sleepInhibitedChanged();
public Q_SLOTS:
void setPreventSleep(bool value);
void retryInhibitingSleep();
private Q_SLOTS:
void hostSleepInhibitChanged();
void inhibitDBusCallFinished(QDBusPendingCallWatcher *aWatcher);
void uninhibitDBusCallFinished(QDBusPendingCallWatcher *aWatcher);
private:
void inhibitSleep();
void uninhibitSleep();
std::unique_ptr<PowerManagementInterfacePrivate> d;
};
#endif // POWERMANAGEMENTINTERFACE_H
......@@ -340,6 +340,11 @@ Kirigami.ApplicationWindow
}
}
PowerManagementInterface {
reason: TransactionModel.mainTransactionText
preventSleep: TransactionModel.count > 0
}
contextDrawer: Kirigami.ContextDrawer {}
globalDrawer: DiscoverDrawer {
......
......@@ -146,6 +146,10 @@ void TransactionModel::addTransaction(Transaction *trans)
int before = m_transactions.size();
beginInsertRows(QModelIndex(), before, before + 1);
m_transactions.append(trans);
if (before == 0) { // Should emit before count changes
Q_EMIT mainTransactionTextChanged();
}
endInsertRows();
connect(trans, &Transaction::statusChanged, this, [this]() {
......@@ -181,6 +185,10 @@ void TransactionModel::removeTransaction(Transaction *trans)
Q_EMIT transactionRemoved(trans);
if (m_transactions.isEmpty())
Q_EMIT lastTransactionFinished();
if (r == 0) {
Q_EMIT mainTransactionTextChanged();
}
}
void TransactionModel::transactionChanged(int role)
......@@ -202,3 +210,8 @@ int TransactionModel::progress() const
}
return count == 0 ? 0 : sum / count;
}
QString TransactionModel::mainTransactionText() const
{
return m_transactions.isEmpty() ? QString() : m_transactions.constFirst()->name();
}
......@@ -18,6 +18,7 @@ class DISCOVERCOMMON_EXPORT TransactionModel : public QAbstractListModel
Q_OBJECT
Q_PROPERTY(int progress READ progress NOTIFY progressChanged)
Q_PROPERTY(int count READ rowCount NOTIFY countChanged)
Q_PROPERTY(QString mainTransactionText READ mainTransactionText NOTIFY mainTransactionTextChanged)
public:
enum Roles {
TransactionRoleRole = Qt::UserRole,
......@@ -54,6 +55,8 @@ public:
return m_transactions;
}
QString mainTransactionText() const;
private:
QVector<Transaction *> m_transactions;
......@@ -65,6 +68,7 @@ Q_SIGNALS:
void countChanged();
void progressChanged();
void proceedRequest(Transaction *transaction, const QString &title, const QString &description);
void mainTransactionTextChanged();
private Q_SLOTS:
void transactionChanged(int role);
......
Supports Markdown
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