File indexing completed on 2025-03-09 04:54:36

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "openurlwithjob.h"
0008 #include "messageviewer_debug.h"
0009 #include <KIO/CommandLauncherJob>
0010 #include <KMacroExpander>
0011 #include <KNotificationJobUiDelegate>
0012 #include <KShell>
0013 using namespace MessageViewer;
0014 OpenUrlWithJob::OpenUrlWithJob(QObject *parent)
0015     : QObject{parent}
0016 {
0017 }
0018 
0019 OpenUrlWithJob::~OpenUrlWithJob() = default;
0020 
0021 bool OpenUrlWithJob::canStart() const
0022 {
0023     return mInfo.isValid() && !mUrl.isEmpty();
0024 }
0025 
0026 void OpenUrlWithJob::start()
0027 {
0028     if (!canStart()) {
0029         deleteLater();
0030         qCWarning(MESSAGEVIEWER_LOG) << " Impossible to start OpenUrlWithJob";
0031         return;
0032     }
0033     QHash<QChar, QString> map;
0034     map.insert(QLatin1Char('u'), mUrl.toString());
0035 
0036     const QString commandLine = mInfo.commandLine();
0037     const QString cmd = KMacroExpander::expandMacrosShellQuote(commandLine, map);
0038     const QStringList arg = KShell::splitArgs(cmd);
0039     // qDebug() << " cmd " << cmd << " arg " << arg << " mInfo.command() " << mInfo.command();
0040     auto job = new KIO::CommandLauncherJob(mInfo.command(), QStringList() << arg);
0041     job->setUiDelegate(new KNotificationJobUiDelegate(KJobUiDelegate::AutoHandlingEnabled));
0042     job->start();
0043     deleteLater();
0044 }
0045 
0046 const OpenWithUrlInfo &OpenUrlWithJob::info() const
0047 {
0048     return mInfo;
0049 }
0050 
0051 void OpenUrlWithJob::setInfo(const OpenWithUrlInfo &newInfo)
0052 {
0053     mInfo = newInfo;
0054 }
0055 
0056 void OpenUrlWithJob::setUrl(const QUrl &url)
0057 {
0058     mUrl = url;
0059 }
0060 
0061 #include "moc_openurlwithjob.cpp"