1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3
4 Based on KMail code by:
5 Copyright (c) 1996-1998 Stefan Taferner <taferner@kde.org>
6 Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
7
8 This library is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 This library is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to the
20 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
22*/
23
24#include "precommandjob.h"
25
26#include <KDebug>
27#include <KLocalizedString>
28
29#include <QProcess>
30
31using namespace MailTransport;
32
33/**
34 * Private class that helps to provide binary compatibility between releases.
35 * @internal
36 */
37class PreCommandJobPrivate
38{
39 public:
40 PreCommandJobPrivate( PrecommandJob *parent );
41 QProcess *process;
42 QString precommand;
43 PrecommandJob *q;
44
45 // Slots
46 void slotFinished( int, QProcess::ExitStatus );
47 void slotStarted();
48 void slotError( QProcess::ProcessError error );
49};
50
51PreCommandJobPrivate::PreCommandJobPrivate( PrecommandJob *parent )
52 : process( 0 ), q( parent )
53{
54}
55
56PrecommandJob::PrecommandJob( const QString &precommand, QObject *parent )
57 : KJob( parent ), d( new PreCommandJobPrivate( this ) )
58{
59 d->precommand = precommand;
60 d->process = new QProcess( this );
61 connect( d->process, SIGNAL(started()), SLOT(slotStarted()) );
62 connect( d->process, SIGNAL(error(QProcess::ProcessError)),
63 SLOT(slotError(QProcess::ProcessError)) );
64 connect( d->process, SIGNAL(finished(int,QProcess::ExitStatus)),
65 SLOT(slotFinished(int,QProcess::ExitStatus)) );
66}
67
68PrecommandJob::~ PrecommandJob()
69{
70 delete d;
71}
72
73void PrecommandJob::start()
74{
75 d->process->start( d->precommand );
76}
77
78void PreCommandJobPrivate::slotStarted()
79{
80 emit q->infoMessage( q, i18n( "Executing precommand" ),
81 i18n( "Executing precommand '%1'.", precommand ) );
82}
83
84void PreCommandJobPrivate::slotError( QProcess::ProcessError error )
85{
86 q->setError( KJob::UserDefinedError );
87 if ( error == QProcess::FailedToStart ) {
88 q->setErrorText( i18n( "Unable to start precommand '%1'.", precommand ) );
89 } else {
90 q->setErrorText( i18n( "Error while executing precommand '%1'.", precommand ) );
91 }
92 q->emitResult();
93}
94
95bool PrecommandJob::doKill()
96{
97 delete d->process;
98 d->process = 0;
99 return true;
100}
101
102void PreCommandJobPrivate::slotFinished( int exitCode, QProcess::ExitStatus exitStatus )
103{
104 if ( exitStatus == QProcess::CrashExit ) {
105 q->setError( KJob::UserDefinedError );
106 q->setErrorText( i18n( "The precommand crashed." ) );
107 } else if ( exitCode != 0 ) {
108 q->setError( KJob::UserDefinedError );
109 q->setErrorText( i18n( "The precommand exited with code %1.",
110 process->exitStatus() ) );
111 }
112 q->emitResult();
113}
114
115#include "moc_precommandjob.cpp"
116