File indexing completed on 2024-05-19 04:45:16

0001 /*
0002     SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include <KUnifiedPush/Connector>
0007 
0008 #include <QCommandLineParser>
0009 #include <QCoreApplication>
0010 #include <QDebug>
0011 #include <QDBusConnection>
0012 #include <QProcess>
0013 #include <QTimer>
0014 
0015 int main(int argc, char **argv)
0016 {
0017     QCommandLineParser parser;
0018     parser.addHelpOption();
0019     parser.addVersionOption();
0020     QCommandLineOption unregisterOpt(QStringLiteral("unregister"));
0021     parser.addOption(unregisterOpt);
0022     QCommandLineOption dbusActivatedOpt(QStringLiteral("dbus-activated"));
0023     parser.addOption(dbusActivatedOpt);
0024 
0025 
0026     QCoreApplication app(argc, argv);
0027     parser.process(app);
0028 
0029     const auto unregisterRequested = parser.isSet(unregisterOpt);
0030 
0031     const auto serviceName = QStringLiteral("org.kde.kunifiedpush.demo-notifier");
0032     if (!QDBusConnection::sessionBus().registerService(serviceName)) {
0033         qCritical("Service name already in use.");
0034         return 1;
0035     }
0036 
0037     KUnifiedPush::Connector connector(serviceName);
0038     QObject::connect(&connector, &KUnifiedPush::Connector::stateChanged, [unregisterRequested](auto state) {
0039         qDebug() << "Connector state changed:" << state;
0040         if (unregisterRequested && state == KUnifiedPush::Connector::Unregistered) {
0041             QCoreApplication::quit();
0042         }
0043     });
0044     QObject::connect(&connector, &KUnifiedPush::Connector::messageReceived, [](const auto &msg) {
0045         QProcess::startDetached(QStringLiteral("kdialog"), { QStringLiteral("--msgbox"), QString::fromUtf8(msg)});
0046     });
0047 
0048     if (!connector.endpoint().isEmpty()) {
0049         qDebug() << "Notification endpoint:" << connector.endpoint();
0050     }
0051     QObject::connect(&connector, &KUnifiedPush::Connector::endpointChanged, [](const auto &endpoint) {
0052         qDebug() << "New notification endpoint:" << endpoint;
0053     });
0054 
0055     if (unregisterRequested) {
0056         if (connector.state() == KUnifiedPush::Connector::Unregistered) {
0057             return 0;
0058         }
0059         connector.unregisterClient();
0060     } else {
0061         connector.registerClient(QStringLiteral("Demonstrating push notifications."));
0062     }
0063 
0064     if (parser.isSet(dbusActivatedOpt)) {
0065         QTimer::singleShot(std::chrono::seconds(5), &app, &QCoreApplication::quit);
0066     }
0067 
0068     return app.exec();
0069 }