Line data Source code
1 : /*
2 : encryptionresult.cpp - wraps a gpgme verify result
3 : Copyright (C) 2004 Klarälvdalens Datakonsult AB
4 :
5 : This file is part of GPGME++.
6 :
7 : GPGME++ is free software; you can redistribute it and/or
8 : modify it under the terms of the GNU Library General Public
9 : License as published by the Free Software Foundation; either
10 : version 2 of the License, or (at your option) any later version.
11 :
12 : GPGME++ is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU Library General Public License for more details.
16 :
17 : You should have received a copy of the GNU Library General Public License
18 : along with GPGME++; see the file COPYING.LIB. If not, write to the
19 : Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 : Boston, MA 02110-1301, USA.
21 : */
22 :
23 : #ifdef HAVE_CONFIG_H
24 : #include "config.h"
25 : #endif
26 :
27 : #include <encryptionresult.h>
28 : #include "result_p.h"
29 : #include "util.h"
30 :
31 : #include <gpgme.h>
32 :
33 : #include <cstring>
34 : #include <cstdlib>
35 : #include <istream>
36 : #include <algorithm>
37 : #include <iterator>
38 :
39 : #include <string.h>
40 :
41 : class GpgME::EncryptionResult::Private
42 : {
43 : public:
44 3 : explicit Private(const gpgme_encrypt_result_t r)
45 3 : {
46 3 : if (!r) {
47 0 : return;
48 : }
49 3 : for (gpgme_invalid_key_t ik = r->invalid_recipients ; ik ; ik = ik->next) {
50 0 : gpgme_invalid_key_t copy = new _gpgme_invalid_key(*ik);
51 0 : if (ik->fpr) {
52 0 : copy->fpr = strdup(ik->fpr);
53 : }
54 0 : copy->next = 0;
55 0 : invalid.push_back(copy);
56 : }
57 : }
58 3 : ~Private()
59 3 : {
60 3 : for (std::vector<gpgme_invalid_key_t>::iterator it = invalid.begin() ; it != invalid.end() ; ++it) {
61 0 : std::free((*it)->fpr);
62 0 : delete *it; *it = 0;
63 : }
64 3 : }
65 :
66 : std::vector<gpgme_invalid_key_t> invalid;
67 : };
68 :
69 0 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, int error)
70 0 : : GpgME::Result(error), d()
71 : {
72 0 : init(ctx);
73 0 : }
74 :
75 3 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, const Error &error)
76 3 : : GpgME::Result(error), d()
77 : {
78 3 : init(ctx);
79 3 : }
80 :
81 3 : void GpgME::EncryptionResult::init(gpgme_ctx_t ctx)
82 : {
83 3 : if (!ctx) {
84 0 : return;
85 : }
86 3 : gpgme_encrypt_result_t res = gpgme_op_encrypt_result(ctx);
87 3 : if (!res) {
88 0 : return;
89 : }
90 3 : d.reset(new Private(res));
91 : }
92 :
93 6 : make_standard_stuff(EncryptionResult)
94 :
95 0 : unsigned int GpgME::EncryptionResult::numInvalidRecipients() const
96 : {
97 0 : return d ? d->invalid.size() : 0 ;
98 : }
99 :
100 0 : GpgME::InvalidRecipient GpgME::EncryptionResult::invalidEncryptionKey(unsigned int idx) const
101 : {
102 0 : return InvalidRecipient(d, idx);
103 : }
104 :
105 0 : std::vector<GpgME::InvalidRecipient> GpgME::EncryptionResult::invalidEncryptionKeys() const
106 : {
107 0 : if (!d) {
108 0 : return std::vector<GpgME::InvalidRecipient>();
109 : }
110 0 : std::vector<GpgME::InvalidRecipient> result;
111 0 : result.reserve(d->invalid.size());
112 0 : for (unsigned int i = 0 ; i < d->invalid.size() ; ++i) {
113 0 : result.push_back(InvalidRecipient(d, i));
114 : }
115 0 : return result;
116 : }
117 :
118 0 : GpgME::InvalidRecipient::InvalidRecipient(const std::shared_ptr<EncryptionResult::Private> &parent, unsigned int i)
119 0 : : d(parent), idx(i)
120 : {
121 :
122 0 : }
123 :
124 0 : GpgME::InvalidRecipient::InvalidRecipient() : d(), idx(0) {}
125 :
126 0 : bool GpgME::InvalidRecipient::isNull() const
127 : {
128 0 : return !d || idx >= d->invalid.size() ;
129 : }
130 :
131 0 : const char *GpgME::InvalidRecipient::fingerprint() const
132 : {
133 0 : return isNull() ? 0 : d->invalid[idx]->fpr ;
134 : }
135 :
136 0 : GpgME::Error GpgME::InvalidRecipient::reason() const
137 : {
138 0 : return Error(isNull() ? 0 : d->invalid[idx]->reason);
139 : }
140 :
141 0 : std::ostream &GpgME::operator<<(std::ostream &os, const EncryptionResult &result)
142 : {
143 0 : os << "GpgME::EncryptionResult(";
144 0 : if (!result.isNull()) {
145 0 : os << "\n error: " << result.error()
146 0 : << "\n invalid recipients:\n";
147 0 : const std::vector<InvalidRecipient> ir = result.invalidEncryptionKeys();
148 : std::copy(ir.begin(), ir.end(),
149 0 : std::ostream_iterator<InvalidRecipient>(os, "\n"));
150 : }
151 0 : return os << ')';
152 : }
153 :
154 0 : std::ostream &GpgME::operator<<(std::ostream &os, const InvalidRecipient &ir)
155 : {
156 0 : os << "GpgME::InvalidRecipient(";
157 0 : if (!ir.isNull()) {
158 : os << "\n fingerprint: " << protect(ir.fingerprint())
159 0 : << "\n reason: " << ir.reason()
160 0 : << '\n';
161 : }
162 0 : return os << ')';
163 24 : }
|