Changeset View
Changeset View
Standalone View
Standalone View
src/gui/processlauncherjob.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | This file is part of the KDE libraries | ||||
3 | Copyright (c) 2020 David Faure <faure@kde.org> | ||||
4 | | ||||
5 | This library is free software; you can redistribute it and/or modify | ||||
6 | it under the terms of the GNU Lesser General Public License as published by | ||||
7 | the Free Software Foundation; either version 2 of the License or ( at | ||||
8 | your option ) version 3 or, at the discretion of KDE e.V. ( which shall | ||||
9 | act as a proxy as in section 14 of the GPLv3 ), any later version. | ||||
10 | | ||||
11 | This library is distributed in the hope that it will be useful, | ||||
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
14 | Library General Public License for more details. | ||||
15 | | ||||
16 | You should have received a copy of the GNU Lesser General Public License | ||||
17 | along with this library; see the file COPYING.LIB. If not, write to | ||||
18 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||||
19 | Boston, MA 02110-1301, USA. | ||||
20 | */ | ||||
21 | | ||||
22 | #include "processlauncherjob.h" | ||||
23 | #include "kprocessrunner_p.h" | ||||
24 | #include "kiogui_debug.h" | ||||
25 | | ||||
26 | class KIO::ProcessLauncherJobPrivate | ||||
27 | { | ||||
28 | public: | ||||
29 | ProcessLauncherJobPrivate(const KService::Ptr &service, WId windowId) | ||||
30 | : m_service(service), m_windowId(windowId) {} | ||||
31 | | ||||
32 | void slotStarted(KIO::ProcessLauncherJob *q, KProcessRunner *processRunner) { | ||||
33 | m_pids.append(processRunner->pid()); | ||||
34 | if (--m_numProcessesPending == 0) { | ||||
35 | q->emitResult(); | ||||
36 | } | ||||
37 | } | ||||
38 | const KService::Ptr m_service; | ||||
39 | const WId m_windowId; | ||||
40 | QList<QUrl> m_urls; | ||||
41 | KIO::ProcessLauncherJob::RunFlags m_runFlags; | ||||
42 | QString m_suggestedFileName; | ||||
43 | QByteArray m_startupId; | ||||
44 | QVector<qint64> m_pids; | ||||
45 | QVector<KProcessRunner *> m_processRunners; | ||||
46 | int m_numProcessesPending = 0; | ||||
47 | }; | ||||
48 | | ||||
49 | KIO::ProcessLauncherJob::ProcessLauncherJob(const KService::Ptr &service, WId windowId, QObject *parent) | ||||
50 | : KJob(parent), d(new ProcessLauncherJobPrivate(service, windowId)) | ||||
51 | { | ||||
52 | } | ||||
53 | | ||||
54 | KIO::ProcessLauncherJob::~ProcessLauncherJob() | ||||
55 | { | ||||
56 | // Do *NOT* delete the KProcessRunner instances here. | ||||
57 | // We need it to keep running so it can do terminate startup notification on process exit. | ||||
58 | } | ||||
59 | | ||||
60 | void KIO::ProcessLauncherJob::setUrls(const QList<QUrl> &urls) | ||||
61 | { | ||||
62 | d->m_urls = urls; | ||||
63 | } | ||||
64 | | ||||
65 | void KIO::ProcessLauncherJob::setRunFlags(RunFlags runFlags) | ||||
66 | { | ||||
67 | d->m_runFlags = runFlags; | ||||
68 | } | ||||
69 | | ||||
70 | void KIO::ProcessLauncherJob::setSuggestedFileName(const QString &suggestedFileName) | ||||
71 | { | ||||
72 | d->m_suggestedFileName = suggestedFileName; | ||||
73 | } | ||||
74 | | ||||
75 | void KIO::ProcessLauncherJob::setStartupId(const QByteArray &startupId) | ||||
76 | { | ||||
77 | d->m_startupId = startupId; | ||||
78 | } | ||||
79 | | ||||
80 | void KIO::ProcessLauncherJob::start() | ||||
81 | { | ||||
82 | if (d->m_urls.count() > 1 && !d->m_service->allowMultipleFiles()) { | ||||
83 | // We need to launch the application N times. | ||||
84 | // We ignore the result for application 2 to N. | ||||
85 | // For the first file we launch the application in the | ||||
86 | // usual way. The reported result is based on this application. | ||||
87 | d->m_numProcessesPending = d->m_urls.count(); | ||||
88 | d->m_processRunners.reserve(d->m_numProcessesPending); | ||||
89 | for (int i = 1; i < d->m_urls.count(); ++i) { | ||||
90 | auto *processRunner = new KProcessRunner(d->m_service, { d->m_urls.at(i) }, d->m_windowId, | ||||
91 | d->m_runFlags, d->m_suggestedFileName, QByteArray()); | ||||
92 | d->m_processRunners.push_back(processRunner); | ||||
93 | connect(processRunner, &KProcessRunner::processStarted, this, [this, processRunner]() { | ||||
94 | d->slotStarted(this, processRunner); | ||||
95 | }); | ||||
96 | } | ||||
97 | d->m_urls = { d->m_urls.at(0) }; | ||||
98 | } else { | ||||
99 | d->m_numProcessesPending = 1; | ||||
100 | } | ||||
101 | | ||||
102 | auto *processRunner = new KProcessRunner(d->m_service, d->m_urls, d->m_windowId, | ||||
103 | d->m_runFlags, d->m_suggestedFileName, d->m_startupId); | ||||
104 | d->m_processRunners.push_back(processRunner); | ||||
105 | connect(processRunner, &KProcessRunner::error, this, [this](const QString &errorText) { | ||||
106 | setError(KJob::UserDefinedError); | ||||
107 | setErrorText(errorText); | ||||
108 | emitResult(); | ||||
109 | }); | ||||
110 | connect(processRunner, &KProcessRunner::processStarted, this, [this, processRunner]() { | ||||
111 | d->slotStarted(this, processRunner); | ||||
112 | }); | ||||
113 | } | ||||
114 | | ||||
115 | bool KIO::ProcessLauncherJob::waitForStarted() | ||||
116 | { | ||||
117 | const bool ret = std::all_of(d->m_processRunners.cbegin(), | ||||
118 | d->m_processRunners.cend(), | ||||
119 | [](KProcessRunner *r) { return r->waitForStarted(); }); | ||||
120 | for (KProcessRunner *r : qAsConst(d->m_processRunners)) { | ||||
121 | qApp->sendPostedEvents(r); // so slotStarted gets called | ||||
122 | } | ||||
123 | return ret; | ||||
124 | } | ||||
125 | | ||||
126 | qint64 KIO::ProcessLauncherJob::pid() const | ||||
127 | { | ||||
128 | return d->m_pids.at(0); | ||||
129 | } | ||||
130 | | ||||
131 | QVector<qint64> KIO::ProcessLauncherJob::pids() const | ||||
132 | { | ||||
133 | return d->m_pids; | ||||
134 | } |