LCOV - code coverage report
Current view: top level - lang/qt/src - qgpgmesignjob.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 39 59 66.1 %
Date: 2016-09-12 13:07:23 Functions: 7 10 70.0 %

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

Generated by: LCOV version 1.11