Changeset View
Changeset View
Standalone View
Standalone View
src/file/powerstatemonitor.cpp
Show All 23 Lines | |||||
24 | #include <QDBusMessage> | 24 | #include <QDBusMessage> | ||
25 | #include <QDBusPendingCallWatcher> | 25 | #include <QDBusPendingCallWatcher> | ||
26 | #include <QDBusPendingReply> | 26 | #include <QDBusPendingReply> | ||
27 | 27 | | |||
28 | using namespace Baloo; | 28 | using namespace Baloo; | ||
29 | 29 | | |||
30 | PowerStateMonitor::PowerStateMonitor(QObject* parent) | 30 | PowerStateMonitor::PowerStateMonitor(QObject* parent) | ||
31 | : QObject(parent) | 31 | : QObject(parent) | ||
32 | , m_isOnBattery(false) | 32 | , m_isOnBattery(true) | ||
33 | { | 33 | { | ||
34 | // monitor the powermanagement to not drain the battery | 34 | // monitor the powermanagement to not drain the battery | ||
35 | QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.PowerManagement"), | 35 | QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.PowerManagement"), | ||
36 | QStringLiteral("/org/freedesktop/PowerManagement"), | 36 | QStringLiteral("/org/freedesktop/PowerManagement"), | ||
37 | QStringLiteral("org.freedesktop.PowerManagement"), | 37 | QStringLiteral("org.freedesktop.PowerManagement"), | ||
38 | QStringLiteral("PowerSaveStatusChanged"), | 38 | QStringLiteral("PowerSaveStatusChanged"), | ||
39 | this, SLOT(slotPowerManagementStatusChanged(bool))); | 39 | this, SLOT(slotPowerManagementStatusChanged(bool))); | ||
40 | 40 | | |||
41 | 41 | | |||
42 | QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement"), | 42 | QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement"), | ||
43 | QStringLiteral("/org/freedesktop/PowerManagement"), | 43 | QStringLiteral("/org/freedesktop/PowerManagement"), | ||
44 | QStringLiteral("org.freedesktop.PowerManagement"), | 44 | QStringLiteral("org.freedesktop.PowerManagement"), | ||
45 | QStringLiteral("GetPowerSaveStatus")); | 45 | QStringLiteral("GetPowerSaveStatus")); | ||
46 | 46 | | |||
47 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(msg); | 47 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(msg); | ||
48 | QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(call, this); | 48 | QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(call, this); | ||
49 | connect(watcher, &QDBusPendingCallWatcher::finished, [&](QDBusPendingCallWatcher* watch) { | 49 | connect(watcher, &QDBusPendingCallWatcher::finished, [&](QDBusPendingCallWatcher* watch) { | ||
50 | QDBusPendingReply<bool> reply = *watch; | 50 | QDBusPendingReply<bool> reply = *watch; | ||
51 | if (!reply.isError()) { | 51 | if (!reply.isError()) { | ||
52 | bool onBattery = reply.argumentAt<0>(); | 52 | bool onBattery = reply.argumentAt<0>(); | ||
53 | slotPowerManagementStatusChanged(onBattery); | 53 | slotPowerManagementStatusChanged(onBattery); | ||
54 | } else { | ||||
55 | slotPowerManagementStatusChanged(false); | ||||
54 | } | 56 | } | ||
55 | watch->deleteLater(); | 57 | watch->deleteLater(); | ||
56 | }); | 58 | }); | ||
57 | } | 59 | } | ||
58 | 60 | | |||
59 | void PowerStateMonitor::slotPowerManagementStatusChanged(bool conserveResources) | 61 | void PowerStateMonitor::slotPowerManagementStatusChanged(bool conserveResources) | ||
60 | { | 62 | { | ||
61 | if (m_isOnBattery != conserveResources) { | 63 | if (m_isOnBattery != conserveResources) { | ||
62 | m_isOnBattery = conserveResources; | 64 | m_isOnBattery = conserveResources; | ||
63 | Q_EMIT powerManagementStatusChanged(conserveResources); | 65 | Q_EMIT powerManagementStatusChanged(conserveResources); | ||
64 | } | 66 | } | ||
65 | } | 67 | } |