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