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

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • crypto
encryptemailtask.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  crypto/encryptemailtask.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2007 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "encryptemailtask.h"
36 
37 #include <utils/input.h>
38 #include <utils/output.h>
39 #include <utils/kleo_assert.h>
40 #include <utils/auditlog.h>
41 
42 #include <kleo/stl_util.h>
43 #include <kleo/cryptobackendfactory.h>
44 #include <kleo/cryptobackend.h>
45 #include <kleo/encryptjob.h>
46 
47 #include <gpgme++/encryptionresult.h>
48 #include <gpgme++/key.h>
49 
50 #include <KLocalizedString>
51 
52 #include <QPointer>
53 #include <QTextDocument> // for Qt::escape
54 
55 #ifndef Q_MOC_RUN
56 #include <boost/bind.hpp>
57 #endif
58 
59 using namespace Kleo;
60 using namespace Kleo::Crypto;
61 using namespace boost;
62 using namespace GpgME;
63 
64 namespace {
65 
66  class EncryptEMailResult : public Task::Result {
67  const EncryptionResult m_result;
68  const AuditLog m_auditLog;
69  public:
70  EncryptEMailResult( const EncryptionResult & r, const AuditLog & auditLog )
71  : Task::Result(), m_result( r ), m_auditLog( auditLog ) {}
72 
73  /* reimp */ QString overview() const;
74  /* reimp */ QString details() const;
75  /* reimp */ int errorCode() const;
76  /* reimp */ QString errorString() const;
77  /* reimp */ VisualCode code() const;
78  /* reimp */ AuditLog auditLog() const;
79  };
80 
81  QString makeResultString( const EncryptionResult& res )
82  {
83  const Error err = res.error();
84 
85  if ( err.isCanceled() )
86  return i18n( "Encryption canceled." );
87 
88  if ( err )
89  return i18n( "Encryption failed: %1", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
90 
91  return i18n( "Encryption succeeded." );
92  }
93 
94 }
95 
96 class EncryptEMailTask::Private {
97  friend class ::Kleo::Crypto::EncryptEMailTask;
98  EncryptEMailTask * const q;
99 public:
100  explicit Private( EncryptEMailTask * qq );
101 
102 private:
103  std::auto_ptr<Kleo::EncryptJob> createJob( GpgME::Protocol proto );
104 
105 private:
106  void slotResult( const EncryptionResult & );
107 
108 private:
109  shared_ptr<Input> input;
110  shared_ptr<Output> output;
111  std::vector<Key> recipients;
112 
113  QPointer<Kleo::EncryptJob> job;
114 };
115 
116 EncryptEMailTask::Private::Private( EncryptEMailTask * qq )
117  : q( qq ),
118  input(),
119  output(),
120  job( 0 )
121 {
122 
123 }
124 
125 EncryptEMailTask::EncryptEMailTask( QObject * p )
126  : Task( p ), d( new Private( this ) )
127 {
128 
129 }
130 
131 EncryptEMailTask::~EncryptEMailTask() {}
132 
133 void EncryptEMailTask::setInput( const shared_ptr<Input> & input ) {
134  kleo_assert( !d->job );
135  kleo_assert( input );
136  d->input = input;
137 }
138 
139 void EncryptEMailTask::setOutput( const shared_ptr<Output> & output ) {
140  kleo_assert( !d->job );
141  kleo_assert( output );
142  d->output = output;
143 }
144 
145 void EncryptEMailTask::setRecipients( const std::vector<Key> & recipients ) {
146  kleo_assert( !d->job );
147  kleo_assert( !recipients.empty() );
148  d->recipients = recipients;
149 }
150 
151 Protocol EncryptEMailTask::protocol() const {
152  kleo_assert( !d->recipients.empty() );
153  return d->recipients.front().protocol();
154 }
155 
156 QString EncryptEMailTask::label() const
157 {
158  return d->input ? d->input->label() : QString();
159 }
160 
161 unsigned long long EncryptEMailTask::inputSize() const {
162  return d->input ? d->input->size() : 0;
163 }
164 
165 void EncryptEMailTask::doStart() {
166  kleo_assert( !d->job );
167  kleo_assert( d->input );
168  kleo_assert( d->output );
169  kleo_assert( !d->recipients.empty() );
170 
171  std::auto_ptr<Kleo::EncryptJob> job = d->createJob( protocol() );
172  kleo_assert( job.get() );
173 
174  job->start( d->recipients,
175  d->input->ioDevice(), d->output->ioDevice(),
176  /*alwaysTrust=*/true );
177 
178  d->job = job.release();
179 }
180 
181 void EncryptEMailTask::cancel() {
182  if ( d->job )
183  d->job->slotCancel();
184 }
185 
186 std::auto_ptr<Kleo::EncryptJob> EncryptEMailTask::Private::createJob( GpgME::Protocol proto ) {
187  const CryptoBackend::Protocol * const backend = CryptoBackendFactory::instance()->protocol( proto );
188  kleo_assert( backend );
189  bool shouldArmor = ( proto == OpenPGP || q->asciiArmor() ) && !output->binaryOpt();
190  std::auto_ptr<Kleo::EncryptJob> encryptJob( backend->encryptJob( shouldArmor, /*textmode=*/false ) );
191  kleo_assert( encryptJob.get() );
192  if ( proto == CMS && !q->asciiArmor() && !output->binaryOpt() )
193  encryptJob->setOutputIsBase64Encoded( true );
194  connect( encryptJob.get(), SIGNAL(progress(QString,int,int)),
195  q, SLOT(setProgress(QString,int,int)) );
196  connect( encryptJob.get(), SIGNAL(result(GpgME::EncryptionResult,QByteArray)),
197  q, SLOT(slotResult(GpgME::EncryptionResult)) );
198  return encryptJob;
199 }
200 
201 void EncryptEMailTask::Private::slotResult( const EncryptionResult & result ) {
202  const Job * const job = qobject_cast<const Job*>( q->sender() );
203  if ( result.error().code() ) {
204  output->cancel();
205  } else {
206  output->finalize();
207  }
208  q->emitResult( shared_ptr<Result>( new EncryptEMailResult( result, AuditLog::fromJob( job ) ) ) );
209 }
210 
211 QString EncryptEMailResult::overview() const {
212  return makeOverview( makeResultString( m_result ) );
213 }
214 
215 QString EncryptEMailResult::details() const {
216  return QString();
217 }
218 
219 int EncryptEMailResult::errorCode() const {
220  return m_result.error().encodedError();
221 }
222 
223 QString EncryptEMailResult::errorString() const {
224  return hasError() ? makeResultString( m_result ) : QString();
225 }
226 
227 AuditLog EncryptEMailResult::auditLog() const {
228  return m_auditLog;
229 }
230 
231 Task::Result::VisualCode EncryptEMailResult::code() const
232 {
233  if ( m_result.error().isCanceled() )
234  return Warning;
235  return m_result.error().code() ? NeutralError : NeutralSuccess;
236 }
237 
238 
239 #include "moc_encryptemailtask.cpp"
240 
241 
auditlog.h
output.h
Kleo::Crypto::EncryptEMailTask::setInput
void setInput(const boost::shared_ptr< Input > &input)
Definition: encryptemailtask.cpp:133
input.h
QByteArray
Kleo::Crypto::Task::Result::VisualCode
VisualCode
Definition: task.h:129
Kleo::Crypto::EncryptEMailTask::label
QString label() const
Definition: encryptemailtask.cpp:156
Kleo::AuditLog
Definition: auditlog.h:48
Kleo::Crypto::EncryptEMailTask::protocol
GpgME::Protocol protocol() const
Definition: encryptemailtask.cpp:151
QPointer< Kleo::EncryptJob >
Kleo::Crypto::Task
Definition: task.h:57
kleo_assert.h
boost::shared_ptr
Definition: encryptemailcontroller.h:51
encryptemailtask.h
QString::fromLocal8Bit
QString fromLocal8Bit(const char *str, int size)
d
#define d
Definition: adduseridcommand.cpp:89
QObject
Kleo::Class::OpenPGP
Definition: classify.h:49
Kleo::Class::CMS
Definition: classify.h:48
Kleo::AuditLog::fromJob
static AuditLog fromJob(const Job *)
Definition: auditlog.cpp:45
QString
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:86
Kleo::Crypto::EncryptEMailTask::setRecipients
void setRecipients(const std::vector< GpgME::Key > &recipients)
Definition: encryptemailtask.cpp:145
Kleo::Crypto::Task::Result
Definition: task.h:119
Kleo::AuditLog::error
GpgME::Error error() const
Definition: auditlog.h:58
Qt::escape
QString escape(const QString &plain)
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Crypto::EncryptEMailTask::~EncryptEMailTask
~EncryptEMailTask()
Definition: encryptemailtask.cpp:131
Kleo::Crypto::EncryptEMailTask
Definition: encryptemailtask.h:60
Kleo::Crypto::EncryptEMailTask::cancel
void cancel()
Definition: encryptemailtask.cpp:181
Kleo::Crypto::EncryptEMailTask::EncryptEMailTask
EncryptEMailTask(QObject *parent=0)
Definition: encryptemailtask.cpp:125
Kleo::Crypto::EncryptEMailTask::setOutput
void setOutput(const boost::shared_ptr< Output > &output)
Definition: encryptemailtask.cpp:139
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:10 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

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

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal