diff --git a/libtaskmanager/abstracttasksmodel.h b/libtaskmanager/abstracttasksmodel.h index e40d714c6..159513ff7 100644 --- a/libtaskmanager/abstracttasksmodel.h +++ b/libtaskmanager/abstracttasksmodel.h @@ -1,306 +1,310 @@ /******************************************************************** 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. */ WinIdList, /**< NOTE: On Wayland, these ids are only useful within the same process. On X11, they are global window ids. */ 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. */ IsVirtualDesktopsChangeable, /**< requestVirtualDesktop (see below) available. */ VirtualDesktops, /**< Virtual desktops 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. 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. */ StackingOrder, /**< A window task's index in the window stacking order. Care must be taken not to assume this index to be unique when iterating over model contents due to the asynchronous nature of the windowing system. */ LastActivated, /**< The timestamp of the last time a task was the active task. */ + ApplicationMenuServiceName, /**< The DBus service name for the application's menu. + May be empty. @since 5.19 */ + ApplicationMenuObjectPath,/**< The DBus object path for the application's menu. + May be empty. @since 5.19 */ }; Q_ENUM(AdditionalRoles) explicit AbstractTasksModel(QObject *parent = nullptr); ~AbstractTasksModel() override; QHash roleNames() const override; 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ 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. **/ void requestToggleShaded(const QModelIndex &index) override; /** * Request entering the window at the given index on the specified virtual desktops, * leaving any other desktops. * * On Wayland, virtual desktop ids are QStrings. On X11, they are uint >0. * * An empty list has a special meaning: The window is entered on all virtual desktops * in the session. * * On X11, a window can only be on one or all virtual desktops. Therefore, only the * first list entry is actually used. * * On X11, the id 0 has a special meaning: The window is entered on all virtual * desktops in the session. * * @param index An index in this window tasks model. * @param desktops A list of virtual desktop ids. **/ void requestVirtualDesktops(const QModelIndex &index, const QVariantList &desktops) override; /** * Request entering the window at the given index on a new virtual desktop, * which is created in response to this request. * * @param index An index in this window tasks model. **/ void requestNewVirtualDesktop(const QModelIndex &index) 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. **/ 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. **/ void requestPublishDelegateGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate = nullptr) override; }; } #endif diff --git a/libtaskmanager/waylandtasksmodel.cpp b/libtaskmanager/waylandtasksmodel.cpp index 7b0b8d718..661cc9c60 100644 --- a/libtaskmanager/waylandtasksmodel.cpp +++ b/libtaskmanager/waylandtasksmodel.cpp @@ -1,768 +1,778 @@ /******************************************************************** 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 "virtualdesktopinfo.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace TaskManager { class Q_DECL_HIDDEN WaylandTasksModel::Private { public: Private(WaylandTasksModel *q); QList windows; QHash appDataCache; KWayland::Client::PlasmaWindowManagement *windowManagement = nullptr; KSharedConfig::Ptr rulesConfig; KDirWatch *configWatcher = nullptr; VirtualDesktopInfo *virtualDesktopInfo = nullptr; static QUuid uuid; void init(); void initWayland(); void addWindow(KWayland::Client::PlasmaWindow *window); AppData appData(KWayland::Client::PlasmaWindow *window); QIcon icon(KWayland::Client::PlasmaWindow *window); static QString mimeType(); static QString groupMimeType(); void dataChanged(KWayland::Client::PlasmaWindow *window, int role); void dataChanged(KWayland::Client::PlasmaWindow *window, const QVector &roles); private: WaylandTasksModel *q; }; QUuid WaylandTasksModel::Private::uuid = QUuid::createUuid(); WaylandTasksModel::Private::Private(WaylandTasksModel *q) : q(q) { } void WaylandTasksModel::Private::init() { auto clearCacheAndRefresh = [this] { if (!windows.count()) { return; } appDataCache.clear(); // Emit changes of all roles satisfied from app data cache. q->dataChanged(q->index(0, 0), q->index(windows.count() - 1, 0), QVector{Qt::DecorationRole, AbstractTasksModel::AppId, AbstractTasksModel::AppName, AbstractTasksModel::GenericName, AbstractTasksModel::LauncherUrl, AbstractTasksModel::LauncherUrlWithoutIcon, AbstractTasksModel::SkipTaskbar}); }; rulesConfig = KSharedConfig::openConfig(QStringLiteral("taskmanagerrulesrc")); configWatcher = new KDirWatch(q); foreach (const QString &location, QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)) { configWatcher->addFile(location + QLatin1String("/taskmanagerrulesrc")); } auto rulesConfigChange = [this, clearCacheAndRefresh] { rulesConfig->reparseConfiguration(); clearCacheAndRefresh(); }; QObject::connect(configWatcher, &KDirWatch::dirty, rulesConfigChange); QObject::connect(configWatcher, &KDirWatch::created, rulesConfigChange); QObject::connect(configWatcher, &KDirWatch::deleted, rulesConfigChange); virtualDesktopInfo = new VirtualDesktopInfo(q); initWayland(); } 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] { this->dataChanged(window, Qt::DisplayRole); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::iconChanged, q, [window, this] { // The icon in the AppData struct might come from PlasmaWindow if it wasn't // filled in by windowUrlFromMetadata+appDataFromUrl. // TODO: Don't evict the cache unnecessarily if this isn't the case. As icons // are currently very static on Wayland, this eviction is unlikely to happen // frequently as of now. appDataCache.remove(window); this->dataChanged(window, Qt::DecorationRole); } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::appIdChanged, q, [window, this] { // The AppData struct in the cache is derived from this and needs // to be evicted in favor of a fresh struct based on the changed // window metadata. appDataCache.remove(window); // Refresh roles satisfied from the app data cache. this->dataChanged(window, QVector{AppId, AppName, GenericName, LauncherUrl, LauncherUrlWithoutIcon, SkipTaskbar}); } ); 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); } ); // FIXME // QObject::connect(window, &KWayland::Client::PlasmaWindow::virtualDesktopChangeableChanged, q, // // TODO: This is marked deprecated in KWayland, but (IMHO) shouldn't be. // [window, this] { this->dataChanged(window, IsVirtualDesktopsChangeable); } // ); QObject::connect(window, &KWayland::Client::PlasmaWindow::plasmaVirtualDesktopEntered, q, [window, this] { this->dataChanged(window, VirtualDesktops); // If the count has changed from 0, the window may no longer be on all virtual // desktops. if (window->plasmaVirtualDesktops().count() > 0) { this->dataChanged(window, VirtualDesktops); } } ); QObject::connect(window, &KWayland::Client::PlasmaWindow::plasmaVirtualDesktopLeft, q, [window, this] { this->dataChanged(window, VirtualDesktops); // If the count has changed to 0, the window is now on all virtual desktops. if (window->plasmaVirtualDesktops().count() == 0) { this->dataChanged(window, VirtualDesktops); } } ); 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::applicationMenuChanged, q, + [window, this] { + this->dataChanged(window, QVector{ApplicationMenuServiceName, ApplicationMenuObjectPath}); + } + ); } AppData WaylandTasksModel::Private::appData(KWayland::Client::PlasmaWindow *window) { const auto &it = appDataCache.constFind(window); if (it != appDataCache.constEnd()) { return *it; } const AppData &data = appDataFromUrl(windowUrlFromMetadata(window->appId(), window->pid(), rulesConfig)); appDataCache.insert(window, data); return data; } QIcon WaylandTasksModel::Private::icon(KWayland::Client::PlasmaWindow *window) { const AppData &app = appData(window); if (!app.icon.isNull()) { return app.icon; } appDataCache[window].icon = window->icon(); return window->icon(); } QString WaylandTasksModel::Private::mimeType() { // Use a unique format id to make this intentionally useless for // cross-process DND. return QStringLiteral("windowsystem/winid+") + uuid.toString(); } QString WaylandTasksModel::Private::groupMimeType() { // Use a unique format id to make this intentionally useless for // cross-process DND. return QStringLiteral("windowsystem/multiple-winids+") + uuid.toString(); } 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->init(); } 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 d->icon(window); } else if (role == AppId) { const QString &id = d->appData(window).id; if (id.isEmpty()) { return window->appId(); } else { return id; } } 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 == WinIdList) { return QVariantList() << window->internalId(); } else if (role == MimeType) { return d->mimeType(); } else if (role == MimeData) { return QByteArray::number(window->internalId()); } 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 == IsVirtualDesktopsChangeable) { // FIXME Currently not implemented in KWayland. return true; } else if (role == VirtualDesktops) { return window->plasmaVirtualDesktops(); } else if (role == IsOnAllVirtualDesktops) { return window->plasmaVirtualDesktops().isEmpty(); } 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() || d->appData(window).skipTaskbar; } else if (role == SkipPager) { // FIXME Implement. } else if (role == AppPid) { return window->pid(); + } else if (role == ApplicationMenuObjectPath) { + return window->applicationMenuObjectPath(); + } else if (role == ApplicationMenuServiceName) { + return window->applicationMenuServiceName(); } 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; } runApp(d->appData(d->windows.at(index.row()))); } 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; } runApp(d->appData(d->windows.at(index.row())), urls); } 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) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } KWayland::Client::PlasmaWindow *window = d->windows.at(index.row()); const QString ¤tDesktop = d->virtualDesktopInfo->currentDesktop().toString(); if (!currentDesktop.isEmpty()) { window->requestEnterVirtualDesktop(currentDesktop); } window->requestMove(); } void WaylandTasksModel::requestResize(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()); const QString ¤tDesktop = d->virtualDesktopInfo->currentDesktop().toString(); if (!currentDesktop.isEmpty()) { window->requestEnterVirtualDesktop(currentDesktop); } window->requestResize(); } void WaylandTasksModel::requestToggleMinimized(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()); const QString ¤tDesktop = d->virtualDesktopInfo->currentDesktop().toString(); if (!currentDesktop.isEmpty()) { window->requestEnterVirtualDesktop(currentDesktop); } window->requestToggleMinimized(); } void WaylandTasksModel::requestToggleMaximized(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()); const QString ¤tDesktop = d->virtualDesktopInfo->currentDesktop().toString(); if (!currentDesktop.isEmpty()) { window->requestEnterVirtualDesktop(currentDesktop); } window->requestToggleMaximized(); } void WaylandTasksModel::requestToggleKeepAbove(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestToggleKeepAbove(); } void WaylandTasksModel::requestToggleKeepBelow(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestToggleKeepBelow(); } 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::requestVirtualDesktops(const QModelIndex &index, const QVariantList &desktops) { // FIXME TODO: Lacks the "if we've requested the current desktop, force-activate // the window" logic from X11 version. This behavior should be in KWin rather than // libtm however. 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 (desktops.isEmpty()) { foreach (const QVariant &desktop, window->plasmaVirtualDesktops()) { window->requestLeaveVirtualDesktop(desktop.toString()); } } else { const QStringList &now = window->plasmaVirtualDesktops(); QStringList next; foreach (const QVariant &desktop, desktops) { const QString &desktopId = desktop.toString(); if (!desktopId.isEmpty()) { next << desktopId; if (!now.contains(desktopId)) { window->requestEnterVirtualDesktop(desktopId); } } } foreach (const QString &desktop, now) { if (!next.contains(desktop)) { window->requestLeaveVirtualDesktop(desktop); } } } } void WaylandTasksModel::requestNewVirtualDesktop(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } d->windows.at(index.row())->requestEnterNewVirtualDesktop(); } 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); } quint32 WaylandTasksModel::winIdFromMimeData(const QMimeData *mimeData, bool *ok) { Q_ASSERT(mimeData); if (ok) { *ok = false; } if (!mimeData->hasFormat(Private::mimeType())) { return 0; } quint32 id = mimeData->data(Private::mimeType()).toUInt(ok); return id; } QList WaylandTasksModel::winIdsFromMimeData(const QMimeData *mimeData, bool *ok) { Q_ASSERT(mimeData); QList ids; if (ok) { *ok = false; } if (!mimeData->hasFormat(Private::groupMimeType())) { // Try to extract single window id. bool singularOk; WId id = winIdFromMimeData(mimeData, &singularOk); if (ok) { *ok = singularOk; } if (singularOk) { ids << id; } return ids; } // FIXME: Extracting multiple winids is still unimplemented; // TaskGroupingProxy::data(..., ::MimeData) can't produce // a payload with them anyways. return ids; } } diff --git a/libtaskmanager/xwindowtasksmodel.cpp b/libtaskmanager/xwindowtasksmodel.cpp index ac484185b..ff9169394 100644 --- a/libtaskmanager/xwindowtasksmodel.cpp +++ b/libtaskmanager/xwindowtasksmodel.cpp @@ -1,1133 +1,1151 @@ /******************************************************************** Copyright 2016 Eike Hein Copyright 2008 Aaron J. Seigo 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 "xwindowtasksmodel.h" #include "tasktools.h" #include "xwindowsystemeventbatcher.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace TaskManager { static const NET::Properties windowInfoFlags = NET::WMState | NET::XAWMState | NET::WMDesktop | NET::WMVisibleName | NET::WMGeometry | NET::WMFrameExtents | NET::WMWindowType | NET::WMPid; static const NET::Properties2 windowInfoFlags2 = NET::WM2DesktopFileName | NET::WM2Activities | NET::WM2WindowClass | NET::WM2AllowedActions; class Q_DECL_HIDDEN XWindowTasksModel::Private { public: Private(XWindowTasksModel *q); ~Private(); QVector windows; QSet transients; QMultiHash transientsDemandingAttention; QHash windowInfoCache; QHash appDataCache; QHash delegateGeometries; QSet usingFallbackIcon; QHash lastActivated; QList cachedStackingOrder; WId activeWindow = -1; KSharedConfig::Ptr rulesConfig; KDirWatch *configWatcher = nullptr; QTimer sycocaChangeTimer; void init(); void addWindow(WId window); void removeWindow(WId window); void windowChanged(WId window, NET::Properties properties, NET::Properties2 properties2); void transientChanged(WId window, NET::Properties properties, NET::Properties2 properties2); void dataChanged(WId window, const QVector &roles); KWindowInfo* windowInfo(WId window); AppData appData(WId window); + QString appMenuServiceName(WId window); + QString appMenuObjectPath(WId window); QIcon icon(WId window); static QString mimeType(); static QString groupMimeType(); QUrl windowUrl(WId window); QUrl launcherUrl(WId window, bool encodeFallbackIcon = true); bool demandsAttention(WId window); private: XWindowTasksModel *q; }; XWindowTasksModel::Private::Private(XWindowTasksModel *q) : q(q) { } XWindowTasksModel::Private::~Private() { qDeleteAll(windowInfoCache); windowInfoCache.clear(); } void XWindowTasksModel::Private::init() { auto clearCacheAndRefresh = [this] { if (!windows.count()) { return; } appDataCache.clear(); // Emit changes of all roles satisfied from app data cache. q->dataChanged(q->index(0, 0), q->index(windows.count() - 1, 0), QVector{Qt::DecorationRole, AbstractTasksModel::AppId, AbstractTasksModel::AppName, AbstractTasksModel::GenericName, AbstractTasksModel::LauncherUrl, AbstractTasksModel::LauncherUrlWithoutIcon, AbstractTasksModel::SkipTaskbar}); }; cachedStackingOrder = KWindowSystem::stackingOrder(); sycocaChangeTimer.setSingleShot(true); sycocaChangeTimer.setInterval(100); QObject::connect(&sycocaChangeTimer, &QTimer::timeout, q, clearCacheAndRefresh); void (KSycoca::*myDatabaseChangeSignal)(const QStringList &) = &KSycoca::databaseChanged; QObject::connect(KSycoca::self(), myDatabaseChangeSignal, q, [this](const QStringList &changedResources) { if (changedResources.contains(QLatin1String("services")) || changedResources.contains(QLatin1String("apps")) || changedResources.contains(QLatin1String("xdgdata-apps"))) { sycocaChangeTimer.start(); } } ); rulesConfig = KSharedConfig::openConfig(QStringLiteral("taskmanagerrulesrc")); configWatcher = new KDirWatch(q); foreach (const QString &location, QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)) { configWatcher->addFile(location + QLatin1String("/taskmanagerrulesrc")); } auto rulesConfigChange = [this, clearCacheAndRefresh] { rulesConfig->reparseConfiguration(); clearCacheAndRefresh(); }; QObject::connect(configWatcher, &KDirWatch::dirty, rulesConfigChange); QObject::connect(configWatcher, &KDirWatch::created, rulesConfigChange); QObject::connect(configWatcher, &KDirWatch::deleted, rulesConfigChange); auto windowSystem = new XWindowSystemEventBatcher(q); QObject::connect(windowSystem, &XWindowSystemEventBatcher::windowAdded, q, [this](WId window) { addWindow(window); } ); QObject::connect(windowSystem, &XWindowSystemEventBatcher::windowRemoved, q, [this](WId window) { removeWindow(window); } ); QObject::connect(windowSystem, &XWindowSystemEventBatcher::windowChanged, q, [this](WId window, NET::Properties properties, NET::Properties2 properties2) { windowChanged(window, properties, properties2); } ); // Update IsActive for previously- and newly-active windows. QObject::connect(KWindowSystem::self(), &KWindowSystem::activeWindowChanged, q, [this](WId window) { const WId oldActiveWindow = activeWindow; activeWindow = window; lastActivated[activeWindow] = QTime::currentTime(); int row = windows.indexOf(oldActiveWindow); if (row != -1) { dataChanged(oldActiveWindow, QVector{IsActive}); } row = windows.indexOf(window); if (row != -1) { dataChanged(window, QVector{IsActive}); } } ); QObject::connect(KWindowSystem::self(), &KWindowSystem::stackingOrderChanged, q, [this]() { cachedStackingOrder = KWindowSystem::stackingOrder(); q->dataChanged(q->index(0, 0), q->index(q->rowCount() - 1, 0), QVector{StackingOrder}); } ); activeWindow = KWindowSystem::activeWindow(); // Add existing windows. foreach(const WId window, KWindowSystem::windows()) { addWindow(window); } } void XWindowTasksModel::Private::addWindow(WId window) { // Don't add window twice. if (windows.contains(window)) { return; } KWindowInfo info(window, NET::WMWindowType | NET::WMState | NET::WMName | NET::WMVisibleName, NET::WM2TransientFor); NET::WindowType wType = info.windowType(NET::NormalMask | NET::DesktopMask | NET::DockMask | NET::ToolbarMask | NET::MenuMask | NET::DialogMask | NET::OverrideMask | NET::TopMenuMask | NET::UtilityMask | NET::SplashMask); const WId leader = info.transientFor(); // Handle transient. if (leader > 0 && leader != window && leader != QX11Info::appRootWindow() && !transients.contains(window) && windows.contains(leader)) { transients.insert(window); // Update demands attention state for leader. if (info.hasState(NET::DemandsAttention) && windows.contains(leader)) { transientsDemandingAttention.insertMulti(leader, window); dataChanged(leader, QVector{IsDemandingAttention}); } return; } // Ignore NET::Tool and other special window types; they are not considered tasks. if (wType != NET::Normal && wType != NET::Override && wType != NET::Unknown && wType != NET::Dialog && wType != NET::Utility) { return; } const int count = windows.count(); q->beginInsertRows(QModelIndex(), count, count); windows.append(window); q->endInsertRows(); } void XWindowTasksModel::Private::removeWindow(WId window) { const int row = windows.indexOf(window); if (row != -1) { q->beginRemoveRows(QModelIndex(), row, row); windows.removeAt(row); transientsDemandingAttention.remove(window); delete windowInfoCache.take(window); appDataCache.remove(window); delegateGeometries.remove(window); usingFallbackIcon.remove(window); lastActivated.remove(window); q->endRemoveRows(); } else { // Could be a transient. // Removing a transient might change the demands attention state of the leader. if (transients.remove(window)) { const WId leader = transientsDemandingAttention.key(window, XCB_WINDOW_NONE); if (leader != XCB_WINDOW_NONE) { transientsDemandingAttention.remove(leader, window); dataChanged(leader, QVector{IsDemandingAttention}); } } } if (activeWindow == window) { activeWindow = -1; } } void XWindowTasksModel::Private::transientChanged(WId window, NET::Properties properties, NET::Properties2 properties2) { // Changes to a transient's state might change demands attention state for leader. if (properties & (NET::WMState | NET::XAWMState)) { const KWindowInfo info(window, NET::WMState | NET::XAWMState, NET::WM2TransientFor); const WId leader = info.transientFor(); if (!windows.contains(leader)) { return; } if (info.hasState(NET::DemandsAttention)) { if (!transientsDemandingAttention.values(leader).contains(window)) { transientsDemandingAttention.insertMulti(leader, window); dataChanged(leader, QVector{IsDemandingAttention}); } } else if (transientsDemandingAttention.remove(window)) { dataChanged(leader, QVector{IsDemandingAttention}); } // Leader might have changed. } else if (properties2 & NET::WM2TransientFor) { const KWindowInfo info(window, NET::WMState | NET::XAWMState, NET::WM2TransientFor); if (info.hasState(NET::DemandsAttention)) { const WId oldLeader = transientsDemandingAttention.key(window, XCB_WINDOW_NONE); if (oldLeader != XCB_WINDOW_NONE) { const WId leader = info.transientFor(); if (leader != oldLeader) { transientsDemandingAttention.remove(oldLeader, window); transientsDemandingAttention.insertMulti(leader, window); dataChanged(oldLeader, QVector{IsDemandingAttention}); dataChanged(leader, QVector{IsDemandingAttention}); } } } } } void XWindowTasksModel::Private::windowChanged(WId window, NET::Properties properties, NET::Properties2 properties2) { if (transients.contains(window)) { transientChanged(window, properties, properties2); return; } bool wipeInfoCache = false; bool wipeAppDataCache = false; QVector changedRoles; if (properties & (NET::WMPid) || properties2 & (NET::WM2DesktopFileName | NET::WM2WindowClass)) { wipeInfoCache = true; wipeAppDataCache = true; changedRoles << Qt::DecorationRole << AppId << AppName << GenericName << LauncherUrl << AppPid << SkipTaskbar; } if (properties & (NET::WMName | NET::WMVisibleName)) { changedRoles << Qt::DisplayRole; wipeInfoCache = true; } if ((properties & NET::WMIcon) && usingFallbackIcon.contains(window)) { wipeAppDataCache = true; if (!changedRoles.contains(Qt::DecorationRole)) { changedRoles << Qt::DecorationRole; } } // FIXME TODO: It might be worth keeping track of which windows were demanding // attention (or not) to avoid emitting this role on every state change, as // TaskGroupingProxyModel needs to check for group-ability when a change to it // is announced and the queried state is false. if (properties & (NET::WMState | NET::XAWMState)) { wipeInfoCache = true; changedRoles << IsFullScreen << IsMaximized << IsMinimized << IsKeepAbove << IsKeepBelow; changedRoles << IsShaded << IsDemandingAttention << SkipTaskbar << SkipPager; } if (properties & NET::WMWindowType) { wipeInfoCache = true; changedRoles << SkipTaskbar; } if (properties2 & NET::WM2AllowedActions) { wipeInfoCache = true; changedRoles << IsClosable << IsMovable << IsResizable << IsMaximizable << IsMinimizable; changedRoles << IsFullScreenable << IsShadeable << IsVirtualDesktopsChangeable; } if (properties & NET::WMDesktop) { wipeInfoCache = true; changedRoles << VirtualDesktops << IsOnAllVirtualDesktops; } if (properties & NET::WMGeometry) { wipeInfoCache = true; changedRoles << Geometry << ScreenGeometry; } if (properties2 & NET::WM2Activities) { changedRoles << Activities; } if (wipeInfoCache) { delete windowInfoCache.take(window); } if (wipeAppDataCache) { appDataCache.remove(window); usingFallbackIcon.remove(window); } if (!changedRoles.isEmpty()) { dataChanged(window, changedRoles); } } void XWindowTasksModel::Private::dataChanged(WId window, const QVector &roles) { const int i = windows.indexOf(window); if (i == -1) { return; } QModelIndex idx = q->index(i); emit q->dataChanged(idx, idx, roles); } KWindowInfo* XWindowTasksModel::Private::windowInfo(WId window) { const auto &it = windowInfoCache.constFind(window); if (it != windowInfoCache.constEnd()) { return *it; } KWindowInfo *info = new KWindowInfo(window, windowInfoFlags, windowInfoFlags2); windowInfoCache.insert(window, info); return info; } AppData XWindowTasksModel::Private::appData(WId window) { const auto &it = appDataCache.constFind(window); if (it != appDataCache.constEnd()) { return *it; } const AppData &data = appDataFromUrl(windowUrl(window)); // If we weren't able to derive a launcher URL from the window meta data, // fall back to WM_CLASS Class string as app id. This helps with apps we // can't map to an URL due to existing outside the regular system // environment, e.g. wine clients. if (data.id.isEmpty() && data.url.isEmpty()) { AppData dataCopy = data; dataCopy.id = windowInfo(window)->windowClassClass(); appDataCache.insert(window, dataCopy); return dataCopy; } appDataCache.insert(window, data); return data; } +QString XWindowTasksModel::Private::appMenuServiceName(WId window) +{ + const KWindowInfo *info = windowInfo(window); + return QString::fromUtf8(info->applicationMenuServiceName()); +} + +QString XWindowTasksModel::Private::appMenuObjectPath(WId window) +{ + const KWindowInfo *info = windowInfo(window); + return QString::fromUtf8(info->applicationMenuServiceName()); +} + QIcon XWindowTasksModel::Private::icon(WId window) { const AppData &app = appData(window); if (!app.icon.isNull()) { return app.icon; } QIcon icon; icon.addPixmap(KWindowSystem::icon(window, KIconLoader::SizeSmall, KIconLoader::SizeSmall, false)); icon.addPixmap(KWindowSystem::icon(window, KIconLoader::SizeSmallMedium, KIconLoader::SizeSmallMedium, false)); icon.addPixmap(KWindowSystem::icon(window, KIconLoader::SizeMedium, KIconLoader::SizeMedium, false)); icon.addPixmap(KWindowSystem::icon(window, KIconLoader::SizeLarge, KIconLoader::SizeLarge, false)); appDataCache[window].icon = icon; usingFallbackIcon.insert(window); return icon; } QString XWindowTasksModel::Private::mimeType() { return QStringLiteral("windowsystem/winid"); } QString XWindowTasksModel::Private::groupMimeType() { return QStringLiteral("windowsystem/multiple-winids"); } QUrl XWindowTasksModel::Private::windowUrl(WId window) { const KWindowInfo *info = windowInfo(window); QString desktopFile = QString::fromUtf8(info->desktopFileName()); if (!desktopFile.isEmpty()) { KService::Ptr service = KService::serviceByStorageId(desktopFile); if (service) { const QString &menuId = service->menuId(); // applications: URLs are used to refer to applications by their KService::menuId // (i.e. .desktop file name) rather than the absolute path to a .desktop file. if (!menuId.isEmpty()) { return QUrl(QStringLiteral("applications:") + menuId); } return QUrl::fromLocalFile(service->entryPath()); } if (!desktopFile.endsWith(QLatin1String(".desktop"))) { desktopFile.append(QLatin1String(".desktop")); } if (KDesktopFile::isDesktopFile(desktopFile) && QFile::exists(desktopFile)) { return QUrl::fromLocalFile(desktopFile); } } return windowUrlFromMetadata(info->windowClassClass(), info->pid(), rulesConfig, info->windowClassName()); } QUrl XWindowTasksModel::Private::launcherUrl(WId window, bool encodeFallbackIcon) { const AppData &data = appData(window); QUrl url = data.url; if (!encodeFallbackIcon || !data.icon.name().isEmpty()) { return url; } // Forego adding the window icon pixmap if the URL is otherwise empty. if (!url.isValid()) { return QUrl(); } // Only serialize pixmap data if the window pixmap is actually being used. // QIcon::name() used above only returns a themed icon name but nothing when // the icon was created using an absolute path, as can be the case with, e.g. // containerized apps. if (!usingFallbackIcon.contains(window)) { return url; } QPixmap pixmap; if (!data.icon.isNull()) { pixmap = data.icon.pixmap(KIconLoader::SizeLarge); } if (pixmap.isNull()) { pixmap = KWindowSystem::icon(window, KIconLoader::SizeLarge, KIconLoader::SizeLarge, false); } if (pixmap.isNull()) { return data.url; } QUrlQuery uQuery(url); QByteArray bytes; QBuffer buffer(&bytes); buffer.open(QIODevice::WriteOnly); pixmap.save(&buffer, "PNG"); uQuery.addQueryItem(QStringLiteral("iconData"), bytes.toBase64(QByteArray::Base64UrlEncoding)); url.setQuery(uQuery); return url; } bool XWindowTasksModel::Private::demandsAttention(WId window) { if (windows.contains(window)) { return ((windowInfo(window)->hasState(NET::DemandsAttention)) || transientsDemandingAttention.contains(window)); } return false; } XWindowTasksModel::XWindowTasksModel(QObject *parent) : AbstractWindowTasksModel(parent) , d(new Private(this)) { d->init(); } XWindowTasksModel::~XWindowTasksModel() { } QVariant XWindowTasksModel::data(const QModelIndex &index, int role) const { if (!index.isValid() || index.row() >= d->windows.count()) { return QVariant(); } const WId window = d->windows.at(index.row()); if (role == Qt::DisplayRole) { return d->windowInfo(window)->visibleName(); } else if (role == Qt::DecorationRole) { return d->icon(window); } else if (role == AppId) { return d->appData(window).id; } else if (role == AppName) { return d->appData(window).name; } else if (role == GenericName) { return d->appData(window).genericName; } else if (role == LauncherUrl) { return d->launcherUrl(window); } else if (role == LauncherUrlWithoutIcon) { return d->launcherUrl(window, false /* encodeFallbackIcon */); } else if (role == WinIdList) { return QVariantList() << window; } else if (role == MimeType) { return d->mimeType(); } else if (role == MimeData) { return QByteArray((char*)&window, sizeof(window)); } else if (role == IsWindow) { return true; } else if (role == IsActive) { return (window == d->activeWindow); } else if (role == IsClosable) { return d->windowInfo(window)->actionSupported(NET::ActionClose); } else if (role == IsMovable) { return d->windowInfo(window)->actionSupported(NET::ActionMove); } else if (role == IsResizable) { return d->windowInfo(window)->actionSupported(NET::ActionResize); } else if (role == IsMaximizable) { return d->windowInfo(window)->actionSupported(NET::ActionMax); } else if (role == IsMaximized) { const KWindowInfo *info = d->windowInfo(window); return info->hasState(NET::MaxHoriz) && info->hasState(NET::MaxVert); } else if (role == IsMinimizable) { return d->windowInfo(window)->actionSupported(NET::ActionMinimize); } else if (role == IsMinimized) { return d->windowInfo(window)->isMinimized(); } else if (role == IsKeepAbove) { return d->windowInfo(window)->hasState(NET::KeepAbove); } else if (role == IsKeepBelow) { return d->windowInfo(window)->hasState(NET::KeepBelow); } else if (role == IsFullScreenable) { return d->windowInfo(window)->actionSupported(NET::ActionFullScreen); } else if (role == IsFullScreen) { return d->windowInfo(window)->hasState(NET::FullScreen); } else if (role == IsShadeable) { return d->windowInfo(window)->actionSupported(NET::ActionShade); } else if (role == IsShaded) { return d->windowInfo(window)->hasState(NET::Shaded); } else if (role == IsVirtualDesktopsChangeable) { return d->windowInfo(window)->actionSupported(NET::ActionChangeDesktop); } else if (role == VirtualDesktops) { return QVariantList() << d->windowInfo(window)->desktop(); } else if (role == IsOnAllVirtualDesktops) { return d->windowInfo(window)->onAllDesktops(); } else if (role == Geometry) { return d->windowInfo(window)->frameGeometry(); } else if (role == ScreenGeometry) { return screenGeometry(d->windowInfo(window)->frameGeometry().center()); } else if (role == Activities) { return d->windowInfo(window)->activities(); } else if (role == IsDemandingAttention) { return d->demandsAttention(window); } else if (role == SkipTaskbar) { const KWindowInfo *info = d->windowInfo(window); // _NET_WM_WINDOW_TYPE_UTILITY type windows should not be on task bars, // but they should be shown on pagers. return (info->hasState(NET::SkipTaskbar) || info->windowType(NET::UtilityMask) == NET::Utility || d->appData(window).skipTaskbar); } else if (role == SkipPager) { return d->windowInfo(window)->hasState(NET::SkipPager); } else if (role == AppPid) { return d->windowInfo(window)->pid(); } else if (role == StackingOrder) { return d->cachedStackingOrder.indexOf(window); } else if (role == LastActivated) { if (d->lastActivated.contains(window)) { return d->lastActivated.value(window); } + } else if (role == ApplicationMenuObjectPath) { + return d->appMenuObjectPath(window); + } else if (role == ApplicationMenuServiceName) { + return d->appMenuServiceName(window); } return QVariant(); } int XWindowTasksModel::rowCount(const QModelIndex &parent) const { return parent.isValid() ? 0 : d->windows.count(); } void XWindowTasksModel::requestActivate(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } if (index.row() >= 0 && index.row() < d->windows.count()) { WId window = d->windows.at(index.row()); // Pull forward any transient demanding attention. if (d->transientsDemandingAttention.contains(window)) { window = d->transientsDemandingAttention.value(window); // Quote from legacy libtaskmanager: // "this is a work around for (at least?) kwin where a shaded transient will prevent the main // window from being brought forward unless the transient is actually pulled forward, most // easily reproduced by opening a modal file open/save dialog on an app then shading the file // dialog and trying to bring the window forward by clicking on it in a tasks widget // TODO: do we need to check all the transients for shaded?" } else if (!d->transients.isEmpty()) { foreach (const WId transient, d->transients) { KWindowInfo info(transient, NET::WMState, NET::WM2TransientFor); if (info.valid(true) && info.hasState(NET::Shaded) && info.transientFor() == window) { window = transient; break; } } } KWindowSystem::forceActiveWindow(window); } } void XWindowTasksModel::requestNewInstance(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } runApp(d->appData(d->windows.at(index.row()))); } void XWindowTasksModel::requestOpenUrls(const QModelIndex &index, const QList &urls) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count() || urls.isEmpty()) { return; } runApp(d->appData(d->windows.at(index.row())), urls); } void XWindowTasksModel::requestClose(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } NETRootInfo ri(QX11Info::connection(), NET::CloseWindow); ri.closeWindowRequest(d->windows.at(index.row())); } void XWindowTasksModel::requestMove(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); bool onCurrent = info->isOnCurrentDesktop(); if (!onCurrent) { KWindowSystem::setCurrentDesktop(info->desktop()); KWindowSystem::forceActiveWindow(window); } if (info->isMinimized()) { KWindowSystem::unminimizeWindow(window); } const QRect &geom = info->geometry(); NETRootInfo ri(QX11Info::connection(), NET::WMMoveResize); ri.moveResizeRequest(window, geom.center().x(), geom.center().y(), NET::Move); } void XWindowTasksModel::requestResize(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); bool onCurrent = info->isOnCurrentDesktop(); if (!onCurrent) { KWindowSystem::setCurrentDesktop(info->desktop()); KWindowSystem::forceActiveWindow(window); } if (info->isMinimized()) { KWindowSystem::unminimizeWindow(window); } const QRect &geom = info->geometry(); NETRootInfo ri(QX11Info::connection(), NET::WMMoveResize); ri.moveResizeRequest(window, geom.bottomRight().x(), geom.bottomRight().y(), NET::BottomRight); } void XWindowTasksModel::requestToggleMinimized(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); if (info->isMinimized()) { bool onCurrent = info->isOnCurrentDesktop(); // FIXME: Move logic up into proxy? (See also others.) if (!onCurrent) { KWindowSystem::setCurrentDesktop(info->desktop()); } KWindowSystem::unminimizeWindow(window); if (onCurrent) { KWindowSystem::forceActiveWindow(window); } } else { KWindowSystem::minimizeWindow(window); } } void XWindowTasksModel::requestToggleMaximized(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); bool onCurrent = info->isOnCurrentDesktop(); bool restore = (info->hasState(NET::MaxHoriz) && info->hasState(NET::MaxVert)); // FIXME: Move logic up into proxy? (See also others.) if (!onCurrent) { KWindowSystem::setCurrentDesktop(info->desktop()); } if (info->isMinimized()) { KWindowSystem::unminimizeWindow(window); } NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::WMState, NET::Properties2()); if (restore) { ni.setState(NET::States(), NET::Max); } else { ni.setState(NET::Max, NET::Max); } if (!onCurrent) { KWindowSystem::forceActiveWindow(window); } } void XWindowTasksModel::requestToggleKeepAbove(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::WMState, NET::Properties2()); if (info->hasState(NET::KeepAbove)) { ni.setState(NET::States(), NET::KeepAbove); } else { ni.setState(NET::KeepAbove, NET::KeepAbove); } } void XWindowTasksModel::requestToggleKeepBelow(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::WMState, NET::Properties2()); if (info->hasState(NET::KeepBelow)) { ni.setState(NET::States(), NET::KeepBelow); } else { ni.setState(NET::KeepBelow, NET::KeepBelow); } } void XWindowTasksModel::requestToggleFullScreen(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::WMState, NET::Properties2()); if (info->hasState(NET::FullScreen)) { ni.setState(NET::States(), NET::FullScreen); } else { ni.setState(NET::FullScreen, NET::FullScreen); } } void XWindowTasksModel::requestToggleShaded(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::WMState, NET::Properties2()); if (info->hasState(NET::Shaded)) { ni.setState(NET::States(), NET::Shaded); } else { ni.setState(NET::Shaded, NET::Shaded); } } void XWindowTasksModel::requestVirtualDesktops(const QModelIndex &index, const QVariantList &desktops) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } int desktop = 0; if (!desktops.isEmpty()) { bool ok = false; desktop = desktops.first().toUInt(&ok); if (!ok) { return; } } if (desktop > KWindowSystem::numberOfDesktops()) { return; } const WId window = d->windows.at(index.row()); const KWindowInfo *info = d->windowInfo(window); if (desktop == 0) { if (info->onAllDesktops()) { KWindowSystem::setOnDesktop(window, KWindowSystem::currentDesktop()); KWindowSystem::forceActiveWindow(window); } else { KWindowSystem::setOnAllDesktops(window, true); } return; } KWindowSystem::setOnDesktop(window, desktop); if (desktop == KWindowSystem::currentDesktop()) { KWindowSystem::forceActiveWindow(window); } } void XWindowTasksModel::requestNewVirtualDesktop(const QModelIndex &index) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); const int desktop = KWindowSystem::numberOfDesktops() + 1; // FIXME Arbitrary limit of 20 copied from old code. if (desktop > 20) { return; } NETRootInfo ri(QX11Info::connection(), NET::NumberOfDesktops); ri.setNumberOfDesktops(desktop); KWindowSystem::setOnDesktop(window, desktop); } void XWindowTasksModel::requestActivities(const QModelIndex &index, const QStringList &activities) { if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); KWindowSystem::setOnActivities(window, activities); } void XWindowTasksModel::requestPublishDelegateGeometry(const QModelIndex &index, const QRect &geometry, QObject *delegate) { Q_UNUSED(delegate) if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) { return; } const WId window = d->windows.at(index.row()); if (d->delegateGeometries.contains(window) && d->delegateGeometries.value(window) == geometry) { return; } NETWinInfo ni(QX11Info::connection(), window, QX11Info::appRootWindow(), NET::Properties(), NET::Properties2()); NETRect rect; if (geometry.isValid()) { rect.pos.x = geometry.x(); rect.pos.y = geometry.y(); rect.size.width = geometry.width(); rect.size.height = geometry.height(); d->delegateGeometries.insert(window, geometry); } else { d->delegateGeometries.remove(window); } ni.setIconGeometry(rect); } WId XWindowTasksModel::winIdFromMimeData(const QMimeData *mimeData, bool *ok) { Q_ASSERT(mimeData); if (ok) { *ok = false; } if (!mimeData->hasFormat(Private::mimeType())) { return 0; } QByteArray data(mimeData->data(Private::mimeType())); if (data.size() != sizeof(WId)) { return 0; } WId id; memcpy(&id, data.data(), sizeof(WId)); if (ok) { *ok = true; } return id; } QList XWindowTasksModel::winIdsFromMimeData(const QMimeData *mimeData, bool *ok) { Q_ASSERT(mimeData); QList ids; if (ok) { *ok = false; } if (!mimeData->hasFormat(Private::groupMimeType())) { // Try to extract single window id. bool singularOk; WId id = winIdFromMimeData(mimeData, &singularOk); if (ok) { *ok = singularOk; } if (singularOk) { ids << id; } return ids; } QByteArray data(mimeData->data(Private::groupMimeType())); if ((unsigned int)data.size() < sizeof(int) + sizeof(WId)) { return ids; } int count = 0; memcpy(&count, data.data(), sizeof(int)); if (count < 1 || (unsigned int)data.size() < sizeof(int) + sizeof(WId) * count) { return ids; } WId id; for (int i = 0; i < count; ++i) { memcpy(&id, data.data() + sizeof(int) + sizeof(WId) * i, sizeof(WId)); ids << id; } if (ok) { *ok = true; } return ids; } }