KIO

commandlauncherjob.cpp
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 2020 David Faure <faure@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6*/
7
8#include "commandlauncherjob.h"
9#include "kiogui_debug.h"
10#include "kprocessrunner_p.h"
11#include <KLocalizedString>
12#include <KShell>
13
14#include <QPointer>
15
16class KIO::CommandLauncherJobPrivate
17{
18public:
19 QString m_command;
20 QString m_desktopName;
21 QString m_executable;
22 QString m_workingDirectory;
23 QStringList m_arguments;
24 QByteArray m_startupId;
25 QPointer<KProcessRunner> m_processRunner;
27 qint64 m_pid = 0;
28};
29
31 : KJob(parent)
32 , d(new CommandLauncherJobPrivate())
33{
34 d->m_command = command;
35}
36
38 : KJob(parent)
39 , d(new CommandLauncherJobPrivate())
40{
41 d->m_executable = executable;
42 d->m_arguments = args;
43}
44
46{
47 // Do *NOT* delete the KProcessRunner instances here.
48 // We need it to keep running so it can terminate startup notification on process exit.
49}
50
52{
53 d->m_command = command;
54}
55
57{
58 if (d->m_command.isEmpty()) {
59 return KShell::quoteArg(d->m_executable) + QLatin1Char(' ') + KShell::joinArgs(d->m_arguments);
60 }
61 return d->m_command;
62}
63
65{
66 d->m_executable = executable;
67}
68
70{
71 d->m_desktopName = desktopName;
72}
73
75{
76 d->m_startupId = startupId;
77}
78
80{
81 d->m_workingDirectory = workingDirectory;
82}
83
85{
86 return d->m_workingDirectory;
87}
88
90{
91 d->m_environment = environment;
92}
93
95{
96 // Some fallback for lazy callers, not 100% accurate though
97 if (d->m_executable.isEmpty()) {
98 const QStringList args = KShell::splitArgs(d->m_command);
99 if (!args.isEmpty()) {
100 d->m_executable = args.first();
101 }
102 }
103
104 if (d->m_executable.isEmpty()) {
105 setError(KJob::UserDefinedError);
106 setErrorText(i18nc("An error message", "Empty command provided"));
107 emitResult();
108 return;
109 }
110
111 QString displayName = d->m_executable;
112 KService::Ptr service = KService::serviceByDesktopName(d->m_desktopName);
113 if (service) {
114 displayName = service->name();
115 }
116 Q_EMIT description(this, i18nc("Launching application", "Launching %1", displayName), {}, {});
117
118 if (d->m_command.isEmpty() && !d->m_executable.isEmpty()) {
119 d->m_processRunner =
120 KProcessRunner::fromExecutable(d->m_executable, d->m_arguments, d->m_desktopName, d->m_startupId, d->m_workingDirectory, d->m_environment);
121 } else {
122 d->m_processRunner =
123 KProcessRunner::fromCommand(d->m_command, d->m_desktopName, d->m_executable, d->m_startupId, d->m_workingDirectory, d->m_environment);
124 }
125 connect(d->m_processRunner, &KProcessRunner::error, this, [this](const QString &errorText) {
126 setError(KJob::UserDefinedError);
127 setErrorText(errorText);
128 emitResult();
129 });
130 connect(d->m_processRunner, &KProcessRunner::processStarted, this, [this](qint64 pid) {
131 d->m_pid = pid;
132 emitResult();
133 });
134}
135
136bool KIO::CommandLauncherJob::waitForStarted()
137{
138 if (d->m_processRunner.isNull()) {
139 return false;
140 }
141 const bool ret = d->m_processRunner->waitForStarted();
142 if (!d->m_processRunner.isNull()) {
143 qApp->sendPostedEvents(d->m_processRunner); // so slotStarted gets called
144 }
145 return ret;
146}
147
149{
150 return d->m_pid;
151}
void setDesktopName(const QString &desktopName)
Set the name of the desktop file (e.g. "org.kde.dolphin", without the ".desktop" filename extension).
QString command() const
Returns the command executed by this job.
void setExecutable(const QString &executable)
Sets the name of the executable, used in the startup notification (see KStartupInfoData::setBin()).
~CommandLauncherJob() override
Destructor.
void setWorkingDirectory(const QString &workingDirectory)
Sets the working directory from which to run the command.
void setCommand(const QString &command)
Sets the command to execute, this will change the command that was set by any of the constructors.
void start() override
Starts the job.
void setProcessEnvironment(const QProcessEnvironment &environment)
Can be used to pass environment variables to the child process.
void setStartupId(const QByteArray &startupId)
Sets the platform-specific startup id of the command launch.
QString workingDirectory() const
Returns the working directory, which was previously set with setWorkingDirectory().
CommandLauncherJob(const QString &command, QObject *parent=nullptr)
Creates a CommandLauncherJob.
static Ptr serviceByDesktopName(const QString &_name)
QString name() const
QString i18nc(const char *context, const char *text, const TYPE &arg...)
KCOREADDONS_EXPORT QStringList splitArgs(const QString &cmd, Options flags=NoOptions, Errors *err=nullptr)
KCOREADDONS_EXPORT QString quoteArg(const QString &arg)
KCOREADDONS_EXPORT QString joinArgs(const QStringList &args)
T & first()
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:16:28 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.