waylandtasksmodel.cpp 24.5 KB
Newer Older
Eike Hein's avatar
Eike Hein committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/********************************************************************
Copyright 2016  Eike Hein <hein@kde.org>

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 <http://www.gnu.org/licenses/>.
*********************************************************************/

#include "waylandtasksmodel.h"
22
#include "tasktools.h"
23
#include "virtualdesktopinfo.h"
Eike Hein's avatar
Eike Hein committed
24

25
#include <KDirWatch>
Eike Hein's avatar
Eike Hein committed
26
#include <KService>
27
#include <KSharedConfig>
Eike Hein's avatar
Eike Hein committed
28
29
30
31
#include <KWayland/Client/connection_thread.h>
#include <KWayland/Client/plasmawindowmanagement.h>
#include <KWayland/Client/registry.h>
#include <KWayland/Client/surface.h>
32
#include <KWindowSystem>
Eike Hein's avatar
Eike Hein committed
33
34

#include <QGuiApplication>
35
#include <QMimeData>
Eike Hein's avatar
Eike Hein committed
36
37
38
39
#include <QQuickItem>
#include <QQuickWindow>
#include <QSet>
#include <QUrl>
40
#include <QUuid>
Eike Hein's avatar
Eike Hein committed
41
42
43
44
45
#include <QWindow>

namespace TaskManager
{

46
class Q_DECL_HIDDEN WaylandTasksModel::Private
Eike Hein's avatar
Eike Hein committed
47
48
49
50
{
public:
    Private(WaylandTasksModel *q);
    QList<KWayland::Client::PlasmaWindow*> windows;
51
    QHash<KWayland::Client::PlasmaWindow*, AppData> appDataCache;
Eike Hein's avatar
Eike Hein committed
52
    KWayland::Client::PlasmaWindowManagement *windowManagement = nullptr;
53
54
    KSharedConfig::Ptr rulesConfig;
    KDirWatch *configWatcher = nullptr;
55
56
    VirtualDesktopInfo *virtualDesktopInfo = nullptr;
    static QUuid uuid;
Eike Hein's avatar
Eike Hein committed
57

58
    void init();
Eike Hein's avatar
Eike Hein committed
59
60
    void initWayland();
    void addWindow(KWayland::Client::PlasmaWindow *window);
61
62
63

    AppData appData(KWayland::Client::PlasmaWindow *window);

64
65
    QIcon icon(KWayland::Client::PlasmaWindow *window);

66
67
68
    static QString mimeType();
    static QString groupMimeType();

Eike Hein's avatar
Eike Hein committed
69
    void dataChanged(KWayland::Client::PlasmaWindow *window, int role);
70
    void dataChanged(KWayland::Client::PlasmaWindow *window, const QVector<int> &roles);
Eike Hein's avatar
Eike Hein committed
71
72
73
74
75

private:
    WaylandTasksModel *q;
};

76
77
QUuid WaylandTasksModel::Private::uuid = QUuid::createUuid();

Eike Hein's avatar
Eike Hein committed
78
79
80
81
82
WaylandTasksModel::Private::Private(WaylandTasksModel *q)
    : q(q)
{
}

83
84
85
86
87
88
89
90
91
92
93
94
95
96
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<int>{Qt::DecorationRole, AbstractTasksModel::AppId,
            AbstractTasksModel::AppName, AbstractTasksModel::GenericName,
            AbstractTasksModel::LauncherUrl,
97
98
            AbstractTasksModel::LauncherUrlWithoutIcon,
            AbstractTasksModel::SkipTaskbar});
99
100
101
102
103
104
105
106
107
    };

    rulesConfig = KSharedConfig::openConfig(QStringLiteral("taskmanagerrulesrc"));
    configWatcher = new KDirWatch(q);

    foreach (const QString &location, QStandardPaths::standardLocations(QStandardPaths::ConfigLocation)) {
        configWatcher->addFile(location + QLatin1String("/taskmanagerrulesrc"));
    }

