diff --git a/kate/kateapp.h b/kate/kateapp.h --- a/kate/kateapp.h +++ b/kate/kateapp.h @@ -191,6 +191,13 @@ */ 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(const QString &activity); + KTextEditor::Document *openDocUrl(const QUrl &url, const QString &encoding, bool isTempFile); void emitDocumentClosed(const QString &token); diff --git a/kate/kateapp.cpp b/kate/kateapp.cpp --- a/kate/kateapp.cpp +++ b/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(const QString &activity) +{ + for (const auto& window : 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(); diff --git a/kate/kateappadaptor.h b/kate/kateappadaptor.h --- a/kate/kateappadaptor.h +++ b/kate/kateappadaptor.h @@ -48,6 +48,13 @@ */ 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(const QString &activity); + /** * open a file with given url and encoding * will get view created diff --git a/kate/kateappadaptor.cpp b/kate/kateappadaptor.cpp --- a/kate/kateappadaptor.cpp +++ b/kate/kateappadaptor.cpp @@ -59,6 +59,10 @@ return m_app->openUrl(QUrl(url), encoding, isTempFile); } +bool KateAppAdaptor::isOnActivity(const QString &activity) { + return m_app->isOnActivity(activity); +} + //----------- QString KateAppAdaptor::tokenOpenUrl(QString url, QString encoding) { diff --git a/kate/main.cpp b/kate/main.cpp --- a/kate/main.cpp +++ b/kate/main.cpp @@ -283,10 +283,46 @@ 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) { + const bool canBeUsed = answer.at(0).toBool(); + + // If the Kate instance 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;