Line data Source code
1 : /* tofuinfo.cpp - wraps gpgme tofu info
2 : Copyright (C) 2016 Intevation GmbH
3 :
4 : This file is part of GPGME++.
5 :
6 : GPGME++ is free software; you can redistribute it and/or
7 : modify it under the terms of the GNU Library General Public
8 : License as published by the Free Software Foundation; either
9 : version 2 of the License, or (at your option) any later version.
10 :
11 : GPGME++ is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU Library General Public License for more details.
15 :
16 : You should have received a copy of the GNU Library General Public License
17 : along with GPGME++; see the file COPYING.LIB. If not, write to the
18 : Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 : Boston, MA 02110-1301, USA.
20 : */
21 :
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif
25 :
26 : #include "tofuinfo.h"
27 :
28 : #include <istream>
29 : #include "util.h"
30 :
31 : class GpgME::TofuInfo::Private
32 : {
33 : public:
34 : Private() {}
35 0 : Private(gpgme_tofu_info_t info)
36 0 : : mInfo(info ? new _gpgme_tofu_info(*info) : nullptr)
37 : {
38 0 : if (mInfo && mInfo->description) {
39 0 : mInfo->description = strdup(mInfo->description);
40 : }
41 0 : }
42 :
43 : Private(const Private &other)
44 : : mInfo(other.mInfo)
45 : {
46 : if (mInfo && mInfo->description) {
47 : mInfo->description = strdup(mInfo->description);
48 : }
49 : }
50 :
51 0 : ~Private()
52 0 : {
53 0 : if (mInfo) {
54 0 : std::free(mInfo->description);
55 0 : mInfo->description = nullptr;
56 :
57 0 : delete mInfo;
58 : }
59 0 : }
60 :
61 : gpgme_tofu_info_t mInfo;
62 : };
63 :
64 0 : GpgME::TofuInfo::TofuInfo(gpgme_tofu_info_t info)
65 0 : : d(new Private(info))
66 : {
67 0 : }
68 :
69 0 : GpgME::TofuInfo::TofuInfo() : d()
70 : {
71 0 : }
72 :
73 0 : bool GpgME::TofuInfo::isNull() const
74 : {
75 0 : return !d || !d->mInfo;
76 : }
77 :
78 0 : GpgME::TofuInfo::Validity GpgME::TofuInfo::validity() const
79 : {
80 0 : if (isNull()) {
81 0 : return ValidityUnknown;
82 : }
83 0 : switch (d->mInfo->validity) {
84 : case 0:
85 0 : return Conflict;
86 : case 1:
87 0 : return NoHistory;
88 : case 2:
89 0 : return LittleHistory;
90 : case 3:
91 0 : return BasicHistory;
92 : case 4:
93 0 : return LargeHistory;
94 : default:
95 0 : return ValidityUnknown;
96 : }
97 : }
98 :
99 0 : GpgME::TofuInfo::Policy GpgME::TofuInfo::policy() const
100 : {
101 0 : if (isNull()) {
102 0 : return PolicyUnknown;
103 : }
104 0 : switch (d->mInfo->policy) {
105 : case GPGME_TOFU_POLICY_NONE:
106 0 : return PolicyNone;
107 : case GPGME_TOFU_POLICY_AUTO:
108 0 : return PolicyAuto;
109 : case GPGME_TOFU_POLICY_GOOD:
110 0 : return PolicyGood;
111 : case GPGME_TOFU_POLICY_BAD:
112 0 : return PolicyBad;
113 : case GPGME_TOFU_POLICY_ASK:
114 0 : return PolicyAsk;
115 : case GPGME_TOFU_POLICY_UNKNOWN:
116 : default:
117 0 : return PolicyUnknown;
118 : }
119 : }
120 :
121 0 : const char *GpgME::TofuInfo::description() const
122 : {
123 0 : return isNull() ? nullptr : d->mInfo->description;
124 : }
125 :
126 0 : unsigned short GpgME::TofuInfo::signCount() const
127 : {
128 0 : return isNull() ? 0 : d->mInfo->signcount;
129 : }
130 :
131 0 : unsigned short GpgME::TofuInfo::encrCount() const
132 : {
133 0 : return isNull() ? 0 : d->mInfo->encrcount;
134 : }
135 :
136 0 : unsigned long GpgME::TofuInfo::signFirst() const
137 : {
138 0 : return isNull() ? 0 : d->mInfo->signfirst;
139 : }
140 :
141 0 : unsigned long GpgME::TofuInfo::signLast() const
142 : {
143 0 : return isNull() ? 0 : d->mInfo->signlast;
144 : }
145 :
146 0 : unsigned long GpgME::TofuInfo::encrFirst() const
147 : {
148 0 : return isNull() ? 0 : d->mInfo->encrfirst;
149 : }
150 :
151 0 : unsigned long GpgME::TofuInfo::encrLast() const
152 : {
153 0 : return isNull() ? 0 : d->mInfo->encrlast;
154 : }
155 :
156 0 : std::ostream &GpgME::operator<<(std::ostream &os, const GpgME::TofuInfo &info)
157 : {
158 0 : os << "GpgME::Signature::TofuInfo(";
159 0 : if (!info.isNull()) {
160 : os << "\n desc: " << protect(info.description())
161 0 : << "\n validity: " << info.validity()
162 0 : << "\n policy: " << info.policy()
163 0 : << "\n signcount: "<< info.signCount()
164 0 : << "\n signfirst: "<< info.signFirst()
165 0 : << "\n signlast: " << info.signLast()
166 0 : << "\n encrcount: "<< info.encrCount()
167 0 : << "\n encrfirst: "<< info.encrFirst()
168 0 : << "\n encrlast: " << info.encrLast()
169 0 : << '\n';
170 : }
171 0 : return os << ")";
172 0 : }
|