File indexing completed on 2024-04-14 14:25:21

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 Waldo Bastian <bastian@kde.org>
0004     SPDX-FileCopyrightText: 2009, 2020 David Faure <faure@kde.org>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #include <KIO/ApplicationLauncherJob>
0010 #include <KIO/JobUiDelegateFactory>
0011 
0012 #include <KService>
0013 
0014 #include <QApplication>
0015 #include <QDebug>
0016 
0017 int main(int argc, char *argv[])
0018 {
0019     QApplication app(argc, argv);
0020 
0021     QString serviceId = QStringLiteral("org.kde.kwrite");
0022     if (argc > 1) {
0023         serviceId = QString::fromLocal8Bit(argv[1]);
0024     }
0025     QList<QUrl> urls;
0026     if (argc > 2) {
0027         urls << QUrl::fromUserInput(QString::fromLocal8Bit(argv[2]));
0028     }
0029 
0030     KService::Ptr service = KService::serviceByDesktopName(serviceId);
0031     if (!service) {
0032         service = KService::serviceByStorageId(serviceId + QLatin1String(".desktop"));
0033         if (!service) {
0034             qWarning() << "Service not found" << serviceId;
0035             return 1;
0036         }
0037     }
0038 
0039     KIO::ApplicationLauncherJob *job = new KIO::ApplicationLauncherJob(service);
0040     job->setUrls(urls);
0041     job->setUiDelegate(KIO::createDefaultJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled, nullptr));
0042     job->start();
0043 
0044     QObject::connect(job, &KJob::result, &app, [&]() {
0045         if (job->error()) {
0046             app.exit(1);
0047         } else {
0048             qDebug() << "Started. pid=" << job->pid();
0049         }
0050     });
0051 
0052     return app.exec();
0053 }