File: battery.cpp

package info (click to toggle)
cutefish-core 0.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,076 kB
  • sloc: cpp: 11,327; xml: 443; sh: 29; makefile: 6
file content (412 lines) | stat: -rw-r--r-- 10,602 bytes parent folder | download
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#include "battery.h"
#include "power.h"
#include "primarybatteryadaptor.h"

#include <QDateTime>
#include <QDebug>

enum TimeConstants {
    SecsInDay = 86400,
    SecsInHour = 3600,
    SecsInMinute = 60
};

// ref: https://upower.freedesktop.org/docs/Device.html
//      kf5solid

Battery::Battery(UPowerDevice *device, QObject *parent)
    : QObject(parent)
    , m_device(device)
    , m_settings(nullptr)
    , m_lastChargedPercent(0)
    , m_lastChargedSecs(0)
{
    connect(device, SIGNAL(changed()), this, SLOT(slotChanged()));

    updateCache();
    init();
}

void Battery::updateCache()
{    
    m_chargeState = chargeState();
    m_isPresent = isPresent();
    m_chargePercent = chargePercent();
    m_capacity = capacity();
    m_isPowerSupply = isPowerSupply();
    m_timeToEmpty = timeToEmpty();
    m_timeToFull = timeToFull();
    m_energy = energy();
    m_energyFull = energyFull();
    m_energyFullDesign = energyFullDesign();
    m_energyRate = energyRate();
    m_voltage = voltage();
    m_temperature = temperature();
}

void Battery::init()
{
    if (type() == Battery::PrimaryBattery) {
        new PrimaryBatteryAdaptor(this);
        QDBusConnection::sessionBus().registerObject(QStringLiteral("/PrimaryBattery"), this);

        m_settings = new QSettings(QStringLiteral("cutefishos"), QStringLiteral("PrimaryBattery"));
        m_lastChargedPercent = m_settings->value("LastChargedPercent", 0).toInt();
        m_lastChargedSecs = m_settings->value("LastChargedSecs").toLongLong();
    }
}

void Battery::refresh()
{
    QDBusInterface iface(UP_DBUS_SERVICE, m_device->udi(),
                         UP_DBUS_INTERFACE_DEVICE, QDBusConnection::systemBus());
    if (iface.isValid())
        iface.call("Refresh");
}

bool Battery::isPresent() const
{
    return m_device->prop("IsPresent").toBool();
}

Battery::BatteryType Battery::type() const
{
    Battery::BatteryType result = Battery::UnknownBattery;
    const uint t = m_device->prop("Type").toUInt();

    switch (t) {
        case UP_DEVICE_KIND_LINE_POWER: // TODO
            break;
        case UP_DEVICE_KIND_BATTERY:
            result = Battery::PrimaryBattery;
            break;
        case UP_DEVICE_KIND_UPS:
            result = Battery::UpsBattery;
            break;
        case UP_DEVICE_KIND_MONITOR:
            result = Battery::MonitorBattery;
            break;
        case UP_DEVICE_KIND_MOUSE:
            result = Battery::MouseBattery;
            break;
        case UP_DEVICE_KIND_KEYBOARD:
            result = Battery::KeyboardBattery;
            break;
        case UP_DEVICE_KIND_PDA:
            result = Battery::PdaBattery;
            break;
        case UP_DEVICE_KIND_PHONE:
            result = Battery::PhoneBattery;
            break;
        case UP_DEVICE_KIND_GAMING_INPUT:
            result = Battery::GamingInputBattery;
            break;
        case UP_DEVICE_KIND_UNKNOWN: {
            // There is currently no "Bluetooth battery" type, so check if it comes from Bluez
            if (m_device->prop("NativePath").toString().startsWith(QLatin1String("/org/bluez/"))) {
                result = Battery::BluetoothBattery;
            }
            break;
        }
    }

    return result;
}

int Battery::chargePercent() const
{
    return qRound(m_device->prop("Percentage").toDouble());
}

int Battery::capacity() const
{
    return m_device->prop("Capacity").toDouble();
}

bool Battery::isRechargeable() const
{
    return m_device->prop("IsRechargeable").toBool();
}

bool Battery::isPowerSupply() const
{
    return m_device->prop("PowerSupply").toBool();
}

Battery::ChargeState Battery::chargeState() const
{
    Battery::ChargeState result = Battery::NoCharge;
    const uint state = m_device->prop("State").toUInt();
    switch (state) {
    case 0:
        result = Battery::NoCharge; // stable or unknown
        break;
    case 1:
        result = Battery::Charging;
        break;
    case 2:
        result = Battery::Discharging;
        break;
    case 3: // TODO "Empty"
        break;
    case 4:
        result = Battery::FullyCharged;
        break;
    case 5: // TODO "Pending charge"
        break;
    case 6: // TODO "Pending discharge"
        break;
    }

    return result;
}

qlonglong Battery::timeToEmpty() const
{
    return m_device->prop("TimeToEmpty").toLongLong();
}

qlonglong Battery::timeToFull() const
{
    return m_device->prop("TimeToFull").toLongLong();
}

Battery::Technology Battery::technology() const
{
    const uint tech = m_device->prop("Technology").toUInt();
    switch (tech) {
    case 1:
        return Battery::LithiumIon;
    case 2:
        return Battery::LithiumPolymer;
    case 3:
        return Battery::LithiumIronPhosphate;
    case 4:
        return Battery::LeadAcid;
    case 5:
        return Battery::NickelCadmium;
    case 6:
        return Battery::NickelMetalHydride;
    default:
        return Battery::UnknownTechnology;
    }
}

double Battery::energy() const
{
    return m_device->prop("Energy").toDouble();
}

