KDBusAddons

kquitapp.cpp
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("main", "Quit a D-Bus enabled application easily"));
20 parser.addOption(QCommandLineOption(QStringLiteral("service"),
21 QCoreApplication::translate("main", "Full service name, overrides application name provided"),
22 QStringLiteral("service")));
23 parser.addOption(QCommandLineOption(QStringLiteral("path"),
24 QCoreApplication::translate("main", "Path in the D-Bus interface to use"),
25 QStringLiteral("path"),
26 QStringLiteral("/MainApplication")));
27 parser.addPositionalArgument(QStringLiteral("[application]"), QCoreApplication::translate("main", "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(parser.positionalArguments().at(0));
37 } else {
38 parser.showHelp(1);
39 }
40
41 QString path(parser.value(QStringLiteral("path")));
42
43 QDBusInterface interface(service, path);
44 if (!interface.isValid()) {
45 qWarning() << QCoreApplication::translate("main", "Application %1 could not be found using service %2 and path %3.")
46 .arg(parser.positionalArguments().at(0), service, 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("main", "Quitting application %1 failed. Error reported was:\n\n %2 : %3")
53 .arg(parser.positionalArguments().join(QStringLiteral(" ")), error.name(), error.message());
54 return 1;
55 }
56 return 0;
57}
QString path(const QString &relativePath)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QCommandLineOption addHelpOption()
bool addOption(const QCommandLineOption &option)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QCommandLineOption addVersionOption()
bool isSet(const QCommandLineOption &option) const const
QStringList positionalArguments() const const
void process(const QCoreApplication &app)
void setApplicationDescription(const QString &description)
void showHelp(int exitCode)
QString value(const QCommandLineOption &option) const const
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
const_reference at(qsizetype i) const const
bool isEmpty() const const
QString arg(Args &&... args) const const
QString join(QChar separator) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 16:59:44 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.