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 "../core/global.h"
10#include "kiogui_debug.h"
11#include "kprocessrunner_p.h"
12#include <KLocalizedString>
13#include <KShell>
14
15#include <QPointer>
16
17class KIO::CommandLauncherJobPrivate
18{
19public:
20 QString m_command;
21 QString m_desktopName;
22 QString m_executable;
23 QString m_workingDirectory;
24 QStringList m_arguments;
25 QByteArray m_startupId;
26 QPointer<KProcessRunner> m_processRunner;
28 qint64 m_pid = 0;
29};
30
32 : KJob(parent)
33 , d(new CommandLauncherJobPrivate())
34{
35 d->m_command = command;
36}
37
39 : KJob(parent)
40 , d(new CommandLauncherJobPrivate())
41{
42 d->m_executable = executable;
43 d->m_arguments = args;
44}
45
47{
48 // Do *NOT* delete the KProcessRunner instances here.
49 // We need it to keep running so it can terminate startup notification on process exit.
50}
51
53{
54 d->m_command = command;
55}
56
58{
59 if (d->m_command.isEmpty()) {
60 return KShell::quoteArg(d->m_executable) + QLatin1Char(' ') + KShell::joinArgs(d->m_arguments);
61 }
62 return d->m_command;
63}
64
66{
67 d->m_executable = executable;
68}
69
71{
72 d->m_desktopName = desktopName;
73}
74
76{
77 d->m_startupId = startupId;
78}
79
81{
82 d->m_workingDirectory = workingDirectory;
83}
84
86{
87 return d->m_workingDirectory;
88}
89
91{
92 d->m_environment = environment;
93}
94
96{
97 // Some fallback for lazy callers, not 100% accurate though
98 if (d->m_executable.isEmpty()) {
99 const QStringList args = KShell::splitArgs(d->m_command);
100 if (!args.isEmpty()) {
101 d->m_executable = args.first();
102 }
103 }
104
105 if (d->m_executable.isEmpty()) {
106 setError(KJob::UserDefinedError);
107 setErrorText(i18nc("An error message", "Empty command provided"));
108 emitResult();
109 return;
110 }
111
112 QString displayName = d->m_executable;
113 KService::Ptr service = KService::serviceByDesktopName(d->m_desktopName);
114 if (service) {
115 displayName = service->name();
116 }
117 Q_EMIT description(this, i18nc("Launching application", "Launching %1", displayName), {}, {});
118
119 if (d->m_command.isEmpty() && !d->m_executable.isEmpty()) {
120 d->m_processRunner =
121 KProcessRunner::fromExecutable(d->m_executable, d->m_arguments, d->m_desktopName, d->m_startupId, d->m_workingDirectory, d->m_environment);
122
123 if (!d->m_processRunner) {
124 setError(KIO::ERR_DOES_NOT_EXIST);
125 setErrorText(d->m_executable);
126 emitResult();
127 return;
128 }
129 } else {
130 d->m_processRunner =
131 KProcessRunner::fromCommand(d->m_command, d->m_desktopName, d->m_executable, d->m_startupId, d->m_workingDirectory, d->m_environment);
132 }
133 connect(d->m_processRunner, &KProcessRunner::error, this, [this](const QString &errorText) {
134 setError(KJob::UserDefinedError);
135 setErrorText(errorText);
136 emitResult();
137 });
138 connect(d->m_processRunner, &KProcessRunner::processStarted, this, [this](qint64 pid) {
139 d->m_pid = pid;
140 emitResult();
141 });
142}
143
144bool KIO::CommandLauncherJob::waitForStarted()
145{
146 if (d->m_processRunner.isNull()) {
147 return false;
148 }
149 const bool ret = d->m_processRunner->waitForStarted();
150 if (!d->m_processRunner.isNull()) {
151 qApp->sendPostedEvents(d->m_processRunner); // so slotStarted gets called
152 }
153 return ret;
154}
155
157{
158 return d->m_pid;
159}
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 Fri Dec 27 2024 11:48:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.