MailTransport

precommandjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2007 Volker Krause <[email protected]>
3 
4  Based on KMail code by:
5  SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <[email protected]>
6  SPDX-FileCopyrightText: 2000-2002 Michael Haeckel <[email protected]>
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 
17 using namespace MailTransport;
18 
19 /**
20  * Private class that helps to provide binary compatibility between releases.
21  * @internal
22  */
23 class PreCommandJobPrivate
24 {
25 public:
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 
37 PreCommandJobPrivate::PreCommandJobPrivate(PrecommandJob *parent)
38  : q(parent)
39 {
40 }
41 
42 PrecommandJob::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  });
51  connect(d->process, &QProcess::errorOccurred, this, [this](QProcess::ProcessError error) {
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 
66 void PreCommandJobPrivate::slotStarted()
67 {
68  Q_EMIT q->infoMessage(q, i18n("Executing precommand"), i18n("Executing precommand '%1'.", precommand));
69 }
70 
71 void 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 
89 void 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"
~PrecommandJob() override
Destroys this job.
bool doKill() override
Reimplemented from KJob.
void finished(int exitCode)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
Job to execute a command.
Definition: precommandjob.h:31
QString i18n(const char *text, const TYPE &arg...)
Q_SCRIPTABLE CaptureState status()
void start() override
Executes the precommand.
PrecommandJob(const QString &precommand, QObject *parent=nullptr)
Creates a new precommand job.
void started()
int error() const
void errorOccurred(QProcess::ProcessError error)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 6 2023 03:57:53 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.