• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdepim
  • Sitemap
  • Contact Us
 

kleopatra

encryptemailtask.cpp

Go to the documentation of this file.
00001 /* -*- mode: c++; c-basic-offset:4 -*-
00002     crypto/encryptemailtask.cpp
00003 
00004     This file is part of Kleopatra, the KDE keymanager
00005     Copyright (c) 2007 Klarälvdalens Datakonsult AB
00006 
00007     Kleopatra is free software; you can redistribute it and/or modify
00008     it under the terms of the GNU General Public License as published by
00009     the Free Software Foundation; either version 2 of the License, or
00010     (at your option) any later version.
00011 
00012     Kleopatra is distributed in the hope that it will be useful,
00013     but WITHOUT ANY WARRANTY; without even the implied warranty of
00014     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015     General Public License for more details.
00016 
00017     You should have received a copy of the GNU General Public License
00018     along with this program; if not, write to the Free Software
00019     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00020 
00021     In addition, as a special exception, the copyright holders give
00022     permission to link the code of this program with any edition of
00023     the Qt library by Trolltech AS, Norway (or with modified versions
00024     of Qt that use the same license as Qt), and distribute linked
00025     combinations including the two.  You must obey the GNU General
00026     Public License in all respects for all of the code used other than
00027     Qt.  If you modify this file, you may extend this exception to
00028     your version of the file, but you are not obligated to do so.  If
00029     you do not wish to do so, delete this exception statement from
00030     your version.
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> // for Qt::escape
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         /* reimp */ QString overview() const;
00072         /* reimp */ QString details() const;
00073         /* reimp */ int errorCode() const;
00074         /* reimp */ QString errorString() const;
00075         /* reimp */ VisualCode code() const;
00076         /* reimp */ 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                 /*alwaysTrust=*/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( /*armor=*/proto == OpenPGP || q->asciiArmor(), /*textmode=*/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 

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

kdepim

Skip menu "kdepim"
  • akonadi
  •   clients
  •   kabc
  •   kcal
  •   kcm
  • akregator
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt
  • kdgantt1
  • kjots
  • kleopatra
  • kmail
  • kmobiletools
  • knode
  • knotes
  • kontact
  • kontactinterfaces
  • korganizer
  •   korgac
  • kpilot
  • ktimetracker
  • libkdepim
  • libkholidays
  • libkleo
  • libkpgp
  • maildir
Generated for kdepim by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal