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