File indexing completed on 2024-06-02 05:42:15

0001 
0002 // SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0003 // SPDX-License-Identifier: GPL-2.0-or-later
0004 
0005 #include "folioapplication.h"
0006 #include "windowlistener.h"
0007 
0008 #include <QQuickWindow>
0009 
0010 #include <KNotificationJobUiDelegate>
0011 
0012 FolioApplication::FolioApplication(QObject *parent, KService::Ptr service)
0013     : QObject{parent}
0014     , m_running{false}
0015     , m_name{service->name()}
0016     , m_icon{service->icon()}
0017     , m_storageId{service->storageId()}
0018 {
0019     auto windows = WindowListener::instance()->windowsFromStorageId(m_storageId);
0020     if (windows.empty()) {
0021         m_window = nullptr;
0022     } else {
0023         m_window = windows[0];
0024     }
0025 
0026     connect(WindowListener::instance(), &WindowListener::windowChanged, this, [this](QString storageId) {
0027         if (storageId == m_storageId) {
0028             auto windows = WindowListener::instance()->windowsFromStorageId(m_storageId);
0029             if (windows.empty()) {
0030                 setWindow(nullptr);
0031             } else {
0032                 setWindow(windows[0]);
0033             }
0034         }
0035     });
0036 }
0037 
0038 FolioApplication *FolioApplication::fromJson(QJsonObject &obj, QObject *parent)
0039 {
0040     QString storageId = obj[QStringLiteral("storageId")].toString();
0041     if (KService::Ptr service = KService::serviceByStorageId(storageId)) {
0042         return new FolioApplication(parent, service);
0043     }
0044     return nullptr;
0045 }
0046 
0047 QJsonObject FolioApplication::toJson() const
0048 {
0049     QJsonObject obj;
0050     obj[QStringLiteral("type")] = "application";
0051     obj[QStringLiteral("storageId")] = m_storageId;
0052     return obj;
0053 }
0054 
0055 bool FolioApplication::running() const
0056 {
0057     return m_window != nullptr;
0058 }
0059 
0060 QString FolioApplication::name() const
0061 {
0062     return m_name;
0063 }
0064 
0065 QString FolioApplication::icon() const
0066 {
0067     return m_icon;
0068 }
0069 
0070 QString FolioApplication::storageId() const
0071 {
0072     return m_storageId;
0073 }
0074 
0075 KWayland::Client::PlasmaWindow *FolioApplication::window() const
0076 {
0077     return m_window;
0078 }
0079 
0080 void FolioApplication::setName(QString &name)
0081 {
0082     m_name = name;
0083     Q_EMIT nameChanged();
0084 }
0085 
0086 void FolioApplication::setIcon(QString &icon)
0087 {
0088     m_icon = icon;
0089     Q_EMIT iconChanged();
0090 }
0091 
0092 void FolioApplication::setStorageId(QString &storageId)
0093 {
0094     m_storageId = storageId;
0095     Q_EMIT storageIdChanged();
0096 }
0097 
0098 void FolioApplication::setWindow(KWayland::Client::PlasmaWindow *window)
0099 {
0100     m_window = window;
0101     Q_EMIT windowChanged();
0102 }
0103 
0104 void FolioApplication::setMinimizedDelegate(QQuickItem *delegate)
0105 {
0106     QWindow *delegateWindow = delegate->window();
0107     if (!delegateWindow) {
0108         return;
0109     }
0110     if (!m_window) {
0111         return;
0112     }
0113 
0114     KWayland::Client::Surface *surface = KWayland::Client::Surface::fromWindow(delegateWindow);
0115     if (!surface) {
0116         return;
0117     }
0118 
0119     QRect rect = delegate->mapRectToScene(QRectF(0, 0, delegate->width(), delegate->height())).toRect();
0120     m_window->setMinimizedGeometry(surface, rect);
0121 }
0122 
0123 void FolioApplication::unsetMinimizedDelegate(QQuickItem *delegate)
0124 {
0125     QWindow *delegateWindow = delegate->window();
0126     if (!delegateWindow) {
0127         return;
0128     }
0129     if (!m_window) {
0130         return;
0131     }
0132 
0133     KWayland::Client::Surface *surface = KWayland::Client::Surface::fromWindow(delegateWindow);
0134     if (!surface) {
0135         return;
0136     }
0137 
0138     m_window->unsetMinimizedGeometry(surface);
0139 }