Changeset View
Changeset View
Standalone View
Standalone View
applets/kicker/plugin/kastatsfavoritesmodel.cpp
- This file was added.
1 | /*************************************************************************** | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2014-2015 by Eike Hein <hein@kde.org> * | ||||
3 | * Copyright (C) 2016-2017 by Ivan Cukic <ivan.cukic@kde.org> * | ||||
4 | * * | ||||
5 | * This program is free software; you can redistribute it and/or modify * | ||||
6 | * it under the terms of the GNU General Public License as published by * | ||||
7 | * the Free Software Foundation; either version 2 of the License, or * | ||||
8 | * (at your option) any later version. * | ||||
9 | * * | ||||
10 | * This program is distributed in the hope that it will be useful, * | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||||
13 | * GNU General Public License for more details. * | ||||
14 | * * | ||||
15 | * You should have received a copy of the GNU General Public License * | ||||
16 | * along with this program; if not, write to the * | ||||
17 | * Free Software Foundation, Inc., * | ||||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * | ||||
19 | ***************************************************************************/ | ||||
20 | | ||||
21 | #include "kastatsfavoritesmodel.h" | ||||
22 | #include "appentry.h" | ||||
23 | #include "contactentry.h" | ||||
24 | #include "fileentry.h" | ||||
25 | #include "actionlist.h" | ||||
26 | | ||||
27 | #include <QDebug> | ||||
28 | #include <QFileInfo> | ||||
29 | #include <QTimer> | ||||
30 | | ||||
31 | #include <KLocalizedString> | ||||
32 | #include <KSharedConfig> | ||||
33 | #include <KConfigGroup> | ||||
34 | | ||||
35 | #include <KActivities/Consumer> | ||||
36 | #include <KActivities/Stats/Terms> | ||||
37 | #include <KActivities/Stats/Query> | ||||
38 | #include <KActivities/Stats/ResultSet> | ||||
39 | #include <KActivities/Stats/ResultModel> | ||||
40 | | ||||
41 | namespace KAStats = KActivities::Stats; | ||||
42 | | ||||
43 | using namespace KAStats; | ||||
44 | using namespace KAStats::Terms; | ||||
45 | | ||||
46 | QString agentForPath(const QString &url) | ||||
47 | { | ||||
48 | return url.startsWith("ktp:") ? "org.kde.plasma.favorites.contacts" | ||||
49 | : "org.kde.plasma.favorites.applications"; | ||||
50 | } | ||||
51 | | ||||
52 | class KAStatsFavoritesModel::Private: public ResultModel { | ||||
53 | public: | ||||
54 | Private(const KAStats::Query &query, const QString &clientId, KAStatsFavoritesModel *parent) | ||||
55 | : ResultModel(query, clientId, parent) | ||||
56 | , q(parent) | ||||
57 | { | ||||
58 | } | ||||
59 | | ||||
60 | QString pathForId(const QString &id) const | ||||
61 | { | ||||
62 | if (id.isEmpty()) return QString(); | ||||
63 | | ||||
64 | const auto entry = favoriteFromId(id); | ||||
65 | | ||||
66 | const auto url = entry && entry->isValid() ? entry->url() | ||||
67 | : QUrl(); | ||||
68 | | ||||
69 | // We want to resolve symbolic links not to have two paths | ||||
70 | // refer to the same .desktop file | ||||
71 | if (url.isLocalFile()) { | ||||
72 | QFileInfo file(url.toLocalFile()); | ||||
73 | | ||||
74 | if (file.exists()) { | ||||
75 | return file.canonicalFilePath(); | ||||
76 | } | ||||
77 | } | ||||
78 | | ||||
79 | if (url.scheme() == "file") { | ||||
80 | return QString(); | ||||
81 | } | ||||
82 | | ||||
83 | return url.toString(); | ||||
84 | } | ||||
85 | | ||||
86 | AbstractEntry *favoriteFromId(const QString &id) const | ||||
87 | { | ||||
88 | // IDs can be plain appname.desktop | ||||
89 | // Also, it can be a file:///... URL | ||||
90 | // Or a file path | ||||
91 | // Or it is an URL which is not a file | ||||
92 | if (!m_entries.contains(id)) { | ||||
93 | AbstractEntry *entry = nullptr; | ||||
94 | | ||||
95 | if (QUrl(id).scheme() == "ktp") { | ||||
96 | entry = new ContactEntry(q, id); | ||||
97 | } else { | ||||
98 | entry = new AppEntry(q, id); | ||||
99 | } | ||||
100 | | ||||
101 | if (!entry->isValid()) return nullptr; | ||||
102 | | ||||
103 | const QUrl url = entry->url(); | ||||
104 | | ||||
105 | // Registering the entry for the ID we got | ||||
106 | m_entries[id] = entry; | ||||
107 | | ||||
108 | // Registering the entry for the ID it thinks it has | ||||
109 | m_entries[entry->id()] = entry; | ||||
110 | | ||||
111 | // Registering the entry for the URL it thinks it has | ||||
112 | m_entries[url.toString()] = entry; | ||||
113 | | ||||
114 | // And if it is a local file, registering it for the path | ||||
115 | if (url.isLocalFile()) { | ||||
116 | m_entries[entry->url().toLocalFile()] = entry; | ||||
117 | } | ||||
118 | } | ||||
119 | | ||||
120 | return m_entries[id]; | ||||
121 | } | ||||
122 | | ||||
123 | void removeOldCachedEntries() const | ||||
124 | { | ||||
125 | QSet<QString> knownIds; | ||||
126 | for (int row = 0; row < rowCount(); ++row) { | ||||
127 | const auto id = data(index(row, 0), Kicker::UrlRole).toString(); | ||||
128 | // const auto id = data(index(row, 0), ResultModel::ResourceRole).toString(); | ||||
129 | Q_ASSERT(!id.isEmpty()); | ||||
130 | | ||||
131 | const auto favorite = favoriteFromId(id); | ||||
132 | | ||||
133 | knownIds << id | ||||
134 | << favorite->id() | ||||
135 | << favorite->url().toString() | ||||
136 | << favorite->url().toLocalFile(); | ||||
137 | } | ||||
138 | | ||||
139 | QSet<AbstractEntry*> entriesToDelete; | ||||
140 | | ||||
141 | QHashIterator<QString, AbstractEntry*> i(m_entries); | ||||
142 | while (i.hasNext()) { | ||||
143 | i.next(); | ||||
144 | | ||||
145 | if (!knownIds.contains(i.key())) { | ||||
146 | entriesToDelete << i.value(); | ||||
147 | } | ||||
148 | } | ||||
149 | | ||||
150 | QMutableHashIterator<QString, AbstractEntry*> mi(m_entries); | ||||
151 | while (mi.hasNext()) { | ||||
152 | mi.next(); | ||||
153 | | ||||
154 | if (entriesToDelete.contains(mi.value())) { | ||||
155 | mi.remove(); | ||||
156 | } | ||||
157 | } | ||||
158 | | ||||
159 | qDeleteAll(entriesToDelete); | ||||
160 | } | ||||
161 | | ||||
162 | QModelIndex cache_lastIndex; | ||||
163 | AbstractEntry* cache_lastEntry = nullptr; | ||||
164 | | ||||
165 | QVariant data(const QModelIndex &index, int role) const override | ||||
166 | { | ||||
167 | auto entry = cache_lastEntry; | ||||
168 | | ||||
169 | if (cache_lastIndex != index) { | ||||
170 | if (!index.isValid() || index.row() >= rowCount()) { | ||||
171 | return QVariant(); | ||||
172 | } | ||||
173 | | ||||
174 | const auto _this = const_cast<Private*>(this); | ||||
175 | const auto id = ResultModel::data(index, ResultModel::ResourceRole).toString(); | ||||
176 | | ||||
177 | if (id.isEmpty()) return QVariant(); | ||||
178 | | ||||
179 | entry = _this->favoriteFromId(id); | ||||
180 | | ||||
181 | if (!entry || !entry->isValid()) { | ||||
182 | // If the result is not valid, we need to unlink it -- to | ||||
183 | // remove it from the model | ||||
184 | const auto path = pathForId(id); | ||||
185 | | ||||
186 | if (!path.isEmpty() && !m_invalidPaths.contains(path)) { | ||||
187 | _this->unlinkFromActivity(QUrl(path), Activity::any(), | ||||
188 | Agent(agentForPath(path))); | ||||
189 | m_invalidPaths << path; | ||||
190 | } | ||||
191 | | ||||
192 | return role == Qt::DecorationRole ? "unknown" | ||||
193 | : QVariant(); | ||||
194 | } | ||||
195 | | ||||
196 | _this->cache_lastIndex = index; | ||||
197 | _this->cache_lastEntry = entry; | ||||
198 | } | ||||
199 | | ||||
200 | return entry == nullptr ? QVariant() | ||||
201 | : role == Qt::DisplayRole ? entry->name() | ||||
202 | : role == Qt::DecorationRole ? entry->icon() | ||||
203 | : role == Kicker::DescriptionRole ? entry->description() | ||||
204 | : role == Kicker::FavoriteIdRole ? entry->id() | ||||
205 | : role == Kicker::UrlRole ? entry->url() | ||||
206 | : role == Kicker::HasActionListRole ? entry->hasActions() | ||||
207 | : role == Kicker::ActionListRole ? entry->actions() | ||||
208 | : QVariant(); | ||||
209 | } | ||||
210 | | ||||
211 | bool trigger(int row, const QString &actionId, const QVariant &argument) | ||||
212 | { | ||||
213 | if (row < 0 || row >= rowCount()) { | ||||
214 | return false; | ||||
215 | } | ||||
216 | | ||||
217 | const QString id = data(index(row, 0), Kicker::UrlRole).toString(); | ||||
218 | | ||||
219 | return m_entries.contains(id) | ||||
220 | ? m_entries[id]->run(actionId, argument) | ||||
221 | : false; | ||||
222 | } | ||||
223 | | ||||
224 | KAStatsFavoritesModel *const q; | ||||
225 | mutable QList<QString> m_invalidPaths; | ||||
226 | | ||||
227 | // This can contain an entry multiple times - for the id, file and url | ||||
228 | mutable QHash<QString, AbstractEntry *> m_entries; | ||||
229 | }; | ||||
230 | | ||||
231 | | ||||
232 | | ||||
233 | | ||||
234 | | ||||
235 | KAStatsFavoritesModel::KAStatsFavoritesModel(QObject *parent) | ||||
236 | : PlaceholderModel(parent) | ||||
237 | , d(new Private( | ||||
238 | LinkedResources | ||||
239 | | Agent { | ||||
240 | "org.kde.plasma.favorites.applications", | ||||
241 | "org.kde.plasma.favorites.contacts" | ||||
242 | } | ||||
243 | | Type::any() | ||||
244 | | Activity::current() | ||||
245 | | Activity::global() | ||||
246 | | Limit(15), | ||||
247 | QString(), | ||||
248 | this) | ||||
249 | ) | ||||
250 | , m_enabled(true) | ||||
251 | , m_maxFavorites(-1) | ||||
252 | , m_activities(new KActivities::Consumer(this)) | ||||
253 | { | ||||
254 | QModelIndex index; | ||||
255 | | ||||
256 | if (d->canFetchMore(index)) { | ||||
257 | d->fetchMore(index); | ||||
258 | } | ||||
259 | | ||||
260 | setSourceModel(d); | ||||
261 | } | ||||
262 | | ||||
263 | KAStatsFavoritesModel::~KAStatsFavoritesModel() | ||||
264 | { | ||||
265 | } | ||||
266 | | ||||
267 | void KAStatsFavoritesModel::initForClient(const QString &clientId) | ||||
268 | { | ||||
269 | setSourceModel(nullptr); | ||||
270 | delete d; | ||||
271 | d = new Private( | ||||
272 | LinkedResources | ||||
273 | | Agent { | ||||
274 | "org.kde.plasma.favorites.applications", | ||||
275 | "org.kde.plasma.favorites.contacts" | ||||
276 | } | ||||
277 | | Type::any() | ||||
278 | | Activity::current() | ||||
279 | | Activity::global() | ||||
280 | | Limit(15), | ||||
281 | clientId, | ||||
282 | this); | ||||
283 | setSourceModel(d); | ||||
284 | } | ||||
285 | | ||||
286 | QString KAStatsFavoritesModel::description() const | ||||
287 | { | ||||
288 | return i18n("Favorites"); | ||||
289 | } | ||||
290 | | ||||
291 | bool KAStatsFavoritesModel::trigger(int row, const QString &actionId, const QVariant &argument) | ||||
292 | { | ||||
293 | // Do not allow triggering an item while another one is being dropped | ||||
294 | if (m_dropPlaceholderIndex != -1) return false; | ||||
295 | | ||||
296 | return d->trigger(row, actionId, argument); | ||||
297 | } | ||||
298 | | ||||
299 | bool KAStatsFavoritesModel::enabled() const | ||||
300 | { | ||||
301 | return m_enabled; | ||||
302 | } | ||||
303 | | ||||
304 | int KAStatsFavoritesModel::maxFavorites() const | ||||
305 | { | ||||
306 | return m_maxFavorites; | ||||
307 | } | ||||
308 | | ||||
309 | void KAStatsFavoritesModel::setMaxFavorites(int max) | ||||
310 | { | ||||
311 | Q_UNUSED(max); | ||||
312 | } | ||||
313 | | ||||
314 | void KAStatsFavoritesModel::setEnabled(bool enable) | ||||
315 | { | ||||
316 | if (m_enabled != enable) { | ||||
317 | m_enabled = enable; | ||||
318 | | ||||
319 | emit enabledChanged(); | ||||
320 | } | ||||
321 | } | ||||
322 | | ||||
323 | QStringList KAStatsFavoritesModel::favorites() const | ||||
324 | { | ||||
325 | qWarning() << "KAStatsFavoritesModel::favorites returns nothing, it is here just to keep API backwards-compatible"; | ||||
326 | return QStringList(); | ||||
327 | } | ||||
328 | | ||||
329 | void KAStatsFavoritesModel::setFavorites(const QStringList& favorites) | ||||
330 | { | ||||
331 | Q_UNUSED(favorites); | ||||
332 | qWarning() << "KAStatsFavoritesModel::setFavorites is ignorred"; | ||||
333 | } | ||||
334 | | ||||
335 | bool KAStatsFavoritesModel::isFavorite(const QString &id) const | ||||
336 | { | ||||
337 | d->removeOldCachedEntries(); | ||||
338 | return d->m_entries.contains(id); | ||||
339 | } | ||||
340 | | ||||
341 | void KAStatsFavoritesModel::addFavorites(const QStringList &ids, int index) | ||||
342 | { | ||||
343 | qDebug() << "Adding the favourites: " << ids; | ||||
344 | std::for_each(ids.rbegin(), ids.rend(), [&] (const QString &id) { | ||||
345 | addFavorite(id, index); | ||||
346 | }); | ||||
347 | } | ||||
348 | | ||||
349 | void KAStatsFavoritesModel::addFavoritesTo(const QStringList &ids, const QString &activityId, int index) | ||||
350 | { | ||||
351 | qDebug() << "Adding the favourites: " << ids << "to" << activityId; | ||||
352 | std::for_each(ids.rbegin(), ids.rend(), [&] (const QString &id) { | ||||
353 | addFavoriteTo(id, activityId, index); | ||||
354 | }); | ||||
355 | } | ||||
356 | | ||||
357 | void KAStatsFavoritesModel::addFavorite(const QString &id, int index) | ||||
358 | { | ||||
359 | addFavoriteTo(id, Activity::current(), index); | ||||
360 | } | ||||
361 | | ||||
362 | void KAStatsFavoritesModel::removeFavorite(const QString &id) | ||||
363 | { | ||||
364 | removeFavoriteFrom(id, Activity::current()); | ||||
365 | } | ||||
366 | | ||||
367 | void KAStatsFavoritesModel::addFavoriteTo(const QString &id, const QString &activityId, int index) | ||||
368 | { | ||||
369 | addFavoriteTo(id, Activity(activityId), index); | ||||
370 | } | ||||
371 | | ||||
372 | void KAStatsFavoritesModel::removeFavoriteFrom(const QString &id, const QString &activityId) | ||||
373 | { | ||||
374 | removeFavoriteFrom(id, Activity(activityId)); | ||||
375 | } | ||||
376 | | ||||
377 | void KAStatsFavoritesModel::addFavoriteTo(const QString &id, const Activity &activity, int index) | ||||
378 | { | ||||
379 | if (id.isEmpty()) return; | ||||
380 | | ||||
381 | setDropPlaceholderIndex(-1); | ||||
382 | | ||||
383 | const auto path = d->pathForId(id); | ||||
384 | | ||||
385 | if (path.isEmpty()) return; | ||||
386 | | ||||
387 | // This is a file, we want to check that it exists | ||||
388 | // if (url.isLocalFile() && !QFileInfo::exists(url.toLocalFile())) return; | ||||
389 | | ||||
390 | d->linkToActivity(QUrl(path), activity, | ||||
391 | Agent(agentForPath(path))); | ||||
392 | | ||||
393 | if (index != -1) { | ||||
394 | d->setResultPosition(path, index); | ||||
395 | } | ||||
396 | } | ||||
397 | | ||||
398 | void KAStatsFavoritesModel::removeFavoriteFrom(const QString &id, const Activity &activity) | ||||
399 | { | ||||
400 | const auto path = d->pathForId(id); | ||||
401 | | ||||
402 | if (path.isEmpty()) return; | ||||
403 | | ||||
404 | d->unlinkFromActivity( | ||||
405 | QUrl(path), activity, | ||||
406 | Agent(agentForPath(path)) | ||||
407 | ); | ||||
408 | } | ||||
409 | | ||||
410 | void KAStatsFavoritesModel::moveRow(int from, int to) | ||||
411 | { | ||||
412 | const auto id = data(index(from, 0), Kicker::UrlRole).toString(); | ||||
413 | const auto path = d->pathForId(id); | ||||
414 | | ||||
415 | if (path.isEmpty()) return; | ||||
416 | | ||||
417 | d->setResultPosition(path, to); | ||||
418 | } | ||||
419 | | ||||
420 | AbstractModel *KAStatsFavoritesModel::favoritesModel() | ||||
421 | { | ||||
422 | return this; | ||||
423 | } | ||||
424 | | ||||
425 | void KAStatsFavoritesModel::refresh() | ||||
426 | { | ||||
427 | } | ||||
428 | | ||||
429 | QObject *KAStatsFavoritesModel::activities() const | ||||
430 | { | ||||
431 | return m_activities; | ||||
432 | } | ||||
433 | | ||||
434 | QString KAStatsFavoritesModel::activityNameForId(const QString &activityId) const | ||||
435 | { | ||||
436 | // It is safe to use a short-lived object here, | ||||
437 | // we are always synced with KAMD in plasma | ||||
438 | KActivities::Info info(activityId); | ||||
439 | return info.name(); | ||||
440 | } | ||||
441 | | ||||
442 | QStringList KAStatsFavoritesModel::linkedActivitiesFor(const QString &id) const | ||||
443 | { | ||||
444 | const auto path = d->pathForId(id); | ||||
445 | | ||||
446 | if (path.isEmpty()) { | ||||
447 | return {}; | ||||
448 | } | ||||
449 | | ||||
450 | auto query = LinkedResources | ||||
451 | | Agent { | ||||
452 | "org.kde.plasma.favorites.applications", | ||||
453 | "org.kde.plasma.favorites.contacts" | ||||
454 | } | ||||
455 | | Type::any() | ||||
456 | | Activity::any() | ||||
457 | | Url(path); | ||||
458 | | ||||
459 | ResultSet results(query); | ||||
460 | | ||||
461 | for (const auto &result: results) { | ||||
462 | return result.linkedActivities(); | ||||
463 | } | ||||
464 | | ||||
465 | return {}; | ||||
466 | } | ||||
467 | |