Baloo

powerstatemonitor.cpp
1 /*
2  This file is part of the KDE Project
3  SPDX-FileCopyrightText: 2008 Sebastian Trueg <[email protected]>
4  SPDX-FileCopyrightText: 2010-15 Vishesh Handa <[email protected]>
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  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 
35  QDBusPendingCallWatcher* watcher = new QDBusPendingCallWatcher(call, this);
36  connect(watcher, &QDBusPendingCallWatcher::finished, [&](QDBusPendingCallWatcher* watch) {
37  QDBusPendingReply<bool> reply = *watch;
38  if (!reply.isError()) {
39  bool onBattery = reply.argumentAt<0>();
40  slotPowerManagementStatusChanged(onBattery);
41  } else {
42  slotPowerManagementStatusChanged(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"
bool connect(const QString &service, const QString &path, const QString &interface, const QString &name, QObject *receiver, const char *slot)
void finished(QDBusPendingCallWatcher *self)
bool isError() const const
void deleteLater()
QDBusConnection sessionBus()
Implements storage for docIds without any associated data Instantiated for:
Definition: coding.cpp:11
QDBusMessage createMethodCall(const QString &service, const QString &path, const QString &interface, const QString &method)
QDBusPendingCall asyncCall(const QDBusMessage &message, int timeout) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Wed Nov 29 2023 03:56:26 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.