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

libkleo

  • sources
  • kde-4.14
  • kdepim
  • libkleo
  • backends
  • qgpgme
qgpgmeencryptjob.cpp
Go to the documentation of this file.
1 /*
2  qgpgmeencryptjob.cpp
3 
4  This file is part of libkleopatra, the KDE keymanagement library
5  Copyright (c) 2004,2007,2008 Klarälvdalens Datakonsult AB
6 
7  Libkleopatra is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of the
10  License, or (at your option) any later version.
11 
12  Libkleopatra 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 along
18  with this program; if not, write to the Free Software Foundation, Inc.,
19  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 "qgpgmeencryptjob.h"
34 
35 #include "ui/messagebox.h"
36 
37 #include <qgpgme/dataprovider.h>
38 
39 #include <gpgme++/context.h>
40 #include <gpgme++/encryptionresult.h>
41 #include <gpgme++/data.h>
42 
43 #include <QBuffer>
44 
45 #ifndef Q_MOC_RUN
46 #include <boost/weak_ptr.hpp>
47 #endif
48 
49 #include <cassert>
50 
51 using namespace Kleo;
52 using namespace GpgME;
53 using namespace boost;
54 
55 QGpgMEEncryptJob::QGpgMEEncryptJob( Context * context )
56  : mixin_type( context ),
57  mOutputIsBase64Encoded( false )
58 {
59  lateInitialization();
60 }
61 
62 QGpgMEEncryptJob::~QGpgMEEncryptJob() {}
63 
64 void QGpgMEEncryptJob::setOutputIsBase64Encoded( bool on ) {
65  mOutputIsBase64Encoded = on;
66 }
67 
68 static QGpgMEEncryptJob::result_type encrypt( Context * ctx, QThread * thread,
69  const std::vector<Key> & recipients,
70  const weak_ptr<QIODevice> & plainText_,
71  const weak_ptr<QIODevice> & cipherText_,
72  bool alwaysTrust,
73  bool outputIsBsse64Encoded ) {
74 
75  const shared_ptr<QIODevice> plainText = plainText_.lock();
76  const shared_ptr<QIODevice> cipherText = cipherText_.lock();
77 
78  const _detail::ToThreadMover ctMover( cipherText, thread );
79  const _detail::ToThreadMover ptMover( plainText, thread );
80 
81  QGpgME::QIODeviceDataProvider in( plainText );
82  const Data indata( &in );
83 
84  const Context::EncryptionFlags eflags =
85  alwaysTrust ? Context::AlwaysTrust : Context::None ;
86 
87  if ( !cipherText ) {
88  QGpgME::QByteArrayDataProvider out;
89  Data outdata( &out );
90 
91  if ( outputIsBsse64Encoded )
92  outdata.setEncoding( Data::Base64Encoding );
93 
94  const EncryptionResult res = ctx->encrypt( recipients, indata, outdata, eflags );
95  Error ae;
96  const QString log = _detail::audit_log_as_html( ctx, ae );
97  return make_tuple( res, out.data(), log, ae );
98  } else {
99  QGpgME::QIODeviceDataProvider out( cipherText );
100  Data outdata( &out );
101 
102  if ( outputIsBsse64Encoded )
103  outdata.setEncoding( Data::Base64Encoding );
104 
105  const EncryptionResult res = ctx->encrypt( recipients, indata, outdata, eflags );
106  Error ae;
107  const QString log = _detail::audit_log_as_html( ctx, ae );
108  return make_tuple( res, QByteArray(), log, ae );
109  }
110 
111 }
112 
113 static QGpgMEEncryptJob::result_type encrypt_qba( Context * ctx, const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust, bool outputIsBsse64Encoded ) {
114  const shared_ptr<QBuffer> buffer( new QBuffer );
115  buffer->setData( plainText );
116  if ( !buffer->open( QIODevice::ReadOnly ) )
117  assert( !"This should never happen: QBuffer::open() failed" );
118  return encrypt( ctx, 0, recipients, buffer, shared_ptr<QIODevice>(), alwaysTrust, outputIsBsse64Encoded );
119 }
120 
121 Error QGpgMEEncryptJob::start( const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust ) {
122  run( boost::bind( &encrypt_qba, _1, recipients, plainText, alwaysTrust, mOutputIsBase64Encoded ) );
123  return Error();
124 }
125 
126 void QGpgMEEncryptJob::start( const std::vector<Key> & recipients, const shared_ptr<QIODevice> & plainText, const shared_ptr<QIODevice> & cipherText, bool alwaysTrust ) {
127  run( boost::bind( &encrypt,
128  _1, _2,
129  recipients,
130  _3, _4,
131  alwaysTrust,
132  mOutputIsBase64Encoded ),
133  plainText, cipherText );
134 }
135 
136 EncryptionResult QGpgMEEncryptJob::exec( const std::vector<Key> & recipients, const QByteArray & plainText, bool alwaysTrust, QByteArray & cipherText ) {
137  const result_type r = encrypt_qba( context(), recipients, plainText, alwaysTrust, mOutputIsBase64Encoded );
138  cipherText = get<1>( r );
139  resultHook( r );
140  return mResult;
141 }
142 
143 void QGpgMEEncryptJob::resultHook( const result_type & tuple ) {
144  mResult = get<0>( tuple );
145 }
146 
147 void QGpgMEEncryptJob::showErrorDialog( QWidget * parent, const QString & caption ) const {
148  if ( mResult.error() && !mResult.error().isCanceled() )
149  MessageBox::error( parent, mResult, this, caption );
150 }
151 
QWidget
Kleo::QGpgMEEncryptJob::showErrorDialog
void showErrorDialog(QWidget *parent, const QString &caption) const
Definition: qgpgmeencryptjob.cpp:147
QByteArray
Kleo::_detail::ThreadedJobMixin< EncryptJob, boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::context
GpgME::Context * context() const
Definition: threadedjobmixin.h:178
Kleo::MessageBox::error
static void error(QWidget *parent, const GpgME::SigningResult &result, const Kleo::Job *job, const QString &caption, KMessageBox::Options options=KMessageBox::Notify)
Kleo::QGpgMEEncryptJob::setOutputIsBase64Encoded
void setOutputIsBase64Encoded(bool on)
Definition: qgpgmeencryptjob.cpp:64
QBuffer
Kleo::QGpgMEEncryptJob::~QGpgMEEncryptJob
~QGpgMEEncryptJob()
Definition: qgpgmeencryptjob.cpp:62
Kleo::QGpgMEEncryptJob::QGpgMEEncryptJob
QGpgMEEncryptJob(GpgME::Context *context)
Definition: qgpgmeencryptjob.cpp:55
boost::shared_ptr
Definition: checksumdefinition.h:46
Kleo::_detail::ToThreadMover
Definition: threadedjobmixin.h:77
qgpgmeencryptjob.h
Kleo::_detail::ThreadedJobMixin< EncryptJob, boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::lateInitialization
void lateInitialization()
Definition: threadedjobmixin.h:146
Kleo::_detail::audit_log_as_html
QString audit_log_as_html(GpgME::Context *ctx, GpgME::Error &err)
encrypt_qba
static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector< Key > &recipients, const QByteArray &plainText, bool alwaysTrust, bool outputIsBsse64Encoded)
Definition: qgpgmeencryptjob.cpp:113
messagebox.h
QString
Kleo::_detail::ThreadedJobMixin< EncryptJob, boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::result_type
boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > result_type
Definition: threadedjobmixin.h:117
Kleo::_detail::ThreadedJobMixin< EncryptJob, boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >::run
void run(const T_binder &func)
Definition: threadedjobmixin.h:153
encrypt
static QGpgMEEncryptJob::result_type encrypt(Context *ctx, QThread *thread, const std::vector< Key > &recipients, const weak_ptr< QIODevice > &plainText_, const weak_ptr< QIODevice > &cipherText_, bool alwaysTrust, bool outputIsBsse64Encoded)
Definition: qgpgmeencryptjob.cpp:68
Kleo::QGpgMEEncryptJob::exec
GpgME::EncryptionResult exec(const std::vector< GpgME::Key > &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
Definition: qgpgmeencryptjob.cpp:136
QThread
Kleo::QGpgMEEncryptJob::resultHook
void resultHook(const result_type &r)
Definition: qgpgmeencryptjob.cpp:143
Kleo::_detail::ThreadedJobMixin< EncryptJob, boost::tuple< GpgME::EncryptionResult, QByteArray, QString, GpgME::Error > >
Kleo::QGpgMEEncryptJob::start
GpgME::Error start(const std::vector< GpgME::Key > &recipients, const QByteArray &plainText, bool alwaysTrust)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:38 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkleo

Skip menu "libkleo"
  • 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