diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 5061a10af..76b8ded6f 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -277,6 +277,31 @@ void DolphinMainWindow::openFiles(const QStringList &files, bool splitView) openFiles(QUrl::fromStringList(files), splitView); } +bool DolphinMainWindow::isOnCurrentDesktop() const +{ +#if HAVE_X11 + if (KWindowSystem::isPlatformX11()) { + const NET::Properties properties = NET::WMDesktop; + KWindowInfo info(this->winId(), properties); + return info.isOnCurrentDesktop(); + } +#endif + return true; +} + +bool DolphinMainWindow::isOnActivity(const QString &activityId) const +{ +#if HAVE_X11 && HAVE_KACTIVITIES + if (KWindowSystem::isPlatformX11()) { + const NET::Properties properties = NET::Supported; + const NET::Properties2 properties2 = NET::WM2Activities; + KWindowInfo info(this->winId(), properties, properties2); + return info.activities().contains(activityId); + } +#endif + return true; +} + void DolphinMainWindow::activateWindow(const QString &activationToken) { window()->setAttribute(Qt::WA_NativeWindow, true); diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 2c6874966..fe07cbc17 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -204,6 +204,9 @@ public Q_SLOTS: /** @see GeneralSettings::splitViewChanged() */ void slotSplitViewChanged(); + bool isOnActivity(const QString &activityId) const; + bool isOnCurrentDesktop() const; + Q_SIGNALS: /** * Is sent if the selection of the currently active view has diff --git a/src/global.cpp b/src/global.cpp index 554eb41fa..8babbbddc 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -16,6 +16,9 @@ #include #include #include +#ifdef HAVE_KACTIVITIES +#include +#endif #include @@ -140,13 +143,37 @@ bool Dolphin::attachToExistingInstance(const QList &inputUrls, QVector, QStringList>> Dolphin::dolphinGuiInstances(const QString &preferredService) { +#ifdef HAVE_KACTIVITIES + static std::once_flag one_consumer; + static KActivities::Consumer *consumer; + std::call_once(one_consumer, []() { + consumer = new KActivities::Consumer(); + // ensures the consumer is ready for query + QEventLoop loop; + QObject::connect(consumer, &KActivities::Consumer::serviceStatusChanged, &loop, &QEventLoop::quit); + loop.exec(); + }); +#endif + QVector, QStringList>> dolphinInterfaces; - if (!preferredService.isEmpty()) { - QSharedPointer preferredInterface( - new OrgKdeDolphinMainWindowInterface(preferredService, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus())); - if (preferredInterface->isValid() && !preferredInterface->lastError().isValid()) { - dolphinInterfaces.append(qMakePair(preferredInterface, QStringList())); + const auto tryAppendInterface = [&dolphinInterfaces](const QString &service) { + // Check if instance can handle our URLs + QSharedPointer interface( + new OrgKdeDolphinMainWindowInterface(service, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus())); + if (interface->isValid() && !interface->lastError().isValid()) { +#ifdef HAVE_KACTIVITIES + const auto currentActivity = consumer->currentActivity(); + if (currentActivity.isEmpty() || currentActivity == QStringLiteral("00000000-0000-0000-0000-000000000000") + || interface->isOnActivity(consumer->currentActivity())) +#endif + if (interface->isOnCurrentDesktop()) { + dolphinInterfaces.append(qMakePair(interface, QStringList())); + } } + }; + + if (!preferredService.isEmpty()) { + tryAppendInterface(preferredService); } // Look for dolphin instances among all available dbus services. @@ -158,12 +185,7 @@ QVector, QStringList>> Do const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid()); for (const QString &service : dbusServices) { if (service.startsWith(pattern) && !service.endsWith(myPid)) { - // Check if instance can handle our URLs - QSharedPointer interface( - new OrgKdeDolphinMainWindowInterface(service, QStringLiteral("/dolphin/Dolphin_1"), QDBusConnection::sessionBus())); - if (interface->isValid() && !interface->lastError().isValid()) { - dolphinInterfaces.append(qMakePair(interface, QStringList())); - } + tryAppendInterface(service); } }