Changeset View
Changeset View
Standalone View
Standalone View
applets/kicker/plugin/systemmodel.cpp
Show All 14 Lines | |||||
15 | * along with this program; if not, write to the * | 15 | * along with this program; if not, write to the * | ||
16 | * Free Software Foundation, Inc., * | 16 | * Free Software Foundation, Inc., * | ||
17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||
18 | ***************************************************************************/ | 18 | ***************************************************************************/ | ||
19 | 19 | | |||
20 | #include "systemmodel.h" | 20 | #include "systemmodel.h" | ||
21 | #include "actionlist.h" | 21 | #include "actionlist.h" | ||
22 | #include "simplefavoritesmodel.h" | 22 | #include "simplefavoritesmodel.h" | ||
23 | #include "systementry.h" | | |||
24 | 23 | | |||
25 | #include <QStandardPaths> | 24 | #include <QStandardPaths> | ||
26 | 25 | | |||
27 | #include <KDirWatch> | 26 | #include <KDirWatch> | ||
28 | #include <KLocalizedString> | 27 | #include <KLocalizedString> | ||
29 | 28 | | |||
30 | SystemModel::SystemModel(QObject *parent) : AbstractModel(parent) | 29 | SystemModel::SystemModel(QObject *parent) : AbstractModel(parent) | ||
31 | { | 30 | { | ||
32 | init(); | | |||
33 | | ||||
34 | m_favoritesModel = new SimpleFavoritesModel(this); | 31 | m_favoritesModel = new SimpleFavoritesModel(this); | ||
35 | 32 | | |||
36 | const QString configFile = QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) + QStringLiteral("/ksmserverrc"); | 33 | m_entries[SystemEntry::LockSession] = new SystemEntry(this, SystemEntry::LockSession); | ||
37 | 34 | m_entries[SystemEntry::LogoutSession] = new SystemEntry(this, SystemEntry::LogoutSession); | |||
38 | KDirWatch *watch = new KDirWatch(this); | 35 | m_entries[SystemEntry::SaveSession] = new SystemEntry(this, SystemEntry::SaveSession); | ||
39 | 36 | m_entries[SystemEntry::SwitchUser] = new SystemEntry(this, SystemEntry::SwitchUser); | |||
40 | watch->addFile(configFile); | 37 | m_entries[SystemEntry::Suspend] = new SystemEntry(this, SystemEntry::Suspend); | ||
41 | 38 | m_entries[SystemEntry::Hibernate] = new SystemEntry(this, SystemEntry::Hibernate); | |||
42 | connect(watch, &KDirWatch::dirty, this, &SystemModel::refresh); | 39 | m_entries[SystemEntry::Reboot] = new SystemEntry(this, SystemEntry::Reboot); | ||
43 | connect(watch, &KDirWatch::created, this, &SystemModel::refresh); | 40 | m_entries[SystemEntry::Shutdown] = new SystemEntry(this, SystemEntry::Shutdown); | ||
41 | | ||||
42 | for (SystemEntry *entry : m_entries.values()) { | ||||
broulik: range-for gives you values already, no need for `values()` | |||||
43 | QObject::connect(entry, &SystemEntry::isValidChanged, this, | ||||
44 | [this, entry]() { | ||||
45 | const QModelIndex &idx = index(entry->action(), 0); | ||||
broulik: Just capture `entry` into the lambda | |||||
46 | emit dataChanged(idx, idx, QVector<int>{Kicker::DisabledRole}); | ||||
broulik: `static_cast`, also everywhere else | |||||
44 | } | 47 | } | ||
48 | ); | ||||
45 | 49 | | |||
46 | SystemModel::~SystemModel() | 50 | QObject::connect(entry, &SystemEntry::isValidChanged, m_favoritesModel, &AbstractModel::refresh); | ||
47 | { | 51 | } | ||
48 | qDeleteAll(m_entryList); | | |||
49 | } | 52 | } | ||
50 | 53 | | |||
51 | void SystemModel::init() | 54 | SystemModel::~SystemModel() | ||
52 | { | 55 | { | ||
53 | QList<SystemEntry *> actions; | 56 | qDeleteAll(m_entries); | ||
davidedmundson: qDeleteAll(m_entries) | |||||
54 | | ||||
55 | actions << new SystemEntry(this, SystemEntry::LockSession); | | |||
56 | actions << new SystemEntry(this, SystemEntry::LogoutSession); | | |||
57 | actions << new SystemEntry(this, SystemEntry::SaveSession); | | |||
58 | actions << new SystemEntry(this, SystemEntry::SwitchUser); | | |||
59 | actions << new SystemEntry(this, SystemEntry::SuspendToRam); | | |||
60 | actions << new SystemEntry(this, SystemEntry::SuspendToDisk); | | |||
61 | actions << new SystemEntry(this, SystemEntry::Reboot); | | |||
62 | actions << new SystemEntry(this, SystemEntry::Shutdown); | | |||
63 | | ||||
64 | foreach(SystemEntry *entry, actions) { | | |||
65 | if (entry->isValid()) { | | |||
66 | m_entryList << entry; | | |||
67 | } else { | | |||
68 | delete entry; | | |||
69 | } | | |||
70 | } | | |||
71 | } | 57 | } | ||
72 | 58 | | |||
73 | QString SystemModel::description() const | 59 | QString SystemModel::description() const | ||
74 | { | 60 | { | ||
75 | return i18n("System actions"); | 61 | return i18n("System actions"); | ||
76 | } | 62 | } | ||
77 | 63 | | |||
78 | QVariant SystemModel::data(const QModelIndex &index, int role) const | 64 | QVariant SystemModel::data(const QModelIndex &index, int role) const | ||
79 | { | 65 | { | ||
80 | if (!index.isValid() || index.row() >= m_entryList.count()) { | 66 | if (!index.isValid() || index.row() >= m_entries.count()) { | ||
81 | return QVariant(); | 67 | return QVariant(); | ||
82 | } | 68 | } | ||
83 | 69 | | |||
84 | const SystemEntry *entry = m_entryList.at(index.row()); | 70 | const SystemEntry *entry = m_entries.value(static_cast<SystemEntry::Action>(index.row() + 1)); | ||
85 | 71 | | |||
86 | if (role == Qt::DisplayRole) { | 72 | if (role == Qt::DisplayRole) { | ||
87 | return entry->name(); | 73 | return entry->name(); | ||
88 | } else if (role == Qt::DecorationRole) { | 74 | } else if (role == Qt::DecorationRole) { | ||
89 | return entry->iconName(); | 75 | return entry->iconName(); | ||
90 | } else if (role == Kicker::DescriptionRole) { | 76 | } else if (role == Kicker::DescriptionRole) { | ||
91 | return entry->description(); | 77 | return entry->description(); | ||
92 | } else if (role == Kicker::GroupRole) { | 78 | } else if (role == Kicker::GroupRole) { | ||
93 | return entry->group(); | 79 | return entry->group(); | ||
94 | } else if (role == Kicker::FavoriteIdRole) { | 80 | } else if (role == Kicker::FavoriteIdRole) { | ||
95 | return entry->id(); | 81 | return entry->id(); | ||
96 | } else if (role == Kicker::HasActionListRole) { | 82 | } else if (role == Kicker::HasActionListRole) { | ||
97 | return entry->hasActions(); | 83 | return entry->hasActions(); | ||
98 | } else if (role == Kicker::ActionListRole) { | 84 | } else if (role == Kicker::ActionListRole) { | ||
99 | return entry->actions(); | 85 | return entry->actions(); | ||
86 | } else if (role == Kicker::DisabledRole) { | ||||
87 | return !entry->isValid(); | ||||
100 | } | 88 | } | ||
101 | 89 | | |||
102 | return QVariant(); | 90 | return QVariant(); | ||
103 | } | 91 | } | ||
104 | 92 | | |||
105 | int SystemModel::rowCount(const QModelIndex &parent) const | 93 | int SystemModel::rowCount(const QModelIndex &parent) const | ||
106 | { | 94 | { | ||
107 | return parent.isValid() ? 0 : m_entryList.count(); | 95 | return parent.isValid() ? 0 : m_entries.count(); | ||
108 | } | 96 | } | ||
109 | 97 | | |||
110 | bool SystemModel::trigger(int row, const QString &actionId, const QVariant &argument) | 98 | bool SystemModel::trigger(int row, const QString &actionId, const QVariant &argument) | ||
111 | { | 99 | { | ||
112 | if (row >= 0 && row < m_entryList.count()) { | 100 | if (row >= 0 && row < m_entries.count()) { | ||
113 | m_entryList.at(row)->run(actionId, argument); | 101 | m_entries.value(static_cast<SystemEntry::Action>(row + 1))->run(actionId, argument); | ||
114 | 102 | | |||
115 | return true; | 103 | return true; | ||
116 | } | 104 | } | ||
117 | 105 | | |||
118 | return false; | 106 | return false; | ||
119 | } | 107 | } | ||
120 | 108 | | |||
121 | void SystemModel::refresh() | 109 | void SystemModel::refresh() | ||
122 | { | 110 | { | ||
123 | beginResetModel(); | | |||
124 | | ||||
125 | qDeleteAll(m_entryList); | | |||
126 | m_entryList.clear(); | | |||
127 | | ||||
128 | init(); | | |||
129 | | ||||
130 | endResetModel(); | | |||
131 | | ||||
132 | emit countChanged(); | | |||
133 | | ||||
134 | m_favoritesModel->refresh(); | 111 | m_favoritesModel->refresh(); | ||
135 | } | 112 | } |
range-for gives you values already, no need for values()