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: return item.status; | ||||
69 | case ModuleNameRole: return item.moduleName; | ||||
70 | } | ||||
71 | | ||||
72 | return QVariant(); | ||||
73 | } | ||||
74 | | ||||
75 | bool ModulesModel::setData(const QModelIndex &index, const QVariant &value, int role) | ||||
76 | { | ||||
77 | bool dirty = false; | ||||
78 | | ||||
79 | if (!checkIndex(index)) { | ||||
80 | return dirty; | ||||
81 | } | ||||
82 | | ||||
83 | auto &item = m_data[index.row()]; | ||||
84 | | ||||
85 | switch (role) { | ||||
86 | case AutoloadEnabledRole: | ||||
87 | const bool autoloadEnabled = value.toBool(); | ||||
88 | if (item.type == KDEDConfig::AutostartType | ||||
89 | && item.autoloadEnabled != autoloadEnabled) { | ||||
90 | item.autoloadEnabled = value.toBool(); | ||||
91 | dirty = true; | ||||
92 | | ||||
93 | emit autoloadedModulesChanged(); | ||||
94 | } | ||||
95 | break; | ||||
96 | } | ||||
97 | | ||||
98 | if (dirty) { | ||||
99 | emit dataChanged(index, index, {role}); | ||||
100 | } | ||||
101 | | ||||
102 | return dirty; | ||||
103 | } | ||||
104 | | ||||
105 | QHash<int, QByteArray> ModulesModel::roleNames() const | ||||
106 | { | ||||
107 | return { | ||||
108 | {Qt::DisplayRole, QByteArrayLiteral("display")}, | ||||
109 | {DescriptionRole, QByteArrayLiteral("description")}, | ||||
110 | {TypeRole, QByteArrayLiteral("type")}, | ||||
111 | {AutoloadEnabledRole, QByteArrayLiteral("autoloadEnabled")}, | ||||
112 | {StatusRole, QByteArrayLiteral("status")}, | ||||
113 | {ModuleNameRole, QByteArrayLiteral("moduleName")}, | ||||
114 | }; | ||||
115 | } | ||||
116 | | ||||
117 | // This code was copied from kded.cpp | ||||
118 | // TODO: move this KCM to the KDED framework and share the code? | ||||
119 | static QVector<KPluginMetaData> availableModules() | ||||
120 | { | ||||
121 | QVector<KPluginMetaData> plugins = KPluginLoader::findPlugins(QStringLiteral("kf5/kded")); | ||||
122 | QSet<QString> moduleIds; | ||||
123 | for (const KPluginMetaData &md : qAsConst(plugins)) { | ||||
124 | moduleIds.insert(md.pluginId()); | ||||
125 | } | ||||
126 | // also search for old .desktop based kded modules | ||||
127 | const KPluginInfo::List oldStylePlugins = KPluginInfo::fromServices(KServiceTypeTrader::self()->query(QStringLiteral("KDEDModule"))); | ||||
128 | for (const KPluginInfo &info : oldStylePlugins) { | ||||
129 | if (moduleIds.contains(info.pluginName())) { | ||||
130 | qCWarning(KCM_KDED).nospace() << "kded module " << info.pluginName() << " has already been found using " | ||||
131 | "JSON metadata, please don't install the now unneeded .desktop file (" << info.entryPath() << ")."; | ||||
132 | } else { | ||||
133 | qCDebug(KCM_KDED).nospace() << "kded module " << info.pluginName() << " still uses .desktop files (" | ||||
134 | << info.entryPath() << "). Please port it to JSON metadata."; | ||||
135 | plugins.append(info.toMetaData()); | ||||
136 | } | ||||
137 | } | ||||
138 | return plugins; | ||||
139 | } | ||||
140 | | ||||
141 | // this code was copied from kded.cpp | ||||
142 | static bool isModuleLoadedOnDemand(const KPluginMetaData &module) | ||||
143 | { | ||||
144 | bool loadOnDemand = true; | ||||
145 | // use toVariant() since it could be string or bool in the json and QJsonObject does not convert | ||||
146 | QVariant p = module.rawData().value(QStringLiteral("X-KDE-Kded-load-on-demand")).toVariant(); | ||||
147 | if (p.isValid() && p.canConvert<bool>() && (p.toBool() == false)) { | ||||
148 | loadOnDemand = false; | ||||
149 | } | ||||
150 | return loadOnDemand; | ||||
151 | } | ||||
152 | | ||||
153 | void ModulesModel::load() | ||||
154 | { | ||||
155 | beginResetModel(); | ||||
156 | | ||||
157 | m_data.clear(); | ||||
158 | | ||||
159 | KConfig kdedrc(QStringLiteral("kded5rc"), KConfig::NoGlobals); | ||||
160 | | ||||
161 | QVector<ModulesModelData> autostartModules; | ||||
162 | QVector<ModulesModelData> onDemandModules; | ||||
163 | | ||||
164 | const auto modules = availableModules(); | ||||
165 | for (const KPluginMetaData &module : modules) { | ||||
166 | QString servicePath = module.metaDataFileName(); | ||||
167 | | ||||
168 | // autoload defaults to false if it is not found | ||||
169 | const bool autoload = module.rawData().value(QStringLiteral("X-KDE-Kded-autoload")).toVariant().toBool(); | ||||
170 | // keep estimating dbusModuleName in sync with KDEDModule (kdbusaddons) and kded (kded) | ||||
171 | // currently (KF5) the module name in the D-Bus object path is set by the pluginId | ||||
172 | const QString dbusModuleName = module.pluginId(); | ||||
173 | qCDebug(KCM_KDED) << "reading kded info from" << servicePath << "autoload =" << autoload << "dbus module name =" << dbusModuleName; | ||||
174 | | ||||
175 | KConfigGroup cg(&kdedrc, QStringLiteral("Module-%1").arg(dbusModuleName)); | ||||
176 | const bool autoloadEnabled = cg.readEntry("autoload", true); | ||||
177 | | ||||
178 | ModulesModelData data{ | ||||
179 | module.name(), | ||||
180 | module.description(), | ||||
181 | KDEDConfig::UnknownType, | ||||
182 | autoloadEnabled, | ||||
183 | KDEDConfig::UnknownStatus, | ||||
184 | dbusModuleName | ||||
185 | }; | ||||
186 | | ||||
187 | // The logic has to be identical to Kded::initModules. | ||||
188 | // They interpret X-KDE-Kded-autoload as false if not specified | ||||
189 | // X-KDE-Kded-load-on-demand as true if not specified | ||||
190 | if (autoload) { | ||||
191 | data.type = KDEDConfig::AutostartType; | ||||
192 | autostartModules << data; | ||||
193 | } else if (isModuleLoadedOnDemand(module)) { | ||||
194 | data.type = KDEDConfig::OnDemandType; | ||||
195 | onDemandModules << data; | ||||
196 | } else { | ||||
197 | qCWarning(KCM_KDED) << "kcmkded: Module " << module.name() << "from file" << module.metaDataFileName() << " not loaded on demand or startup! Skipping."; | ||||
198 | continue; | ||||
199 | } | ||||
200 | } | ||||
201 | | ||||
202 | QCollator collator; | ||||
203 | // Otherwise "Write" daemon with quotes will be at the top | ||||
204 | collator.setIgnorePunctuation(true); | ||||
205 | auto sortAlphabetically = [&collator](const ModulesModelData &a, const ModulesModelData &b) { | ||||
206 | return collator.compare(a.display, b.display) < 0; | ||||
207 | }; | ||||
208 | | ||||
209 | std::sort(autostartModules.begin(), autostartModules.end(), sortAlphabetically); | ||||
210 | std::sort(onDemandModules.begin(), onDemandModules.end(), sortAlphabetically); | ||||
211 | | ||||
212 | m_data << autostartModules << onDemandModules; | ||||
213 | | ||||
214 | endResetModel(); | ||||
215 | } | ||||
216 | | ||||
217 | QStringList ModulesModel::runningModules() const | ||||
218 | { | ||||
219 | return m_runningModules; | ||||
220 | } | ||||
221 | | ||||
222 | void ModulesModel::setRunningModules(const QStringList &runningModules) | ||||
223 | { | ||||
224 | if (m_runningModules == runningModules) { | ||||
225 | return; | ||||
226 | } | ||||
227 | | ||||
228 | m_runningModules = runningModules; | ||||
229 | | ||||
230 | for (auto it = m_data.begin(), end = m_data.end(); it != end; ++it) { | ||||
231 | if (runningModules.contains(it->moduleName)) { | ||||
232 | it->status = KDEDConfig::Running; | ||||
233 | } else { | ||||
234 | it->status = KDEDConfig::NotRunning; | ||||
235 | } | ||||
236 | } | ||||
237 | | ||||
238 | emit dataChanged(index(0, 0), index(m_data.count() - 1, 0), {StatusRole}); | ||||
239 | } |