Eike Hein's avatar
Eike Hein committed
108
    auto rulesConfigChange = [this, clearCacheAndRefresh] {
109
110
111
112
113
114
115
116
        rulesConfig->reparseConfiguration();
        clearCacheAndRefresh();
    };

    QObject::connect(configWatcher, &KDirWatch::dirty, rulesConfigChange);
    QObject::connect(configWatcher, &KDirWatch::created, rulesConfigChange);
    QObject::connect(configWatcher, &KDirWatch::deleted, rulesConfigChange);

117
118
    virtualDesktopInfo = new VirtualDesktopInfo(q);

119
120
121
    initWayland();
}

Eike Hein's avatar
Eike Hein committed
122
123
void WaylandTasksModel::Private::initWayland()
{
124
    if (!KWindowSystem::isPlatformWayland()) {
Eike Hein's avatar
Eike Hein committed
125
126
127
128
129
130
131
132
133
134
135
136
        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);

137
    QObject::connect(registry, &KWayland::Client::Registry::plasmaWindowManagementAnnounced, q, [this, registry] (quint32 name, quint32 version) {
Eike Hein's avatar
Eike Hein committed
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
            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);
                }
            );

154
155
            const auto windows = windowManagement->windows();
            for (auto it = windows.constBegin(); it != windows.constEnd(); ++it) {
Eike Hein's avatar
Eike Hein committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
                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);
183
            appDataCache.remove(window);
Eike Hein's avatar
Eike Hein committed
184
185
186
187
188
189
190
191
            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,
192
        [window, this] { this->dataChanged(window, Qt::DisplayRole); }
Eike Hein's avatar
Eike Hein committed
193
194
195
    );

    QObject::connect(window, &KWayland::Client::PlasmaWindow::iconChanged, q,
196
197
198
199
200
201
202
203
204
205
        [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);
        }
Eike Hein's avatar
Eike Hein committed
206
207
208
209
    );

    QObject::connect(window, &KWayland::Client::PlasmaWindow::appIdChanged, q,
        [window, this] {
210
211
212
            // 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.
213
            appDataCache.remove(window);
Eike Hein's avatar
Eike Hein committed
214

215
216
            // Refresh roles satisfied from the app data cache.
            this->dataChanged(window, QVector<int>{AppId, AppName, GenericName,
217
                LauncherUrl, LauncherUrlWithoutIcon, SkipTaskbar});
Eike Hein's avatar
Eike Hein committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
        }
    );

    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); }
    );

273
274
275
276
277
278
279
280
281
// 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);
Eike Hein's avatar
Eike Hein committed
282

283
284
285
286
287
288
            // 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);
            }
        }
Eike Hein's avatar
Eike Hein committed
289
290
    );

291
292
293
294
295
296
297
298
299
    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);
            }
        }
Eike Hein's avatar
Eike Hein committed
300
301
    );

302
303
304
305
    QObject::connect(window, &KWayland::Client::PlasmaWindow::geometryChanged, q,
        [window, this] { this->dataChanged(window, QVector<int>{Geometry, ScreenGeometry}); }
    );

Eike Hein's avatar
Eike Hein committed
306
307
308
309
310
311
312
    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); }
    );
313
314
315
316
317
318

    QObject::connect(window, &KWayland::Client::PlasmaWindow::applicationMenuChanged, q,
        [window, this] {
            this->dataChanged(window, QVector<int>{ApplicationMenuServiceName, ApplicationMenuObjectPath});
        }
    );
Eike Hein's avatar
Eike Hein committed
319
320
}

321
322
323
324
325
326
327
328
AppData WaylandTasksModel::Private::appData(KWayland::Client::PlasmaWindow *window)
{
    const auto &it = appDataCache.constFind(window);

    if (it != appDataCache.constEnd()) {
        return *it;
    }

329
330
    const AppData &data = appDataFromUrl(windowUrlFromMetadata(window->appId(),
        window->pid(), rulesConfig));
331
332
333
334
335
336

    appDataCache.insert(window, data);

    return data;
}

337
338
339
340
341
342
343
344
345
346
347
348
349
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();
}

