File indexing completed on 2024-04-21 04:56:46

0001 /*
0002  * SPDX-FileCopyrightText: 2016 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  * SPDX-FileCopyrightText: 2020 Piyush Aggarwal <piyushaggarwal002@gmail.com>
0004  *
0005  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006  */
0007 
0008 #include "battery_action.h"
0009 
0010 BatteryAction::BatteryAction(DeviceDbusInterface *device)
0011     : QAction(nullptr)
0012     , m_batteryIface(device->id())
0013 {
0014     setCharge(m_batteryIface.charge());
0015     setCharging(m_batteryIface.isCharging());
0016 
0017     connect(&m_batteryIface, &BatteryDbusInterface::refreshed, this, [this](bool isCharging, int charge) {
0018         setCharge(charge);
0019         setCharging(isCharging);
0020     });
0021 
0022     BatteryAction::update();
0023 }
0024 
0025 void BatteryAction::update()
0026 {
0027     if (m_charge < 0)
0028         setText(i18n("No Battery"));
0029     else if (m_charging)
0030         setText(i18n("Battery: %1% (Charging)", m_charge));
0031     else
0032         setText(i18n("Battery: %1%", m_charge));
0033 
0034     // set icon name
0035     QString iconName = QStringLiteral("battery");
0036     if (m_charge < 0) {
0037         iconName += QStringLiteral("-missing");
0038     } else {
0039         int val = int(m_charge / 10) * 10;
0040         QString numberPaddedString = QStringLiteral("%1").arg(val, 3, 10, QLatin1Char('0'));
0041         iconName += QStringLiteral("-") + numberPaddedString;
0042     }
0043 
0044     if (m_charging) {
0045         iconName += QStringLiteral("-charging");
0046     }
0047 
0048     setIcon(QIcon::fromTheme(iconName));
0049 }
0050 
0051 void BatteryAction::setCharge(int charge)
0052 {
0053     m_charge = charge;
0054     update();
0055 }
0056 
0057 void BatteryAction::setCharging(bool charging)
0058 {
0059     m_charging = charging;
0060     update();
0061 }
0062 
0063 #include "moc_battery_action.cpp"