1/*
2 SPDX-FileCopyrightText: 2006 Aaron Seigo <aseigo@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include <QCommandLineParser>
8#include <QCoreApplication>
9#include <QDBusInterface>
10#include <QDebug>
11
12int main(int argc, char *argv[])
13{
14 QCoreApplication app(argc, argv);
15 app.setApplicationName(QStringLiteral("kquitapp"));
16 app.setApplicationVersion(QStringLiteral("2.0"));
17
18 QCommandLineParser parser;
19 parser.setApplicationDescription(QCoreApplication::translate(context: "main", key: "Quit a D-Bus enabled application easily"));
20 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("service"),
21 QCoreApplication::translate(context: "main", key: "Full service name, overrides application name provided"),
22 QStringLiteral("service")));
23 parser.addOption(commandLineOption: QCommandLineOption(QStringLiteral("path"),
24 QCoreApplication::translate(context: "main", key: "Path in the D-Bus interface to use"),
25 QStringLiteral("path"),
26 QStringLiteral("/MainApplication")));
27 parser.addPositionalArgument(QStringLiteral("[application]"), description: QCoreApplication::translate(context: "main", key: "The name of the application to quit"));
28 parser.addHelpOption();
29 parser.addVersionOption();
30 parser.process(app);
31
32 QString service;
33 if (parser.isSet(QStringLiteral("service"))) {
34 service = parser.value(QStringLiteral("service"));
35 } else if (!parser.positionalArguments().isEmpty()) {
36 service = QStringLiteral("org.kde.%1").arg(a: parser.positionalArguments().at(i: 0));
37 } else {
38 parser.showHelp(exitCode: 1);
39 }
40
41 QString path(parser.value(QStringLiteral("path")));
42
43 QDBusInterface interface(service, path);
44 if (!interface.isValid()) {
45 qWarning() << QCoreApplication::translate(context: "main", key: "Application %1 could not be found using service %2 and path %3.")
46 .arg(args: parser.positionalArguments().at(i: 0), args&: service, args&: path);
47 return 1;
48 }
49 interface.call(QStringLiteral("quit"));
50 QDBusError error = interface.lastError();
51 if (error.type() != QDBusError::NoError) {
52 qWarning() << QCoreApplication::translate(context: "main", key: "Quitting application %1 failed. Error reported was:\n\n %2 : %3")
53 .arg(args: parser.positionalArguments().join(QStringLiteral(" ")), args: error.name(), args: error.message());
54 return 1;
55 }
56 return 0;
57}
58

source code of kdbusaddons/src/tools/kquitapp/kquitapp.cpp