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-12-01 18:45:36 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             : #ifdef HAVE_CONFIG_H
      35             :  #include "config.h"
      36             : #endif
      37             : 
      38             : #include "qgpgmesignjob.h"
      39             : 
      40             : #include "dataprovider.h"
      41             : 
      42             : #include "context.h"
      43             : #include "signingresult.h"
      44             : #include "data.h"
      45             : 
      46             : #include <QBuffer>
      47             : 
      48             : 
      49             : #include <cassert>
      50             : 
      51             : using namespace QGpgME;
      52             : using namespace GpgME;
      53             : 
      54           6 : QGpgMESignJob::QGpgMESignJob(Context *context)
      55             :     : mixin_type(context),
      56           6 :       mOutputIsBase64Encoded(false)
      57             : {
      58           6 :     lateInitialization();
      59           6 : }
      60             : 
      61          12 : QGpgMESignJob::~QGpgMESignJob() {}
      62             : 
      63           0 : void QGpgMESignJob::setOutputIsBase64Encoded(bool on)
      64             : {
      65           0 :     mOutputIsBase64Encoded = on;
      66           0 : }
      67             : 
      68           6 : static QGpgMESignJob::result_type sign(Context *ctx, QThread *thread,
      69             :                                        const std::vector<Key> &signers,
      70             :                                        const std::weak_ptr<QIODevice> &plainText_,
      71             :                                        const std::weak_ptr<QIODevice> &signature_,
      72             :                                        SignatureMode mode,
      73             :                                        bool outputIsBsse64Encoded)
      74             : {
      75             : 
      76           6 :     const std::shared_ptr<QIODevice> plainText = plainText_.lock();
      77          12 :     const std::shared_ptr<QIODevice> signature = signature_.lock();
      78             : 
      79          12 :     const _detail::ToThreadMover ptMover(plainText, thread);
      80          12 :     const _detail::ToThreadMover sgMover(signature, thread);
      81             : 
      82          12 :     QGpgME::QIODeviceDataProvider in(plainText);
      83          12 :     const Data indata(&in);
      84             : 
      85           6 :     ctx->clearSigningKeys();
      86          12 :     Q_FOREACH (const Key &signer, signers)
      87           6 :         if (!signer.isNull())
      88           6 :             if (const Error err = ctx->addSigningKey(signer)) {
      89           0 :                 return std::make_tuple(SigningResult(err), QByteArray(), QString(), Error());
      90          12 :             }
      91             : 
      92           6 :     if (!signature) {
      93           6 :         QGpgME::QByteArrayDataProvider out;
      94          12 :         Data outdata(&out);
      95             : 
      96           6 :         if (outputIsBsse64Encoded) {
      97           0 :             outdata.setEncoding(Data::Base64Encoding);
      98             :         }
      99             : 
     100          12 :         const SigningResult res = ctx->sign(indata, outdata, mode);
     101          12 :         Error ae;
     102          12 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     103          12 :         return std::make_tuple(res, out.data(), log, ae);
     104             :     } else {
     105           0 :         QGpgME::QIODeviceDataProvider out(signature);
     106           0 :         Data outdata(&out);
     107             : 
     108           0 :         if (outputIsBsse64Encoded) {
     109           0 :             outdata.setEncoding(Data::Base64Encoding);
     110             :         }
     111             : 
     112           0 :         const SigningResult res = ctx->sign(indata, outdata, mode);
     113           0 :         Error ae;
     114           0 :         const QString log = _detail::audit_log_as_html(ctx, ae);
     115           0 :         return std::make_tuple(res, QByteArray(), log, ae);
     116           6 :     }
     117             : 
     118             : }
     119             : 
     120           6 : static QGpgMESignJob::result_type sign_qba(Context *ctx,
     121             :         const std::vector<Key> &signers,
     122             :         const QByteArray &plainText,
     123             :         SignatureMode mode,
     124             :         bool outputIsBsse64Encoded)
     125             : {
     126           6 :     const std::shared_ptr<QBuffer> buffer(new QBuffer);
     127           6 :     buffer->setData(plainText);
     128           6 :     if (!buffer->open(QIODevice::ReadOnly)) {
     129           0 :         assert(!"This should never happen: QBuffer::open() failed");
     130             :     }
     131           6 :     return sign(ctx, 0, signers, buffer, std::shared_ptr<QIODevice>(), mode, outputIsBsse64Encoded);
     132             : }
     133             : 
     134           0 : Error QGpgMESignJob::start(const std::vector<Key> &signers, const QByteArray &plainText, SignatureMode mode)
     135             : {
     136           0 :     run(std::bind(&sign_qba, std::placeholders::_1, signers, plainText, mode, mOutputIsBase64Encoded));
     137           0 :     return Error();
     138             : }
     139             : 
     140           0 : void QGpgMESignJob::start(const std::vector<Key> &signers, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &signature, SignatureMode mode)
     141             : {
     142           0 :     run(std::bind(&sign, std::placeholders::_1, std::placeholders::_2, signers, std::placeholders::_3, std::placeholders::_4, mode, mOutputIsBase64Encoded), plainText, signature);
     143           0 : }
     144             : 
     145           6 : SigningResult QGpgMESignJob::exec(const std::vector<Key> &signers, const QByteArray &plainText, SignatureMode mode, QByteArray &signature)
     146             : {
     147           6 :     const result_type r = sign_qba(context(), signers, plainText, mode, mOutputIsBase64Encoded);
     148           6 :     signature = std::get<1>(r);
     149           6 :     resultHook(r);
     150           6 :     return mResult;
     151             : }
     152             : 
     153           6 : void QGpgMESignJob::resultHook(const result_type &tuple)
     154             : {
     155           6 :     mResult = std::get<0>(tuple);
     156           6 : }
     157             : 
     158             : #if 0
     159             : TODO port
     160             : void QGpgMESignJob::showErrorDialog(QWidget *parent, const QString &caption) const
     161             : {
     162             :     if (mResult.error() && !mResult.error().isCanceled()) {
     163             :         MessageBox::error(parent, mResult, this, caption);
     164             :     }
     165             : }
     166             : #endif
     167             : #include "qgpgmesignjob.moc"

Generated by: LCOV version 1.11