| 1 | /* |
| 2 | This file is part of the KDE Project |
| 3 | SPDX-FileCopyrightText: 2008 Sebastian Trueg <trueg@kde.org> |
| 4 | SPDX-FileCopyrightText: 2010-15 Vishesh Handa <vhanda@kde.org> |
| 5 | |
| 6 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 7 | */ |
| 8 | |
| 9 | #include "powerstatemonitor.h" |
| 10 | |
| 11 | #include <QDBusConnection> |
| 12 | #include <QDBusMessage> |
| 13 | #include <QDBusPendingCallWatcher> |
| 14 | #include <QDBusPendingReply> |
| 15 | |
| 16 | using namespace Baloo; |
| 17 | |
| 18 | PowerStateMonitor::PowerStateMonitor(QObject* parent) |
| 19 | : QObject(parent) |
| 20 | , m_isOnBattery(true) |
| 21 | { |
| 22 | // monitor the powermanagement to not drain the battery |
| 23 | QDBusConnection::sessionBus().connect(QStringLiteral("org.freedesktop.PowerManagement" ), |
| 24 | QStringLiteral("/org/freedesktop/PowerManagement" ), |
| 25 | QStringLiteral("org.freedesktop.PowerManagement" ), |
| 26 | QStringLiteral("PowerSaveStatusChanged" ), |
| 27 | receiver: this, SLOT(slotPowerManagementStatusChanged(bool))); |
| 28 | |
| 29 | QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.freedesktop.PowerManagement" ), |
| 30 | QStringLiteral("/org/freedesktop/PowerManagement" ), |
| 31 | QStringLiteral("org.freedesktop.PowerManagement" ), |
| 32 | QStringLiteral("GetPowerSaveStatus" )); |
| 33 | |
| 34 | QDBusPendingCall call = QDBusConnection::sessionBus().asyncCall(message: msg); |
| 35 | QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(call, this); |
| 36 | connect(sender: watcher, signal: &QDBusPendingCallWatcher::finished, slot: [&](QDBusPendingCallWatcher* watch) { |
| 37 | QDBusPendingReply<bool> reply = *watch; |
| 38 | if (!reply.isError()) { |
| 39 | bool onBattery = reply.argumentAt<0>(); |
| 40 | slotPowerManagementStatusChanged(conserveResources: onBattery); |
| 41 | } else { |
| 42 | slotPowerManagementStatusChanged(conserveResources: false); |
| 43 | } |
| 44 | watch->deleteLater(); |
| 45 | }); |
| 46 | } |
| 47 | |
| 48 | void PowerStateMonitor::slotPowerManagementStatusChanged(bool conserveResources) |
| 49 | { |
| 50 | if (m_isOnBattery != conserveResources) { |
| 51 | m_isOnBattery = conserveResources; |
| 52 | Q_EMIT powerManagementStatusChanged(conserveResources); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | #include "moc_powerstatemonitor.cpp" |
| 57 | |