33 #include <config-kleopatra.h>
42 #include <kleo/stl_util.h>
43 #include <kleo/cryptobackendfactory.h>
44 #include <kleo/cryptobackend.h>
45 #include <kleo/encryptjob.h>
47 #include <gpgme++/encryptionresult.h>
48 #include <gpgme++/key.h>
54 #include <QTextDocument>
56 #include <boost/bind.hpp>
59 using namespace Kleo::Crypto;
60 using namespace boost;
61 using namespace GpgME;
66 const EncryptionResult m_result;
69 EncryptEMailResult(
const EncryptionResult & r,
const AuditLog & auditLog )
70 :
Task::Result(), m_result( r ), m_auditLog( auditLog ) {}
72 QString overview()
const;
73 QString details()
const;
74 int errorCode()
const;
75 QString errorString()
const;
76 VisualCode code()
const;
80 QString makeResultString(
const EncryptionResult& res )
82 const Error err = res.error();
84 if ( err.isCanceled() )
85 return i18n(
"Encryption canceled." );
88 return i18n(
"Encryption failed: %1", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
90 return i18n(
"Encryption succeeded." );
95 class EncryptEMailTask::Private {
96 friend class ::Kleo::Crypto::EncryptEMailTask;
102 std::auto_ptr<Kleo::EncryptJob> createJob( GpgME::Protocol proto );
105 void slotResult(
const EncryptionResult & );
110 std::vector<Key> recipients;
112 QPointer<Kleo::EncryptJob> job;
125 :
Task( p ),
d( new Private( this ) )
147 d->recipients = recipients;
152 return d->recipients.front().protocol();
157 return d->input ? d->input->label() : QString();
160 unsigned long long EncryptEMailTask::inputSize()
const {
161 return d->input ? d->input->size() : 0;
164 void EncryptEMailTask::doStart() {
170 std::auto_ptr<Kleo::EncryptJob> job = d->createJob(
protocol() );
173 job->start( d->recipients,
174 d->input->ioDevice(), d->output->ioDevice(),
177 d->job = job.release();
182 d->job->slotCancel();
185 std::auto_ptr<Kleo::EncryptJob> EncryptEMailTask::Private::createJob( GpgME::Protocol proto ) {
186 const CryptoBackend::Protocol *
const backend = CryptoBackendFactory::instance()->protocol( proto );
188 bool shouldArmor = ( proto ==
OpenPGP ||
q->asciiArmor() ) && !output->binaryOpt();
189 std::auto_ptr<Kleo::EncryptJob> encryptJob( backend->encryptJob( shouldArmor,
false ) );
191 if ( proto ==
CMS && !
q->asciiArmor() && !output->binaryOpt() )
192 encryptJob->setOutputIsBase64Encoded(
true );
193 connect( encryptJob.get(), SIGNAL(progress(QString,
int,
int)),
194 q, SLOT(setProgress(QString,
int,
int)) );
195 connect( encryptJob.get(), SIGNAL(result(GpgME::EncryptionResult,QByteArray)),
196 q, SLOT(slotResult(GpgME::EncryptionResult)) );
200 void EncryptEMailTask::Private::slotResult(
const EncryptionResult & result ) {
201 const Job *
const job = qobject_cast<
const Job*>(
q->sender() );
202 if ( result.error().code() ) {
210 QString EncryptEMailResult::overview()
const {
211 return makeOverview( makeResultString( m_result ) );
214 QString EncryptEMailResult::details()
const {
218 int EncryptEMailResult::errorCode()
const {
219 return m_result.error().encodedError();
222 QString EncryptEMailResult::errorString()
const {
223 return hasError() ? makeResultString( m_result ) : QString();
226 AuditLog EncryptEMailResult::auditLog()
const {
232 if ( m_result.error().isCanceled() )
234 return m_result.
error().code() ? NeutralError : NeutralSuccess;
238 #include "moc_encryptemailtask.cpp"
void setInput(const boost::shared_ptr< Input > &input)
GpgME::Protocol protocol() const
static AuditLog fromJob(const Job *)
#define kleo_assert(cond)
void setRecipients(const std::vector< GpgME::Key > &recipients)
GpgME::Error error() const
EncryptEMailTask(QObject *parent=0)
void setOutput(const boost::shared_ptr< Output > &output)