KUnifiedPush

connectorutils.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "connectorutils_p.h"
7#include "unifiedpush-constants.h"
8
9#ifndef Q_OS_ANDROID
10#include <QDBusConnection>
11#include <QDBusConnectionInterface>
12#else
13#include <QCoreApplication>
14#include <QJniObject>
16#endif
17#include <QStringList>
18
19using namespace KUnifiedPush;
20
21QString ConnectorUtils::selectDistributor()
22{
23#ifndef Q_OS_ANDROID
24 return selectDistributor(QDBusConnection::sessionBus().interface()->registeredServiceNames());
25#else
27 return QAndroidJniObject::callStaticObjectMethod("org/kde/kunifiedpush/Distributor", "selectDistributor", "(Landroid/content/Context;)Ljava/lang/String;", context.object()).toString();
28#endif
29}
30
31QString ConnectorUtils::selectDistributor(QStringList &&services)
32{
33 services.erase(std::remove_if(services.begin(), services.end(), [](const auto &s) { return !s.startsWith(QLatin1String(UP_DISTRIBUTOR_SERVICE_NAME_PREFIX)); }), services.end());
34 std::sort(services.begin(), services.end()); // make sure results are reproducible
35
36 if (services.isEmpty()) {
37 return {};
38 }
39
40 // check if one specific distributor was requested
41 const auto requestedDist = QString::fromUtf8(qgetenv("UNIFIEDPUSH_DISTRIBUTOR"));
42 if (!requestedDist.isEmpty()) {
43 const QString distServiceName = QLatin1String(UP_DISTRIBUTOR_SERVICE_NAME_PREFIX) + requestedDist;
44 if (!services.contains(distServiceName)) {
45 return {};
46 } else {
47 return distServiceName;
48 }
49 return {};
50 }
51
52 // prefer our own distributor over 3rdparty ones
53 const auto it = std::find(services.begin(), services.end(), QLatin1String(KDE_DISTRIBUTOR_SERVICE_NAME));
54 if (it != services.end()) {
55 return (*it);
56 }
57
58 // ... otherwise take a random one
59 return services.at(0);
60}
Client-side integration with UnifiedPush.
Definition connector.h:16
QDBusConnection sessionBus()
QJniObject callStaticObjectMethod(const char *className, const char *methodName, Args &&... args)
jobject object() const const
QString toString() const const
iterator erase(const_iterator first, const_iterator last)
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:49:08 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.