00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "sendmailjob.h"
00025 #include "transport.h"
00026
00027 #include <KLocalizedString>
00028
00029 #include <QtCore/QProcess>
00030 #include <QtCore/QBuffer>
00031
00032 using namespace MailTransport;
00033
00038 class SendMailJobPrivate
00039 {
00040 public:
00041 QProcess *process;
00042 QString lastError;
00043 };
00044
00045 SendmailJob::SendmailJob( Transport *transport, QObject *parent )
00046 : TransportJob( transport, parent ), d( new SendMailJobPrivate )
00047 {
00048 d->process = new QProcess( this );
00049 connect( d->process,
00050 SIGNAL(finished(int, QProcess::ExitStatus)),
00051 SLOT(sendmailExited(int, QProcess::ExitStatus)) );
00052 connect( d->process, SIGNAL(error(QProcess::ProcessError)),
00053 SLOT(receivedError()) );
00054 connect( d->process, SIGNAL(readyReadStandardError()),
00055 SLOT(receivedStdErr()) );
00056 }
00057
00058 SendmailJob::~ SendmailJob()
00059 {
00060 delete d;
00061 }
00062
00063 void SendmailJob::doStart()
00064 {
00065 QStringList arguments;
00066 arguments << QLatin1String( "-i" ) << QLatin1String( "-f" )
00067 << sender() << to() << cc() << bcc();
00068 d->process->start( transport()->host(), arguments );
00069
00070 if ( !d->process->waitForStarted() ) {
00071 setError( UserDefinedError );
00072 setErrorText( i18n( "Failed to execute mailer program %1", transport()->host() ) );
00073 emitResult();
00074 } else {
00075 d->process->write( buffer()->readAll() );
00076 d->process->closeWriteChannel();
00077 }
00078 }
00079
00080 void SendmailJob::sendmailExited( int exitCode, QProcess::ExitStatus exitStatus )
00081 {
00082 if ( exitStatus != 0 || exitCode != 0 ) {
00083 setError( UserDefinedError );
00084 if ( d->lastError.isEmpty() ) {
00085 setErrorText( i18n( "Sendmail exited abnormally." ) );
00086 } else {
00087 setErrorText( i18n( "Sendmail exited abnormally: %1", d->lastError ) );
00088 }
00089 }
00090 emitResult();
00091 }
00092
00093 void SendmailJob::receivedError()
00094 {
00095 d->lastError += d->process->errorString();
00096 }
00097
00098 void SendmailJob::receivedStdErr()
00099 {
00100 d->lastError += QLatin1String( d->process->readAllStandardError() );
00101 }
00102
00103 bool SendmailJob::doKill()
00104 {
00105 delete d->process;
00106 d->process = 0;
00107 return true;
00108 }
00109
00110 #include "sendmailjob.moc"