File: ExternalServiceSettings.cpp

package info (click to toggle)
powerdevil 4%3A6.5.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,680 kB
  • sloc: cpp: 13,284; xml: 1,911; python: 1,204; sh: 19; makefile: 10
file content (283 lines) | stat: -rw-r--r-- 10,777 bytes parent folder | download | duplicates (2)
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
/*
 *  SPDX-FileCopyrightText: 2008-2010 Dario Freddi <drf@kde.org>
 *  SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com>
 *  SPDX-FileCopyrightText: 2024 Fabian Arndt <fabian.arndt@root-core.net>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "ExternalServiceSettings.h"

// KDE
#include <KAuth/Action>
#include <KAuth/ExecuteJob>
#include <KLocalizedString>
#include <Solid/Battery>
#include <Solid/Device>

// Qt
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusMessage>
#include <QDBusPendingCall>
#include <QDBusServiceWatcher>
#include <QPointer>

// debug category for qCInfo()
#include <powerdevil_debug.h>

using namespace Qt::StringLiterals;

namespace
{
constexpr int ChargeThresholdUnsupported = -1;
}

namespace PowerDevil
{

ExternalServiceSettings::ExternalServiceSettings(QObject *parent)
    : QObject(parent)
    , m_chargeStartThreshold(ChargeThresholdUnsupported)
    , m_chargeStopThreshold(ChargeThresholdUnsupported)
    , m_savedChargeStartThreshold(ChargeThresholdUnsupported)
    , m_savedChargeStopThreshold(ChargeThresholdUnsupported)
    , m_chargeStopThresholdMightNeedReconnect(false)
    , m_isBatteryConservationModeSupported(false)
    , m_batteryConservationMode(false)
    , m_savedBatteryConservationMode(false)
{
}

void ExternalServiceSettings::executeChargeThresholdHelperAction(const QString &actionName,
                                                                 QWindow *parentWindowForKAuth,
                                                                 const QVariantMap &arguments,
                                                                 const std::function<void(KAuth::ExecuteJob *job)> callback)
{
    KAuth::Action action(QStringLiteral("org.kde.powerdevil.chargethresholdhelper.%1").arg(actionName));
    action.setHelperId(QStringLiteral("org.kde.powerdevil.chargethresholdhelper"));
    action.setParentWindow(parentWindowForKAuth);
    action.setArguments(arguments);

    KAuth::ExecuteJob *job = action.execute();
    QPointer thisAlive(this);
    QPointer jobAlive(job);
    job->exec();

    if (!thisAlive || !jobAlive) {
        qCInfo(POWERDEVIL) << action.name() << "failed: was deleted during job execution";
        return;
    }

    if (job->error()) {
        qCInfo(POWERDEVIL) << "KAuth action" << action.name() << "failed:" << job->errorText();
    }
    callback(job);
}

void ExternalServiceSettings::load(QWindow *parentWindowForKAuth)
{
    // Battery thresholds (start / stop)
    executeChargeThresholdHelperAction(u"getthreshold"_s, parentWindowForKAuth, {}, [&](KAuth::ExecuteJob *job) {
        if (job->error()) {
            setSavedChargeStartThreshold(ChargeThresholdUnsupported);
            setSavedChargeStopThreshold(ChargeThresholdUnsupported);
            return;
        }

        const auto data = job->data();
        setSavedChargeStartThreshold(data.value(QStringLiteral("chargeStartThreshold")).toInt());
        setSavedChargeStopThreshold(data.value(QStringLiteral("chargeStopThreshold")).toInt());
        setChargeStopThreshold(m_savedChargeStopThreshold);
        setChargeStartThreshold(m_savedChargeStartThreshold);
    });

    // Battery Conservation Mode (fixed)
    executeChargeThresholdHelperAction(u"getconservationmode"_s, parentWindowForKAuth, {}, [&](KAuth::ExecuteJob *job) {
        if (job->error()) {
            setBatteryConservationModeSupported(false);
            setSavedBatteryConservationMode(false);
            return;
        }

        const auto data = job->data();
        setSavedBatteryConservationMode(data.value(QStringLiteral("batteryConservationModeEnabled")).toBool());
        setBatteryConservationMode(m_savedBatteryConservationMode);
        setBatteryConservationModeSupported(true);
    });
}

void ExternalServiceSettings::save(QWindow *parentWindowForKAuth)
{
    // Battery threshold (start / stop)
    if ((isChargeStartThresholdSupported() && m_chargeStartThreshold != m_savedChargeStartThreshold)
        || (isChargeStopThresholdSupported() && m_chargeStopThreshold != m_savedChargeStopThreshold)) {
        int newChargeStartThreshold = isChargeStartThresholdSupported() ? m_chargeStartThreshold : ChargeThresholdUnsupported;
        int newChargeStopThreshold = isChargeStopThresholdSupported() ? m_chargeStopThreshold : ChargeThresholdUnsupported;

        executeChargeThresholdHelperAction(u"setthreshold"_s,
                                           parentWindowForKAuth,
                                           {
                                               {QStringLiteral("chargeStartThreshold"), newChargeStartThreshold},
                                               {QStringLiteral("chargeStopThreshold"), newChargeStopThreshold},
                                           },
                                           [&](KAuth::ExecuteJob *job) {
                                               if (job->error()) {
                                                   setChargeStopThreshold(m_savedChargeStopThreshold);
                                                   setChargeStartThreshold(m_savedChargeStartThreshold);
                                                   return;
                                               }

                                               setSavedChargeStartThreshold(newChargeStartThreshold);
                                               setSavedChargeStopThreshold(newChargeStopThreshold);
                                           });
    }

    // Battery Conservation Mode (fixed)
    if (isBatteryConservationModeSupported() && m_batteryConservationMode != m_savedBatteryConservationMode) {
        executeChargeThresholdHelperAction(u"setconservationmode"_s,
                                           parentWindowForKAuth,
                                           {
                                               {QStringLiteral("batteryConservationModeEnabled"), m_batteryConservationMode},
                                           },
                                           [&](KAuth::ExecuteJob *job) {
                                               if (job->error()) {
                                                   setBatteryConservationMode(m_savedBatteryConservationMode);
                                                   return;
                                               }

                                               setSavedBatteryConservationMode(m_batteryConservationMode);
                                           });
    }
}

bool ExternalServiceSettings::isSaveNeeded() const
{
    return (isChargeStartThresholdSupported() && m_chargeStartThreshold != m_savedChargeStartThreshold)
        || (isChargeStopThresholdSupported() && m_chargeStopThreshold != m_savedChargeStopThreshold)
        || (isBatteryConservationModeSupported() && m_batteryConservationMode != m_savedBatteryConservationMode);
}

void ExternalServiceSettings::setBatteryConservationModeSupported(bool supported)
{
    if (m_isBatteryConservationModeSupported != supported) {
        m_isBatteryConservationModeSupported = supported;
        Q_EMIT isBatteryConservationModeSupportedChanged();
    }
}

bool ExternalServiceSettings::isBatteryConservationModeSupported() const
{
    return m_isBatteryConservationModeSupported;
}

void ExternalServiceSettings::setSavedBatteryConservationMode(bool enabled)
{
    m_savedBatteryConservationMode = enabled;
}

bool ExternalServiceSettings::isChargeStartThresholdSupported() const
{
    return m_savedChargeStartThreshold != ChargeThresholdUnsupported;
}

bool ExternalServiceSettings::isChargeStopThresholdSupported() const
{
    return m_savedChargeStopThreshold != ChargeThresholdUnsupported;
}

void ExternalServiceSettings::setSavedChargeStartThreshold(int threshold)
{
    bool wasChargeStartThresholdSupported = isChargeStartThresholdSupported();
    m_savedChargeStartThreshold = threshold;
    if (wasChargeStartThresholdSupported != isChargeStartThresholdSupported()) {
        Q_EMIT isChargeStartThresholdSupportedChanged();
    }
}

void ExternalServiceSettings::setSavedChargeStopThreshold(int threshold)
{
    bool wasChargeStopThresholdSupported = isChargeStopThresholdSupported();
    m_savedChargeStopThreshold = threshold;
    if (wasChargeStopThresholdSupported != isChargeStopThresholdSupported()) {
        Q_EMIT isChargeStopThresholdSupportedChanged();
    }
}

bool ExternalServiceSettings::batteryConservationMode() const
{
    return m_batteryConservationMode;
}

void ExternalServiceSettings::setBatteryConservationMode(bool enabled)
{
    if (enabled == m_batteryConservationMode) {
        return;
    }
    m_batteryConservationMode = enabled;
    Q_EMIT batteryConservationModeChanged();
    Q_EMIT settingsChanged();
}

int ExternalServiceSettings::chargeStartThreshold() const
{
    return m_chargeStartThreshold;
}

int ExternalServiceSettings::chargeStopThreshold() const
{
    return m_chargeStopThreshold;
}

void ExternalServiceSettings::setChargeStartThreshold(int threshold)
{
    if (threshold == m_chargeStartThreshold) {
        return;
    }
    m_chargeStartThreshold = threshold;
    Q_EMIT chargeStartThresholdChanged();
    Q_EMIT settingsChanged();
}

void ExternalServiceSettings::setChargeStopThreshold(int threshold)
{
    if (threshold == m_chargeStopThreshold) {
        return;
    }
    m_chargeStopThreshold = threshold;
    Q_EMIT chargeStopThresholdChanged();

    if (m_chargeStopThreshold > m_savedChargeStopThreshold) {
        // Only show message if there is actually a charging or fully charged battery
        const auto devices = Solid::Device::listFromType(Solid::DeviceInterface::Battery, QString());
        for (const Solid::Device &device : devices) {
            const Solid::Battery *b = qobject_cast<const Solid::Battery *>(device.asDeviceInterface(Solid::DeviceInterface::Battery));
            if (b->chargeState() == Solid::Battery::Charging || b->chargeState() == Solid::Battery::FullyCharged) {
                setChargeStopThresholdMightNeedReconnect(true);
                break;
            }
        }
    } else {
        setChargeStopThresholdMightNeedReconnect(false);
    }

    Q_EMIT settingsChanged();
}

bool ExternalServiceSettings::chargeStopThresholdMightNeedReconnect() const
{
    return m_chargeStopThresholdMightNeedReconnect;
}

void ExternalServiceSettings::setChargeStopThresholdMightNeedReconnect(bool mightNeedReconnect)
{
    if (mightNeedReconnect == m_chargeStopThresholdMightNeedReconnect) {
        return;
    }
    m_chargeStopThresholdMightNeedReconnect = mightNeedReconnect;
    Q_EMIT chargeStopThresholdMightNeedReconnectChanged();
}

} // namespace PowerDevil

#include "moc_ExternalServiceSettings.cpp"