Index: kate/kateapp.h =================================================================== --- kate/kateapp.h +++ kate/kateapp.h @@ -190,6 +190,13 @@ * @return success */ bool openUrl(const QUrl &url, const QString &encoding, bool isTempFile); + + /** + * checks if the current instance is in a given activity + * @param activity activity to check + * @return true if the window is in the given activity, false otherwise + */ + bool isOnActivity(QString activity); KTextEditor::Document *openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile); Index: kate/kateapp.cpp =================================================================== --- kate/kateapp.cpp +++ kate/kateapp.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include #include @@ -272,6 +273,20 @@ return openDocUrl(url, encoding, isTempFile); } +bool KateApp::isOnActivity(QString activity) +{ + for (const auto& window : this->m_mainWindows) { + WId id = window->winId(); + + KWindowInfo info = KWindowInfo(id, 0, NET::WM2Activities); + + if (info.activities().contains(activity)) + return true; + } + + return false; +} + KTextEditor::Document *KateApp::openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile) { KateMainWindow *mainWindow = activeKateMainWindow(); Index: kate/kateappadaptor.h =================================================================== --- kate/kateappadaptor.h +++ kate/kateappadaptor.h @@ -47,6 +47,13 @@ * @return success */ bool openUrl(QString url, QString encoding); + + /** + * checks if the Kate instance is in the specified activity + * @param activity activity to check + * @return true if it is in the specified activity, false otherwise + */ + bool isOnActivity(QString activity); /** * open a file with given url and encoding Index: kate/kateappadaptor.cpp =================================================================== --- kate/kateappadaptor.cpp +++ kate/kateappadaptor.cpp @@ -59,6 +59,10 @@ return m_app->openUrl(QUrl(url), encoding, isTempFile); } +bool KateAppAdaptor::isOnActivity(QString activity) { + return m_app->isOnActivity(activity); +} + //----------- QString KateAppAdaptor::tokenOpenUrl(QString url, QString encoding) { Index: kate/main.cpp =================================================================== --- kate/main.cpp +++ kate/main.cpp @@ -282,11 +282,47 @@ if (!fillinRunningKateAppInstances(&mapSessionRii)) { return 1; } + + QString currentActivity; + QDBusMessage m = QDBusMessage::createMethodCall( + QStringLiteral("org.kde.ActivityManager"), + QStringLiteral("/ActivityManager/Activities"), QStringLiteral("org.kde.ActivityManager.Activities"), QStringLiteral("CurrentActivity")); + QDBusMessage res = QDBusConnection::sessionBus().call(m); + QList answer = res.arguments(); + if (answer.size() == 1) { + currentActivity = answer.at(0).toString(); + } QStringList kateServices; for (KateRunningInstanceMap::const_iterator it = mapSessionRii.constBegin(); it != mapSessionRii.constEnd(); ++it) { - kateServices << (*it)->serviceName; + QString serviceName = (*it)->serviceName; + + if (currentActivity.length() != 0) { + QDBusMessage m = QDBusMessage::createMethodCall(serviceName, + QStringLiteral("/MainApplication"), QStringLiteral("org.kde.Kate.Application"), QStringLiteral("isOnActivity")); + + QList dbargs; + + // convert to an url + dbargs.append(currentActivity); + m.setArguments(dbargs); + + QDBusMessage res = QDBusConnection::sessionBus().call(m); + QList answer = res.arguments(); + if (answer.size() == 1) { + bool canBeUsed = answer.at(0).toBool(); + + // If the Kate instace is in a specific activity, add it to + // the list of candidate reusable services + if (canBeUsed) { + kateServices << serviceName; + } + } + } else { + kateServices << serviceName; + } } + QString serviceName; QString start_session; @@ -322,7 +358,7 @@ } } } - + // prefer the Kate instance running on the current virtual desktop bool foundRunningService = false; if ((!force_new) && (serviceName.isEmpty())) {