KUnifiedPush

kcmpushnotifications.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "kcmpushnotifications.h"
7#include "managementinterface.h"
8#include "nextcloudauthenticator.h"
9#include "selftest.h"
10
11#include "../shared/clientinfo_p.h"
12#include "../shared/connectorutils_p.h"
13#include "../shared/distributorstatus_p.h"
14#include "../shared/unifiedpush-constants.h"
15
16#include <KPluginFactory>
17
18#include <QHostInfo>
19#include <QStandardPaths>
20
21K_PLUGIN_CLASS_WITH_JSON(KCMPushNotifications, "kcm_push_notifications.json")
22
23using namespace KUnifiedPush;
24
25KCMPushNotifications::KCMPushNotifications(QObject *parent, const KPluginMetaData &data, [[maybe_unused]] const QVariantList &args)
26 : KQuickConfigModule(parent, data)
27{
28 qDBusRegisterMetaType<KUnifiedPush::ClientInfo>();
29 qDBusRegisterMetaType<QList<KUnifiedPush::ClientInfo>>();
30
31 m_nam.setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
32 m_nam.setStrictTransportSecurityEnabled(true);
33 m_nam.enableStrictTransportSecurityStore(true, QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/org.kde.kunifiedpush/hsts/"));
34
35 // TODO do this only when we are using the KDE distributor
36 m_mgmtIface = new OrgKdeKunifiedpushManagementInterface(KDE_DISTRIBUTOR_SERVICE_NAME, KDE_DISTRIBUTOR_MANAGEMENT_PATH, QDBusConnection::sessionBus(), this);
37 connect(m_mgmtIface, &OrgKdeKunifiedpushManagementInterface::statusChanged, this, &KCMPushNotifications::distributorStatusChanged);
38 connect(m_mgmtIface, &OrgKdeKunifiedpushManagementInterface::errorMessageChanged, this, &KCMPushNotifications::distributorErrorMessageChanged);
39 connect(m_mgmtIface, &OrgKdeKunifiedpushManagementInterface::pushProviderChanged, this, &KCMPushNotifications::pushProviderChanged);
40
41 m_clientModel = new ClientModel(m_mgmtIface, this);
42 connect(this, &KCMPushNotifications::distributorChanged, m_clientModel, &ClientModel::reload);
43
44 connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceRegistered, this, &KCMPushNotifications::distributorChanged);
45 connect(&m_serviceWatcher, &QDBusServiceWatcher::serviceUnregistered, this, &KCMPushNotifications::distributorChanged);
46 m_serviceWatcher.setConnection(QDBusConnection::sessionBus());
48 m_serviceWatcher.addWatchedService(UP_DISTRIBUTOR_SERVICE_NAME_FILTER);
49 connect(this, &KCMPushNotifications::distributorChanged, this, &KCMPushNotifications::distributorStatusChanged);
50 connect(this, &KCMPushNotifications::distributorChanged, this, &KCMPushNotifications::pushProviderChanged);
51
52 m_selfTest = new SelfTest(this);
53 m_selfTest->setNetworkAccessManager(&m_nam);
54
55 qmlRegisterUncreatableMetaObject(DistributorStatus::staticMetaObject, "org.kde.kunifiedpush.kcm", 1, 0, "DistributorStatus", {});
56 qmlRegisterUncreatableMetaObject(SelfTest::staticMetaObject, "org.kde.kunifiedpush.kcm", 1, 0, "SelfTest", {});
57}
58
59KCMPushNotifications::~KCMPushNotifications() = default;
60
61bool KCMPushNotifications::hasDistributor() const
62{
63 return !ConnectorUtils::selectDistributor().isEmpty();
64}
65
66bool KCMPushNotifications::hasKDEDistributor() const
67{
68 return ConnectorUtils::selectDistributor() == KDE_DISTRIBUTOR_SERVICE_NAME;
69}
70
71int KCMPushNotifications::distributorStatus() const
72{
73 return m_mgmtIface->status();
74}
75
76QString KCMPushNotifications::distributorErrorMessage() const
77{
78 return m_mgmtIface->errorMessage();
79}
80
81QString KCMPushNotifications::pushProviderId() const
82{
83 return m_mgmtIface->pushProviderId();
84}
85
86ClientModel* KCMPushNotifications::clientModel() const
87{
88 return m_clientModel;
89}
90
91SelfTest* KCMPushNotifications::selfTest() const
92{
93 return m_selfTest;
94}
95
96QVariantMap KCMPushNotifications::pushProviderConfiguration(const QString &pushProviderId) const
97{
98 return m_mgmtIface->pushProviderConfiguration(pushProviderId);
99}
100
101void KCMPushNotifications::setPushProviderConfiguration(const QString &pushProviderId, const QVariantMap &config)
102{
103 m_mgmtIface->setPushProvider(pushProviderId, config);
104}
105
106void KCMPushNotifications::forceUnregister(const QString &token)
107{
108 m_mgmtIface->forceUnregisterClient(token);
109}
110
111void KCMPushNotifications::nextcloudAuthenticate(const QUrl &url)
112{
113 if (!url.isValid()) {
114 return;
115 }
116
117 m_nextcloudAuthenticator.reset(new NextcloudAuthenticator);
118 connect(m_nextcloudAuthenticator.get(), &NextcloudAuthenticator::authenticated, this, &KCMPushNotifications::nextcloudAuthenticated);
119 m_nextcloudAuthenticator->setNetworkAccessManager(&m_nam);
120 m_nextcloudAuthenticator->authenticate(url, QStringLiteral("KUnifiedPush Distributor (%1)").arg(QHostInfo::localHostName()));
121}
122
123void KCMPushNotifications::save()
124{
125 Q_EMIT saveRequested();
127 m_selfTest->reset();
128}
129
130#include "kcmpushnotifications.moc"
131#include "../shared/moc_distributorstatus_p.cpp"
132
133#include "moc_kcmpushnotifications.cpp"
Model for all registered push notification client.
Definition clientmodel.h:15
virtual void save()
KCM to configure push notifications.
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
Push notification self-test state machine.
Definition selftest.h:22
Client-side integration with UnifiedPush.
Definition connector.h:14
QDBusConnection sessionBus()
void serviceRegistered(const QString &serviceName)
void serviceUnregistered(const QString &serviceName)
QString localHostName()
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QString writableLocation(StandardLocation type)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
bool isValid() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Apr 25 2025 12:05:39 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.