Changeset View
Changeset View
Standalone View
Standalone View
kcms/kded/modulesmodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2020 Kai Uwe Broulik <kde@broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "modulesmodel.h" | ||||
22 | | ||||
23 | #include <QCollator> | ||||
24 | | ||||
25 | #include <KConfig> | ||||
26 | #include <KConfigGroup> | ||||
27 | #include <KPluginInfo> | ||||
28 | #include <KPluginLoader> | ||||
29 | #include <KServiceTypeTrader> | ||||
30 | | ||||
31 | #include <algorithm> | ||||
32 | | ||||
33 | #include "debug.h" | ||||
34 | | ||||
35 | ModulesModel::ModulesModel(QObject *parent) : QAbstractListModel(parent) | ||||
36 | { | ||||
37 | | ||||
38 | } | ||||
39 | | ||||
40 | ModulesModel::~ModulesModel() = default; | ||||
41 | | ||||
42 | int ModulesModel::rowCount(const QModelIndex &parent) const | ||||
43 | { | ||||
44 | if (parent.isValid()) { | ||||
45 | return 0; | ||||
46 | } | ||||
47 | | ||||
48 | return m_data.count(); | ||||
49 | } | ||||
50 | | ||||
51 | QVariant ModulesModel::data(const QModelIndex &index, int role) const | ||||
52 | { | ||||
53 | if (!checkIndex(index)) { | ||||
54 | return QVariant(); | ||||
55 | } | ||||
56 | | ||||
57 | const auto &item = m_data.at(index.row()); | ||||
58 | | ||||
59 | switch (role) { | ||||
60 | case Qt::DisplayRole: return item.display; | ||||
61 | case DescriptionRole: return item.description; | ||||
62 | case TypeRole: return item.type; | ||||
63 | case AutoloadEnabledRole: | ||||
64 | if (item.type == KDEDConfig::AutostartType) { | ||||
65 | return item.autoloadEnabled; | ||||
66 | } | ||||
67 | return QVariant(); | ||||
68 | case StatusRole: { | ||||
69 | if (!m_runningModulesKnown) { | ||||
70 | return KDEDConfig::UnknownStatus; | ||||
71 | } | ||||
72 | if (m_runningModules.contains(item.moduleName)) { | ||||
73 | return KDEDConfig::Running; | ||||
74 | } | ||||
75 | return KDEDConfig::NotRunning; | ||||
76 | } | ||||
77 | case ModuleNameRole: return item.moduleName; | ||||
78 | } | ||||
79 | | ||||
80 | return QVariant(); | ||||
81 | } | ||||
82 | | ||||
83 | bool ModulesModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||||
84 | { | ||||
85 | bool dirty = false; | ||||
86 | | ||||
87 | if (!checkIndex(index)) { | ||||
88 | return dirty; | ||||
89 | } | ||||
90 | | ||||
91 | auto &item = m_data[index.row()]; | ||||
92 | | ||||
93 | switch (role) { | ||||
94 | case AutoloadEnabledRole: | ||||
95 | const bool autoloadEnabled = value.toBool(); | ||||
96 | if (item.type == KDEDConfig::AutostartType | ||||
97 | && item.autoloadEnabled != autoloadEnabled) { | ||||
98 | item.autoloadEnabled = value.toBool(); | ||||
99 | dirty = true; | ||||
100 | | ||||
101 | emit autoloadedModulesChanged(); | ||||
102 | } | ||||
103 | break; | ||||
104 | } | ||||
105 | | ||||
106 | if (dirty) { | ||||
107 | emit dataChanged(index, index, {role}); | ||||
108 | } | ||||
109 | | ||||
110 | return dirty; | ||||
111 | } | ||||
112 | | ||||
113 | QHash<int, QByteArray> ModulesModel::roleNames() const | ||||
114 | { | ||||
115 | return { | ||||
116 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | ||||
117 | {DescriptionRole, QByteArrayLiteral("description")}, | ||||
118 | {TypeRole, QByteArrayLiteral("type")}, | ||||
119 | {AutoloadEnabledRole, QByteArrayLiteral("autoloadEnabled")}, | ||||
120 | {StatusRole, QByteArrayLiteral("status")}, | ||||
121 | {ModuleNameRole, QByteArrayLiteral("moduleName")}, | ||||
122 | }; | ||||
123 | } | ||||
124 | | ||||
125 | // This code was copied from kded.cpp | ||||
126 | // TODO: move this KCM to the KDED framework and share the code? | ||||
127 | static QVector<KPluginMetaData> availableModules() | ||||
128 | { | ||||
129 | QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("kf5/kded")); | ||||
130 | QSet<QString> moduleIds; | ||||
131 | for (const KPluginMetaData &md : qAsConst(plugins)) { | ||||
132 | moduleIds.insert(md.pluginId()); | ||||
133 | } | ||||
134 | // also search for old .desktop based kded modules | ||||
135 | const KPluginInfo::List oldStylePlugins = KPluginInfo::fromServices(KServiceTypeTrader::self()->query(QStringLiteral("KDEDModule"))); | ||||
136 | for (const KPluginInfo &info : oldStylePlugins) { | ||||
137 | if (moduleIds.contains(info.pluginName())) { | ||||
138 | qCWarning(KCM_KDED).nospace() << "kded module " << info.pluginName() << " has already been found using " | ||||
139 | "JSON metadata, please don't install the now unneeded .desktop file (" << info.entryPath() << ")."; | ||||
140 | } else { | ||||
141 | qCDebug(KCM_KDED).nospace() << "kded module " << info.pluginName() << " still uses .desktop files (" | ||||
142 | << info.entryPath() << "). Please port it to JSON metadata."; | ||||
143 | plugins.append(info.toMetaData()); | ||||
144 | } | ||||
145 | } | ||||
146 | return plugins; | ||||
147 | } | ||||
148 | | ||||
149 | // this code was copied from kded.cpp | ||||
150 | static bool isModuleLoadedOnDemand(const KPluginMetaData &module) | ||||
151 | { | ||||
152 | bool loadOnDemand = true; | ||||
153 | // use toVariant() since it could be string or bool in the json and QJsonObject does not convert | ||||
154 | QVariant p = module.rawData().value(QStringLiteral("X-KDE-Kded-load-on-demand")).toVariant(); | ||||
155 | if (p.isValid() && p.canConvert<bool>() && (p.toBool() == false)) { | ||||
156 | loadOnDemand = false; | ||||
157 | } | ||||
158 | return loadOnDemand; | ||||
159 | } | ||||
160 | | ||||
161 | void ModulesModel::load() | ||||
162 | { | ||||
163 | beginResetModel(); | ||||
164 | | ||||
165 | m_data.clear(); | ||||
166 | | ||||
167 | KConfig kdedrc(QStringLiteral("kded5rc"), KConfig::NoGlobals); | ||||
168 | | ||||
169 | QStringList knownModules; | ||||
170 | | ||||
171 | QVector<ModulesModelData> autostartModules; | ||||
172 | QVector<ModulesModelData> onDemandModules; | ||||
173 | | ||||
174 | const auto modules = availableModules(); | ||||
175 | for (const KPluginMetaData &module : modules) { | ||||
176 | QString servicePath = module.metaDataFileName(); | ||||
177 | | ||||
178 | // autoload defaults to false if it is not found | ||||
179 | const bool autoload = module.rawData().value(QStringLiteral("X-KDE-Kded-autoload")).toVariant().toBool(); | ||||
180 | // keep estimating dbusModuleName in sync with KDEDModule (kdbusaddons) and kded (kded) | ||||
181 | // currently (KF5) the module name in the D-Bus object path is set by the pluginId | ||||
182 | const QString dbusModuleName = module.pluginId(); | ||||
183 | qCDebug(KCM_KDED) << "reading kded info from" << servicePath << "autoload =" << autoload << "dbus module name =" << dbusModuleName; | ||||
184 | | ||||
185 | if (knownModules.contains(dbusModuleName)) { | ||||
186 | continue; | ||||
187 | } | ||||
188 | | ||||
189 | knownModules.append(dbusModuleName); | ||||
190 | | ||||
191 | KConfigGroup cg(&kdedrc, QStringLiteral("Module-%1").arg(dbusModuleName)); | ||||
192 | const bool autoloadEnabled = cg.readEntry("autoload", true); | ||||
193 | | ||||
194 | ModulesModelData data{ | ||||
195 | module.name(), | ||||
196 | module.description(), | ||||
197 | KDEDConfig::UnknownType, | ||||
198 | autoloadEnabled, | ||||
199 | dbusModuleName | ||||
200 | }; | ||||
201 | | ||||
202 | // The logic has to be identical to Kded::initModules. | ||||
203 | // They interpret X-KDE-Kded-autoload as false if not specified | ||||
204 | // X-KDE-Kded-load-on-demand as true if not specified | ||||
205 | if (autoload) { | ||||
206 | data.type = KDEDConfig::AutostartType; | ||||
207 | autostartModules << data; | ||||
208 | } else if (isModuleLoadedOnDemand(module)) { | ||||
209 | data.type = KDEDConfig::OnDemandType; | ||||
210 | onDemandModules << data; | ||||
211 | } else { | ||||
212 | qCWarning(KCM_KDED) << "kcmkded: Module " << module.name() << "from file" << module.metaDataFileName() << " not loaded on demand or startup! Skipping."; | ||||
213 | continue; | ||||
214 | } | ||||
215 | } | ||||
216 | | ||||
217 | QCollator collator; | ||||
218 | // Otherwise "Write" daemon with quotes will be at the top | ||||
219 | collator.setIgnorePunctuation(true); | ||||
220 | auto sortAlphabetically = [&collator](const ModulesModelData &a, const ModulesModelData &b) { | ||||
221 | return collator.compare(a.display, b.display) < 0; | ||||
222 | }; | ||||
223 | | ||||
224 | std::sort(autostartModules.begin(), autostartModules.end(), sortAlphabetically); | ||||
225 | std::sort(onDemandModules.begin(), onDemandModules.end(), sortAlphabetically); | ||||
226 | | ||||
227 | m_data << autostartModules << onDemandModules; | ||||
228 | | ||||
229 | endResetModel(); | ||||
230 | } | ||||
231 | | ||||
232 | bool ModulesModel::runningModulesKnown() const | ||||
233 | { | ||||
234 | return m_runningModulesKnown; | ||||
235 | } | ||||
236 | | ||||
237 | void ModulesModel::setRunningModulesKnown(bool known) | ||||
238 | { | ||||
239 | if (m_runningModulesKnown != known) { | ||||
240 | m_runningModulesKnown = known; | ||||
241 | emit dataChanged(index(0, 0), index(m_data.count() - 1, 0), {StatusRole}); | ||||
242 | } | ||||
243 | } | ||||
244 | | ||||
245 | QStringList ModulesModel::runningModules() const | ||||
246 | { | ||||
247 | return m_runningModules; | ||||
248 | } | ||||
249 | | ||||
250 | void ModulesModel::setRunningModules(const QStringList &runningModules) | ||||
251 | { | ||||
252 | if (m_runningModules == runningModules) { | ||||
253 | return; | ||||
254 | } | ||||
255 | | ||||
256 | m_runningModules = runningModules; | ||||
257 | if (m_runningModulesKnown) { | ||||
258 | emit dataChanged(index(0, 0), index(m_data.count() - 1, 0), {StatusRole}); | ||||
259 | } | ||||
260 | } |