]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/global.cpp
Restrict attaching instances to those on the same activity or same virtual desktop
[dolphin.git] / src / global.cpp
index c9ec6a1200036caaa1b7e6fb15bb6f516bc7e709..8babbbddc05fae6e03b1d0437dc51aceb912a4d6 100644 (file)
 #include <KDialogJobUiDelegate>
 #include <KIO/ApplicationLauncherJob>
 #include <KService>
+#include <KWindowSystem>
+#ifdef HAVE_KACTIVITIES
+#include <KActivities/Consumer>
+#endif
 
 #include <QApplication>
 
-QList<QUrl> Dolphin::validateUris(const QStringListuriList)
+QList<QUrl> Dolphin::validateUris(const QStringList &uriList)
 {
     const QString currentDir = QDir::currentPath();
     QList<QUrl> urls;
-    for (const QStringstr : uriList) {
+    for (const QString &str : uriList) {
         const QUrl url = QUrl::fromUserInput(str, currentDir, QUrl::AssumeLocalFile);
         if (url.isValid()) {
             urls.append(url);
@@ -56,7 +60,11 @@ void Dolphin::openNewWindow(const QList<QUrl> &urls, QWidget *window, const Open
     job->start();
 }
 
-bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFiles, bool splitView, const QString& preferredService)
+bool Dolphin::attachToExistingInstance(const QList<QUrl> &inputUrls,
+                                       bool openFiles,
+                                       bool splitView,
+                                       const QString &preferredService,
+                                       const QString &activationToken)
 {
     bool attached = false;
 
@@ -70,7 +78,7 @@ bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFi
     }
 
     int activeWindowIndex = -1;
-    for (const auto& interface: qAsConst(dolphinInterfaces)) {
+    for (const auto &interface : qAsConst(dolphinInterfaces)) {
         ++activeWindowIndex;
 
         auto isActiveWindowReply = interface.first->isActiveWindow();
@@ -82,7 +90,7 @@ bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFi
 
     // check to see if any instances already have any of the given URLs or their parents open
     QList<QUrl> newWindowURLs;
-    for (const QUrlurl : inputUrls) {
+    for (const QUrl &url : inputUrls) {
         bool urlFound = false;
         const QString urlString = url.toString();
 
@@ -100,8 +108,7 @@ bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFi
             }
 
             i = (i + 1) % dolphinInterfaces.size();
-        }
-        while (i != activeWindowIndex);
+        } while (i != activeWindowIndex);
 
         if (!urlFound) {
             if (GeneralSettings::openExternallyCalledFolderInNewTab()) {
@@ -112,16 +119,14 @@ bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFi
         }
     }
 
-    for (const auto& interface: qAsConst(dolphinInterfaces)) {
+    for (const auto &interface : qAsConst(dolphinInterfaces)) {
         if (interface.second.isEmpty()) {
             continue;
         }
-        auto reply = openFiles ?
-                    interface.first->openFiles(interface.second, splitView) :
-                    interface.first->openDirectories(interface.second, splitView);
+        auto reply = openFiles ? interface.first->openFiles(interface.second, splitView) : interface.first->openDirectories(interface.second, splitView);
         reply.waitForFinished();
         if (!reply.isError()) {
-            interface.first->activateWindow();
+            interface.first->activateWindow(activationToken);
             attached = true;
         }
     }
@@ -136,17 +141,39 @@ bool Dolphin::attachToExistingInstance(const QList<QUrl>& inputUrls, bool openFi
     return attached;
 }
 
-QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Dolphin::dolphinGuiInstances(const QStringpreferredService)
+QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, 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<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> dolphinInterfaces;
-    if (!preferredService.isEmpty()) {
-        QSharedPointer<OrgKdeDolphinMainWindowInterface> 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<OrgKdeDolphinMainWindowInterface> 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.
@@ -156,16 +183,9 @@ QVector<QPair<QSharedPointer<OrgKdeDolphinMainWindowInterface>, QStringList>> Do
     const QString pattern = QStringLiteral("org.kde.dolphin-");
     // Don't match the pid without leading "-"
     const QString myPid = QLatin1Char('-') + QString::number(QCoreApplication::applicationPid());
-    for (const QStringservice : dbusServices) {
+    for (const QString &service : dbusServices) {
         if (service.startsWith(pattern) && !service.endsWith(myPid)) {
-            // Check if instance can handle our URLs
-            QSharedPointer<OrgKdeDolphinMainWindowInterface> interface(
-                        new OrgKdeDolphinMainWindowInterface(service,
-                            QStringLiteral("/dolphin/Dolphin_1"),
-                            QDBusConnection::sessionBus()));
-            if (interface->isValid() && !interface->lastError().isValid()) {
-                dolphinInterfaces.append(qMakePair(interface, QStringList()));
-            }
+            tryAppendInterface(service);
         }
     }
 
@@ -188,17 +208,14 @@ double GlobalConfig::animationDurationFactor()
     updateAnimationDurationFactor(kdeGlobalsConfig, {"AnimationDurationFactor"});
 
     KConfigWatcher::Ptr configWatcher = KConfigWatcher::create(KSharedConfig::openConfig());
-    connect(configWatcher.data(), &KConfigWatcher::configChanged,
-            &GlobalConfig::updateAnimationDurationFactor);
+    connect(configWatcher.data(), &KConfigWatcher::configChanged, &GlobalConfig::updateAnimationDurationFactor);
     return s_animationDurationFactor;
 }
 
 void GlobalConfig::updateAnimationDurationFactor(const KConfigGroup &group, const QByteArrayList &names)
 {
-    if (group.name() == QLatin1String("KDE") &&
-        names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
-        s_animationDurationFactor = std::max(0.0,
-                group.readEntry("AnimationDurationFactor", 1.0));
+    if (group.name() == QLatin1String("KDE") && names.contains(QByteArrayLiteral("AnimationDurationFactor"))) {
+        s_animationDurationFactor = std::max(0.0, group.readEntry("AnimationDurationFactor", 1.0));
     }
 }