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