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 
12 int 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 }
void setApplicationDescription(const QString &description)
QString translate(const char *context, const char *sourceText, const char *disambiguation, int n)
void addPositionalArgument(const QString &name, const QString &description, const QString &syntax)
QStringList positionalArguments() const const
void showHelp(int exitCode)
void process(const QStringList &arguments)
QCommandLineOption addVersionOption()
const T & at(int i) const const
QString value(const QString &optionName) const const
bool isEmpty() const const
QString join(const QString &separator) const const
bool isSet(const QString &name) const const
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QString path(const QString &relativePath)
bool addOption(const QCommandLineOption &option)
QCommandLineOption addHelpOption()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:49:05 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.