File indexing completed on 2024-05-12 05:36:09

0001 // SPDX-FileCopyrightText: 2023 Devin Lin <devin@kde.org>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "shelldbusclient.h"
0005 
0006 #include <QDBusServiceWatcher>
0007 
0008 ShellDBusClient::ShellDBusClient(QObject *parent)
0009     : QObject{parent}
0010     , m_interface{new OrgKdePlasmashellInterface{QStringLiteral("org.kde.plasmashell"), QStringLiteral("/Mobile"), QDBusConnection::sessionBus(), this}}
0011     , m_watcher{new QDBusServiceWatcher(QStringLiteral("org.kde.plasmashell"), QDBusConnection::sessionBus(), QDBusServiceWatcher::WatchForOwnerChange, this)}
0012     , m_connected{false}
0013 {
0014     if (m_interface->isValid()) {
0015         connectSignals();
0016     }
0017 
0018     connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, [this]() -> void {
0019         m_connected = true;
0020         if (m_interface->isValid()) {
0021             connectSignals();
0022         }
0023     });
0024 
0025     connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, [this]() -> void {
0026         m_connected = false;
0027     });
0028 }
0029 
0030 void ShellDBusClient::connectSignals()
0031 {
0032     connect(m_interface, &OrgKdePlasmashellInterface::isActionDrawerOpenChanged, this, &ShellDBusClient::updateIsActionDrawerOpen);
0033     connect(m_interface, &OrgKdePlasmashellInterface::doNotDisturbChanged, this, &ShellDBusClient::updateDoNotDisturb);
0034     connect(m_interface, &OrgKdePlasmashellInterface::isTaskSwitcherVisibleChanged, this, &ShellDBusClient::updateIsTaskSwitcherVisible);
0035     connect(m_interface, &OrgKdePlasmashellInterface::openActionDrawerRequested, this, &ShellDBusClient::openActionDrawerRequested);
0036     connect(m_interface, &OrgKdePlasmashellInterface::closeActionDrawerRequested, this, &ShellDBusClient::closeActionDrawerRequested);
0037     connect(m_interface, &OrgKdePlasmashellInterface::openAppLaunchAnimationRequested, this, &ShellDBusClient::openAppLaunchAnimationRequested);
0038     connect(m_interface, &OrgKdePlasmashellInterface::closeAppLaunchAnimationRequested, this, &ShellDBusClient::closeAppLaunchAnimationRequested);
0039     connect(m_interface, &OrgKdePlasmashellInterface::openHomeScreenRequested, this, &ShellDBusClient::openHomeScreenRequested);
0040     connect(m_interface, &OrgKdePlasmashellInterface::resetHomeScreenPositionRequested, this, &ShellDBusClient::resetHomeScreenPositionRequested);
0041     connect(m_interface, &OrgKdePlasmashellInterface::showVolumeOSDRequested, this, &ShellDBusClient::showVolumeOSDRequested);
0042 
0043     updateIsActionDrawerOpen();
0044     updateDoNotDisturb();
0045     updateIsTaskSwitcherVisible();
0046 }
0047 
0048 bool ShellDBusClient::doNotDisturb() const
0049 {
0050     return m_doNotDisturb;
0051 }
0052 
0053 void ShellDBusClient::setDoNotDisturb(bool value)
0054 {
0055     m_interface->setDoNotDisturb(value);
0056 }
0057 
0058 bool ShellDBusClient::isActionDrawerOpen() const
0059 {
0060     return m_isActionDrawerOpen;
0061 }
0062 
0063 void ShellDBusClient::setIsActionDrawerOpen(bool value)
0064 {
0065     m_interface->setIsActionDrawerOpen(value);
0066 }
0067 
0068 void ShellDBusClient::openActionDrawer()
0069 {
0070     m_interface->openActionDrawer();
0071 }
0072 
0073 void ShellDBusClient::closeActionDrawer()
0074 {
0075     m_interface->closeActionDrawer();
0076 }
0077 
0078 bool ShellDBusClient::isTaskSwitcherVisible() const
0079 {
0080     return m_isTaskSwitcherVisible;
0081 }
0082 
0083 void ShellDBusClient::openAppLaunchAnimation(QString splashIcon, QString title, qreal x, qreal y, qreal sourceIconSize)
0084 {
0085     m_interface->openAppLaunchAnimation(splashIcon, title, x, y, sourceIconSize);
0086 }
0087 
0088 void ShellDBusClient::closeAppLaunchAnimation()
0089 {
0090     m_interface->closeAppLaunchAnimation();
0091 }
0092 
0093 void ShellDBusClient::openHomeScreen()
0094 {
0095     m_interface->openHomeScreen();
0096 }
0097 
0098 void ShellDBusClient::resetHomeScreenPosition()
0099 {
0100     m_interface->resetHomeScreenPosition();
0101 }
0102 
0103 void ShellDBusClient::showVolumeOSD()
0104 {
0105     m_interface->showVolumeOSD();
0106 }
0107 
0108 void ShellDBusClient::updateDoNotDisturb()
0109 {
0110     auto reply = m_interface->doNotDisturb();
0111     auto watcher = new QDBusPendingCallWatcher(reply, this);
0112 
0113     QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](auto watcher) {
0114         QDBusPendingReply<bool> reply = *watcher;
0115         m_doNotDisturb = reply.argumentAt<0>();
0116         Q_EMIT doNotDisturbChanged();
0117     });
0118 }
0119 
0120 void ShellDBusClient::updateIsActionDrawerOpen()
0121 {
0122     auto reply = m_interface->isActionDrawerOpen();
0123     auto watcher = new QDBusPendingCallWatcher(reply, this);
0124 
0125     QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](auto watcher) {
0126         QDBusPendingReply<bool> reply = *watcher;
0127         m_isActionDrawerOpen = reply.argumentAt<0>();
0128         Q_EMIT isActionDrawerOpenChanged();
0129     });
0130 }
0131 
0132 void ShellDBusClient::updateIsTaskSwitcherVisible()
0133 {
0134     auto reply = m_interface->isTaskSwitcherVisible();
0135     auto watcher = new QDBusPendingCallWatcher(reply, this);
0136 
0137     QObject::connect(watcher, &QDBusPendingCallWatcher::finished, this, [this](auto watcher) {
0138         QDBusPendingReply<bool> reply = *watcher;
0139         m_isTaskSwitcherVisible = reply.argumentAt<0>();
0140         Q_EMIT isTaskSwitcherVisibleChanged();
0141     });
0142 }