qt5base-lts/examples/dbus/pingpong/ping.cpp
Ievgenii Meshcheriakov 07b8a5a409 pingpong example: Modernize the code
Use qWarning()/std::cout instead of C IO functions.

Use default parameters for QDBusInterface's constructor.

Remove useless Q_SCRIPTABLE.

Use type-safe invokeMethod().

Extract a local variable for the used D-Bus connection.

Task-number: QTBUG-111366
Pick-to: 6.5
Change-Id: Idc38a7c1dd97d71308d9491193039744c759d6f2
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
2023-02-28 18:15:24 +01:00

41 lines
1.1 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "ping-common.h"
#include <QCoreApplication>
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDBusReply>
#include <iostream>
int main(int argc, char **argv)
{
QCoreApplication app(argc, argv);
auto connection = QDBusConnection::sessionBus();
if (!connection.isConnected()) {
qWarning("Cannot connect to the D-Bus session bus.\n"
"To start it, run:\n"
"\teval `dbus-launch --auto-syntax`\n");
return 1;
}
QDBusInterface iface(SERVICE_NAME, "/");
if (iface.isValid()) {
QDBusReply<QString> reply = iface.call("ping", argc > 1 ? argv[1] : "");
if (reply.isValid()) {
std::cout << "Reply was: " << qPrintable(reply.value()) << std::endl;
return 0;
}
qWarning("Call failed: %s\n", qPrintable(reply.error().message()));
return 1;
}
qWarning("%s\n", qPrintable(connection.lastError().message()));
return 1;
}