double Battery::energyFull() const
{
    return m_device->prop("EnergyFull").toDouble();
}

double Battery::energyFullDesign() const
{
    return m_device->prop("EnergyFullDesign").toDouble();
}

double Battery::energyRate() const
{
    return m_device->prop("EnergyRate").toDouble();
}

double Battery::voltage() const
{
    return m_device->prop("Voltage").toDouble();
}

double Battery::temperature() const
{
    return m_device->prop("Temperature").toDouble();
}

bool Battery::isRecalled() const
{
    return m_device->prop("RecallNotice").toBool();
}

QString Battery::recallVendor() const
{
    return m_device->prop("RecallVendor").toString();
}

QString Battery::recallUrl() const
{
    return m_device->prop("RecallUrl").toString();
}

QString Battery::serial() const
{
    return m_device->prop("Serial").toString();
}

qlonglong Battery::remainingTime() const
{
    if (chargeState() == Battery::Charging) {
        return timeToFull();
    } else if (chargeState() == Battery::Discharging) {
        return timeToEmpty();
    }

    return -1;
}

QString Battery::formatDuration(qlonglong seconds) const
{
    QString result;
    quint64 secs = seconds;

    int days = secs / SecsInDay;
    secs = secs % SecsInDay;
    int hours = secs / SecsInHour;
    secs = secs % SecsInHour;
    int minutes = secs / SecsInMinute;

    if (days > 0)
        result.push_back(tr("%1d").arg(days));

    if (hours > 0) {
        if (days > 0)
            result.push_back(" ");

        result.push_back(tr("%1h").arg(hours));
    }

    if (minutes > 0) {
        if (hours > 0)
            result.push_back(" ");

        result.push_back(tr("%1m").arg(minutes));
    }

    return result;
}

QString Battery::statusString() const
{
    if (chargeState() == Battery::Charging) {
        return tr("%1 until fully charged").arg(formatDuration(remainingTime()));
    } else if (chargeState() == Battery::Discharging) {
        return tr("%1 remaining").arg(formatDuration(remainingTime()));
    } else if (chargeState() == Battery::FullyCharged) {
        return tr("Fully charged.");
    }

    return QString();
}

int Battery::lastChargedPercent() const
{
    return m_lastChargedPercent;
}

quint64 Battery::lastChargedSecs() const
{
    return m_lastChargedSecs;
}

QString Battery::lastChargedTime() const
{
    QDateTime now = QDateTime::currentDateTime();
    QDateTime time = QDateTime::fromSecsSinceEpoch(m_lastChargedSecs);
    qint64 minutes = qRound64(time.secsTo(now) / 60.0f);

    if (minutes == 0)
        return tr("now");

    return tr("%1 ago").arg(formatDuration(time.secsTo(now)));
}

void Battery::slotChanged()
{
    if (m_device) {
        const bool old_isPresent = m_isPresent;
        const int old_chargePercent = m_chargePercent;
        const int old_capacity = m_capacity;
        const bool old_isPowerSupply = m_isPowerSupply;
        const Battery::ChargeState old_chargeState = m_chargeState;
        const qlonglong old_timeToEmpty = m_timeToEmpty;
        const qlonglong old_timeToFull = m_timeToFull;
        const double old_energy = m_energy;
        const double old_energyFull = m_energyFull;
        const double old_energyFullDesign = m_energyFullDesign;
        const double old_energyRate = m_energyRate;
        const double old_voltage = m_voltage;
        const double old_temperature = m_temperature;

        updateCache();

        if (old_isPresent != m_isPresent) {
            emit presentStateChanged(m_isPresent);
        }

        if (old_chargePercent != m_chargePercent) {
            emit chargePercentChanged(m_chargePercent);
        }

        if (old_capacity != m_capacity) {
            emit capacityChanged(m_capacity);
        }

        if (old_isPowerSupply != m_isPowerSupply) {
            emit powerSupplyStateChanged(m_isPowerSupply);
        }

        if (old_chargeState != m_chargeState) {
            emit chargeStateChanged(m_chargeState);
        }

        // Save last charge percentage
        if ((old_chargeState == Battery::Charging || old_chargeState == Battery::FullyCharged) && 
             m_chargeState == Battery::Discharging) {
            m_lastChargedPercent = m_chargePercent;
            m_lastChargedSecs = QDateTime::currentSecsSinceEpoch();

            if (m_settings) {
                m_settings->setValue("LastChargedPercent", m_lastChargedPercent);
                m_settings->setValue("LastChargedSecs", m_lastChargedSecs);
            }

            emit lastChargedPercentChanged();
            emit lastChargedSecsChanged();
        }

        if (old_timeToEmpty != m_timeToEmpty) {
            emit timeToEmptyChanged(m_timeToEmpty);
        }

        if (old_timeToFull != m_timeToFull) {
            emit timeToFullChanged(m_timeToFull);
        }

        if (old_energy != m_energy) {
            emit energyChanged(m_energy);
        }

        if (old_energyFull != m_energyFull) {
            emit energyFullChanged(m_energyFull);
        }

        if (old_energyFullDesign != m_energyFullDesign) {
            emit energyFullChanged(m_energyFullDesign);
        }

        if (old_energyRate != m_energyRate) {
            emit energyRateChanged(m_energyRate);
        }

        if (old_voltage != m_voltage) {
            emit voltageChanged(m_voltage);
        }

        if (old_temperature != m_temperature) {
            emit temperatureChanged(m_temperature);
        }

        if (old_timeToFull != m_timeToFull || old_timeToEmpty != m_timeToEmpty) {
            emit remainingTimeChanged(remainingTime());
        }
    }
}