1 | /* |
2 | * Copyright (C) 2006 Aaron Seigo <aseigo@kde.org> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU Library General Public License version 2 as |
6 | * published by the Free Software Foundation |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details |
12 | * |
13 | * You should have received a copy of the GNU Library General Public |
14 | * License along with this program; if not, write to the |
15 | * Free Software Foundation, Inc., |
16 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | */ |
18 | |
19 | #include <QCoreApplication> |
20 | #include <QDBusInterface> |
21 | |
22 | #include <KAboutData> |
23 | #include <KCmdLineArgs> |
24 | #include <KLocale> |
25 | #include <KDebug> |
26 | |
27 | int main(int argc, char* argv[]) |
28 | { |
29 | KAboutData aboutData( "kquitapp" , 0, ki18n("Command-line application quitter" ), |
30 | "1.0" , ki18n("Quit a D-Bus enabled application easily" ), KAboutData::License_GPL, |
31 | ki18n("(c) 2006, Aaron Seigo" ) ); |
32 | aboutData.addAuthor(ki18n("Aaron J. Seigo" ), ki18n("Current maintainer" ), "aseigo@kde.org" ); |
33 | KCmdLineArgs::init(argc, argv, &aboutData); |
34 | QCoreApplication app(argc, argv); |
35 | |
36 | KCmdLineOptions options; |
37 | options.add("service <service>" , ki18n("Full service name, overrides application name provided" )); |
38 | options.add("path <path>" , ki18n("Path in the D-Bus interface to use" ), "/MainApplication" ); |
39 | options.add("+application" , ki18n("The name of the application to quit" )); |
40 | KCmdLineArgs::addCmdLineOptions(options); |
41 | KCmdLineArgs* args = KCmdLineArgs::parsedArgs(); |
42 | |
43 | if (args->count() == 0) |
44 | { |
45 | args->usage(0); |
46 | } |
47 | |
48 | QString service; |
49 | if (args->isSet("service" )) |
50 | { |
51 | service = args->getOption("service" ); |
52 | } |
53 | else |
54 | { |
55 | service = QString("org.kde.%1" ).arg(QString(args->arg(0))); |
56 | } |
57 | |
58 | QString path("/MainApplication" ); |
59 | if (args->isSet("path" )) |
60 | { |
61 | path = args->getOption("path" ); |
62 | } |
63 | |
64 | QDBusInterface interface(service, path); |
65 | if (!interface.isValid()) { |
66 | kError() << i18n("Application %1 could not be found using service %2 and path %3." ,args->arg(0),service,path); |
67 | return 1; |
68 | } |
69 | interface.call("quit" ); |
70 | QDBusError error = interface.lastError(); |
71 | if (error.type() != QDBusError::NoError) { |
72 | kError() << i18n("Quitting application %1 failed. Error reported was:\n\n %2 : %3" ,args->arg(0),error.name(), error.message()); |
73 | return 1; |
74 | } |
75 | return 0; |
76 | } |
77 | |
78 | |