-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathBattery.cxx
More file actions
199 lines (151 loc) · 5.46 KB
/
Battery.cxx
File metadata and controls
199 lines (151 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
This file is part of liquidshell.
SPDX-FileCopyrightText: 2017 - 2024 Martin Koller <martin@kollix.at>
SPDX-License-Identifier: GPL-3.0-or-later
*/
#include <Battery.hxx>
#include <QIcon>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusVariant>
#include <QDebug>
#include <Solid/Battery>
#include <KLocalizedString>
#include <KIconLoader>
#include <cmath>
//--------------------------------------------------------------------------------
Battery::Battery(QWidget *parent)
: SysTrayItem(parent)
{
QList<Solid::Device> devices = Solid::Device::listFromType(Solid::DeviceInterface::Battery);
for (Solid::Device dev : devices)
{
if ( dev.is<Solid::Battery>() && (dev.as<Solid::Battery>()->type() == Solid::Battery::PrimaryBattery) )
{
device = dev;
break;
}
}
if ( !device.isValid() )
hide();
else
{
QDBusConnection::systemBus()
.connect("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.DBus.Properties",
"PropertiesChanged",
this,
SLOT(upowerPropertiesChanged(QString, QVariantMap, QStringList)));
QDBusMessage msg =
QDBusMessage::createMethodCall("org.freedesktop.UPower",
"/org/freedesktop/UPower",
"org.freedesktop.DBus.Properties",
"Get");
msg << QLatin1String("org.freedesktop.UPower") << QLatin1String("OnBattery");
QDBusConnection::systemBus().callWithCallback(msg, this, SLOT(onBatteryReply(QDBusMessage)), nullptr);
connect(device.as<Solid::Battery>(), &Solid::Battery::chargePercentChanged, this, &Battery::changed);
connect(device.as<Solid::Battery>(), &Solid::Battery::chargeStateChanged, this, &Battery::changed);
connect(device.as<Solid::Battery>(), &Solid::Battery::timeToFullChanged, this, &Battery::changed);
connect(device.as<Solid::Battery>(), &Solid::Battery::timeToEmptyChanged, this, &Battery::changed);
connect(KIconLoader::global(), &KIconLoader::iconLoaderSettingsChanged, this, &Battery::changed);
changed();
}
}
//--------------------------------------------------------------------------------
QString Battery::secsToHM(int secs) const
{
int h = secs / 3600;
int m = (secs % 3600) / 60;
QString hStr = i18np("%1 hour", "%1 hours", h);
QString mStr = i18np("%1 minute", "%1 minutes", m);
QString result;
if ( h )
result = hStr;
if ( m )
{
if ( h )
result += ", ";
result += mStr;
}
return result;
}
//--------------------------------------------------------------------------------
void Battery::onBatteryReply(const QDBusMessage &msg)
{
onBattery = msg.arguments()[0].value<QDBusVariant>().variant().toBool();
changed();
}
//--------------------------------------------------------------------------------
void Battery::upowerPropertiesChanged(const QString &interface,
const QVariantMap &properties,
const QStringList &invalidated)
{
Q_UNUSED(interface)
Q_UNUSED(invalidated)
if ( properties.contains("OnBattery") )
{
onBattery = properties.value("OnBattery").toBool();
changed();
}
}
//--------------------------------------------------------------------------------
QIcon Battery::getStatusIcon(int charge, bool isCharging)
{
QString iconName;
int p = qRound(charge / 20.0) * 20;
if ( p < 20 )
iconName = "caution";
else if ( p < 40 )
iconName = "low";
else
iconName = QString("%1").arg(p, 3, 10, QLatin1Char('0'));
iconName = QString("battery%1-%2").arg(isCharging ? "-charging" : "").arg(iconName);
return QIcon::fromTheme(iconName);
}
//--------------------------------------------------------------------------------
void Battery::changed()
{
Solid::Battery *battery = device.as<Solid::Battery>();
QString tip;
switch ( battery->chargeState() )
{
case Solid::Battery::NoCharge: tip = i18n("Not Charging"); break;
case Solid::Battery::FullyCharged: tip = i18n("Fully Charged"); break;
case Solid::Battery::Charging:
case Solid::Battery::Discharging:
{
if ( battery->chargeState() == Solid::Battery::Charging )
{
tip = i18n("Charging at %1%", battery->chargePercent());
if ( battery->timeToFull() ) // it can be 0, so we don't know
tip += '\n' + i18n("Time until full: ") + secsToHM(battery->timeToFull());
}
else
{
tip = i18n("Discharging at %1%", battery->chargePercent());
if ( battery->timeToEmpty() ) // it can be 0, so we don't know
tip += '\n' + i18n("Remaining Time: ") + secsToHM(battery->timeToEmpty());
}
break;
}
}
setPixmap(getStatusIcon(battery->chargePercent(),
battery->chargeState() == Solid::Battery::Charging).pixmap(size()));
setToolTip(tip);
setVisible(onBattery || (battery->chargeState() != Solid::Battery::FullyCharged));
}
//--------------------------------------------------------------------------------
QWidget *Battery::getDetailsList()
{
if ( !dialog )
{
dialog = new KCMultiDialog(this);
dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->addModule(KPluginMetaData("plasma/kcms/systemsettings/kcm_powerdevilprofilesconfig"));
dialog->adjustSize();
dialog->setWindowTitle(i18n("Power Management"));
}
return dialog;
}
//--------------------------------------------------------------------------------