File indexing completed on 2024-04-21 05:33:52

0001 /*
0002  *   SPDX-FileCopyrightText: 2021 Aleix Pol Gonzalez <aleixpol@kde.org>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "startupnotifier.h"
0008 #include <KApplicationTrader>
0009 #include <KWayland/Client/connection_thread.h>
0010 #include <KWayland/Client/plasmawindowmanagement.h>
0011 #include <KWayland/Client/registry.h>
0012 #include <KWindowSystem>
0013 
0014 #include <QDebug>
0015 
0016 StartupNotifier::StartupNotifier(QObject *parent)
0017     : QObject(parent)
0018 {
0019     if (!KWindowSystem::isPlatformWayland()) {
0020         return;
0021     }
0022     using namespace KWayland::Client;
0023     ConnectionThread *connection = ConnectionThread::fromApplication(this);
0024     if (!connection) {
0025         return;
0026     }
0027     Registry *registry = new Registry(this);
0028     registry->create(connection);
0029 
0030     connect(registry, &Registry::plasmaActivationFeedbackAnnounced, this, [this, registry](quint32 name, quint32 version) {
0031         auto iface = registry->createPlasmaActivationFeedback(name, version, this);
0032 
0033         connect(iface, &PlasmaActivationFeedback::activation, this, [this](PlasmaActivation *activation) {
0034             connect(activation, &PlasmaActivation::applicationId, this, [this](const QString &appId) {
0035                 const auto servicesFound = KApplicationTrader::query([&appId](const KService::Ptr &service) {
0036                     if (service->exec().isEmpty())
0037                         return false;
0038 
0039                     if (service->desktopEntryName().compare(appId, Qt::CaseInsensitive) == 0)
0040                         return true;
0041 
0042                     const auto idWithoutDesktop = QString(appId).remove(QStringLiteral(".desktop"));
0043                     if (service->desktopEntryName().compare(idWithoutDesktop, Qt::CaseInsensitive) == 0)
0044                         return true;
0045 
0046                     const auto renamedFrom = service->property<QStringList>(QStringLiteral("X-Flatpak-RenamedFrom"));
0047                     if (renamedFrom.contains(appId, Qt::CaseInsensitive) || renamedFrom.contains(idWithoutDesktop, Qt::CaseInsensitive))
0048                         return true;
0049 
0050                     return false;
0051                 });
0052 
0053                 if (!servicesFound.isEmpty()) {
0054                     Q_EMIT activationStarted(appId, servicesFound.constFirst()->icon());
0055                 } else {
0056                     qDebug() << "Could not find" << appId;
0057                 }
0058             });
0059 
0060             connect(activation, &PlasmaActivation::finished, this, &StartupNotifier::activationFinished);
0061         });
0062     });
0063 
0064     registry->setup();
0065 }
0066 
0067 bool StartupNotifier::isValid() const
0068 {
0069     return KWindowSystem::isPlatformWayland();
0070 }