File indexing completed on 2024-04-28 05:27:05

0001 /*
0002  * kstart.C. Part of the KDE project.
0003  *
0004  * SPDX-FileCopyrightText: 1997-2000 Matthias Ettrich <ettrich@kde.org>
0005  * SPDX-FileCopyrightText: David Faure <faure@kde.org>
0006  * SPDX-FileCopyrightText: Richard Moore <rich@kde.org>
0007  *
0008  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0009  */
0010 
0011 #include <config-kde-cli-tools.h>
0012 
0013 #include "kstart.h"
0014 
0015 #include <iostream>
0016 #include <stdlib.h>
0017 
0018 #include "private/qtx11extras_p.h"
0019 #include <QCommandLineOption>
0020 #include <QCommandLineParser>
0021 #include <QDebug>
0022 #include <QGuiApplication>
0023 #include <QUrl>
0024 
0025 #include <kaboutdata.h>
0026 #include <klocalizedstring.h>
0027 #include <kprocess.h>
0028 
0029 #include <KIO/ApplicationLauncherJob>
0030 #include <KIO/CommandLauncherJob>
0031 
0032 // some globals
0033 
0034 static QString serviceName;
0035 static QString exe;
0036 static QStringList exeArgs;
0037 static QString url;
0038 
0039 KStart::KStart()
0040     : QObject()
0041 {
0042 #if WITH_X11
0043     if (QX11Info::isPlatformX11()) {
0044         // propagate the startup identification to the started process
0045         qputenv("DESKTOP_STARTUP_ID", QX11Info::nextStartupId());
0046     }
0047 #endif
0048 
0049     if (!serviceName.isEmpty()) {
0050         KService::Ptr service = KService::serviceByDesktopName(serviceName);
0051         if (!service) {
0052             qCritical() << "No such service" << exe;
0053         } else {
0054             auto *job = new KIO::ApplicationLauncherJob(service);
0055             if (!url.isEmpty()) {
0056                 job->setUrls({QUrl(url)}); // TODO use QUrl::fromUserInput(PreferLocalFile)?
0057             }
0058             job->exec();
0059             if (job->error()) {
0060                 qCritical() << job->errorString();
0061             }
0062         }
0063     } else {
0064         auto *job = new KIO::CommandLauncherJob(exe, exeArgs);
0065         job->exec();
0066     }
0067 }
0068 
0069 int main(int argc, char *argv[])
0070 {
0071     QGuiApplication app(argc, argv);
0072     KLocalizedString::setApplicationDomain(QByteArrayLiteral("kstart"));
0073 
0074     KAboutData aboutData(QStringLiteral("kstart"),
0075                          i18n("KStart"),
0076                          QString::fromLatin1(PROJECT_VERSION),
0077                          i18n("Utility to launch applications"),
0078                          KAboutLicense::GPL,
0079                          i18n("(C) 1997-2000 Matthias Ettrich (ettrich@kde.org)"));
0080 
0081     aboutData.addAuthor(i18n("Matthias Ettrich"), QString(), QStringLiteral("ettrich@kde.org"));
0082     aboutData.addAuthor(i18n("David Faure"), QString(), QStringLiteral("faure@kde.org"));
0083     aboutData.addAuthor(i18n("Richard J. Moore"), QString(), QStringLiteral("rich@kde.org"));
0084     KAboutData::setApplicationData(aboutData);
0085 
0086     QCommandLineParser parser;
0087     aboutData.setupCommandLine(&parser);
0088     parser.addPositionalArgument(QStringLiteral("command"), i18n("Command to execute"));
0089     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("application"),
0090                                         i18n("Alternative to <command>: desktop file name to start, e.g. org.kde.kate"),
0091                                         QLatin1String("desktopfile")));
0092     parser.addOption(QCommandLineOption(QStringList() << QLatin1String("url"),
0093                                         i18n("Optional URL to pass to the application when using --application"),
0094                                         QLatin1String("url")));
0095 
0096     parser.process(app);
0097     aboutData.processCommandLine(&parser);
0098 
0099     if (parser.isSet(QStringLiteral("application"))) {
0100         serviceName = parser.value(QStringLiteral("application"));
0101         url = parser.value(QStringLiteral("url"));
0102     } else {
0103         QStringList positionalArgs = parser.positionalArguments();
0104         if (positionalArgs.isEmpty()) {
0105             qCritical() << i18n("No command specified");
0106             parser.showHelp(1);
0107         }
0108 
0109         exe = positionalArgs.takeFirst();
0110         exeArgs = positionalArgs;
0111     }
0112 
0113     KStart start;
0114 
0115     return app.exec();
0116 }
0117 
0118 #include "moc_kstart.cpp"