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 : #include <encryptionresult.h>
24 : #include "result_p.h"
25 : #include "util.h"
26 :
27 : #include <gpgme.h>
28 :
29 : #include <cstring>
30 : #include <cstdlib>
31 : #include <istream>
32 : #include <algorithm>
33 : #include <iterator>
34 :
35 : #include <string.h>
36 :
37 : class GpgME::EncryptionResult::Private
38 : {
39 : public:
40 3 : explicit Private(const gpgme_encrypt_result_t r)
41 3 : {
42 3 : if (!r) {
43 3 : return;
44 : }
45 3 : for (gpgme_invalid_key_t ik = r->invalid_recipients ; ik ; ik = ik->next) {
46 0 : gpgme_invalid_key_t copy = new _gpgme_invalid_key(*ik);
47 0 : if (ik->fpr) {
48 0 : copy->fpr = strdup(ik->fpr);
49 : }
50 0 : copy->next = 0;
51 0 : invalid.push_back(copy);
52 : }
53 : }
54 3 : ~Private()
55 3 : {
56 3 : for (std::vector<gpgme_invalid_key_t>::iterator it = invalid.begin() ; it != invalid.end() ; ++it) {
57 0 : std::free((*it)->fpr);
58 0 : delete *it; *it = 0;
59 : }
60 3 : }
61 :
62 : std::vector<gpgme_invalid_key_t> invalid;
63 : };
64 :
65 0 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, int error)
66 0 : : GpgME::Result(error), d()
67 : {
68 0 : init(ctx);
69 0 : }
70 :
71 3 : GpgME::EncryptionResult::EncryptionResult(gpgme_ctx_t ctx, const Error &error)
72 3 : : GpgME::Result(error), d()
73 : {
74 3 : init(ctx);
75 3 : }
76 :
77 3 : void GpgME::EncryptionResult::init(gpgme_ctx_t ctx)
78 : {
79 3 : if (!ctx) {
80 0 : return;
81 : }
82 3 : gpgme_encrypt_result_t res = gpgme_op_encrypt_result(ctx);
83 3 : if (!res) {
84 0 : return;
85 : }
86 3 : d.reset(new Private(res));
87 : }
88 :
89 6 : make_standard_stuff(EncryptionResult)
90 :
91 0 : unsigned int GpgME::EncryptionResult::numInvalidRecipients() const
92 : {
93 0 : return d ? d->invalid.size() : 0 ;
94 : }
95 :
96 0 : GpgME::InvalidRecipient GpgME::EncryptionResult::invalidEncryptionKey(unsigned int idx) const
97 : {
98 0 : return InvalidRecipient(d, idx);
99 : }
100 :
101 0 : std::vector<GpgME::InvalidRecipient> GpgME::EncryptionResult::invalidEncryptionKeys() const
102 : {
103 0 : if (!d) {
104 0 : return std::vector<GpgME::InvalidRecipient>();
105 : }
106 0 : std::vector<GpgME::InvalidRecipient> result;
107 0 : result.reserve(d->invalid.size());
108 0 : for (unsigned int i = 0 ; i < d->invalid.size() ; ++i) {
109 0 : result.push_back(InvalidRecipient(d, i));
110 : }
111 0 : return result;
112 : }
113 :
114 0 : GpgME::InvalidRecipient::InvalidRecipient(const std::shared_ptr<EncryptionResult::Private> &parent, unsigned int i)
115 0 : : d(parent), idx(i)
116 : {
117 :
118 0 : }
119 :
120 0 : GpgME::InvalidRecipient::InvalidRecipient() : d(), idx(0) {}
121 :
122 0 : bool GpgME::InvalidRecipient::isNull() const
123 : {
124 0 : return !d || idx >= d->invalid.size() ;
125 : }
126 :
127 0 : const char *GpgME::InvalidRecipient::fingerprint() const
128 : {
129 0 : return isNull() ? 0 : d->invalid[idx]->fpr ;
130 : }
131 :
132 0 : GpgME::Error GpgME::InvalidRecipient::reason() const
133 : {
134 0 : return Error(isNull() ? 0 : d->invalid[idx]->reason);
135 : }
136 :
137 0 : std::ostream &GpgME::operator<<(std::ostream &os, const EncryptionResult &result)
138 : {
139 0 : os << "GpgME::EncryptionResult(";
140 0 : if (!result.isNull()) {
141 0 : os << "\n error: " << result.error()
142 0 : << "\n invalid recipients:\n";
143 0 : const std::vector<InvalidRecipient> ir = result.invalidEncryptionKeys();
144 : std::copy(ir.begin(), ir.end(),
145 0 : std::ostream_iterator<InvalidRecipient>(os, "\n"));
146 : }
147 0 : return os << ')';
148 : }
149 :
150 0 : std::ostream &GpgME::operator<<(std::ostream &os, const InvalidRecipient &ir)
151 : {
152 0 : os << "GpgME::InvalidRecipient(";
153 0 : if (!ir.isNull()) {
154 0 : os << "\n fingerprint: " << protect(ir.fingerprint())
155 0 : << "\n reason: " << ir.reason()
156 0 : << '\n';
157 : }
158 0 : return os << ')';
159 18 : }
|