1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
|
/*
SPDX-FileCopyrightText: 2016 Eike Hein <hein@kde.org>
SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
*/
#include "launchertasksmodel.h"
#include "tasktools.h"
#include <KDesktopFile>
#include <KNotificationJobUiDelegate>
#include <KService>
#include <KSycoca>
#include <KWindowSystem>
#include <KActivities/Consumer>
#include <KActivities/ResourceInstance>
#include <KIO/ApplicationLauncherJob>
#include <QHash>
#include <QIcon>
#include <QSet>
#include <QTimer>
#include <QUrlQuery>
#include "launchertasksmodel_p.h"
#include <chrono>
using namespace std::chrono_literals;
namespace TaskManager
{
typedef QSet<QString> ActivitiesSet;
template<typename ActivitiesCollection>
inline bool isOnAllActivities(const ActivitiesCollection &activities)
{
return activities.isEmpty() || activities.contains(NULL_UUID);
}
class Q_DECL_HIDDEN LauncherTasksModel::Private
{
public:
Private(LauncherTasksModel *q);
KActivities::Consumer activitiesConsumer;
QList<QUrl> launchersOrder;
QHash<QUrl, ActivitiesSet> activitiesForLauncher;
inline void setActivitiesForLauncher(const QUrl &url, const ActivitiesSet &activities)
{
if (activities.size() == activitiesConsumer.activities().size()) {
activitiesForLauncher[url] = {NULL_UUID};
} else {
activitiesForLauncher[url] = activities;
}
}
QHash<QUrl, AppData> appDataCache;
QTimer sycocaChangeTimer;
void init();
AppData appData(const QUrl &url);
bool requestAddLauncherToActivities(const QUrl &_url, const QStringList &activities);
bool requestRemoveLauncherFromActivities(const QUrl &_url, const QStringList &activities);
private:
LauncherTasksModel *q;
};
LauncherTasksModel::Private::Private(LauncherTasksModel *q)
: q(q)
{
}
void LauncherTasksModel::Private::init()
{
sycocaChangeTimer.setSingleShot(true);
sycocaChangeTimer.setInterval(100ms);
QObject::connect(&sycocaChangeTimer, &QTimer::timeout, q, [this]() {
if (!launchersOrder.count()) {
return;
}
appDataCache.clear();
// Emit changes of all roles satisfied from app data cache.
Q_EMIT q->dataChanged(q->index(0, 0),
q->index(launchersOrder.count() - 1, 0),
QVector<int>{Qt::DisplayRole,
Qt::DecorationRole,
AbstractTasksModel::AppId,
AbstractTasksModel::AppName,
AbstractTasksModel::GenericName,
AbstractTasksModel::LauncherUrl,
AbstractTasksModel::LauncherUrlWithoutIcon});
});
QObject::connect(KSycoca::self(), &KSycoca::databaseChanged, q, [this]() {
sycocaChangeTimer.start();
});
}
AppData LauncherTasksModel::Private::appData(const QUrl &url)
{
const auto &it = appDataCache.constFind(url);
if (it != appDataCache.constEnd()) {
return *it;
}
const AppData &data = appDataFromUrl(url, QIcon::fromTheme(QLatin1String("unknown")));
appDataCache.insert(url, data);
return data;
}
bool LauncherTasksModel::Private::requestAddLauncherToActivities(const QUrl &_url, const QStringList &_activities)
{
QUrl url(_url);
if (!isValidLauncherUrl(url)) {
return false;
}
const auto activities = ActivitiesSet(_activities.cbegin(), _activities.cend());
if (url.isLocalFile() && KDesktopFile::isDesktopFile(url.toLocalFile())) {
KDesktopFile f(url.toLocalFile());
const KService::Ptr service = KService::serviceByStorageId(f.fileName());
// Resolve to non-absolute menuId-based URL if possible.
if (service) {
const QString &menuId = service->menuId();
if (!menuId.isEmpty()) {
url = QUrl(QLatin1String("applications:") + menuId);
}
}
}
// Merge duplicates
int row = -1;
foreach (const QUrl &launcher, launchersOrder) {
++row;
if (launcherUrlsMatch(url, launcher, IgnoreQueryItems)) {
ActivitiesSet newActivities;
// Use the key we established equivalence to ('launcher').
if (!activitiesForLauncher.contains(launcher)) {
// If we don't have the activities assigned to this url
// for some reason
newActivities = activities;
} else {
if (isOnAllActivities(activities)) {
// If the new list is empty, or has a null uuid, this
// launcher should be on all activities
newActivities = ActivitiesSet{NULL_UUID};
} else if (isOnAllActivities(activitiesForLauncher[launcher])) {
// If we have been on all activities before, and we have
// been asked to be on a specific one, lets make an
// exception - we will set the activities to exactly
// what we have been asked
newActivities = activities;
} else {
newActivities += activities;
newActivities += activitiesForLauncher[launcher];
}
}
if (newActivities != activitiesForLauncher[launcher]) {
setActivitiesForLauncher(launcher, newActivities);
Q_EMIT q->dataChanged(q->index(row, 0), q->index(row, 0));
Q_EMIT q->launcherListChanged();
return true;
}
return false;
}
}
// This is a new one
const auto count = launchersOrder.count();
q->beginInsertRows(QModelIndex(), count, count);
setActivitiesForLauncher(url, activities);
launchersOrder.append(url);
q->endInsertRows();
Q_EMIT q->launcherListChanged();
return true;
}
bool LauncherTasksModel::Private::requestRemoveLauncherFromActivities(const QUrl &url, const QStringList &activities)
{
for (int row = 0; row < launchersOrder.count(); ++row) {
const QUrl &launcher = launchersOrder.at(row);
if (launcherUrlsMatch(url, launcher, IgnoreQueryItems) || launcherUrlsMatch(url, appData(launcher).url, IgnoreQueryItems)) {
const auto currentActivities = activitiesForLauncher[url];
ActivitiesSet newActivities;
bool remove = false;
bool update = false;
if (isOnAllActivities(currentActivities)) {
// We are currently on all activities.
// Should we go away, or just remove from the current one?
if (isOnAllActivities(activities)) {
remove = true;
} else {
const auto _activities = activitiesConsumer.activities();
for (const auto &activity : _activities) {
if (!activities.contains(activity)) {
newActivities << activity;
} else {
update = true;
}
}
}
} else if (isOnAllActivities(activities)) {
remove = true;
} else {
// We weren't on all activities, just remove those that
// we were on
for (const auto &activity : currentActivities) {
if (!activities.contains(activity)) {
newActivities << activity;
}
}
if (newActivities.isEmpty()) {
remove = true;
} else {
update = true;
}
}
if (remove) {
q->beginRemoveRows(QModelIndex(), row, row);
launchersOrder.removeAt(row);
activitiesForLauncher.remove(url);
appDataCache.remove(launcher);
q->endRemoveRows();
} else if (update) {
setActivitiesForLauncher(url, newActivities);
Q_EMIT q->dataChanged(q->index(row, 0), q->index(row, 0));
}
if (remove || update) {
Q_EMIT q->launcherListChanged();
return true;
}
}
}
return false;
}
LauncherTasksModel::LauncherTasksModel(QObject *parent)
: AbstractTasksModel(parent)
, d(new Private(this))
{
d->init();
}
LauncherTasksModel::~LauncherTasksModel()
{
}
QVariant LauncherTasksModel::data(const QModelIndex &index, int role) const
{
if (!index.isValid() || index.row() >= d->launchersOrder.count()) {
return QVariant();
}
const QUrl &url = d->launchersOrder.at(index.row());
const AppData &data = d->appData(url);
if (role == Qt::DisplayRole) {
return data.name;
} else if (role == Qt::DecorationRole) {
return data.icon;
} else if (role == AppId) {
return data.id;
} else if (role == AppName) {
return data.name;
} else if (role == GenericName) {
return data.genericName;
} else if (role == LauncherUrl) {
// Take resolved URL from cache.
return data.url;
} else if (role == LauncherUrlWithoutIcon) {
// Take resolved URL from cache.
QUrl url = data.url;
if (url.hasQuery()) {
QUrlQuery query(url);
query.removeQueryItem(QLatin1String("iconData"));
url.setQuery(query);
}
return url;
} else if (role == IsLauncher) {
return true;
} else if (role == IsVirtualDesktopsChangeable) {
return false;
} else if (role == IsOnAllVirtualDesktops) {
return true;
} else if (role == Activities) {
return QStringList(d->activitiesForLauncher[url].values());
} else if (role == CanLaunchNewInstance) {
return false;
}
return QVariant();
}
int LauncherTasksModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : d->launchersOrder.count();
}
int LauncherTasksModel::rowCountForActivity(const QString &activity) const
{
if (activity == NULL_UUID || activity.isEmpty()) {
return rowCount();
}
return std::count_if(d->launchersOrder.cbegin(), d->launchersOrder.cend(), [this, &activity](const QUrl &url) {
const auto &set = d->activitiesForLauncher[url];
return set.contains(NULL_UUID) || set.contains(activity);
});
}
QStringList LauncherTasksModel::launcherList() const
{
// Serializing the launchers
QStringList result;
for (const auto &launcher : qAsConst(d->launchersOrder)) {
const auto &activities = d->activitiesForLauncher[launcher];
QString serializedLauncher;
if (isOnAllActivities(activities)) {
serializedLauncher = launcher.toString();
} else {
serializedLauncher = "[" + d->activitiesForLauncher[launcher].values().join(",") + "]\n" + launcher.toString();
}
result << serializedLauncher;
}
return result;
}
void LauncherTasksModel::setLauncherList(const QStringList &serializedLaunchers)
{
// Clearing everything
QList<QUrl> newLaunchersOrder;
QHash<QUrl, ActivitiesSet> newActivitiesForLauncher;
// Loading the activity to launchers map
for (const auto &serializedLauncher : serializedLaunchers) {
QStringList _activities;
QUrl url;
std::tie(url, _activities) = deserializeLauncher(serializedLauncher);
auto activities = ActivitiesSet(_activities.cbegin(), _activities.cend());
// Is url is not valid, ignore it
if (!isValidLauncherUrl(url)) {
continue;
}
// If we have a null uuid, it means we are on all activities
// and we should contain only the null uuid
if (isOnAllActivities(activities)) {
activities = {NULL_UUID};
} else {
// Filter out invalid activities
const auto allActivities = d->activitiesConsumer.activities();
ActivitiesSet validActivities;
for (const auto &activity : qAsConst(activities)) {
if (allActivities.contains(activity)) {
validActivities << activity;
}
}
if (validActivities.isEmpty()) {
// If all activities that had this launcher are
// removed, we are killing the launcher as well
continue;
}
activities = validActivities;
}
// Is the url a duplicate?
const auto location = std::find_if(newLaunchersOrder.begin(), newLaunchersOrder.end(), [&url](const QUrl &item) {
return launcherUrlsMatch(url, item, IgnoreQueryItems);
});
if (location != newLaunchersOrder.end()) {
// It is a duplicate
url = *location;
} else {
// It is not a duplicate, we need to add it
// to the list of registered launchers
newLaunchersOrder << url;
}
if (!newActivitiesForLauncher.contains(url)) {
// This is the first time we got this url
newActivitiesForLauncher[url] = activities;
} else if (newActivitiesForLauncher[url].contains(NULL_UUID)) {
// Do nothing, we are already on all activities
} else if (activities.contains(NULL_UUID)) {
newActivitiesForLauncher[url] = {NULL_UUID};
} else {
// We are not on all activities, append the new ones
newActivitiesForLauncher[url] += activities;
}
}
if (newLaunchersOrder != d->launchersOrder) {
const bool isOrderChanged = std::all_of(newLaunchersOrder.cbegin(),
newLaunchersOrder.cend(),
[this](const QUrl &url) {
return d->launchersOrder.contains(url);
})
&& newLaunchersOrder.size() == d->launchersOrder.size();
if (isOrderChanged) {
for (int i = 0; i < newLaunchersOrder.size(); i++) {
int oldRow = d->launchersOrder.indexOf(newLaunchersOrder.at(i));
if (oldRow != i) {
beginMoveRows(QModelIndex(), oldRow, oldRow, QModelIndex(), i);
d->launchersOrder.move(oldRow, i);
endMoveRows();
}
}
} else {
// Use Remove/Insert to update the manual sort map in TasksModel
if (!d->launchersOrder.empty()) {
beginRemoveRows(QModelIndex(), 0, d->launchersOrder.size() - 1);
d->launchersOrder.clear();
d->activitiesForLauncher.clear();
endRemoveRows();
}
if (!newLaunchersOrder.empty()) {
beginInsertRows(QModelIndex(), 0, newLaunchersOrder.size() - 1);
d->launchersOrder = newLaunchersOrder;
d->activitiesForLauncher = newActivitiesForLauncher;
endInsertRows();
}
}
Q_EMIT launcherListChanged();
} else if (newActivitiesForLauncher != d->activitiesForLauncher) {
for (int i = 0; i < d->launchersOrder.size(); i++) {
const QUrl &url = d->launchersOrder.at(i);
if (d->activitiesForLauncher[url] != newActivitiesForLauncher[url]) {
d->activitiesForLauncher[url] = newActivitiesForLauncher[url];
Q_EMIT dataChanged(index(i, 0), index(i, 0), {Activities});
}
}
}
}
bool LauncherTasksModel::requestAddLauncher(const QUrl &url)
{
return d->requestAddLauncherToActivities(url, {NULL_UUID});
}
bool LauncherTasksModel::requestRemoveLauncher(const QUrl &url)
{
return d->requestRemoveLauncherFromActivities(url, {NULL_UUID});
}
bool LauncherTasksModel::requestAddLauncherToActivity(const QUrl &url, const QString &activity)
{
return d->requestAddLauncherToActivities(url, {activity});
}
bool LauncherTasksModel::requestRemoveLauncherFromActivity(const QUrl &url, const QString &activity)
{
return d->requestRemoveLauncherFromActivities(url, {activity});
}
QStringList LauncherTasksModel::launcherActivities(const QUrl &_url) const
{
const auto position = launcherPosition(_url);
if (position == -1) {
// If we do not have this launcher, return an empty list
return {};
} else {
const auto url = d->launchersOrder.at(position);
// If the launcher is on all activities, return a null uuid
return d->activitiesForLauncher.contains(url) ? d->activitiesForLauncher[url].values() : QStringList{NULL_UUID};
}
}
int LauncherTasksModel::launcherPosition(const QUrl &url) const
{
for (int i = 0; i < d->launchersOrder.count(); ++i) {
if (launcherUrlsMatch(url, d->appData(d->launchersOrder.at(i)).url, IgnoreQueryItems)) {
return i;
}
}
return -1;
}
void LauncherTasksModel::requestActivate(const QModelIndex &index)
{
requestNewInstance(index);
}
void LauncherTasksModel::requestNewInstance(const QModelIndex &index)
{
if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->launchersOrder.count()) {
return;
}
runApp(d->appData(d->launchersOrder.at(index.row())));
}
void LauncherTasksModel::requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls)
{
if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->launchersOrder.count() || urls.isEmpty()) {
return;
}
const QUrl &url = d->launchersOrder.at(index.row());
KService::Ptr service;
if (url.scheme() == QLatin1String("applications")) {
service = KService::serviceByMenuId(url.path());
} else if (url.scheme() == QLatin1String("preferred")) {
service = KService::serviceByStorageId(defaultApplication(url));
} else {
service = KService::serviceByDesktopPath(url.toLocalFile());
}
if (!service || !service->isApplication()) {
return;
}
auto *job = new KIO::ApplicationLauncherJob(service);
job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoErrorHandlingEnabled));
job->setUrls(urls);
job->start();
KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + service->storageId()), QStringLiteral("org.kde.libtaskmanager"));
}
}
|