1/*
2 importresult.cpp - wraps a gpgme import 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 <gpgme++/config-gpgme++.h>
24
25#include <gpgme++/importresult.h>
26#include "result_p.h"
27
28#include <gpgme.h>
29#include <cstdlib>
30#include <cstring>
31
32#include <string.h>
33
34class GpgME::ImportResult::Private {
35public:
36 Private( const _gpgme_op_import_result & r ) : res( r ) {
37 // copy recursively, using compiler-generated copy ctor.
38 // We just need to handle the pointers in the structs:
39 for ( gpgme_import_status_t is = r.imports ; is ; is = is->next ) {
40 gpgme_import_status_t copy = new _gpgme_import_status( *is );
41 copy->fpr = strdup( is->fpr );
42 copy->next = 0;
43 imports.push_back( copy );
44 }
45 res.imports = 0;
46 }
47 ~Private() {
48 for ( std::vector<gpgme_import_status_t>::iterator it = imports.begin() ; it != imports.end() ; ++it ) {
49 std::free( ( *it )->fpr );
50 delete *it; *it = 0;
51 }
52 }
53
54 _gpgme_op_import_result res;
55 std::vector<gpgme_import_status_t> imports;
56};
57
58GpgME::ImportResult::ImportResult( gpgme_ctx_t ctx, int error )
59 : GpgME::Result( error ), d()
60{
61 init( ctx );
62}
63
64GpgME::ImportResult::ImportResult( gpgme_ctx_t ctx, const Error & error )
65 : GpgME::Result( error ), d()
66{
67 init( ctx );
68}
69
70void GpgME::ImportResult::init( gpgme_ctx_t ctx ) {
71 if ( !ctx ) {
72 return;
73 }
74 gpgme_import_result_t res = gpgme_op_import_result( ctx );
75 if ( !res ) {
76 return;
77 }
78 d.reset( new Private( *res ) );
79}
80
81make_standard_stuff(ImportResult)
82
83int GpgME::ImportResult::numConsidered() const {
84 return d ? d->res.considered : 0 ;
85}
86
87int GpgME::ImportResult::numKeysWithoutUserID() const {
88 return d ? d->res.no_user_id : 0 ;
89}
90
91int GpgME::ImportResult::numImported() const {
92 return d ? d->res.imported : 0 ;
93}
94
95int GpgME::ImportResult::numRSAImported() const {
96 return d ? d->res.imported_rsa : 0 ;
97}
98
99int GpgME::ImportResult::numUnchanged() const {
100 return d ? d->res.unchanged : 0 ;
101}
102
103int GpgME::ImportResult::newUserIDs() const {
104 return d ? d->res.new_user_ids : 0 ;
105}
106
107int GpgME::ImportResult::newSubkeys() const {
108 return d ? d->res.new_sub_keys : 0 ;
109}
110
111int GpgME::ImportResult::newSignatures() const {
112 return d ? d->res.new_signatures : 0 ;
113}
114
115int GpgME::ImportResult::newRevocations() const {
116 return d ? d->res.new_revocations : 0 ;
117}
118
119int GpgME::ImportResult::numSecretKeysConsidered() const {
120 return d ? d->res.secret_read : 0 ;
121}
122
123int GpgME::ImportResult::numSecretKeysImported() const {
124 return d ? d->res.secret_imported : 0 ;
125}
126
127int GpgME::ImportResult::numSecretKeysUnchanged() const {
128 return d ? d->res.secret_unchanged : 0 ;
129}
130
131int GpgME::ImportResult::notImported() const {
132 return d ? d->res.not_imported : 0 ;
133}
134
135GpgME::Import GpgME::ImportResult::import( unsigned int idx ) const {
136 return Import( d, idx );
137}
138
139std::vector<GpgME::Import> GpgME::ImportResult::imports() const {
140 if ( !d ) {
141 return std::vector<Import>();
142 }
143 std::vector<Import> result;
144 result.reserve( d->imports.size() );
145 for ( unsigned int i = 0 ; i < d->imports.size() ; ++i ) {
146 result.push_back( Import( d, i ) );
147 }
148 return result;
149}
150
151
152
153
154
155
156GpgME::Import::Import( const boost::shared_ptr<ImportResult::Private> & parent, unsigned int i )
157 : d( parent ), idx( i )
158{
159
160}
161
162GpgME::Import::Import() : d(), idx( 0 ) {}
163
164bool GpgME::Import::isNull() const {
165 return !d || idx >= d->imports.size() ;
166}
167
168
169
170
171const char * GpgME::Import::fingerprint() const {
172 return isNull() ? 0 : d->imports[idx]->fpr ;
173}
174
175GpgME::Error GpgME::Import::error() const {
176 return Error( isNull() ? 0 : d->imports[idx]->result );
177}
178
179GpgME::Import::Status GpgME::Import::status() const {
180 if ( isNull() ) {
181 return Unknown;
182 }
183 const unsigned int s = d->imports[idx]->status;
184 unsigned int result = Unknown;
185 if ( s & GPGME_IMPORT_NEW ) {
186 result |= NewKey;
187 }
188 if ( s & GPGME_IMPORT_UID ) {
189 result |= NewUserIDs;
190 }
191 if ( s & GPGME_IMPORT_SIG ) {
192 result |= NewSignatures;
193 }
194 if ( s & GPGME_IMPORT_SUBKEY ) {
195 result |= NewSubkeys;
196 }
197 if ( s & GPGME_IMPORT_SECRET ) {
198 result |= ContainedSecretKey;
199 }
200 return static_cast<Status>( result );
201}
202