File indexing completed on 2024-11-17 04:18:46

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "connector_p.h"
0007 #include "connector1adaptor.h"
0008 #include "distributor1iface.h"
0009 #include "logging.h"
0010 
0011 #include "../shared/unifiedpush-constants.h"
0012 #include "../shared/connectorutils_p.h"
0013 
0014 #include <QDBusConnection>
0015 #include <QDBusPendingCallWatcher>
0016 
0017 using namespace KUnifiedPush;
0018 
0019 void ConnectorPrivate::init()
0020 {
0021     new Connector1Adaptor(this);
0022     const auto res = QDBusConnection::sessionBus().registerObject(QLatin1String(UP_CONNECTOR_PATH), this);
0023     if (!res) {
0024         qCWarning(Log) << "Failed to register D-Bus object!" << UP_CONNECTOR_PATH;
0025         // TODO switch to error state?
0026     }
0027 
0028     connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this](const QString &serviceName) {
0029         qCDebug(Log) << "Distributor" << serviceName << "became available";
0030         if (!hasDistributor()) {
0031             setDistributor(ConnectorUtils::selectDistributor());
0032             processNextCommand();
0033         }
0034     });
0035     connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](const QString &serviceName) {
0036         qCDebug(Log) << "Distributor" << serviceName << "is gone";
0037         if (m_distributor->service() == serviceName) {
0038             delete m_distributor;
0039             m_distributor = nullptr;
0040             m_currentCommand = Command::None;
0041             setState(Connector::NoDistributor);
0042         }
0043     });
0044 
0045     m_serviceWatcher.setConnection(QDBusConnection::sessionBus());
0046     m_serviceWatcher.setWatchMode(QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration);
0047     m_serviceWatcher.addWatchedService(QLatin1String(UP_DISTRIBUTOR_SERVICE_NAME_FILTER));
0048 }
0049 
0050 void ConnectorPrivate::deinit()
0051 {
0052     QDBusConnection::sessionBus().unregisterObject(QLatin1String(UP_CONNECTOR_PATH));
0053 }
0054 
0055 void ConnectorPrivate::doSetDistributor(const QString &distServiceName)
0056 {
0057     m_distributor = new OrgUnifiedpushDistributor1Interface(distServiceName, QLatin1String(UP_DISTRIBUTOR_PATH), QDBusConnection::sessionBus(), this);
0058     if (!m_distributor->isValid()) {
0059         qCWarning(Log) << "Invalid distributor D-Bus interface?" << distServiceName;
0060     }
0061 }
0062 
0063 bool ConnectorPrivate::hasDistributor() const
0064 {
0065     return m_distributor && m_distributor->isValid();
0066 }
0067 
0068 void ConnectorPrivate::doRegister()
0069 {
0070     const auto reply = m_distributor->Register(m_serviceName, m_token, m_description);
0071     auto watcher = new QDBusPendingCallWatcher(reply, this);
0072     connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher]() {
0073         if (watcher->isError()) {
0074             qCWarning(Log) << watcher->error();
0075             setState(Connector::Error);
0076         } else {
0077             const auto result = watcher->reply().arguments().at(0).toString();
0078             const auto errorMsg = watcher->reply().arguments().at(1).toString();
0079             qCDebug(Log) << result << errorMsg;
0080             if (result == QLatin1String(UP_REGISTER_RESULT_SUCCESS)) {
0081                 setState(m_endpoint.isEmpty() ? Connector::Registering : Connector::Registered);
0082             } else {
0083                 setState(Connector::Error);
0084             }
0085         }
0086         m_currentCommand = Command::None;
0087         processNextCommand();
0088     });
0089 }
0090 
0091 void ConnectorPrivate::doUnregister()
0092 {
0093     m_distributor->Unregister(m_token);
0094 }