mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-22 20:44:15 +00:00
Detect D-Bus interface
This commit is contained in:
parent
79afa0b84d
commit
64c3845a7c
@ -29,7 +29,7 @@
|
|||||||
#include "powermanagement_x11.h"
|
#include "powermanagement_x11.h"
|
||||||
|
|
||||||
#include <QDBusConnection>
|
#include <QDBusConnection>
|
||||||
#include <QDBusMessage>
|
#include <QDBusInterface>
|
||||||
#include <QDBusPendingCall>
|
#include <QDBusPendingCall>
|
||||||
#include <QDBusPendingReply>
|
#include <QDBusPendingReply>
|
||||||
|
|
||||||
@ -37,16 +37,27 @@
|
|||||||
|
|
||||||
PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
|
PowerManagementInhibitor::PowerManagementInhibitor(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
|
, m_busInterface {new QDBusInterface(u"org.gnome.SessionManager"_s, u"/org/gnome/SessionManager"_s
|
||||||
|
, u"org.gnome.SessionManager"_s, QDBusConnection::sessionBus(), this)}
|
||||||
{
|
{
|
||||||
if (!QDBusConnection::sessionBus().isConnected())
|
if (!m_busInterface->isValid())
|
||||||
{
|
{
|
||||||
qDebug("D-Bus: Could not connect to session bus");
|
delete m_busInterface;
|
||||||
m_state = Error;
|
|
||||||
}
|
m_busInterface = new QDBusInterface(u"org.freedesktop.PowerManagement"_s, u"/org/freedesktop/PowerManagement/Inhibit"_s
|
||||||
else
|
, u"org.freedesktop.PowerManagement.Inhibit"_s, QDBusConnection::sessionBus(), this);
|
||||||
{
|
m_manager = ManagerType::Freedesktop;
|
||||||
m_state = Idle;
|
if (!m_busInterface->isValid())
|
||||||
|
{
|
||||||
|
delete m_busInterface;
|
||||||
|
m_busInterface = nullptr;
|
||||||
|
|
||||||
|
m_state = Error;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_busInterface->setTimeout(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void PowerManagementInhibitor::requestIdle()
|
void PowerManagementInhibitor::requestIdle()
|
||||||
@ -58,20 +69,10 @@ void PowerManagementInhibitor::requestIdle()
|
|||||||
m_state = RequestIdle;
|
m_state = RequestIdle;
|
||||||
qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
|
qDebug("D-Bus: PowerManagementInhibitor: Requesting idle");
|
||||||
|
|
||||||
QDBusMessage call = m_useGSM
|
const QString method = (m_manager == ManagerType::Gnome)
|
||||||
? QDBusMessage::createMethodCall(
|
? u"Uninhibit"_s
|
||||||
u"org.gnome.SessionManager"_s,
|
: u"UnInhibit"_s;
|
||||||
u"/org/gnome/SessionManager"_s,
|
const QDBusPendingCall pcall = m_busInterface->asyncCall(method, m_cookie);
|
||||||
u"org.gnome.SessionManager"_s,
|
|
||||||
u"Uninhibit"_s)
|
|
||||||
: QDBusMessage::createMethodCall(
|
|
||||||
u"org.freedesktop.PowerManagement"_s,
|
|
||||||
u"/org/freedesktop/PowerManagement/Inhibit"_s,
|
|
||||||
u"org.freedesktop.PowerManagement.Inhibit"_s,
|
|
||||||
u"UnInhibit"_s);
|
|
||||||
call.setArguments({m_cookie});
|
|
||||||
|
|
||||||
const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
|
|
||||||
const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
|
const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
|
||||||
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
|
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
|
||||||
}
|
}
|
||||||
@ -85,27 +86,11 @@ void PowerManagementInhibitor::requestBusy()
|
|||||||
m_state = RequestBusy;
|
m_state = RequestBusy;
|
||||||
qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
|
qDebug("D-Bus: PowerManagementInhibitor: Requesting busy");
|
||||||
|
|
||||||
QDBusMessage call = m_useGSM
|
const QString message = u"Active torrents are presented"_s;
|
||||||
? QDBusMessage::createMethodCall(
|
const auto args = (m_manager == ManagerType::Gnome)
|
||||||
u"org.gnome.SessionManager"_s,
|
? QList<QVariant> {u"qBittorrent"_s, 0u, message, 4u}
|
||||||
u"/org/gnome/SessionManager"_s,
|
: QList<QVariant> {u"qBittorrent"_s, message};
|
||||||
u"org.gnome.SessionManager"_s,
|
const QDBusPendingCall pcall = m_busInterface->asyncCallWithArgumentList(u"Inhibit"_s, args);
|
||||||
u"Inhibit"_s)
|
|
||||||
: QDBusMessage::createMethodCall(
|
|
||||||
u"org.freedesktop.PowerManagement"_s,
|
|
||||||
u"/org/freedesktop/PowerManagement/Inhibit"_s,
|
|
||||||
u"org.freedesktop.PowerManagement.Inhibit"_s,
|
|
||||||
u"Inhibit"_s);
|
|
||||||
|
|
||||||
QList<QVariant> args = {u"qBittorrent"_s};
|
|
||||||
if (m_useGSM)
|
|
||||||
args << 0u;
|
|
||||||
args << u"Active torrents are presented"_s;
|
|
||||||
if (m_useGSM)
|
|
||||||
args << 4u;
|
|
||||||
call.setArguments(args);
|
|
||||||
|
|
||||||
const QDBusPendingCall pcall = QDBusConnection::sessionBus().asyncCall(call, 1000);
|
|
||||||
const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
|
const auto *watcher = new QDBusPendingCallWatcher(pcall, this);
|
||||||
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
|
connect(watcher, &QDBusPendingCallWatcher::finished, this, &PowerManagementInhibitor::onAsyncReply);
|
||||||
}
|
}
|
||||||
@ -138,19 +123,7 @@ void PowerManagementInhibitor::onAsyncReply(QDBusPendingCallWatcher *call)
|
|||||||
if (reply.isError())
|
if (reply.isError())
|
||||||
{
|
{
|
||||||
qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
|
qDebug("D-Bus: Reply: Error: %s", qUtf8Printable(reply.error().message()));
|
||||||
|
m_state = Error;
|
||||||
if (m_useGSM)
|
|
||||||
{
|
|
||||||
qDebug("D-Bus: Falling back to org.freedesktop.PowerManagement");
|
|
||||||
m_useGSM = false;
|
|
||||||
m_state = Idle;
|
|
||||||
if (m_intendedState == Busy)
|
|
||||||
requestBusy();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
m_state = Error;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
class QDBusInterface;
|
||||||
class QDBusPendingCallWatcher;
|
class QDBusPendingCallWatcher;
|
||||||
|
|
||||||
class PowerManagementInhibitor final : public QObject
|
class PowerManagementInhibitor final : public QObject
|
||||||
@ -57,9 +58,16 @@ private:
|
|||||||
RequestIdle
|
RequestIdle
|
||||||
};
|
};
|
||||||
|
|
||||||
|
enum class ManagerType
|
||||||
|
{
|
||||||
|
Freedesktop, // https://www.freedesktop.org/wiki/Specifications/power-management-spec/
|
||||||
|
Gnome // https://github.com/GNOME/gnome-settings-daemon/blob/master/gnome-settings-daemon/org.gnome.SessionManager.xml
|
||||||
|
};
|
||||||
|
|
||||||
|
QDBusInterface *m_busInterface = nullptr;
|
||||||
|
ManagerType m_manager = ManagerType::Gnome;
|
||||||
|
|
||||||
enum State m_state = Error;
|
enum State m_state = Error;
|
||||||
enum State m_intendedState = Idle;
|
enum State m_intendedState = Idle;
|
||||||
quint32 m_cookie = 0;
|
quint32 m_cookie = 0;
|
||||||
|
|
||||||
bool m_useGSM = true;
|
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user