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

kleopatra

  • sources
  • kde-4.12
  • 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 <KLocale>
51 
52 #include <QIcon>
53 #include <QPointer>
54 #include <QTextDocument> // for Qt::escape
55 
56 #include <boost/bind.hpp>
57 
58 using namespace Kleo;
59 using namespace Kleo::Crypto;
60 using namespace boost;
61 using namespace GpgME;
62 
63 namespace {
64 
65  class EncryptEMailResult : public Task::Result {
66  const EncryptionResult m_result;
67  const AuditLog m_auditLog;
68  public:
69  EncryptEMailResult( const EncryptionResult & r, const AuditLog & auditLog )
70  : Task::Result(), m_result( r ), m_auditLog( auditLog ) {}
71 
72  /* reimp */ QString overview() const;
73  /* reimp */ QString details() const;
74  /* reimp */ int errorCode() const;
75  /* reimp */ QString errorString() const;
76  /* reimp */ VisualCode code() const;
77  /* reimp */ AuditLog auditLog() const;
78  };
79 
80  QString makeResultString( const EncryptionResult& res )
81  {
82  const Error err = res.error();
83 
84  if ( err.isCanceled() )
85  return i18n( "Encryption canceled." );
86 
87  if ( err )
88  return i18n( "Encryption failed: %1", Qt::escape( QString::fromLocal8Bit( err.asString() ) ) );
89 
90  return i18n( "Encryption succeeded." );
91  }
92 
93 }
94 
95 class EncryptEMailTask::Private {
96  friend class ::Kleo::Crypto::EncryptEMailTask;
97  EncryptEMailTask * const q;
98 public:
99  explicit Private( EncryptEMailTask * qq );
100 
101 private:
102  std::auto_ptr<Kleo::EncryptJob> createJob( GpgME::Protocol proto );
103 
104 private:
105  void slotResult( const EncryptionResult & );
106 
107 private:
108  shared_ptr<Input> input;
109  shared_ptr<Output> output;
110  std::vector<Key> recipients;
111 
112  QPointer<Kleo::EncryptJob> job;
113 };
114 
115 EncryptEMailTask::Private::Private( EncryptEMailTask * qq )
116  : q( qq ),
117  input(),
118  output(),
119  job( 0 )
120 {
121 
122 }
123 
124 EncryptEMailTask::EncryptEMailTask( QObject * p )
125  : Task( p ), d( new Private( this ) )
126 {
127 
128 }
129 
130 EncryptEMailTask::~EncryptEMailTask() {}
131 
132 void EncryptEMailTask::setInput( const shared_ptr<Input> & input ) {
133  kleo_assert( !d->job );
134  kleo_assert( input );
135  d->input = input;
136 }
137 
138 void EncryptEMailTask::setOutput( const shared_ptr<Output> & output ) {
139  kleo_assert( !d->job );
140  kleo_assert( output );
141  d->output = output;
142 }
143 
144 void EncryptEMailTask::setRecipients( const std::vector<Key> & recipients ) {
145  kleo_assert( !d->job );
146  kleo_assert( !recipients.empty() );
147  d->recipients = recipients;
148 }
149 
150 Protocol EncryptEMailTask::protocol() const {
151  kleo_assert( !d->recipients.empty() );
152  return d->recipients.front().protocol();
153 }
154 
155 QString EncryptEMailTask::label() const
156 {
157  return d->input ? d->input->label() : QString();
158 }
159 
160 unsigned long long EncryptEMailTask::inputSize() const {
161  return d->input ? d->input->size() : 0;
162 }
163 
164 void EncryptEMailTask::doStart() {
165  kleo_assert( !d->job );
166  kleo_assert( d->input );
167  kleo_assert( d->output );
168  kleo_assert( !d->recipients.empty() );
169 
170  std::auto_ptr<Kleo::EncryptJob> job = d->createJob( protocol() );
171  kleo_assert( job.get() );
172 
173  job->start( d->recipients,
174  d->input->ioDevice(), d->output->ioDevice(),
175  /*alwaysTrust=*/true );
176 
177  d->job = job.release();
178 }
179 
180 void EncryptEMailTask::cancel() {
181  if ( d->job )
182  d->job->slotCancel();
183 }
184 
185 std::auto_ptr<Kleo::EncryptJob> EncryptEMailTask::Private::createJob( GpgME::Protocol proto ) {
186  const CryptoBackend::Protocol * const backend = CryptoBackendFactory::instance()->protocol( proto );
187  kleo_assert( backend );
188  bool shouldArmor = ( proto == OpenPGP || q->asciiArmor() ) && !output->binaryOpt();
189  std::auto_ptr<Kleo::EncryptJob> encryptJob( backend->encryptJob( shouldArmor, /*textmode=*/false ) );
190  kleo_assert( encryptJob.get() );
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)) );
197  return encryptJob;
198 }
199 
200 void EncryptEMailTask::Private::slotResult( const EncryptionResult & result ) {
201  const Job * const job = qobject_cast<const Job*>( q->sender() );
202  if ( result.error().code() ) {
203  output->cancel();
204  } else {
205  output->finalize();
206  }
207  q->emitResult( shared_ptr<Result>( new EncryptEMailResult( result, AuditLog::fromJob( job ) ) ) );
208 }
209 
210 QString EncryptEMailResult::overview() const {
211  return makeOverview( makeResultString( m_result ) );
212 }
213 
214 QString EncryptEMailResult::details() const {
215  return QString();
216 }
217 
218 int EncryptEMailResult::errorCode() const {
219  return m_result.error().encodedError();
220 }
221 
222 QString EncryptEMailResult::errorString() const {
223  return hasError() ? makeResultString( m_result ) : QString();
224 }
225 
226 AuditLog EncryptEMailResult::auditLog() const {
227  return m_auditLog;
228 }
229 
230 Task::Result::VisualCode EncryptEMailResult::code() const
231 {
232  if ( m_result.error().isCanceled() )
233  return Warning;
234  return m_result.error().code() ? NeutralError : NeutralSuccess;
235 }
236 
237 
238 #include "moc_encryptemailtask.cpp"
239 
240 
auditlog.h
output.h
Kleo::Crypto::EncryptEMailTask::setInput
void setInput(const boost::shared_ptr< Input > &input)
Definition: encryptemailtask.cpp:132
input.h
Kleo::Crypto::Task::Result::VisualCode
VisualCode
Definition: task.h:127
Kleo::Crypto::EncryptEMailTask::label
QString label() const
Definition: encryptemailtask.cpp:155
Kleo::AuditLog
Definition: auditlog.h:48
Kleo::Crypto::EncryptEMailTask::protocol
GpgME::Protocol protocol() const
Definition: encryptemailtask.cpp:150
Kleo::Crypto::Task
Definition: task.h:55
kleo_assert.h
boost::shared_ptr
Definition: encryptemailcontroller.h:51
encryptemailtask.h
d
#define d
Definition: adduseridcommand.cpp:90
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
kleo_assert
#define kleo_assert(cond)
Definition: kleo_assert.h:84
Kleo::Crypto::EncryptEMailTask::setRecipients
void setRecipients(const std::vector< GpgME::Key > &recipients)
Definition: encryptemailtask.cpp:144
Kleo::Crypto::Task::Result
Definition: task.h:117
Kleo::AuditLog::error
GpgME::Error error() const
Definition: auditlog.h:58
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Crypto::EncryptEMailTask::~EncryptEMailTask
~EncryptEMailTask()
Definition: encryptemailtask.cpp:130
Kleo::Crypto::EncryptEMailTask
Definition: encryptemailtask.h:58
Kleo::Crypto::EncryptEMailTask::cancel
void cancel()
Definition: encryptemailtask.cpp:180
Kleo::Crypto::EncryptEMailTask::EncryptEMailTask
EncryptEMailTask(QObject *parent=0)
Definition: encryptemailtask.cpp:124
Kleo::Crypto::EncryptEMailTask::setOutput
void setOutput(const boost::shared_ptr< Output > &output)
Definition: encryptemailtask.cpp:138
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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

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