Line data Source code
1 : /*
2 : qgpgmedecryptjob.cpp
3 :
4 : This file is part of qgpgme, the Qt API binding for gpgme
5 : Copyright (c) 2004,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 "qgpgmedecryptjob.h"
35 :
36 : #include "dataprovider.h"
37 :
38 : #include "context.h"
39 : #include "decryptionresult.h"
40 : #include "data.h"
41 :
42 : #include <QBuffer>
43 :
44 : #include <cassert>
45 :
46 : using namespace QGpgME;
47 : using namespace GpgME;
48 :
49 2 : QGpgMEDecryptJob::QGpgMEDecryptJob(Context *context)
50 2 : : mixin_type(context)
51 : {
52 2 : lateInitialization();
53 2 : }
54 :
55 4 : QGpgMEDecryptJob::~QGpgMEDecryptJob() {}
56 :
57 2 : static QGpgMEDecryptJob::result_type decrypt(Context *ctx, QThread *thread,
58 : const std::weak_ptr<QIODevice> &cipherText_,
59 : const std::weak_ptr<QIODevice> &plainText_)
60 : {
61 :
62 2 : const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
63 4 : const std::shared_ptr<QIODevice> plainText = plainText_.lock();
64 :
65 4 : const _detail::ToThreadMover ctMover(cipherText, thread);
66 4 : const _detail::ToThreadMover ptMover(plainText, thread);
67 :
68 4 : QGpgME::QIODeviceDataProvider in(cipherText);
69 4 : const Data indata(&in);
70 :
71 2 : if (!plainText) {
72 2 : QGpgME::QByteArrayDataProvider out;
73 4 : Data outdata(&out);
74 :
75 4 : const DecryptionResult res = ctx->decrypt(indata, outdata);
76 4 : Error ae;
77 4 : const QString log = _detail::audit_log_as_html(ctx, ae);
78 4 : return std::make_tuple(res, out.data(), log, ae);
79 : } else {
80 0 : QGpgME::QIODeviceDataProvider out(plainText);
81 0 : Data outdata(&out);
82 :
83 0 : const DecryptionResult res = ctx->decrypt(indata, outdata);
84 0 : Error ae;
85 0 : const QString log = _detail::audit_log_as_html(ctx, ae);
86 0 : return std::make_tuple(res, QByteArray(), log, ae);
87 2 : }
88 :
89 : }
90 :
91 2 : static QGpgMEDecryptJob::result_type decrypt_qba(Context *ctx, const QByteArray &cipherText)
92 : {
93 2 : const std::shared_ptr<QBuffer> buffer(new QBuffer);
94 2 : buffer->setData(cipherText);
95 2 : if (!buffer->open(QIODevice::ReadOnly)) {
96 0 : assert(!"This should never happen: QBuffer::open() failed");
97 : }
98 2 : return decrypt(ctx, 0, buffer, std::shared_ptr<QIODevice>());
99 : }
100 :
101 0 : Error QGpgMEDecryptJob::start(const QByteArray &cipherText)
102 : {
103 0 : run(std::bind(&decrypt_qba, std::placeholders::_1, cipherText));
104 0 : return Error();
105 : }
106 :
107 0 : void QGpgMEDecryptJob::start(const std::shared_ptr<QIODevice> &cipherText, const std::shared_ptr<QIODevice> &plainText)
108 : {
109 0 : run(std::bind(&decrypt, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, std::placeholders::_4), cipherText, plainText);
110 0 : }
111 :
112 2 : GpgME::DecryptionResult QGpgME::QGpgMEDecryptJob::exec(const QByteArray &cipherText,
113 : QByteArray &plainText)
114 : {
115 2 : const result_type r = decrypt_qba(context(), cipherText);
116 2 : plainText = std::get<1>(r);
117 2 : resultHook(r);
118 2 : return mResult;
119 : }
120 :
121 : //PENDING(marc) implement showErrorDialog()
122 :
123 2 : void QGpgMEDecryptJob::resultHook(const result_type &tuple)
124 : {
125 2 : mResult = std::get<0>(tuple);
126 2 : }
127 :
128 : #include "qgpgmedecryptjob.moc"
|