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 QString displayName = d->m_executable;
105 KService::Ptr service = KService::serviceByDesktopName(d->m_desktopName);
106 if (service) {
107 displayName = service->name();
108 }
109 Q_EMIT description(this, i18nc("Launching application", "Launching %1", displayName), {}, {});
110
111 if (d->m_command.isEmpty() && !d->m_executable.isEmpty()) {
112 d->m_processRunner =
113 KProcessRunner::fromExecutable(d->m_executable, d->m_arguments, d->m_desktopName, d->m_startupId, d->m_workingDirectory, d->m_environment);
114 } else {
115 d->m_processRunner =
116 KProcessRunner::fromCommand(d->m_command, d->m_desktopName, d->m_executable, d->m_startupId, d->m_workingDirectory, d->m_environment);
117 }
118 connect(d->m_processRunner, &KProcessRunner::error, this, [this](const QString &errorText) {
119 setError(KJob::UserDefinedError);
120 setErrorText(errorText);
121 emitResult();
122 });
123 connect(d->m_processRunner, &KProcessRunner::processStarted, this, [this](qint64 pid) {
124 d->m_pid = pid;
125 emitResult();
126 });
127}
128
129bool KIO::CommandLauncherJob::waitForStarted()
130{
131 if (d->m_processRunner.isNull()) {
132 return false;
133 }
134 const bool ret = d->m_processRunner->waitForStarted();
135 if (!d->m_processRunner.isNull()) {
136 qApp->sendPostedEvents(d->m_processRunner); // so slotStarted gets called
137 }
138 return ret;
139}
140
142{
143 return d->m_pid;
144}
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 Tue Mar 26 2024 11:18:52 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.