Messagelib

openurlwithjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "openurlwithjob.h"
8 #include "messageviewer_debug.h"
9 #include <KIO/CommandLauncherJob>
10 #include <KMacroExpander>
11 #include <KNotificationJobUiDelegate>
12 #include <KShell>
13 using namespace MessageViewer;
14 OpenUrlWithJob::OpenUrlWithJob(QObject *parent)
15  : QObject{parent}
16 {
17 }
18 
19 OpenUrlWithJob::~OpenUrlWithJob() = default;
20 
21 bool OpenUrlWithJob::canStart() const
22 {
23  return mInfo.isValid() && !mUrl.isEmpty();
24 }
25 
27 {
28  if (!canStart()) {
29  deleteLater();
30  qCWarning(MESSAGEVIEWER_LOG) << " Impossible to start OpenUrlWithJob";
31  return;
32  }
34  map.insert(QLatin1Char('u'), mUrl.toString());
35 
36  const QString commandLine = mInfo.commandLine();
37  const QString cmd = KMacroExpander::expandMacrosShellQuote(commandLine, map);
38  const QStringList arg = KShell::splitArgs(cmd);
39  // qDebug() << " cmd " << cmd << " arg " << arg << " mInfo.command() " << mInfo.command();
40  auto job = new KIO::CommandLauncherJob(mInfo.command(), QStringList() << arg);
42  job->start();
43  deleteLater();
44 }
45 
46 const OpenWithUrlInfo &OpenUrlWithJob::info() const
47 {
48  return mInfo;
49 }
50 
51 void OpenUrlWithJob::setInfo(const OpenWithUrlInfo &newInfo)
52 {
53  mInfo = newInfo;
54 }
55 
56 void OpenUrlWithJob::setUrl(const QUrl &url)
57 {
58  mUrl = url;
59 }
60 
61 #include "moc_openurlwithjob.cpp"
The OpenWithUrlInfo class.
Q_SCRIPTABLE Q_NOREPLY void start()
QString expandMacrosShellQuote(const QString &str, const QHash< QChar, QString > &map, QChar c=QLatin1Char('%'))
QFuture< void > map(Sequence &sequence, MapFunctor function)
KCOREADDONS_EXPORT QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=nullptr)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Thu Feb 15 2024 03:55:21 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.