350
351
352
353
QString WaylandTasksModel::Private::mimeType()
{
    // Use a unique format id to make this intentionally useless for
    // cross-process DND.
354
    return QStringLiteral("windowsystem/winid+") + uuid.toString();
355
356
357
358
359
360
}

QString WaylandTasksModel::Private::groupMimeType()
{
    // Use a unique format id to make this intentionally useless for
    // cross-process DND.
361
    return QStringLiteral("windowsystem/multiple-winids+") + uuid.toString();
362
363
}

Eike Hein's avatar
Eike Hein committed
364
365
366
367
368
369
void WaylandTasksModel::Private::dataChanged(KWayland::Client::PlasmaWindow *window, int role)
{
    QModelIndex idx = q->index(windows.indexOf(window));
    emit q->dataChanged(idx, idx, QVector<int>{role});
}

370
371
372
373
374
375
void WaylandTasksModel::Private::dataChanged(KWayland::Client::PlasmaWindow *window, const QVector<int> &roles)
{
    QModelIndex idx = q->index(windows.indexOf(window));
    emit q->dataChanged(idx, idx, roles);
}

Eike Hein's avatar
Eike Hein committed
376
WaylandTasksModel::WaylandTasksModel(QObject *parent)
377
    : AbstractWindowTasksModel(parent)
Eike Hein's avatar
Eike Hein committed
378
379
    , d(new Private(this))
{
380
    d->init();
Eike Hein's avatar
Eike Hein committed
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
}

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) {
396
        return d->icon(window);
Eike Hein's avatar
Eike Hein committed
397
    } else if (role == AppId) {
398
399
400
401
402
403
404
        const QString &id = d->appData(window).id;

        if (id.isEmpty()) {
            return window->appId();
        } else {
            return id;
        }
Eike Hein's avatar
Eike Hein committed
405
    } else if (role == AppName) {
406
        return d->appData(window).name;
Eike Hein's avatar
Eike Hein committed
407
    } else if (role == GenericName) {
408
        return d->appData(window).genericName;
409
    } else if (role == LauncherUrl || role == LauncherUrlWithoutIcon) {
410
        return d->appData(window).url;
411
    } else if (role == WinIdList) {
412
        return QVariantList() << window->uuid();
413
414
415
    } else if (role == MimeType) {
        return d->mimeType();
    } else if (role == MimeData) {
416
        return window->uuid();
Eike Hein's avatar
Eike Hein committed
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
    } 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();
447
448
449
450
451
    } else if (role == IsVirtualDesktopsChangeable) {
        // FIXME Currently not implemented in KWayland.
        return true;
    } else if (role == VirtualDesktops) {
        return window->plasmaVirtualDesktops();
Eike Hein's avatar
Eike Hein committed
452
    } else if (role == IsOnAllVirtualDesktops) {
453
        return window->plasmaVirtualDesktops().isEmpty();
454
455
456
457
    } else if (role == Geometry) {
        return window->geometry();
    } else if (role == ScreenGeometry) {
        return screenGeometry(window->geometry().center());
Eike Hein's avatar
Eike Hein committed
458
459
460
461
462
    } else if (role == Activities) {
        // FIXME Implement.
    } else if (role == IsDemandingAttention) {
        return window->isDemandingAttention();
    } else if (role == SkipTaskbar) {
463
        return window->skipTaskbar() || d->appData(window).skipTaskbar;
464
465
    } else if (role == SkipPager) {
        // FIXME Implement.
466
    } else if (role == AppPid) {
467
        return window->pid();
468
    } else if (role == StackingOrder) {
469
        return d->windowManagement->stackingOrderUuids().indexOf(window->uuid());
470
471
472
473
    } else if (role == ApplicationMenuObjectPath) {
        return window->applicationMenuObjectPath();
    } else if (role == ApplicationMenuServiceName) {
        return window->applicationMenuServiceName();
Eike Hein's avatar
Eike Hein committed
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
    }

    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;
    }

506
    runApp(d->appData(d->windows.at(index.row())));
507
508
509
510
511
512
513
514
515
516
}

