diff --git a/libtaskmanager/tasktools.h b/libtaskmanager/tasktools.h --- a/libtaskmanager/tasktools.h +++ b/libtaskmanager/tasktools.h @@ -126,6 +126,17 @@ * @return The geometry of the screen containing pos or closest to pos. */ TASKMANAGER_EXPORT QRect screenGeometry(const QPoint &pos); + +/** + * Attempts to run the application described by the AppData struct that + * is passed in, optionally also handing the application a list of URLs + * to open. + * + * @param appData An application data struct. + * @param urls A list of URLs for the application to open. + */ +TASKMANAGER_EXPORT void runApp(const AppData &appData, + const QList &urls = QList()); } #endif diff --git a/libtaskmanager/tasktools.cpp b/libtaskmanager/tasktools.cpp --- a/libtaskmanager/tasktools.cpp +++ b/libtaskmanager/tasktools.cpp @@ -21,17 +21,25 @@ #include "tasktools.h" #include "abstracttasksmodel.h" +#include #include #include #include #include #include #include #include +#include +#include + +#include #include #include #include +#if HAVE_X11 +#include +#endif namespace TaskManager { @@ -310,4 +318,34 @@ return screenGeometry; } +void runApp(const AppData &appData, const QList &urls) +{ + if (appData.url.isValid()) { + quint32 timeStamp = 0; + +#if HAVE_X11 + if (KWindowSystem::isPlatformX11()) { + timeStamp = QX11Info::appUserTime(); + } +#endif + + const KService::Ptr service = KService::serviceByDesktopPath(appData.url.toLocalFile()); + + if (service && service->isApplication()) { + KRun::runApplication(*service, urls, nullptr, 0, {}, + KStartupInfo::createNewStartupIdForTimestamp(timeStamp)); + + KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + service->storageId()), + QStringLiteral("org.kde.libtaskmanager")); + } else { + new KRun(appData.url, 0, false, KStartupInfo::createNewStartupIdForTimestamp(timeStamp)); + + if (!appData.id.isEmpty()) { + KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + appData.id), + QStringLiteral("org.kde.libtaskmanager")); + } + } + } +} + } diff --git a/libtaskmanager/waylandtasksmodel.cpp b/libtaskmanager/waylandtasksmodel.cpp --- a/libtaskmanager/waylandtasksmodel.cpp +++ b/libtaskmanager/waylandtasksmodel.cpp @@ -366,18 +366,7 @@ return; } - KWayland::Client::PlasmaWindow* window = d->windows.at(index.row()); - - if (d->appDataCache.contains(window)) { - const AppData &data = d->appData(window); - - new KRun(data.url, 0, false); - - if (!data.id.isEmpty()) { - KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + data.id), - QStringLiteral("org.kde.libtaskmanager")); - } - } + runApp(d->appData(d->windows.at(index.row()))); } void WaylandTasksModel::requestOpenUrls(const QModelIndex &index, const QList &urls) @@ -388,15 +377,7 @@ return; } - const QUrl &url = d->appData(d->windows.at(index.row())).url; - const KService::Ptr service = KService::serviceByDesktopPath(url.toLocalFile()); - - if (service) { - KRun::runApplication(*service, urls, nullptr, 0); - - KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + service->storageId()), - QStringLiteral("org.kde.libtaskmanager")); - } + runApp(d->appData(d->windows.at(index.row())), urls); } void WaylandTasksModel::requestClose(const QModelIndex &index) diff --git a/libtaskmanager/xwindowtasksmodel.cpp b/libtaskmanager/xwindowtasksmodel.cpp --- a/libtaskmanager/xwindowtasksmodel.cpp +++ b/libtaskmanager/xwindowtasksmodel.cpp @@ -1015,16 +1015,7 @@ return; } - const AppData &data = d->appData(d->windows.at(index.row())); - - if (data.url.isValid()) { - new KRun(data.url, 0, false, KStartupInfo::createNewStartupIdForTimestamp(QX11Info::appUserTime())); - - if (!data.id.isEmpty()) { - KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + data.id), - QStringLiteral("org.kde.libtaskmanager")); - } - } + runApp(d->appData(d->windows.at(index.row()))); } void XWindowTasksModel::requestOpenUrls(const QModelIndex &index, const QList &urls) @@ -1035,15 +1026,7 @@ return; } - const QUrl &url = d->appData(d->windows.at(index.row())).url; - const KService::Ptr service = KService::serviceByDesktopPath(url.toLocalFile()); - - if (service) { - KRun::runApplication(*service, urls, nullptr, 0, {}, KStartupInfo::createNewStartupIdForTimestamp(QX11Info::appUserTime())); - - KActivities::ResourceInstance::notifyAccessed(QUrl(QStringLiteral("applications:") + service->storageId()), - QStringLiteral("org.kde.libtaskmanager")); - } + runApp(d->appData(d->windows.at(index.row())), urls); } void XWindowTasksModel::requestClose(const QModelIndex &index)