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 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 "qgpgmeencryptjob.h"
39 :
40 : #include "dataprovider.h"
41 :
42 : #include "context.h"
43 : #include "encryptionresult.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 3 : QGpgMEEncryptJob::QGpgMEEncryptJob(Context *context)
55 : : mixin_type(context),
56 3 : mOutputIsBase64Encoded(false)
57 : {
58 3 : lateInitialization();
59 3 : }
60 :
61 6 : QGpgMEEncryptJob::~QGpgMEEncryptJob() {}
62 :
63 0 : void QGpgMEEncryptJob::setOutputIsBase64Encoded(bool on)
64 : {
65 0 : mOutputIsBase64Encoded = on;
66 0 : }
67 :
68 3 : static QGpgMEEncryptJob::result_type encrypt(Context *ctx, QThread *thread,
69 : const std::vector<Key> &recipients,
70 : const std::weak_ptr<QIODevice> &plainText_,
71 : const std::weak_ptr<QIODevice> &cipherText_,
72 : const Context::EncryptionFlags eflags,
73 : bool outputIsBsse64Encoded)
74 : {
75 :
76 3 : const std::shared_ptr<QIODevice> plainText = plainText_.lock();
77 6 : const std::shared_ptr<QIODevice> cipherText = cipherText_.lock();
78 :
79 6 : const _detail::ToThreadMover ctMover(cipherText, thread);
80 6 : const _detail::ToThreadMover ptMover(plainText, thread);
81 :
82 6 : QGpgME::QIODeviceDataProvider in(plainText);
83 6 : const Data indata(&in);
84 :
85 3 : if (!cipherText) {
86 2 : QGpgME::QByteArrayDataProvider out;
87 4 : Data outdata(&out);
88 :
89 2 : if (outputIsBsse64Encoded) {
90 0 : outdata.setEncoding(Data::Base64Encoding);
91 : }
92 :
93 4 : const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
94 4 : Error ae;
95 4 : const QString log = _detail::audit_log_as_html(ctx, ae);
96 4 : return std::make_tuple(res, out.data(), log, ae);
97 : } else {
98 1 : QGpgME::QIODeviceDataProvider out(cipherText);
99 2 : Data outdata(&out);
100 :
101 1 : if (outputIsBsse64Encoded) {
102 0 : outdata.setEncoding(Data::Base64Encoding);
103 : }
104 :
105 2 : const EncryptionResult res = ctx->encrypt(recipients, indata, outdata, eflags);
106 2 : Error ae;
107 2 : const QString log = _detail::audit_log_as_html(ctx, ae);
108 2 : return std::make_tuple(res, QByteArray(), log, ae);
109 3 : }
110 :
111 : }
112 :
113 2 : static QGpgMEEncryptJob::result_type encrypt_qba(Context *ctx, const std::vector<Key> &recipients, const QByteArray &plainText, const Context::EncryptionFlags eflags, bool outputIsBsse64Encoded)
114 : {
115 2 : const std::shared_ptr<QBuffer> buffer(new QBuffer);
116 2 : buffer->setData(plainText);
117 2 : if (!buffer->open(QIODevice::ReadOnly)) {
118 0 : assert(!"This should never happen: QBuffer::open() failed");
119 : }
120 2 : return encrypt(ctx, 0, recipients, buffer, std::shared_ptr<QIODevice>(), eflags, outputIsBsse64Encoded);
121 : }
122 :
123 0 : Error QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust)
124 : {
125 : run(std::bind(&encrypt_qba, std::placeholders::_1, recipients, plainText,
126 0 : alwaysTrust ? Context::AlwaysTrust : Context::None, mOutputIsBase64Encoded));
127 0 : return Error();
128 : }
129 :
130 1 : void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText,
131 : const std::shared_ptr<QIODevice> &cipherText, const Context::EncryptionFlags eflags)
132 : {
133 : run(std::bind(&encrypt,
134 : std::placeholders::_1, std::placeholders::_2,
135 : recipients,
136 : std::placeholders::_3, std::placeholders::_4,
137 : eflags,
138 : mOutputIsBase64Encoded),
139 1 : plainText, cipherText);
140 1 : }
141 :
142 2 : EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText,
143 : const Context::EncryptionFlags eflags, QByteArray &cipherText)
144 : {
145 2 : const result_type r = encrypt_qba(context(), recipients, plainText, eflags, mOutputIsBase64Encoded);
146 2 : cipherText = std::get<1>(r);
147 2 : resultHook(r);
148 2 : return mResult;
149 : }
150 :
151 0 : void QGpgMEEncryptJob::start(const std::vector<Key> &recipients, const std::shared_ptr<QIODevice> &plainText, const std::shared_ptr<QIODevice> &cipherText, bool alwaysTrust)
152 : {
153 0 : return start(recipients, plainText, cipherText, alwaysTrust ? Context::AlwaysTrust : Context::None);
154 : }
155 :
156 0 : EncryptionResult QGpgMEEncryptJob::exec(const std::vector<Key> &recipients, const QByteArray &plainText, bool alwaysTrust, QByteArray &cipherText)
157 : {
158 0 : return exec(recipients, plainText, alwaysTrust ? Context::AlwaysTrust : Context::None, cipherText);
159 : }
160 :
161 3 : void QGpgMEEncryptJob::resultHook(const result_type &tuple)
162 : {
163 3 : mResult = std::get<0>(tuple);
164 3 : }
165 :
166 : #if 0
167 : void QGpgMEEncryptJob::showErrorDialog(QWidget *parent, const QString &caption) const
168 : {
169 : if (mResult.error() && !mResult.error().isCanceled()) {
170 : MessageBox::error(parent, mResult, this, caption);
171 : }
172 : }
173 : #endif
174 : #include "qgpgmeencryptjob.moc"
|