File: SleepModeModel.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 (76 lines) | stat: -rw-r--r-- 2,542 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
/*
 *  SPDX-FileCopyrightText: 2023 Jakob Petsovits <jpetso@petsovits.com>
 *
 *  SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

#include "SleepModeModel.h"

#include <powerdevilenums.h>
#include <powerdevilpowermanagement.h>

#include <KLocalizedString>

#include <type_traits> // std::is_same_v, std::underlying_type_t

SleepModeModel::SleepModeModel(QObject *parent, PowerDevil::PowerManagement *pm, bool isPowerSupplyBatteryPresent)
    : QAbstractListModel(parent)
{
    if (pm->canSuspend()) {
        m_data.append(Data{
            .name = i18nc("Suspend to RAM", "Standby"),
            .subtitle = i18nc("Subtitle description for 'Standby' sleep option", "Save session to memory"),
            .value = qToUnderlying(PowerDevil::SleepMode::SuspendToRam),
        });
    }

    if (pm->canHybridSuspend()) {
        m_data.append(Data{
            .name = i18n("Hybrid sleep"),
            .subtitle = i18nc("Subtitle description for 'Hybrid sleep' sleep option", "Save session to both memory and disk"),
            .value = qToUnderlying(PowerDevil::SleepMode::HybridSuspend),
        });
    }

    if (pm->canSuspendThenHibernate()) {
        m_data.append(Data{
            .name = i18n("Standby, then hibernate"),
            .subtitle = isPowerSupplyBatteryPresent
                ? i18nc("Subtitle description for 'Standby, then hibernate' sleep option", "Switch to hibernation when battery runs low")
                : i18nc("Subtitle description for 'Standby, then hibernate' sleep option", "Switch to hibernation after a period of inactivity"),
            .value = qToUnderlying(PowerDevil::SleepMode::SuspendThenHibernate),
        });
    }
}

QVariant SleepModeModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() >= m_data.size()) {
        return {};
    }

    switch (role) {
    case Name:
        return m_data[index.row()].name;
    case Subtitle:
        return m_data[index.row()].subtitle;
    case Value:
        static_assert(std::is_same_v<decltype(Data::value), std::underlying_type_t<PowerDevil::SleepMode>>);
        return m_data[index.row()].value;
    default:
        return {};
    }
}

int SleepModeModel::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent);
    return static_cast<int>(m_data.size());
}

QHash<int, QByteArray> SleepModeModel::roleNames() const
{
    return QHash<int, QByteArray>{{Name, "name"}, {Subtitle, "subtext"}, {Value, "value"}};
}

#include "moc_SleepModeModel.cpp"