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
|
/*
SPDX-FileCopyrightText: 2021 Tomaz Canabrava <tcanabrava@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "PowerProfileModel.h"
#include <powerdevilenums.h>
#include <KLocalizedString>
#include <QDBusConnection>
#include <QDBusMessage>
#include <QDBusPendingCallWatcher>
#include <QDBusPendingReply>
#include <QDebug>
using namespace Qt::StringLiterals;
PowerProfileModel::PowerProfileModel(QObject *parent)
: QAbstractListModel(parent)
{
/* Loads the values for the Power Profile modes */
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"),
QStringLiteral("/org/kde/Solid/PowerManagement/Actions/PowerProfile"),
QStringLiteral("org.kde.Solid.PowerManagement.Actions.PowerProfile"),
QStringLiteral("profileChoices"));
auto *watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(msg), this);
QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) {
QDBusPendingReply<QStringList> reply = *watcher;
watcher->deleteLater();
if (reply.isError()) {
qWarning() << "Failed to query platform profile choices" << reply.error().message();
return;
}
const PowerProfileModel::Data profiles[] = {
{.name = i18nc("@option:combobox Power profile", "Power Save"), .iconName = u"battery-profile-powersave"_s, .value = u"power-saver"_s},
{.name = i18nc("@option:combobox Power profile", "Balanced"), .iconName = u"battery-profile-balanced"_s, .value = u"balanced"_s},
{.name = i18nc("@option:combobox Power profile", "Performance"), .iconName = u"battery-profile-performance"_s, .value = u"performance"_s}};
beginResetModel();
m_data.clear();
m_data.append(PowerProfileModel::Data{.name = i18n("Leave unchanged"), .iconName = u"dialog-cancel-symbolic"_s, .value = QString()});
const QStringList availableProfiles = reply.value();
for (const auto &profile : profiles) {
if (availableProfiles.contains(profile.value)) {
m_data.append(profile);
}
}
endResetModel();
});
}
QVariant PowerProfileModel::data(const QModelIndex &index, int role) const
{
if (index.row() < 0 || index.row() >= m_data.size()) {
return {};
}
const auto &item = m_data.at(index.row());
switch (role) {
case Name:
return item.name;
case IconName:
return item.iconName;
case Value:
return item.value;
}
return {};
}
int PowerProfileModel::rowCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return static_cast<int>(m_data.size());
}
QHash<int, QByteArray> PowerProfileModel::roleNames() const
{
return QHash<int, QByteArray>{{Name, "name"_ba}, {IconName, "iconName"_ba}, {Value, "value"_ba}};
}
#include "moc_PowerProfileModel.cpp"
|