1 | /* |
2 | Copyright (c) 2007 Volker Krause <vkrause@kde.org> |
3 | Copyright (c) 2007 KovoKs <kovoks@kovoks.nl> |
4 | |
5 | Based on KMail code by: |
6 | Copyright (c) 1996-1998 Stefan Taferner <taferner@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 "sendmailjob.h" |
25 | #include "transport.h" |
26 | |
27 | #include <KLocalizedString> |
28 | |
29 | #include <QtCore/QProcess> |
30 | #include <QtCore/QBuffer> |
31 | |
32 | using namespace MailTransport; |
33 | |
34 | /** |
35 | * Private class that helps to provide binary compatibility between releases. |
36 | * @internal |
37 | */ |
38 | class SendMailJobPrivate |
39 | { |
40 | public: |
41 | QProcess *process; |
42 | QString lastError; |
43 | }; |
44 | |
45 | SendmailJob::SendmailJob( Transport *transport, QObject *parent ) |
46 | : TransportJob( transport, parent ), d( new SendMailJobPrivate ) |
47 | { |
48 | d->process = new QProcess( this ); |
49 | connect( d->process, |
50 | SIGNAL(finished(int,QProcess::ExitStatus)), |
51 | SLOT(sendmailExited(int,QProcess::ExitStatus)) ); |
52 | connect( d->process, SIGNAL(error(QProcess::ProcessError)), |
53 | SLOT(receivedError()) ); |
54 | connect( d->process, SIGNAL(readyReadStandardError()), |
55 | SLOT(receivedStdErr()) ); |
56 | } |
57 | |
58 | SendmailJob::~ SendmailJob() |
59 | { |
60 | delete d; |
61 | } |
62 | |
63 | void SendmailJob::doStart() |
64 | { |
65 | QStringList arguments; |
66 | arguments << QLatin1String( "-i" ) << QLatin1String( "-f" ) |
67 | << sender() << to() << cc() << bcc(); |
68 | d->process->start( transport()->host(), arguments ); |
69 | |
70 | if ( !d->process->waitForStarted() ) { |
71 | setError( UserDefinedError ); |
72 | setErrorText( i18n( "Failed to execute mailer program %1" , transport()->host() ) ); |
73 | emitResult(); |
74 | } else { |
75 | d->process->write( buffer()->readAll() ); |
76 | d->process->closeWriteChannel(); |
77 | } |
78 | } |
79 | |
80 | void SendmailJob::sendmailExited( int exitCode, QProcess::ExitStatus exitStatus ) |
81 | { |
82 | if ( exitStatus != 0 || exitCode != 0 ) { |
83 | setError( UserDefinedError ); |
84 | if ( d->lastError.isEmpty() ) { |
85 | setErrorText( i18n( "Sendmail exited abnormally." ) ); |
86 | } else { |
87 | setErrorText( i18n( "Sendmail exited abnormally: %1" , d->lastError ) ); |
88 | } |
89 | } |
90 | emitResult(); |
91 | } |
92 | |
93 | void SendmailJob::receivedError() |
94 | { |
95 | d->lastError += d->process->errorString(); |
96 | } |
97 | |
98 | void SendmailJob::receivedStdErr() |
99 | { |
100 | d->lastError += QLatin1String( d->process->readAllStandardError() ); |
101 | } |
102 | |
103 | bool SendmailJob::doKill() |
104 | { |
105 | delete d->process; |
106 | d->process = 0; |
107 | return true; |
108 | } |
109 | |
110 | |