summaryrefslogtreecommitdiffstats
path: root/libtdenetwork/gpgmepp/importresult.cpp
blob: 3b4b713c7de09a1e902a1e083af4dc81dedfb9d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* importresult.cpp - wraps a gpgme import result
   Copyright (C) 2004 Klarälvdalens Datakonsult AB

   This file is part of GPGME++.
 
   GPGME++ is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
 
   GPGME++ is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GPGME++; if not, write to the Free Software Foundation,
   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <gpgmepp/importresult.h>
#include "shared.h"
#include "result_p.h"

#include <gpgme.h>
#include <cstring>
#include <cstdlib>
#include <cstdlib>

class GpgME::ImportResult::Private : public GpgME::Shared {
public:
  Private( const _gpgme_op_import_result & r ) : Shared(), res( r ) {
    // copy recursively, using compiler-generated copy ctor.
    // We just need to handle the pointers in the structs:
    for ( gpgme_import_status_t is = r.imports ; is ; is = is->next ) {
      gpgme_import_status_t copy = new _gpgme_import_status( *is );
      copy->fpr = strdup( is->fpr );
      copy->next = 0;
      imports.push_back( copy );
    }
    res.imports = 0;
  }
  ~Private() {
    for ( std::vector<gpgme_import_status_t>::iterator it = imports.begin() ; it != imports.end() ; ++it ) {
      std::free( (*it)->fpr );
      delete *it; *it = 0;
    }
  }

  _gpgme_op_import_result res;
  std::vector<gpgme_import_status_t> imports;
};

GpgME::ImportResult::ImportResult( gpgme_ctx_t ctx, int error )
  : GpgME::Result( error ), d( 0 )
{
  if ( error || !ctx )
    return;
  gpgme_import_result_t res = gpgme_op_import_result( ctx );
  if ( !res )
    return;
  d = new Private( *res );
  d->ref();
}

make_standard_stuff(ImportResult)

int GpgME::ImportResult::numConsidered() const {
  return d ? d->res.considered : 0 ;
}

int GpgME::ImportResult::numKeysWithoutUserID() const {
  return d ? d->res.no_user_id : 0 ;
}

int GpgME::ImportResult::numImported() const {
  return d ? d->res.imported : 0 ;
}

int GpgME::ImportResult::numRSAImported() const {
  return d ? d->res.imported_rsa : 0 ;
}

int GpgME::ImportResult::numUnchanged() const {
  return d ? d->res.unchanged : 0 ;
}

int GpgME::ImportResult::newUserIDs() const {
  return d ? d->res.new_user_ids : 0 ;
}

int GpgME::ImportResult::newSubkeys() const {
  return d ? d->res.new_sub_keys : 0 ;
}

int GpgME::ImportResult::newSignatures() const {
  return d ? d->res.new_signatures : 0 ;
}

int GpgME::ImportResult::newRevocations() const {
  return d ? d->res.new_revocations : 0 ;
}

int GpgME::ImportResult::numSecretKeysConsidered() const {
  return d ? d->res.secret_read : 0 ;
}

int GpgME::ImportResult::numSecretKeysImported() const {
  return d ? d->res.secret_imported : 0 ;
}

int GpgME::ImportResult::numSecretKeysUnchanged() const {
  return d ? d->res.secret_unchanged : 0 ;
}

int GpgME::ImportResult::notImported() const {
  return d ? d->res.not_imported : 0 ;
}

GpgME::Import GpgME::ImportResult::import( unsigned int idx ) const {
  return Import( d, idx );
}

std::vector<GpgME::Import> GpgME::ImportResult::imports() const {
  if ( !d )
    return std::vector<Import>();
  std::vector<Import> result;
  result.reserve( d->imports.size() );
  for ( unsigned int i = 0 ; i < d->imports.size() ; ++i )
    result.push_back( Import( d, i ) );
  return result;
}






GpgME::Import::Import( ImportResult::Private * parent, unsigned int i )
  : d( parent ), idx( i )
{
  if ( d )
    d->ref();
}

GpgME::Import::Import() : d( 0 ), idx( 0 ) {}

GpgME::Import::Import( const Import & other )
  : d( other.d ), idx( other.idx )
{
  if ( d )
    d->ref();
}

GpgME::Import::~Import() {
  if ( d )
    d->unref();
}

const GpgME::Import & GpgME::Import::operator=( const Import & other ) {
  if ( this->d != other.d ) {
    if ( other.d )
      other.d->ref();
    if ( this->d )
      this->d->unref();
    this->d = other.d;
  }

  this->idx = other.idx;
  return *this;
}


bool GpgME::Import::isNull() const {
  return !d || idx >= d->imports.size() ;
}




const char * GpgME::Import::fingerprint() const {
  return isNull() ? 0 : d->imports[idx]->fpr ;
}

GpgME::Error GpgME::Import::error() const {
  return isNull() ? 0 : d->imports[idx]->result ;
}

GpgME::Import::tqStatus GpgME::Import::status() const {
  if ( isNull() )
    return Unknown;
  unsigned int s = d->imports[idx]->status;
  unsigned int result = Unknown;
  if ( s & GPGME_IMPORT_NEW )    result |= NewKey;
  if ( s & GPGME_IMPORT_UID )    result |= NewUserIDs;
  if ( s & GPGME_IMPORT_SIG )    result |= NewSignatures;
  if ( s & GPGME_IMPORT_SUBKEY ) result |= NewSubkeys;
  if ( s & GPGME_IMPORT_SECRET ) result |= ContainedSecretKey;
  return static_cast<tqStatus>( result );
}