File: waiter.cpp

package info (click to toggle)
plasma-workspace 4%3A5.27.5-2%2Bdeb12u2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 102,040 kB
  • sloc: cpp: 121,800; xml: 3,238; python: 645; perl: 586; sh: 254; javascript: 113; ruby: 62; makefile: 15; ansic: 13
file content (81 lines) | stat: -rw-r--r-- 2,614 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
/*
    SPDX-FileCopyrightText: 2017 Valerio Pilo <vpilo@coldshock.net>

    SPDX-License-Identifier: LGPL-2.0-only
*/

#include <QCommandLineParser>
#include <QDebug>

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusServiceWatcher>

#include <unistd.h>

#include "debug_p.h"
#include "waiter.h"

constexpr static const char dbusServiceName[] = "org.freedesktop.Notifications";

Waiter::Waiter(int argc, char **argv)
    : QCoreApplication(argc, argv)
    , mService(dbusServiceName)
{
    setApplicationName(QStringLiteral("plasma_waitforname"));
    setApplicationVersion(QStringLiteral("1.0"));

    QCommandLineParser parser;
    parser.setApplicationDescription(
        QStringLiteral("Waits for D-Bus registration of the Notifications service.\n"
                       "Prevents notifications from being processed before the desktop is ready."));
    parser.addHelpOption();
    parser.addVersionOption();
    parser.addPositionalArgument(QStringLiteral("service"),
                                 QStringLiteral("Optionally listen for a service different than '%1'").arg(mService),
                                 QStringLiteral("[service]"));
    parser.process(*this);

    const QStringList args = parser.positionalArguments();
    if (!args.isEmpty()) {
        mService = args.at(0);
    }
}

bool Waiter::waitForService()
{
    QDBusConnection sessionBus = QDBusConnection::sessionBus();

    if (sessionBus.interface()->isServiceRegistered(mService)) {
        qCDebug(LOG_PLASMA) << "WaitForName: Service" << mService << "is already registered";
        return false;
    }

    qCDebug(LOG_PLASMA) << "WaitForName: Waiting for appearance of service" << mService << "for" << dbusTimeoutSec << "seconds";

    QDBusServiceWatcher *watcher = new QDBusServiceWatcher(this);
    watcher->setConnection(sessionBus);
    watcher->setWatchMode(QDBusServiceWatcher::WatchForRegistration);
    watcher->addWatchedService(mService);
    connect(watcher, &QDBusServiceWatcher::serviceRegistered, this, &Waiter::registered);

    mTimeoutTimer.setSingleShot(true);
    mTimeoutTimer.setInterval(dbusTimeoutSec * 1000);
    connect(&mTimeoutTimer, &QTimer::timeout, this, &Waiter::timeout);
    mTimeoutTimer.start();

    return true;
}

void Waiter::registered()
{
    qCDebug(LOG_PLASMA) << "WaitForName: Service was registered after" << (dbusTimeoutSec - (mTimeoutTimer.remainingTime() / 1000)) << "seconds";
    mTimeoutTimer.stop();
    exit(0);
}

void Waiter::timeout()
{
    qCInfo(LOG_PLASMA) << "WaitForName: Service was not registered within timeout";
    exit(1);
}