File indexing completed on 2024-05-05 04:20:55

0001 /*
0002     SPDX-FileCopyrightText: 2022 Friedrich W. H. Kossebau <kossebau@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-or-later
0005 */
0006 
0007 #include "launchjob.hpp"
0008 
0009 // KF
0010 #include <KIO/ApplicationLauncherJob>
0011 #include <KService>
0012 #include <KLocalizedString>
0013 // Qt
0014 #include <QJsonArray>
0015 #include <QJsonDocument>
0016 #include <QJsonObject>
0017 
0018 namespace Kodaskanna
0019 {
0020 
0021 namespace PurposePlugin
0022 {
0023 
0024 LaunchJob::LaunchJob(QObject *parent)
0025     : Purpose::Job(parent)
0026 {
0027 }
0028 
0029 void LaunchJob::start()
0030 {
0031     const QJsonArray urls = data().value(QStringLiteral("urls")).toArray();
0032     if (urls.size() != 1) {
0033         setErrorText(i18n("Need one URL."));
0034         emitResult();
0035         return;
0036     }
0037     const QUrl url(urls.at(0).toString());
0038 
0039     KService::Ptr service = KService::serviceByDesktopName(QStringLiteral("org.kde.kodaskanna"));
0040 
0041     if (!service) {
0042         setErrorText(i18n("Could not find Kodaskanna."));
0043         emitResult();
0044         return;
0045     }
0046 
0047     auto *applicationLauncherJob = new KIO::ApplicationLauncherJob(service);
0048     applicationLauncherJob->setUrls({url});
0049     connect(applicationLauncherJob, &KIO::ApplicationLauncherJob::result,
0050             this, &LaunchJob::handleApplicationLaunchJobResult);
0051     applicationLauncherJob->start();
0052 }
0053 
0054 void LaunchJob::handleApplicationLaunchJobResult(KJob *job)
0055 {
0056     if (job->error()) {
0057         setError(job->error());
0058         setErrorText(job->errorString());
0059     }
0060 
0061     emitResult();
0062 }
0063 
0064 }
0065 }