diff --git a/libtaskmanager/abstracttasksmodel.h b/libtaskmanager/abstracttasksmodel.h index 19bd2696e..d62490ea0 100644 --- a/libtaskmanager/abstracttasksmodel.h +++ b/libtaskmanager/abstracttasksmodel.h @@ -1,282 +1,287 @@ /******************************************************************** Copyright 2016 Eike Hein This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #ifndef ABSTRACTTASKSMODEL_H #define ABSTRACTTASKSMODEL_H #include "abstracttasksmodeliface.h" #include #include "taskmanager_export.h" namespace TaskManager { /** * @short An abstract base class for (flat) tasks models. * * This class serves as abstract base class for flat tasks model implementations. * It provides data roles and no-op default implementations of methods in the * AbstractTasksModelIface interface. * * @author Eike Hein **/ class TASKMANAGER_EXPORT AbstractTasksModel : public QAbstractListModel, public AbstractTasksModelIface { Q_OBJECT public: enum AdditionalRoles { AppId = Qt::UserRole + 1, /**< KService storage id (.desktop name sans extension). */ AppName, /**< Application name. */ GenericName, /**< Generic application name. */ LauncherUrl, /**< URL that can be used to launch this application (.desktop or executable). */ LauncherUrlWithoutIcon, /**< Special path to get a launcher URL while skipping fallback icon encoding. Used as speed optimization. */ LegacyWinIdList, /**< X11 window ids. Stopgap until we have something better. */ MimeType, /**< MIME type for this task (window, window group), needed for DND. */ MimeData, /**< Data for MimeType. */ IsWindow, /**< This is a window task. */ IsStartup, /**< This is a startup task. */ IsLauncher, /**< This is a launcher task. */ HasLauncher, /**< A launcher exists for this task. Only implemented by TasksModel, not by either the single-type or munging tasks models. */ IsGroupParent, /**< This is a parent item for a group of child tasks. */ ChildCount, /**< The number of tasks in this group. */ IsGroupable, /**< Whether this task is being ignored by grouping or not. */ IsActive, /**< This is the currently active task. */ IsClosable, /**< requestClose (see below) available. */ IsMovable, /**< requestMove (see below) available. */ IsResizable, /**< requestResize (see below) available. */ IsMaximizable, /**< requestToggleMaximize (see below) available. */ IsMaximized, /**< Task (i.e. window) is maximized. */ IsMinimizable, /**< requestToggleMinimize (see below) available. */ IsMinimized, /**< Task (i.e. window) is minimized. */ IsKeepAbove, /**< Task (i.e. window) is keep-above. */ IsKeepBelow, /**< Task (i.e. window) is keep-below. */ IsFullScreenable, /**< requestToggleFullScreen (see below) available. */ IsFullScreen, /**< Task (i.e. window) is fullscreen. */ IsShadeable, /**< requestToggleShade (see below) available. */ IsShaded, /**< Task (i.e. window) is shaded. */ IsVirtualDesktopChangeable, /**< requestVirtualDesktop (see below) available. */ VirtualDesktop, /**< Virtual desktop for the task (i.e. window). */ IsOnAllVirtualDesktops, /**< Task is on all virtual desktops. */ Geometry, /**< The task's geometry (i.e. the window's). */ ScreenGeometry, /**< Screen geometry for the task (i.e. the window's screen). */ Activities, /**< Activities for the task (i.e. window). */ IsDemandingAttention, /**< Task is demanding attention. */ SkipTaskbar, /**< Task should not be shown in a 'task bar' user interface. */ SkipPager, /**< Task should not to be shown in a 'pager' user interface. */ - AppPid, /**< Application Process ID */ + AppPid, /**< Application Process ID. This is provided best-effort, and may not + be what you expect: For window tasks owned by processes started + from e.g. kwin_wayland, it would be the process id of kwin + itself. DO NOT use this for destructive actions such as closing + the application. The intended use case is to try and (smartly) + gather more information about the task when needed. */ }; Q_ENUM(AdditionalRoles) explicit AbstractTasksModel(QObject *parent = nullptr); virtual ~AbstractTasksModel(); QHash roleNames() const override; virtual QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const override; /** * Request activation of the task at the given index. Derived classes are * free to interpret the meaning of "activate" themselves depending on * the nature and state of the task, e.g. launch or raise a window task. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestActivate(const QModelIndex &index) override; /** * Request an additional instance of the application backing the task * at the given index. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestNewInstance(const QModelIndex &index) override; /** * Requests to open the given URLs with the application backing the task * at the given index. * * This base implementation does nothing. * * @param index An index in this tasks model. * @param urls The URLs to be passed to the application. **/ virtual void requestOpenUrls(const QModelIndex &index, const QList &urls) override; /** * Request the task at the given index be closed. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestClose(const QModelIndex &index) override; /** * Request starting an interactive move for the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestMove(const QModelIndex &index) override; /** * Request starting an interactive resize for the task at the given index. * * This is meant for tasks that have an associated window, and may be a * no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestResize(const QModelIndex &index) override; /** * Request toggling the minimized state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleMinimized(const QModelIndex &index) override; /** * Request toggling the maximized state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleMaximized(const QModelIndex &index) override; /** * Request toggling the keep-above state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleKeepAbove(const QModelIndex &index) override; /** * Request toggling the keep-below state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleKeepBelow(const QModelIndex &index) override; /** * Request toggling the fullscreen state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleFullScreen(const QModelIndex &index) override; /** * Request toggling the shaded state of the task at the given index. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. **/ virtual void requestToggleShaded(const QModelIndex &index) override; /** * Request moving the task at the given index to the specified virtual * desktop. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. * @param desktop A virtual desktop number. **/ virtual void requestVirtualDesktop(const QModelIndex &index, qint32 desktop) override; /** * Request moving the task at the given index to the specified activities. * * This is meant for tasks that have an associated window, and may be * a no-op when there is no window. * * This base implementation does nothing. * * @param index An index in this tasks model. * @param activities The new list of activities. **/ virtual void requestActivities(const QModelIndex &index, const QStringList &activities) override; /** * Request informing the window manager of new geometry for a visual * delegate for the task at the given index. The geometry should be in * screen coordinates. * * This base implementation does nothing. * * @param index An index in this tasks model. * @param geometry Visual delegate geometry in screen coordinates. * @param delegate The delegate. Implementations are on their own with * regard to extracting information from this, and should take care to * reject invalid objects. **/ virtual void requestPublishDelegateGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate = nullptr) override; }; } #endif diff --git a/libtaskmanager/waylandtasksmodel.cpp b/libtaskmanager/waylandtasksmodel.cpp index 1308f538a..85734fe98 100644 --- a/libtaskmanager/waylandtasksmodel.cpp +++ b/libtaskmanager/waylandtasksmodel.cpp @@ -1,546 +1,550 @@ /******************************************************************** Copyright 2016 Eike Hein This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . *********************************************************************/ #include "waylandtasksmodel.h" #include "tasktools.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace TaskManager { class WaylandTasksModel::Private { public: Private(WaylandTasksModel *q); QList windows; QHash appDataCache; KWayland::Client::PlasmaWindowManagement *windowManagement = nullptr; void initWayland(); void addWindow(KWayland::Client::PlasmaWindow *window); AppData appData(KWayland::Client::PlasmaWindow *window); void dataChanged(KWayland::Client::PlasmaWindow *window, int role); void dataChanged(KWayland::Client::PlasmaWindow *window, const QVector &roles); private: WaylandTasksModel *q; }; WaylandTasksModel::Private::Private(WaylandTasksModel *q) : q(q) { } void WaylandTasksModel::Private::initWayland() { if (!KWindowSystem::isPlatformWayland()) { return; } KWayland::Client::ConnectionThread *connection = KWayland::Client::ConnectionThread::fromApplication(q); if (!connection) { return; } KWayland::Client::Registry *registry = new KWayland::Client::Registry(q); registry->create(connection); QObject::connect(registry, &KWayland::Client::Registry::plasmaWindowManagementAnnounced, [this, registry] (quint32 name, quint32 version) { windowManagement = registry->createPlasmaWindowManagement(name, version, q); QObject::connect(windowManagement, &KWayland::Client::PlasmaWindowManagement::interfaceAboutToBeReleased, q, [this] { q->beginResetModel(); windows.clear(); q->endResetModel(); } ); QObject::connect(windowManagement, &KWayland::Client::PlasmaWindowManagement::windowCreated, q, [this](KWayland::Client::PlasmaWindow *window) { addWindow(window); } ); const auto windows = windowManagement->windows(); for (auto it = windows.constBegin(); it != windows.constEnd(); ++it) { addWindow(*it); } } ); registry->setup(); } void WaylandTasksModel::Private::addWindow(KWayland::Client::PlasmaWindow *window) { if (windows.indexOf(window) != -1) { return; } const int count = windows.count(); q->beginInsertRows(QModelIndex(), count, count); windows.append(window); q->endInsertRows(); auto removeWindow = [window, this] { const int row = windows.indexOf(window); if (row != -1) { q->beginRemoveRows(QModelIndex(), row, row); windows.removeAt(row); appDataCache.remove(window); q->endRemoveRows(); } }; QObject::connect(window, &KWayland::Client::PlasmaWindow::unmapped, q, removeWindow); QObject::connect(window, &QObject::destroyed, q, removeWindow); QObject::connect(window, &KWayland::Client::PlasmaWindow::titleChanged, q, [window, this] { dataChanged(window, Qt::DisplayRole); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::iconChanged, q, [window, this] { dataChanged(window, Qt::DecorationRole); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::appIdChanged, q, [window, this] { appDataCache.remove(window); dataChanged(window, QVector{AppId, AppName, GenericName, LauncherUrl, LauncherUrlWithoutIcon}); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::activeChanged, q, [window, this] { this->dataChanged(window, IsActive); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::closeableChanged, q, [window, this] { this->dataChanged(window, IsClosable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::movableChanged, q, [window, this] { this->dataChanged(window, IsMovable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::resizableChanged, q, [window, this] { this->dataChanged(window, IsResizable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::fullscreenableChanged, q, [window, this] { this->dataChanged(window, IsFullScreenable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::fullscreenChanged, q, [window, this] { this->dataChanged(window, IsFullScreen); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::maximizeableChanged, q, [window, this] { this->dataChanged(window, IsMaximizable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::maximizedChanged, q, [window, this] { this->dataChanged(window, IsMaximized); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::minimizeableChanged, q, [window, this] { this->dataChanged(window, IsMinimizable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::minimizedChanged, q, [window, this] { this->dataChanged(window, IsMinimized); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::keepAboveChanged, q, [window, this] { this->dataChanged(window, IsKeepAbove); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::keepBelowChanged, q, [window, this] { this->dataChanged(window, IsKeepBelow); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::shadeableChanged, q, [window, this] { this->dataChanged(window, IsShadeable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::virtualDesktopChangeableChanged, q, [window, this] { this->dataChanged(window, IsVirtualDesktopChangeable); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::virtualDesktopChanged, q, [window, this] { this->dataChanged(window, VirtualDesktop); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::onAllDesktopsChanged, q, [window, this] { this->dataChanged(window, IsOnAllVirtualDesktops); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::geometryChanged, q, [window, this] { this->dataChanged(window, QVector{Geometry, ScreenGeometry}); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::demandsAttentionChanged, q, [window, this] { this->dataChanged(window, IsDemandingAttention); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::skipTaskbarChanged, q, [window, this] { this->dataChanged(window, SkipTaskbar); } ); + + QObject::connect(window, &KWayland::Client::PlasmaWindow::pidChanged, q, + [window, this] { this->dataChanged(window, AppPid); } + ); } AppData WaylandTasksModel::Private::appData(KWayland::Client::PlasmaWindow *window) { const auto &it = appDataCache.constFind(window); if (it != appDataCache.constEnd()) { return *it; } const AppData &data = appDataFromAppId(window->appId()); appDataCache.insert(window, data); return data; } void WaylandTasksModel::Private::dataChanged(KWayland::Client::PlasmaWindow *window, int role) { QModelIndex idx = q->index(windows.indexOf(window)); emit q->dataChanged(idx, idx, QVector{role}); } void WaylandTasksModel::Private::dataChanged(KWayland::Client::PlasmaWindow *window, const QVector &roles) { QModelIndex idx = q->index(windows.indexOf(window)); emit q->dataChanged(idx, idx, roles); } WaylandTasksModel::WaylandTasksModel(QObject *parent) : AbstractWindowTasksModel(parent) , d(new Private(this)) { d->initWayland(); } WaylandTasksModel::~WaylandTasksModel() = default; QVariant WaylandTasksModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= d->windows.count()) { return QVariant(); } KWayland::Client::PlasmaWindow *window = d->windows.at(index.row()); if (role == Qt::DisplayRole) { return window->title(); } else if (role == Qt::DecorationRole) { return window->icon(); } else if (role == AppId) { return window->appId(); } else if (role == AppName) { return d->appData(window).name; } else if (role == GenericName) { return d->appData(window).genericName; } else if (role == LauncherUrl || role == LauncherUrlWithoutIcon) { return d->appData(window).url; } else if (role == IsWindow) { return true; } else if (role == IsActive) { return window->isActive(); } else if (role == IsClosable) { return window->isCloseable(); } else if (role == IsMovable) { return window->isMovable(); } else if (role == IsResizable) { return window->isResizable(); } else if (role == IsMaximizable) { return window->isMaximizeable(); } else if (role == IsMaximized) { return window->isMaximized(); } else if (role == IsMinimizable) { return window->isMinimizeable(); } else if (role == IsMinimized) { return window->isMinimized(); } else if (role == IsKeepAbove) { return window->isKeepAbove(); } else if (role == IsKeepBelow) { return window->isKeepBelow(); } else if (role == IsFullScreenable) { return window->isFullscreenable(); } else if (role == IsFullScreen) { return window->isFullscreen(); } else if (role == IsShadeable) { return window->isShadeable(); } else if (role == IsShaded) { return window->isShaded(); } else if (role == IsVirtualDesktopChangeable) { return window->isVirtualDesktopChangeable(); } else if (role == VirtualDesktop) { return window->virtualDesktop(); } else if (role == IsOnAllVirtualDesktops) { return window->isOnAllDesktops(); } else if (role == Geometry) { return window->geometry(); } else if (role == ScreenGeometry) { return screenGeometry(window->geometry().center()); } else if (role == Activities) { // FIXME Implement. } else if (role == IsDemandingAttention) { return window->isDemandingAttention(); } else if (role == SkipTaskbar) { return window->skipTaskbar(); } else if (role == SkipPager) { // FIXME Implement. } else if (role == AppPid) { - // FIXME Implement. + return window->pid(); } return QVariant(); } int WaylandTasksModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : d->windows.count(); } QModelIndex WaylandTasksModel::index(int row, int column, const QModelIndex &parent) const { return hasIndex(row, column, parent) ? createIndex(row, column, d->windows.at(row)) : QModelIndex(); } void WaylandTasksModel::requestActivate(const QModelIndex &index) { // FIXME Lacks transient handling of the XWindows version. if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestActivate(); } void WaylandTasksModel::requestNewInstance(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { 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")); } } } void WaylandTasksModel::requestOpenUrls(const QModelIndex &index, const QList &urls) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count() || urls.isEmpty()) { 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")); } } void WaylandTasksModel::requestClose(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestClose(); } void WaylandTasksModel::requestMove(const QModelIndex &index) { // FIXME Move-to-desktop logic from XWindows version. (See also others.) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestMove(); } void WaylandTasksModel::requestResize(const QModelIndex &index) { // FIXME Move-to-desktop logic from XWindows version. (See also others.) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestResize(); } void WaylandTasksModel::requestToggleMinimized(const QModelIndex &index) { // FIXME Move-to-desktop logic from XWindows version. (See also others.) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestToggleMinimized(); } void WaylandTasksModel::requestToggleMaximized(const QModelIndex &index) { // FIXME Move-to-desktop logic from XWindows version. (See also others.) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestToggleMaximized(); } void WaylandTasksModel::requestToggleKeepAbove(const QModelIndex &index) { Q_UNUSED(index) // FIXME Implement. } void WaylandTasksModel::requestToggleKeepBelow(const QModelIndex &index) { Q_UNUSED(index) // FIXME Implement. } void WaylandTasksModel::requestToggleFullScreen(const QModelIndex &index) { Q_UNUSED(index) // FIXME Implement. } void WaylandTasksModel::requestToggleShaded(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestToggleShaded(); } void WaylandTasksModel::requestVirtualDesktop(const QModelIndex &index, qint32 desktop) { // FIXME Lacks add-new-desktop code from XWindows version. // FIXME Does this do the set-on-all-desktops stuff from the XWindows version? if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestVirtualDesktop(desktop); } void WaylandTasksModel::requestActivities(const QModelIndex &index, const QStringList &activities) { Q_UNUSED(index) Q_UNUSED(activities) } void WaylandTasksModel::requestPublishDelegateGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate) { /* FIXME: This introduces the dependency on Qt5::Quick. I might prefer reversing this and publishing the window pointer through the model, then calling PlasmaWindow::setMinimizeGeometry in the applet backend, rather than hand delegate items into the lib, keeping the lib more UI- agnostic. */ Q_UNUSED(geometry) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const QQuickItem *item = qobject_cast(delegate); if (!item || !item->parentItem() || !item->window()) { return; } QWindow *itemWindow = item->window(); if (!itemWindow) { return; } using namespace KWayland::Client; Surface *surface = Surface::fromWindow(itemWindow); if (!surface) { return; } QRect rect(item->x(), item->y(), item->width(), item->height()); rect.moveTopLeft(item->parentItem()->mapToScene(rect.topLeft()).toPoint()); KWayland::Client::PlasmaWindow *window = d->windows.at(index.row()); window->setMinimizedGeometry(surface, rect); } }