LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmeencryptjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 44 58 75.9 %
Date: 2018-11-15 08:49:49 Functions: 8 12 66.7 %

          Line data    Source code
       1             : /*
       2             :     qgpgmeencryptjob.cpp
       3             : 
       4             :     This file is part of qgpgme, the Qt API binding for gpgme
       5             :     Copyright (c) 2004,2007,2008 Klarälvdalens Datakonsult AB
       6             :     Copyright (c) 2016 by Bundesamt für Sicherheit in der Informationstechnik
       7             :     Software engineering by Intevation GmbH
       8             : 
       9             :     QGpgME is free software; you can redistribute it and/or
      10             :     modify it under the terms of the GNU General Public License as
      11             :     published by the Free Software Foundation; either version 2 of the
      12             :     License, or (at your option) any later version.
      13             : 
      14             :     QGpgME is distributed in the hope that it will be useful,
      15             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17             :     General Public License for more details.
      18             : 
      19             :     You should have received a copy of the GNU General Public License along
      20             :     with this program; if not, write to the Free Software Foundation, Inc.,
      21             :     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
      22             : 
      23             :     In addition, as a special exception, the copyright holders give
      24             :     permission to link the code of this program with any edition of
      25             :     the Qt library by Trolltech AS, Norway (or with modified versions
      26             :     of Qt that use the same license as Qt), and distribute linked
      27             :     combinations including the two.  You must obey the GNU General
      28             :     Public License in all respects for all of the code used other than
      29             :     Qt.  If you modify this file, you may extend this exception to
      30             :     your version of the file, but you are not obligated to do so.  If
      31             :     you do not wish to do so, delete this exception statement from
      32             :     your version.
      33             : */
      34             : 
      35             : #ifdef HAVE_CONFIG_H
      36             :  #include "config.h"
      37             : #endif
      38             : 
      39             : #include "qgpgmeencryptjob.h"
      40             : 
      41             : #include "dataprovider.h"
      42             : 
      43             : #include "context.h"
      44             : #include "encryptionresult.h"
      45             : #include "data.h"
      46             : 
      47             : #include <QBuffer>
      48             : 
      49             : 
      50             : #include <cassert>
      51             : 
      52             : using namespace QGpgME;
      53             : using namespace GpgME;
      54             : 
      55           3 : QGpgMEEncryptJob::QGpgMEEncryptJob(Context *context)
      56             :     : mixin_type(context),
      57           3 :       mOutputIsBase64Encoded(false)
      58             : {
      59           3 :     lateInitialization();
      60           3 : }
      61             : 
      62           6 : QGpgMEEncryptJob::~QGpgMEEncryptJob() {}
      63             : 
      64           0 : void QGpgMEEncryptJob::setOutputIsBase64Encoded(bool on)
      65             : {
      66           0 :     mOutputIsBase64Encoded = on;
      67           0 : }
      68             : 
      69           3 : static QGpgMEEncryptJob::result_type encrypt(Context *ctx, QThread *thread,
      70             :         const std::vector<Key> &recipients,
      71             :         const std::weak_ptr<QIODevice> &plainText_,
      72             :         const std::weak_ptr<QIODevice> &cipherText_,
      73             :         const Context::EncryptionFlags eflags,
      74             :         bool outputIsBsse64Encoded)
      75             : {
      76             : 
      77           6 :     const std::shared_ptr<QIODevice> plainText = plainText_.lock();
      78           6 :     const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
      79             : 
      80           6 :     const _detail::ToThreadMover ctMover(cipherText, thread);
      81           6 :     const _detail::ToThreadMover ptMover(plainText,  thread);
      82             : 
      83           6 :     QGpgME::QIODeviceDataProvider in(plainText);
      84           6 :     const Data indata(&in);
      85             : 
      86           3 :     if (!cipherText) {
      87           4 :         QGpgME::QByteArrayDataProvider out;
      88           4 :         Data outdata(&out);
      89             : 
      90           2 :         if (outputIsBsse64Encoded) {
      91           0 :             outdata.setEncoding(Data::Base64Encoding);
      92             :         }
      93             : 
      94           4 :         const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
      95           4 :         Error ae;
      96           4 :         const QString log = _detail::audit_log_as_html(ctx, ae);
      97           2 :         return std::make_tuple(res, out.data(), log, ae);
      98             :     } else {
      99           2 :         QGpgME::QIODeviceDataProvider out(cipherText);
     100           2 :         Data outdata(&out);
     101             : 
     102           1 :         if (outputIsBsse64Encoded) {
     103           0 :             outdata.setEncoding(Data::Base64Encoding);
     104             :         }
     105             : 
     106           2 :         const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
     107           2 :         Error ae;
     108           2 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     109           1 :         return std::make_tuple(res, QByteArray(), log, ae);
     110             :     }
     111             : 
     112             : }
     113             : 
     114           2 : static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector<Key> &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded)
     115             : {
     116           4 :     const std::shared_ptr<QBuffer> buffer(new QBuffer);
     117           2 :     buffer->setData(plainText);
     118           2 :     if (!buffer->open(QIODevice::ReadOnly)) {
     119           0 :         assert(!"This should never happen: QBuffer::open() failed");
     120             :     }
     121           4 :     return encrypt(ctx, 0, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded);
     122             : }
     123             : 
     124           0 : Error QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust)
     125             : {
     126           0 :     run(std::bind(&encrypt_qba, std::placeholders::_1, recipients, plainText,
     127           0 :                   alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded));
     128           0 :     return Error();
     129             : }
     130             : 
     131           1 : void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText,
     132             :                              const std::shared_ptr<QIODevice> &cipherText, const Context::EncryptionFlags eflags)
     133             : {
     134           2 :     run(std::bind(&encrypt,
     135             :                     std::placeholders::_1, std::placeholders::_2,
     136             :                     recipients,
     137             :                     std::placeholders::_3, std::placeholders::_4,
     138             :                     eflags,
     139             :                     mOutputIsBase64Encoded),
     140           1 :         plainText, cipherText);
     141           1 : }
     142             : 
     143           2 : EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText,
     144             :                                         const Context::EncryptionFlags eflags, QByteArray &cipherText)
     145             : {
     146           4 :     const result_type r = encrypt_qba(context(), recipients, plainText, eflags, mOutputIsBase64Encoded);
     147           2 :     cipherText = std::get<1>(r);
     148           2 :     resultHook(r);
     149           4 :     return mResult;
     150             : }
     151             : 
     152           0 : void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &cipherText, bool alwaysTrust)
     153             : {
     154           0 :     return start(recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None);
     155             : }
     156             : 
     157           0 : EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
     158             : {
     159           0 :     return exec(recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText);
     160             : }
     161             : 
     162           3 : void QGpgMEEncryptJob::resultHook(const result_type &tuple)
     163             : {
     164           3 :     mResult = std::get<0>(tuple);
     165           3 : }
     166             : 
     167             : #if 0
     168             : void QGpgMEEncryptJob::showErrorDialog(QWidget *parent, const QString &caption) const
     169             : {
     170             :     if (mResult.error() && !mResult.error().isCanceled()) {
     171             :         MessageBox::error(parent, mResult, this, caption);
     172             :     }
     173             : }
     174             : #endif
     175             : #include "qgpgmeencryptjob.moc"

Generated by: LCOV version 1.13