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 : #include "tofuinfo.h"
23 :
24 : #include <istream>
25 : #include "util.h"
26 :
27 : class GpgME::TofuInfo::Private
28 : {
29 : public:
30 : Private() {}
31 18 : Private(gpgme_tofu_info_t info)
32 18 : : mInfo(info ? new _gpgme_tofu_info(*info) : nullptr)
33 : {
34 18 : if (mInfo && mInfo->description) {
35 9 : mInfo->description = strdup(mInfo->description);
36 : }
37 18 : }
38 :
39 : Private(const Private &other)
40 : : mInfo(other.mInfo)
41 : {
42 : if (mInfo && mInfo->description) {
43 : mInfo->description = strdup(mInfo->description);
44 : }
45 : }
46 :
47 18 : ~Private()
48 : {
49 18 : if (mInfo) {
50 17 : std::free(mInfo->description);
51 17 : mInfo->description = nullptr;
52 :
53 17 : delete mInfo;
54 : }
55 18 : }
56 :
57 : gpgme_tofu_info_t mInfo;
58 : };
59 :
60 18 : GpgME::TofuInfo::TofuInfo(gpgme_tofu_info_t info)
61 18 : : d(new Private(info))
62 : {
63 18 : }
64 :
65 1 : GpgME::TofuInfo::TofuInfo() : d()
66 : {
67 1 : }
68 :
69 63 : bool GpgME::TofuInfo::isNull() const
70 : {
71 63 : return !d || !d->mInfo;
72 : }
73 :
74 6 : GpgME::TofuInfo::Validity GpgME::TofuInfo::validity() const
75 : {
76 6 : if (isNull()) {
77 1 : return ValidityUnknown;
78 : }
79 5 : switch (d->mInfo->validity) {
80 : case 0:
81 0 : return Conflict;
82 : case 1:
83 0 : return NoHistory;
84 : case 2:
85 5 : return LittleHistory;
86 : case 3:
87 0 : return BasicHistory;
88 : case 4:
89 0 : return LargeHistory;
90 : default:
91 0 : return ValidityUnknown;
92 : }
93 : }
94 :
95 6 : GpgME::TofuInfo::Policy GpgME::TofuInfo::policy() const
96 : {
97 6 : if (isNull()) {
98 1 : return PolicyUnknown;
99 : }
100 5 : switch (d->mInfo->policy) {
101 : case GPGME_TOFU_POLICY_NONE:
102 0 : return PolicyNone;
103 : case GPGME_TOFU_POLICY_AUTO:
104 5 : return PolicyAuto;
105 : case GPGME_TOFU_POLICY_GOOD:
106 0 : return PolicyGood;
107 : case GPGME_TOFU_POLICY_BAD:
108 0 : return PolicyBad;
109 : case GPGME_TOFU_POLICY_ASK:
110 0 : return PolicyAsk;
111 : case GPGME_TOFU_POLICY_UNKNOWN:
112 0 : return PolicyUnknown;
113 : }
114 0 : }
115 :
116 1 : const char *GpgME::TofuInfo::description() const
117 : {
118 1 : return isNull() ? nullptr : d->mInfo->description;
119 : }
120 :
121 26 : unsigned short GpgME::TofuInfo::signCount() const
122 : {
123 26 : return isNull() ? 0 : d->mInfo->signcount;
124 : }
125 :
126 0 : unsigned short GpgME::TofuInfo::encrCount() const
127 : {
128 0 : return isNull() ? 0 : d->mInfo->encrcount;
129 : }
130 :
131 4 : unsigned long GpgME::TofuInfo::signFirst() const
132 : {
133 4 : return isNull() ? 0 : d->mInfo->signfirst;
134 : }
135 :
136 6 : unsigned long GpgME::TofuInfo::signLast() const
137 : {
138 6 : return isNull() ? 0 : d->mInfo->signlast;
139 : }
140 :
141 0 : unsigned long GpgME::TofuInfo::encrFirst() const
142 : {
143 0 : return isNull() ? 0 : d->mInfo->encrfirst;
144 : }
145 :
146 0 : unsigned long GpgME::TofuInfo::encrLast() const
147 : {
148 0 : return isNull() ? 0 : d->mInfo->encrlast;
149 : }
150 :
151 0 : std::ostream &GpgME::operator<<(std::ostream &os, const GpgME::TofuInfo &info)
152 : {
153 0 : os << "GpgME::Signature::TofuInfo(";
154 0 : if (!info.isNull()) {
155 0 : os << "\n desc: " << protect(info.description())
156 0 : << "\n validity: " << info.validity()
157 0 : << "\n policy: " << info.policy()
158 0 : << "\n signcount: "<< info.signCount()
159 0 : << "\n signfirst: "<< info.signFirst()
160 0 : << "\n signlast: " << info.signLast()
161 0 : << "\n encrcount: "<< info.encrCount()
162 0 : << "\n encrfirst: "<< info.encrFirst()
163 0 : << "\n encrlast: " << info.encrLast()
164 0 : << '\n';
165 : }
166 0 : return os << ")";
167 18 : }
|