void WaylandTasksModel::requestOpenUrls(const QModelIndex &index, const QList<QUrl> &urls)
{
    if (!index.isValid() || index.model() != this || index.row() < 0
        || index.row() >= d->windows.count()
        || urls.isEmpty()) {
        return;
    }

517
    runApp(d->appData(d->windows.at(index.row())), urls);
Eike Hein's avatar
Eike Hein committed
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
}

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;
    }

535
536
537
538
539
540
541
542
543
    KWayland::Client::PlasmaWindow *window = d->windows.at(index.row());

    const QString &currentDesktop = d->virtualDesktopInfo->currentDesktop().toString();

    if (!currentDesktop.isEmpty()) {
        window->requestEnterVirtualDesktop(currentDesktop);
    }

    window->requestMove();
Eike Hein's avatar
Eike Hein committed
544
545
546
547
548
549
550
551
}

void WaylandTasksModel::requestResize(const QModelIndex &index)
{
    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }

552
553
554
555
556
557
558
559
560
    KWayland::Client::PlasmaWindow *window = d->windows.at(index.row());

    const QString &currentDesktop = d->virtualDesktopInfo->currentDesktop().toString();

    if (!currentDesktop.isEmpty()) {
        window->requestEnterVirtualDesktop(currentDesktop);
    }

    window->requestResize();
Eike Hein's avatar
Eike Hein committed
561
562
563
564
565
566
567
568
}

void WaylandTasksModel::requestToggleMinimized(const QModelIndex &index)
{
    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }

569
570
571
572
573
574
575
576
577
    KWayland::Client::PlasmaWindow *window = d->windows.at(index.row());

    const QString &currentDesktop = d->virtualDesktopInfo->currentDesktop().toString();

    if (!currentDesktop.isEmpty()) {
        window->requestEnterVirtualDesktop(currentDesktop);
    }

    window->requestToggleMinimized();
Eike Hein's avatar
Eike Hein committed
578
579
580
581
582
583
584
585
}

void WaylandTasksModel::requestToggleMaximized(const QModelIndex &index)
{
    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }

586
587
588
589
590
591
592
593
594
    KWayland::Client::PlasmaWindow *window = d->windows.at(index.row());

    const QString &currentDesktop = d->virtualDesktopInfo->currentDesktop().toString();

    if (!currentDesktop.isEmpty()) {
        window->requestEnterVirtualDesktop(currentDesktop);
    }

    window->requestToggleMaximized();
Eike Hein's avatar
Eike Hein committed
595
596
597
598
}

void WaylandTasksModel::requestToggleKeepAbove(const QModelIndex &index)
{
599
600
601
    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }
Eike Hein's avatar
Eike Hein committed
602

603
    d->windows.at(index.row())->requestToggleKeepAbove();
Eike Hein's avatar
Eike Hein committed
604
605
606
607
}

void WaylandTasksModel::requestToggleKeepBelow(const QModelIndex &index)
{
608
609
610
    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }
Eike Hein's avatar
Eike Hein committed
611

612
    d->windows.at(index.row())->requestToggleKeepBelow();
Eike Hein's avatar
Eike Hein committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
}

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();
}

631
void WaylandTasksModel::requestVirtualDesktops(const QModelIndex &index, const QVariantList &desktops)
Eike Hein's avatar
Eike Hein committed
632
{
633
634
635
    // 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.
Eike Hein's avatar
Eike Hein committed
636
637
638
639
640

    if (!index.isValid() || index.model() != this || index.row() < 0 || index.row() >= d->windows.count()) {
        return;
    }

641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
    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();
Eike Hein's avatar
Eike Hein committed
678
679
}

680
681
682
683
684
685
void WaylandTasksModel::requestActivities(const QModelIndex &index, const QStringList &activities)
{
    Q_UNUSED(index)
    Q_UNUSED(activities)
}

Eike Hein's avatar
Eike Hein committed
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
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<const QQuickItem *>(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);
}

729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
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<quint32> WaylandTasksModel::winIdsFromMimeData(const QMimeData *mimeData, bool *ok)
{
    Q_ASSERT(mimeData);
    QList<quint32> 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;
}

Eike Hein's avatar
Eike Hein committed
778
}