File: systemmodel.cpp

package info (click to toggle)
plasma-workspace 4%3A6.3.6-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 104,900 kB
  • sloc: cpp: 125,434; xml: 31,579; python: 3,976; perl: 572; sh: 234; javascript: 74; ruby: 39; ansic: 13; makefile: 9
file content (120 lines) | stat: -rw-r--r-- 3,048 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
/*
    SPDX-FileCopyrightText: 2014-2015 Eike Hein <hein@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "systemmodel.h"
#include "actionlist.h"
#include "simplefavoritesmodel.h"

#include <QStandardPaths>

#include <KDirWatch>
#include <KLocalizedString>

SystemModel::SystemModel(QObject *parent)
    : AbstractModel(parent)
{
    m_favoritesModel = new SimpleFavoritesModel(this);

    populate();
}

SystemModel::~SystemModel()
{
    qDeleteAll(m_entries);
}

QString SystemModel::description() const
{
    return i18n("System actions");
}

QVariant SystemModel::data(const QModelIndex &index, int role) const
{
    if (!index.isValid() || index.row() >= m_entries.count()) {
        return QVariant();
    }

    const SystemEntry *entry = m_entries.value(index.row());

    if (role == Qt::DisplayRole) {
        return entry->name();
    } else if (role == Qt::DecorationRole) {
        return entry->iconName();
    } else if (role == Kicker::CompactNameRole) {
        return entry->compactName();
    } else if (role == Kicker::DescriptionRole) {
        return entry->description();
    } else if (role == Kicker::GroupRole) {
        return entry->group();
    } else if (role == Kicker::FavoriteIdRole) {
        return entry->id();
    } else if (role == Kicker::HasActionListRole) {
        return entry->hasActions();
    } else if (role == Kicker::ActionListRole) {
        return entry->actions();
    } else if (role == Kicker::DisabledRole) {
        return !entry->isValid();
    }

    return QVariant();
}

int SystemModel::rowCount(const QModelIndex &parent) const
{
    return parent.isValid() ? 0 : m_entries.count();
}

bool SystemModel::trigger(int row, const QString &actionId, const QVariant &argument)
{
    if (row >= 0 && row < m_entries.count()) {
        m_entries.at(row)->run(actionId, argument);

        return true;
    }

    return false;
}

void SystemModel::refresh()
{
    beginResetModel();
    populate();
    endResetModel();

    m_favoritesModel->refresh();
}

void SystemModel::populate()
{
    qDeleteAll(m_entries);
    qDeleteAll(m_invalidEntries);
    m_entries.clear();
    m_invalidEntries.clear();

    auto addIfValid = [=, this](const SystemEntry::Action action) {
        SystemEntry *entry = new SystemEntry(this, action);
        QObject::connect(entry, &SystemEntry::sessionManagementStateChanged, this, &SystemModel::sessionManagementStateChanged);

        if (entry->isValid()) {
            m_entries << entry;
        } else {
            m_invalidEntries << entry;
        }

        QObject::connect(entry, &SystemEntry::isValidChanged, this, &AbstractModel::refresh, Qt::UniqueConnection);
    };

    addIfValid(SystemEntry::LockSession);
    addIfValid(SystemEntry::LogoutSession);
    addIfValid(SystemEntry::SaveSession);
    addIfValid(SystemEntry::SwitchUser);
    addIfValid(SystemEntry::Suspend);
    addIfValid(SystemEntry::Hibernate);
    addIfValid(SystemEntry::Reboot);
    addIfValid(SystemEntry::Shutdown);
}

#include "moc_systemmodel.cpp"