File: demo-notifier.cpp

package info (click to toggle)
kunifiedpush 25.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,036 kB
  • sloc: cpp: 4,524; xml: 154; java: 141; makefile: 5; sh: 1
file content (95 lines) | stat: -rw-r--r-- 3,773 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
    SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
    SPDX-License-Identifier: LGPL-2.0-or-later
*/

#include <KUnifiedPush/Connector>

#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDebug>
#include <QDBusConnection>
#include <QProcess>
#include <QTimer>

// The VAPID public key is usually provided by the application server,
// the VAPID private key is inaccessible to the application.
// This is only provided here for demonstration purposes as we don't
// have an application server here and instead use the "webpush-notify"
// CLI tool for this.
constexpr inline auto VAPID_PUBLIC_KEY = "BNH1upJppSLEASpoPmGDaeafgJ6KjZjxbDA3LbHG5yE1cAiKQ5qm_K08nVgibtxqm245l0BlaaMsu4iBGzV1wEU";
constexpr inline auto VAPID_PRIVATE_KEY = "9rxELCH8cvjOn2pUjSbfmYrm0NyNusqtPb5-OwcZUD4";

void printNoficationCommand(const KUnifiedPush::Connector &connector)
{
    if (connector.endpoint().isEmpty()) {
        return;
    }

    qDebug().noquote().nospace() << "echo \"Hello world!\" | "
        << QCoreApplication::applicationDirPath() << "/webpush-notify --endpoint \""
        << connector.endpoint() << "\" --user-agent-public-key " << connector.contentEncryptionPublicKey().toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)
        << " --auth-secret " << connector.contentEncryptionAuthSecret().toBase64(QByteArray::Base64UrlEncoding | QByteArray::OmitTrailingEquals)
        << " --vapid-public-key " << VAPID_PUBLIC_KEY
        << " --vapid-private-key " << VAPID_PRIVATE_KEY;
}

int main(int argc, char **argv)
{
    QCommandLineParser parser;
    parser.addHelpOption();
    parser.addVersionOption();
    QCommandLineOption unregisterOpt(QStringLiteral("unregister"));
    parser.addOption(unregisterOpt);
    QCommandLineOption dbusActivatedOpt(QStringLiteral("dbus-activated"));
    parser.addOption(dbusActivatedOpt);


    QCoreApplication app(argc, argv);
    parser.process(app);

    const auto unregisterRequested = parser.isSet(unregisterOpt);

    const auto serviceName = QStringLiteral("org.kde.kunifiedpush.demo-notifier");
    if (!QDBusConnection::sessionBus().registerService(serviceName)) {
        qCritical("Service name already in use.");
        return 1;
    }

    KUnifiedPush::Connector connector(serviceName);
    connector.setVapidPublicKeyRequired(true);
    connector.setVapidPublicKey(QString::fromLatin1(VAPID_PUBLIC_KEY));
    QObject::connect(&connector, &KUnifiedPush::Connector::stateChanged, [unregisterRequested](auto state) {
        qDebug() << "Connector state changed:" << state;
        if (unregisterRequested && state == KUnifiedPush::Connector::Unregistered) {
            QCoreApplication::quit();
        }
    });
    QObject::connect(&connector, &KUnifiedPush::Connector::messageReceived, [](const auto &msg) {
        QProcess::startDetached(QStringLiteral("kdialog"), { QStringLiteral("--msgbox"), QString::fromUtf8(msg)});
    });

    if (!connector.endpoint().isEmpty()) {
        qDebug() << "Notification endpoint:" << connector.endpoint();
        printNoficationCommand(connector);
    }
    QObject::connect(&connector, &KUnifiedPush::Connector::endpointChanged, [&connector](const auto &endpoint) {
        qDebug() << "New notification endpoint:" << endpoint;
        printNoficationCommand(connector);
    });

    if (unregisterRequested) {
        if (connector.state() == KUnifiedPush::Connector::Unregistered) {
            return 0;
        }
        connector.unregisterClient();
    } else {
        connector.registerClient(QStringLiteral("Demonstrating push notifications."));
    }

    if (parser.isSet(dbusActivatedOpt)) {
        QTimer::singleShot(std::chrono::seconds(5), &app, &QCoreApplication::quit);
    }

    return app.exec();
}