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