File indexing completed on 2024-05-12 05:38:47

0001 /*
0002  *  SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com>
0003  *
0004  *  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005  */
0006 
0007 #include "SleepModeModel.h"
0008 
0009 #include <powerdevilenums.h>
0010 #include <powerdevilpowermanagement.h>
0011 
0012 #include <KLocalizedString>
0013 
0014 #include <type_traits> // std::is_same_v, std::underlying_type_t
0015 
0016 SleepModeModel::SleepModeModel(QObject *parent, PowerDevil::PowerManagement *pm)
0017     : QAbstractListModel(parent)
0018 {
0019     if (pm->canSuspend()) {
0020         m_data.append(Data{
0021             .name = i18nc("Suspend to RAM", "Standby"),
0022             .subtitle = i18nc("Subtitle description for 'Standby' sleep option", "Save session to memory"),
0023             .value = qToUnderlying(PowerDevil::SleepMode::SuspendToRam),
0024         });
0025     }
0026 
0027     if (pm->canHybridSuspend()) {
0028         m_data.append(Data{
0029             .name = i18n("Hybrid sleep"),
0030             .subtitle = i18nc("Subtitle description for 'Hybrid sleep' sleep option", "Save session to both memory and disk"),
0031             .value = qToUnderlying(PowerDevil::SleepMode::HybridSuspend),
0032         });
0033     }
0034 
0035     if (pm->canSuspendThenHibernate()) {
0036         m_data.append(Data{
0037             .name = i18n("Standby, then hibernate"),
0038             .subtitle = i18nc("Subtitle description for 'Standby, then hibernate' sleep option", "Switch to hibernation after a period of inactivity"),
0039             .value = qToUnderlying(PowerDevil::SleepMode::SuspendThenHibernate),
0040         });
0041     }
0042 }
0043 
0044 QVariant SleepModeModel::data(const QModelIndex &index, int role) const
0045 {
0046     if (index.row() > m_data.size()) {
0047         return {};
0048     }
0049 
0050     switch (role) {
0051     case Name:
0052         return m_data[index.row()].name;
0053     case Subtitle:
0054         return m_data[index.row()].subtitle;
0055     case Value:
0056         static_assert(std::is_same_v<decltype(Data::value), std::underlying_type_t<PowerDevil::SleepMode>>);
0057         return m_data[index.row()].value;
0058     default:
0059         return {};
0060     }
0061 }
0062 
0063 int SleepModeModel::rowCount(const QModelIndex &parent) const
0064 {
0065     Q_UNUSED(parent);
0066     return static_cast<int>(m_data.size());
0067 }
0068 
0069 QHash<int, QByteArray> SleepModeModel::roleNames() const
0070 {
0071     return QHash<int, QByteArray>{{Name, "name"}, {Subtitle, "subtext"}, {Value, "value"}};
0072 }