MailTransport

precommandjob.cpp
1/*
2 SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
3
4 Based on KMail code by:
5 SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
6 SPDX-FileCopyrightText: 2000-2002 Michael Haeckel <haeckel@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.0-or-later
9*/
10
11#include "precommandjob.h"
12
13#include <KLocalizedString>
14
15#include <QProcess>
16
17using namespace MailTransport;
18
19/**
20 * Private class that helps to provide binary compatibility between releases.
21 * @internal
22 */
23class PreCommandJobPrivate
24{
25public:
26 explicit PreCommandJobPrivate(PrecommandJob *parent);
27 QProcess *process = nullptr;
28 QString precommand;
29 PrecommandJob *const q;
30
31 // Slots
32 void slotFinished(int, QProcess::ExitStatus);
33 void slotStarted();
34 void slotError(QProcess::ProcessError error);
35};
36
37PreCommandJobPrivate::PreCommandJobPrivate(PrecommandJob *parent)
38 : q(parent)
39{
40}
41
42PrecommandJob::PrecommandJob(const QString &precommand, QObject *parent)
43 : KJob(parent)
44 , d(new PreCommandJobPrivate(this))
45{
46 d->precommand = precommand;
47 d->process = new QProcess(this);
48 connect(d->process, &QProcess::started, this, [this]() {
49 d->slotStarted();
50 });
52 d->slotError(error);
53 });
54 connect(d->process, &QProcess::finished, this, [this](int val, QProcess::ExitStatus status) {
55 d->slotFinished(val, status);
56 });
57}
58
60
62{
63 d->process->start(d->precommand, QStringList());
64}
65
66void PreCommandJobPrivate::slotStarted()
67{
68 Q_EMIT q->infoMessage(q, i18n("Executing precommand '%1'.", precommand));
69}
70
71void PreCommandJobPrivate::slotError(QProcess::ProcessError error)
72{
73 q->setError(KJob::UserDefinedError);
74 if (error == QProcess::FailedToStart) {
75 q->setErrorText(i18n("Unable to start precommand '%1'.", precommand));
76 } else {
77 q->setErrorText(i18n("Error while executing precommand '%1'.", precommand));
78 }
79 q->emitResult();
80}
81
83{
84 delete d->process;
85 d->process = nullptr;
86 return true;
87}
88
89void PreCommandJobPrivate::slotFinished(int exitCode, QProcess::ExitStatus exitStatus)
90{
91 if (exitStatus == QProcess::CrashExit) {
92 q->setError(KJob::UserDefinedError);
93 q->setErrorText(i18n("The precommand crashed."));
94 } else if (exitCode != 0) {
95 q->setError(KJob::UserDefinedError);
96 q->setErrorText(i18n("The precommand exited with code %1.", process->exitStatus()));
97 }
98 q->emitResult();
99}
100
101#include "moc_precommandjob.cpp"
int error() const
Job to execute a command.
PrecommandJob(const QString &precommand, QObject *parent=nullptr)
Creates a new precommand job.
void start() override
Executes the precommand.
bool doKill() override
Reimplemented from KJob.
~PrecommandJob() override
Destroys this job.
Q_SCRIPTABLE CaptureState status()
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
T qobject_cast(QObject *object)
void finished(int exitCode)
void errorOccurred(QProcess::ProcessError error)
void started()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sun Feb 25 2024 18:37:24 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.