File indexing completed on 2024-05-12 05:46:41

0001 /*
0002  *   Copyright (C) 2006 Aaron Seigo <aseigo@kde.org>
0003  *
0004  *   This program is free software; you can redistribute it and/or modify
0005  *   it under the terms of the GNU Library General Public License version 2 as
0006  *   published by the Free Software Foundation
0007  *
0008  *   This program is distributed in the hope that it will be useful,
0009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0011  *   GNU General Public License for more details
0012  *
0013  *   You should have received a copy of the GNU Library General Public
0014  *   License along with this program; if not, write to the
0015  *   Free Software Foundation, Inc.,
0016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
0017  */
0018 
0019 #include <QCoreApplication>
0020 #include <QCommandLineParser>
0021 #include <QDBusInterface>
0022 #include <QDebug>
0023 
0024 int main(int argc, char* argv[])
0025 {
0026     QCoreApplication app(argc, argv);
0027     app.setApplicationName(QStringLiteral("kquitapp"));
0028     app.setApplicationVersion(QStringLiteral("2.0"));
0029 
0030     QCommandLineParser parser;
0031     parser.setApplicationDescription(QCoreApplication::translate("main", "Quit a D-Bus enabled application easily"));
0032     parser.addOption(QCommandLineOption(QStringLiteral("service"), QCoreApplication::translate("main", "Full service name, overrides application name provided"), QStringLiteral("service")));
0033     parser.addOption(QCommandLineOption(QStringLiteral("path"), QCoreApplication::translate("main", "Path in the D-Bus interface to use"), QStringLiteral("path"), QStringLiteral("/MainApplication")));
0034     parser.addPositionalArgument(QStringLiteral("[application]"), QCoreApplication::translate("main", "The name of the application to quit"));
0035     parser.addHelpOption();
0036     parser.addVersionOption();
0037     parser.process(app);
0038 
0039     QString service;
0040     if (parser.isSet(QStringLiteral("service")))
0041     {
0042         service = parser.value(QStringLiteral("service"));
0043     }
0044     else if(!parser.positionalArguments().isEmpty())
0045     {
0046         service = QStringLiteral("org.kde.%1").arg(parser.positionalArguments().at(0));
0047     }
0048     else
0049     {
0050         parser.showHelp(1);
0051     }
0052 
0053     QString path(parser.value(QStringLiteral("path")));
0054 
0055     QDBusInterface interface(service, path);
0056     if (!interface.isValid()) {
0057         qWarning() << QCoreApplication::translate("main", "Application %1 could not be found using service %2 and path %3.").arg(parser.positionalArguments().at(0), service, path);
0058         return 1;
0059     }
0060     interface.call(QStringLiteral("quit"));
0061     QDBusError error = interface.lastError();
0062     if (error.type() != QDBusError::NoError) {
0063         qWarning() << QCoreApplication::translate("main", "Quitting application %1 failed. Error reported was:\n\n     %2 : %3").arg(parser.positionalArguments().join(QStringLiteral(" ")), error.name(), error.message());
0064         return 1;
0065     }
0066     return 0;
0067 }
0068