diff --git a/plugins/CMakeLists.txt b/plugins/CMakeLists.txt --- a/plugins/CMakeLists.txt +++ b/plugins/CMakeLists.txt @@ -1,3 +1,4 @@ if (libpcap_FOUND) add_subdirectory(process/network) endif() + add_subdirectory(process/taskmanager) diff --git a/plugins/process/taskmanager/CMakeLists.txt b/plugins/process/taskmanager/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/plugins/process/taskmanager/CMakeLists.txt @@ -0,0 +1,6 @@ +find_package(LibTaskManager 5.15 CONFIG REQUIRED) + +add_library(ksysguard_plugin_taskmanager MODULE taskmanager.cpp) +target_link_libraries(ksysguard_plugin_taskmanager Qt5::Core Qt5::DBus Qt5::Gui KF5::CoreAddons KF5::I18n KF5::ProcessCore PW::LibTaskManager KF5::Service) + +install(TARGETS ksysguard_plugin_taskmanager DESTINATION ${KDE_INSTALL_PLUGINDIR}/ksysguard/process) diff --git a/plugins/process/taskmanager/taskmanager.h b/plugins/process/taskmanager/taskmanager.h new file mode 100644 --- /dev/null +++ b/plugins/process/taskmanager/taskmanager.h @@ -0,0 +1,43 @@ +/* This file is part of the KDE project + + Copyright (C) 2019 David Edmundson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#pragma once + +#include + +class QModelIndex; + +namespace TaskManager { + class WindowTasksModel; +} + +class TaskManagerIntegration : public KSysGuard::ProcessDataProvider +{ + Q_OBJECT +public: + TaskManagerIntegration(QObject *parent, const QVariantList &args); +private: + void attachPidToTMIndex(long pid, const QModelIndex &index); + KSysGuard::ProcessAttribute *m_iconAttribute = nullptr; + KSysGuard::ProcessAttribute *m_titleAttribute = nullptr; + KSysGuard::ProcessAttribute *m_appIdAttribute = nullptr; + + TaskManager::WindowTasksModel *m_model = nullptr; +}; diff --git a/plugins/process/taskmanager/taskmanager.cpp b/plugins/process/taskmanager/taskmanager.cpp new file mode 100644 --- /dev/null +++ b/plugins/process/taskmanager/taskmanager.cpp @@ -0,0 +1,99 @@ +/* This file is part of the KDE project + + Copyright (C) 2019 David Edmundson + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include "taskmanager.h" + +#include + +#include +#include + +#include +#include + +#include +#include + +using namespace KSysGuard; + +TaskManagerIntegration::TaskManagerIntegration(QObject *parent, const QVariantList &args) + : ProcessDataProvider(parent, args) +{ + if (!QGuiApplication::instance()) { + return; + } + m_model = new TaskManager::WindowTasksModel(this); + + m_iconAttribute = new ProcessAttribute(QStringLiteral("windowIcon"), i18n("Window Icon"), this); + addProcessAttribute(m_iconAttribute); + + m_titleAttribute = new ProcessAttribute(QStringLiteral("windowTitle"), i18n("Window Title"), this); + addProcessAttribute(m_titleAttribute); + + m_appIdAttribute = new ProcessAttribute(QStringLiteral("appId"), i18n("Application ID"), this); + addProcessAttribute(m_appIdAttribute); + + auto updateRows = [this](int first, int last) { + for (int i = first ; i <= last; i++) { + const QModelIndex index = m_model->index(i, 0, QModelIndex()); + attachPidToTMIndex(index.data(TaskManager::AbstractTasksModel::AppPid).toLongLong(), index); + } + }; + + connect(m_model, &QAbstractItemModel::rowsInserted, this, [updateRows](const QModelIndex &parent, int first, int last) { + Q_UNUSED(parent) + updateRows(first, last); + }); + + connect(m_model, &QAbstractItemModel::modelReset, this, [this, updateRows]() { + if (m_model->rowCount() > 0) { + updateRows(0, m_model->rowCount() -1); + } + }); + + connect(m_model, &QAbstractItemModel::dataChanged, this, [updateRows](const QModelIndex &topLeft, const QModelIndex &bottomRight, const QVector &roles ) { + if (roles.isEmpty() || roles.contains(Qt::DisplayRole) || roles.contains(Qt::DecorationRole) || roles.contains(TaskManager::AbstractTasksModel::AppId)) { + updateRows(topLeft.row(), bottomRight.row()); + } + }); + + if (m_model->rowCount() > 0) { + updateRows(0, m_model->rowCount() -1); + } +} + +void TaskManagerIntegration::attachPidToTMIndex(long pid, const QModelIndex &index) +{ + auto process = getProcess(pid); + if (!process) { + return; + } + if (!index.data(TaskManager::AbstractTasksModel::IsWindow).toBool()) { + return; + } + + m_appIdAttribute->setData(process, index.data(TaskManager::AbstractTasksModel::AppId)); + m_titleAttribute->setData(process, index.data(Qt::DisplayRole)); + m_iconAttribute->setData(process, index.data(Qt::DecorationRole)); +} + +K_PLUGIN_FACTORY_WITH_JSON(PluginFactory, "taskmanager.json", registerPlugin();) + +#include "taskmanager.moc" diff --git a/plugins/process/taskmanager/taskmanager.json b/plugins/process/taskmanager/taskmanager.json new file mode 100644 --- /dev/null +++ b/plugins/process/taskmanager/taskmanager.json @@ -0,0 +1,5 @@ +{ + "KPlugin": { + "Description": "Link process task manager information" + } +}