KUnifiedPush

connector_dbus.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "connector_p.h"
7#include "connector1adaptor.h"
8#include "distributor1iface.h"
9#include "logging.h"
10
11#include "../shared/unifiedpush-constants.h"
12#include "../shared/connectorutils_p.h"
13
14#include <QDBusConnection>
15#include <QDBusPendingCallWatcher>
16
17using namespace KUnifiedPush;
18
19void ConnectorPrivate::init()
20{
21 new Connector1Adaptor(this);
22 const auto res = QDBusConnection::sessionBus().registerObject(QLatin1String(UP_CONNECTOR_PATH), this);
23 if (!res) {
24 qCWarning(Log) << "Failed to register D-Bus object!" << UP_CONNECTOR_PATH;
25 // TODO switch to error state?
26 }
27
28 connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, [this](const QString &serviceName) {
29 qCDebug(Log) << "Distributor" << serviceName << "became available";
30 if (!hasDistributor()) {
31 setDistributor(ConnectorUtils::selectDistributor());
32 processNextCommand();
33 }
34 });
35 connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, [this](const QString &serviceName) {
36 qCDebug(Log) << "Distributor" << serviceName << "is gone";
37 if (m_distributor->service() == serviceName) {
38 delete m_distributor;
39 m_distributor = nullptr;
40 m_currentCommand = Command::None;
42 }
43 });
44
45 m_serviceWatcher.setConnection(QDBusConnection::sessionBus());
47 m_serviceWatcher.addWatchedService(QLatin1String(UP_DISTRIBUTOR_SERVICE_NAME_FILTER));
48}
49
50void ConnectorPrivate::deinit()
51{
53}
54
55void ConnectorPrivate::doSetDistributor(const QString &distServiceName)
56{
57 m_distributor = new OrgUnifiedpushDistributor1Interface(distServiceName, QLatin1String(UP_DISTRIBUTOR_PATH), QDBusConnection::sessionBus(), this);
58 if (!m_distributor->isValid()) {
59 qCWarning(Log) << "Invalid distributor D-Bus interface?" << distServiceName;
60 }
61}
62
63bool ConnectorPrivate::hasDistributor() const
64{
65 return m_distributor && m_distributor->isValid();
66}
67
68void ConnectorPrivate::doRegister()
69{
70 const auto reply = m_distributor->Register(m_serviceName, m_token, m_description);
71 auto watcher = new QDBusPendingCallWatcher(reply, this);
72 connect(watcher, &QDBusPendingCallWatcher::finished, this, [this, watcher]() {
73 if (watcher->isError()) {
74 qCWarning(Log) << watcher->error();
75 setState(Connector::Error);
76 } else {
77 const auto result = watcher->reply().arguments().at(0).toString();
78 const auto errorMsg = watcher->reply().arguments().at(1).toString();
79 qCDebug(Log) << result << errorMsg;
80 if (result == QLatin1String(UP_REGISTER_RESULT_SUCCESS)) {
81 setState(m_endpoint.isEmpty() ? Connector::Registering : Connector::Registered);
82 } else {
83 setState(Connector::Error);
84 }
85 }
86 m_currentCommand = Command::None;
87 processNextCommand();
88 });
89}
90
91void ConnectorPrivate::doUnregister()
92{
93 m_distributor->Unregister(m_token);
94}
@ Error
Any other error condition.
Definition connector.h:63
@ NoDistributor
Connector cannot find a UnifiedPush distributor to register at.
Definition connector.h:62
@ Registering
Connector is registering with the push provider.
Definition connector.h:60
@ Registered
Connector is registered and thus operational.
Definition connector.h:61
Client-side integration with UnifiedPush.
Definition connector.h:16
bool registerObject(const QString &path, QObject *object, RegisterOptions options)
QDBusConnection sessionBus()
void unregisterObject(const QString &path, UnregisterMode mode)
void finished(QDBusPendingCallWatcher *self)
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:46:50 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.