Commit 04d6142d authored by Volker Krause's avatar Volker Krause
Browse files

implement precommands

svn path=/trunk/KDE/kdepim/mailtransport/; revision=620543
parent 3cd08b15
......@@ -12,6 +12,7 @@ set(mailtransport_lib_srcs
transporttypedialog.cpp
sendmailjob.cpp
smtpjob.cpp
precommandjob.cpp
)
kde4_automoc(${mailtransport_lib_srcs})
......
......@@ -23,7 +23,6 @@ Transport
TransportJob
------------
- execute precommand
- use QByteArray for addresses instead of QString
SMTPJob
......
/*
Copyright (c) 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "precommandjob.h"
#include <klocale.h>
#include <kprocess.h>
using namespace KPIM;
PrecommandJob::PrecommandJob(const QString & precommand, QObject * parent) :
KJob( parent ),
mProcess( 0 ),
mPrecommand( precommand )
{
mProcess = new KProcess( this );
mProcess->setUseShell( true );
*mProcess << precommand;
connect( mProcess, SIGNAL(processExited(KProcess*)), SLOT(processExited(KProcess*)) );
}
PrecommandJob::~ PrecommandJob()
{
delete mProcess;
}
void PrecommandJob::start()
{
if ( !mProcess->start( KProcess::NotifyOnExit ) ) {
setError( UserDefinedError );
setErrorText( i18n("Could not execute precommand '%1'.") );
emitResult();
} else {
emit infoMessage( this, i18n("Executing precommand"),
i18n("Executing precommand '%1'.", mPrecommand ) );
}
}
bool PrecommandJob::doKill()
{
delete mProcess;
mProcess = 0;
return true;
}
void PrecommandJob::processExited(KProcess *process)
{
Q_ASSERT( mProcess == process );
if ( mProcess->normalExit() ) {
if ( mProcess->exitStatus() ) {
setError( UserDefinedError );
setErrorText( i18n("The precommand exited with code %1.", mProcess->exitStatus()) );
}
}
if ( mProcess->signalled() ) {
setError( UserDefinedError );
setErrorText( i18n("The precommand was terminated by signal %1", mProcess->exitSignal() ) );
}
emitResult();
}
#include "precommandjob.moc"
/*
Copyright (c) 2007 Volker Krause <vkrause@kde.org>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#ifndef KPIM_PRECOMMANDJOB_H
#define KPIM_PRECOMMANDJOB_H
#include <kjob.h>
class KProcess;
namespace KPIM {
/**
Job to execute commands before connecting to an account.
*/
class PrecommandJob : public KJob
{
Q_OBJECT
public:
/**
Creates a new precommand job.
@param precommand The command to run.
@param parent The parent object.
*/
PrecommandJob( const QString &precommand, QObject *parent = 0 );
/**
Destroys this job.
*/
virtual ~PrecommandJob();
virtual void start();
protected:
virtual bool doKill();
private slots:
void processExited(KProcess *process);
private:
KProcess *mProcess;
QString mPrecommand;
};
}
#endif
......@@ -37,6 +37,11 @@ SendmailJob::SendmailJob(Transport * transport, QObject * parent) :
SLOT(receivedStdErr(KProcess*,char*,int)) );
}
SendmailJob::~ SendmailJob()
{
delete mProcess;
}
void SendmailJob::doStart()
{
*mProcess << transport()->host() << "-i" << "-f" << sender() << to() << cc() << bcc();
......
......@@ -40,6 +40,11 @@ class MAILTRANSPORT_EXPORT SendmailJob : public TransportJob
*/
SendmailJob( Transport* transport, QObject* parent = 0 );
/**
Destroys this job.
*/
virtual ~SendmailJob();
protected:
virtual void doStart();
virtual bool doKill();
......
......@@ -20,6 +20,7 @@
#include "smtpjob.h"
#include "transport.h"
#include "mailtransport_defs.h"
#include "precommandjob.h"
#include <klocale.h>
#include <kurl.h>
......@@ -53,7 +54,8 @@ static void removeSlaveFromPool( KIO::Slave *slave, bool disconnect = false )
SmtpJob::SmtpJob(Transport * transport, QObject * parent) :
TransportJob( transport, parent ),
mSlave( 0 )
mSlave( 0 ),
state( Idle )
{
slavePoolRef++;
KIO::Scheduler::connect( SIGNAL(slaveError(KIO::Slave*,int,QString)),
......@@ -72,6 +74,19 @@ SmtpJob::~SmtpJob()
}
void SmtpJob::doStart()
{
if ( slavePool.contains( transport()->id() ) || transport()->precommand().isEmpty() ) {
state = Smtp;
startSmtpJob();
} else {
state = Precommand;
PrecommandJob *job = new PrecommandJob( transport()->precommand(), this );
addSubjob( job );
job->start();
}
}
void SmtpJob::startSmtpJob()
{
QString query = "headers=0&from=";
query += KUrl::toPercentEncoding( sender() );
......@@ -169,16 +184,29 @@ bool SmtpJob::doKill()
{
if ( !hasSubjobs() )
return true;
KIO::SimpleJob *job = static_cast<KIO::SimpleJob*>( subjobs().first() );
clearSubjobs();
KIO::Scheduler::cancelJob( job );
return true;
if ( state == Precommand )
return subjobs().first()->kill();
else if ( state == Smtp ) {
KIO::SimpleJob *job = static_cast<KIO::SimpleJob*>( subjobs().first() );
clearSubjobs();
KIO::Scheduler::cancelJob( job );
return true;
}
return false;
}
void SmtpJob::slotResult(KJob * job)
{
TransportJob::slotResult( job );
removeSlaveFromPool( mSlave, error() != KIO::ERR_SLAVE_DIED );
if ( error() && state == Smtp ) {
removeSlaveFromPool( mSlave, error() != KIO::ERR_SLAVE_DIED );
return;
}
if ( !error() && state == Precommand ) {
state = Smtp;
startSmtpJob();
return;
}
if ( !error() )
emitResult();
}
......
......@@ -35,6 +35,9 @@ namespace KPIM {
KIO::Slave. This avoids multiple simultaneous connections to the server,
which is not always allowed. Also, re-using an already existing connection
avoids the login overhead and can improve performance.
Precommands are automatically executed, once per opening a connection to the
server (not necessarily once per message).
*/
class MAILTRANSPORT_EXPORT SmtpJob : public TransportJob
{
......@@ -60,11 +63,17 @@ class MAILTRANSPORT_EXPORT SmtpJob : public TransportJob
virtual void slotResult( KJob *job );
void slaveError(KIO::Slave *slave, int errorCode, const QString &errorMsg);
private:
void startSmtpJob();
private slots:
void dataRequest( KIO::Job* job, QByteArray &data );
private:
KIO::Slave* mSlave;
enum State {
Idle, Precommand, Smtp
} state;
};
}
......
......@@ -87,6 +87,7 @@ void TransportMgr::sendBtnClicked()
job->setData( mMailEdit->document()->toPlainText().toLatin1() );
connect( job, SIGNAL(result(KJob*)), SLOT(jobResult(KJob*)) );
connect( job, SIGNAL(percent(KJob*,unsigned long)), SLOT(jobPercent(KJob*,unsigned long)) );
connect( job, SIGNAL(infoMessage(KJob*,QString,QString)), SLOT(jobInfoMessage(KJob*,QString,QString)) );
mCurrentJob = job;
TransportManager::self()->schedule( job );
}
......@@ -120,5 +121,12 @@ void TransportMgr::jobPercent(KJob * job, unsigned long percent)
kDebug() << k_funcinfo << percent << "%" << endl;
}
void TransportMgr::jobInfoMessage(KJob * job, const QString & info, const QString & info2)
{
Q_UNUSED( job );
kDebug() << k_funcinfo << info << endl;
kDebug() << k_funcinfo << info2 << endl;
}
#include "transportmgr.moc"
......@@ -40,6 +40,7 @@ class TransportMgr : public KVBox
void cancelBtnClicked();
void jobResult( KJob* job );
void jobPercent( KJob* job, unsigned long percent );
void jobInfoMessage( KJob* job, const QString &info, const QString &info2 );
private:
KPIM::TransportComboBox* mComboBox;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment