File indexing completed on 2024-05-12 05:38:47
0001 /* 0002 SPDX-FileCopyrightText: 2021 Tomaz Canabrava <tcanabrava@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0005 */ 0006 0007 #include "PowerProfileModel.h" 0008 0009 #include <powerdevilenums.h> 0010 0011 #include <KLocalizedString> 0012 0013 #include <QDBusConnection> 0014 #include <QDBusMessage> 0015 #include <QDBusPendingCallWatcher> 0016 #include <QDBusPendingReply> 0017 #include <QDebug> 0018 0019 PowerProfileModel::PowerProfileModel(QObject *parent) 0020 : QAbstractListModel(parent) 0021 { 0022 /* Loads the values for the Power Profile modes */ 0023 QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.Solid.PowerManagement"), 0024 QStringLiteral("/org/kde/Solid/PowerManagement/Actions/PowerProfile"), 0025 QStringLiteral("org.kde.Solid.PowerManagement.Actions.PowerProfile"), 0026 QStringLiteral("profileChoices")); 0027 0028 auto *watcher = new QDBusPendingCallWatcher(QDBusConnection::sessionBus().asyncCall(msg), this); 0029 0030 QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](QDBusPendingCallWatcher *watcher) { 0031 QDBusPendingReply<QStringList> reply = *watcher; 0032 watcher->deleteLater(); 0033 0034 if (reply.isError()) { 0035 qWarning() << "Failed to query platform profile choices" << reply.error().message(); 0036 return; 0037 } 0038 0039 const QHash<QString, QString> profileNames = { 0040 {QStringLiteral("power-saver"), i18nc("@option:combobox Power profile", "Power Save")}, 0041 {QStringLiteral("balanced"), i18nc("@option:combobox Power profile", "Balanced")}, 0042 {QStringLiteral("performance"), i18nc("@option:combobox Power profile", "Performance")}, 0043 }; 0044 0045 beginResetModel(); 0046 m_data.clear(); 0047 m_data.append(PowerProfileModel::Data{.name = i18n("Leave unchanged"), .value = QString()}); 0048 0049 for (const QString &choice : reply.value()) { 0050 m_data.append(PowerProfileModel::Data{ 0051 .name = profileNames.value(choice, choice), 0052 .value = choice, 0053 }); 0054 } 0055 endResetModel(); 0056 }); 0057 } 0058 0059 QVariant PowerProfileModel::data(const QModelIndex &index, int role) const 0060 { 0061 if (index.row() > m_data.size()) { 0062 return {}; 0063 } 0064 0065 switch (role) { 0066 case Name: 0067 return m_data[index.row()].name; 0068 case Value: 0069 return m_data[index.row()].value; 0070 default: 0071 return {}; 0072 } 0073 } 0074 0075 int PowerProfileModel::rowCount(const QModelIndex &parent) const 0076 { 0077 Q_UNUSED(parent); 0078 return static_cast<int>(m_data.size()); 0079 } 0080 0081 QHash<int, QByteArray> PowerProfileModel::roleNames() const 0082 { 0083 return QHash<int, QByteArray>{{Name, "name"}, {Value, "value"}}; 0084 }