kleopatra
encryptemailtask.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include <config-kleopatra.h>
00034
00035 #include "encryptemailtask.h"
00036
00037 #include <utils/input.h>
00038 #include <utils/output.h>
00039 #include <utils/stl_util.h>
00040 #include <utils/kleo_assert.h>
00041
00042 #include <kleo/cryptobackendfactory.h>
00043 #include <kleo/cryptobackend.h>
00044 #include <kleo/encryptjob.h>
00045
00046 #include <gpgme++/encryptionresult.h>
00047 #include <gpgme++/key.h>
00048
00049 #include <KLocale>
00050
00051 #include <QIcon>
00052 #include <QPointer>
00053 #include <QTextDocument>
00054
00055 #include <boost/bind.hpp>
00056
00057 using namespace Kleo;
00058 using namespace Kleo::Crypto;
00059 using namespace boost;
00060 using namespace GpgME;
00061
00062 namespace {
00063
00064 class EncryptEMailResult : public Task::Result {
00065 const EncryptionResult m_result;
00066 const QString m_auditLog;
00067 public:
00068 EncryptEMailResult( const EncryptionResult & r, const QString & auditLog )
00069 : Task::Result(), m_result( r ), m_auditLog( auditLog ) {}
00070
00071 QString overview() const;
00072 QString details() const;
00073 int errorCode() const;
00074 QString errorString() const;
00075 VisualCode code() const;
00076 QString auditLogAsHtml() const;
00077 };
00078
00079 QString makeResultString( const EncryptionResult& res )
00080 {
00081 const Error err = res.error();
00082
00083 if ( err.isCanceled() )
00084 return i18n( "Encryption canceled." );
00085
00086 if ( err )
00087 return i18n( "Encryption failed: %1", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
00088
00089 return i18n( "Encryption succeeded." );
00090 }
00091
00092 }
00093
00094 class EncryptEMailTask::Private {
00095 friend class ::Kleo::Crypto::EncryptEMailTask;
00096 EncryptEMailTask * const q;
00097 public:
00098 explicit Private( EncryptEMailTask * qq );
00099
00100 private:
00101 std::auto_ptr<Kleo::EncryptJob> createJob( GpgME::Protocol proto );
00102
00103 private:
00104 void slotResult( const EncryptionResult & );
00105
00106 private:
00107 shared_ptr<Input> input;
00108 shared_ptr<Output> output;
00109 std::vector<Key> recipients;
00110
00111 QPointer<Kleo::EncryptJob> job;
00112 };
00113
00114 EncryptEMailTask::Private::Private( EncryptEMailTask * qq )
00115 : q( qq ),
00116 input(),
00117 output(),
00118 job( 0 )
00119 {
00120
00121 }
00122
00123 EncryptEMailTask::EncryptEMailTask( QObject * p )
00124 : Task( p ), d( new Private( this ) )
00125 {
00126
00127 }
00128
00129 EncryptEMailTask::~EncryptEMailTask() {}
00130
00131 void EncryptEMailTask::setInput( const shared_ptr<Input> & input ) {
00132 kleo_assert( !d->job );
00133 kleo_assert( input );
00134 d->input = input;
00135 }
00136
00137 void EncryptEMailTask::setOutput( const shared_ptr<Output> & output ) {
00138 kleo_assert( !d->job );
00139 kleo_assert( output );
00140 d->output = output;
00141 }
00142
00143 void EncryptEMailTask::setRecipients( const std::vector<Key> & recipients ) {
00144 kleo_assert( !d->job );
00145 kleo_assert( !recipients.empty() );
00146 d->recipients = recipients;
00147 }
00148
00149 Protocol EncryptEMailTask::protocol() const {
00150 kleo_assert( !d->recipients.empty() );
00151 return d->recipients.front().protocol();
00152 }
00153
00154 QString EncryptEMailTask::label() const
00155 {
00156 return d->input ? d->input->label() : QString();
00157 }
00158
00159 unsigned long long EncryptEMailTask::inputSize() const {
00160 return d->input ? d->input->size() : 0;
00161 }
00162
00163 void EncryptEMailTask::doStart() {
00164 kleo_assert( !d->job );
00165 kleo_assert( d->input );
00166 kleo_assert( d->output );
00167 kleo_assert( !d->recipients.empty() );
00168
00169 std::auto_ptr<Kleo::EncryptJob> job = d->createJob( protocol() );
00170 kleo_assert( job.get() );
00171
00172 job->start( d->recipients,
00173 d->input->ioDevice(), d->output->ioDevice(),
00174 true );
00175
00176 d->job = job.release();
00177 }
00178
00179 void EncryptEMailTask::cancel() {
00180 if ( d->job )
00181 d->job->slotCancel();
00182 }
00183
00184 std::auto_ptr<Kleo::EncryptJob> EncryptEMailTask::Private::createJob( GpgME::Protocol proto ) {
00185 const CryptoBackend::Protocol * const backend = CryptoBackendFactory::instance()->protocol( proto );
00186 kleo_assert( backend );
00187 std::auto_ptr<Kleo::EncryptJob> encryptJob( backend->encryptJob( proto == OpenPGP || q->asciiArmor(), false ) );
00188 kleo_assert( encryptJob.get() );
00189 if ( proto == CMS && !q->asciiArmor() )
00190 encryptJob->setOutputIsBase64Encoded( true );
00191 connect( encryptJob.get(), SIGNAL(progress(QString,int,int)),
00192 q, SLOT(setProgress(QString,int,int)) );
00193 connect( encryptJob.get(), SIGNAL(result(GpgME::EncryptionResult,QByteArray)),
00194 q, SLOT(slotResult(GpgME::EncryptionResult)) );
00195 return encryptJob;
00196 }
00197
00198 void EncryptEMailTask::Private::slotResult( const EncryptionResult & result ) {
00199 const Job * const job = qobject_cast<const Job*>( q->sender() );
00200 if ( result.error().code() ) {
00201 output->cancel();
00202 } else {
00203 output->finalize();
00204 }
00205 q->emitResult( shared_ptr<Result>( new EncryptEMailResult( result, job ? job->auditLogAsHtml() : QString() ) ) );
00206 }
00207
00208 QString EncryptEMailResult::overview() const {
00209 return makeOverview( makeResultString( m_result ) );
00210 }
00211
00212 QString EncryptEMailResult::details() const {
00213 return QString();
00214 }
00215
00216 int EncryptEMailResult::errorCode() const {
00217 return m_result.error().encodedError();
00218 }
00219
00220 QString EncryptEMailResult::errorString() const {
00221 return hasError() ? makeResultString( m_result ) : QString();
00222 }
00223
00224 QString EncryptEMailResult::auditLogAsHtml() const {
00225 return m_auditLog;
00226 }
00227
00228 Task::Result::VisualCode EncryptEMailResult::code() const
00229 {
00230 if ( m_result.error().isCanceled() )
00231 return Warning;
00232 return m_result.error().code() ? NeutralError : NeutralSuccess;
00233 }
00234
00235
00236 #include "moc_encryptemailtask.cpp"
00237
00238