-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGUserID.m
209 lines (175 loc) · 4.97 KB
/
GPGUserID.m
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
205
206
207
208
209
//
// GPGUserID.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Fri Dec 27 2002.
//
//
// Copyright (C) 2001-2006 Mac GPG Project.
//
// This code is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License as published by the Free
// Software Foundation; either version 2.1 of the License, or (at your option)
// any later version.
//
// This code 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 Lesser General Public License for more
// details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program; if not, visit <http://www.gnu.org/> or write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
// MA 02111-1307, USA.
//
// More info at <http://macgpg.sourceforge.net/>
//
#include <MacGPGME/GPGUserID.h>
#include <MacGPGME/GPGKeySignature.h>
#include <MacGPGME/GPGPrettyInfo.h>
#include <MacGPGME/GPGInternals.h>
#include <Foundation/Foundation.h>
#define _userID ((gpgme_user_id_t)_internalRepresentation)
@implementation GPGUserID
- (id) retain
{
// See GPGKey.m for more information
[_key retain];
_refCount++;
return self;
}
- (oneway void) release
{
// See GPGKey.m for more information
if(_refCount > 0){
_refCount--;
[_key release];
}
else{
if(_refCount < 0)
NSLog(@"### GPGUserID: _refCount < 0! (%d)", _refCount);
[super release];
}
}
- (void) dealloc
{
if(_signatures != nil)
[_signatures release];
[super dealloc];
}
- (id) copyWithZone:(NSZone *)zone
{
// Implementation is useful to allow use of GPGUserID instances as keys in NSMutableDictionary instances.
return [self retain];
}
- (NSString *) description
{
return [self userID];
}
- (NSString *) userID
{
return GPGStringFromChars(_userID->uid);
}
- (NSString *) name
{
return GPGStringFromChars(_userID->name);
}
- (NSString *) email
{
return GPGStringFromChars(_userID->email);
}
- (NSString *) comment
{
return GPGStringFromChars(_userID->comment);
}
- (GPGValidity) validity
{
return _userID->validity;
}
- (NSString *) validityDescription
{
return GPGValidityDescription([self validity]);
}
- (BOOL) hasBeenRevoked
{
return !!_userID->revoked;
}
- (BOOL) isInvalid
{
return !!_userID->invalid;
}
- (NSArray *) signatures
{
// We cannot force fetch of signatures because when using -refreshKey:
// we need to return a new GPGKey instance, because userIDs could have changed,
// thus self (GPGUserID) could even disappear.
if(_signatures == nil && _userID->signatures != NULL){
// Check that there is a signature; keyID is mandatory, AFAIK
gpgme_key_sig_t aSignature = _userID->signatures;
_signatures = [[NSMutableArray allocWithZone:[self zone]] init];
while(aSignature != NULL){
GPGKeySignature *newSignature = [[GPGKeySignature allocWithZone:[self zone]] initWithKeySignature:aSignature userID:self];
[(NSMutableArray *)_signatures addObject:newSignature];
[newSignature release];
aSignature = aSignature->next;
}
}
return _signatures;
}
- (GPGKey *) key
{
return _key;
}
@end
@implementation GPGUserID(GPGInternals)
- (id) initWithInternalRepresentation:(void *)aPtr key:(GPGKey *)key
{
if(self = [self initWithInternalRepresentation:aPtr])
((GPGUserID *)self)->_key = key; // Not retained
return self;
}
- (NSDictionary *) dictionaryRepresentation
/*
* Returns a dictionary that looks something like this:
*
* {
* comment = "Gordon Worley <[email protected]>";
* email = "Gordon Worley <[email protected]>";
* invalid = 0;
* name = "Gordon Worley <[email protected]>";
* raw = "Gordon Worley <[email protected]>";
* revoked = 0;
* validity = 1;
* }
*
* or
*
* {
* comment = "";
* email = "";
* invalid = 0;
* name = "[image of size 2493]";
* raw = "[image of size 2493]";
* revoked = 0;
* validity = 1;
* }
*/
{
NSMutableDictionary *aDictionary = [NSMutableDictionary dictionaryWithCapacity:7];
NSString *aString;
[aDictionary setObject:[NSNumber numberWithBool:[self isInvalid]] forKey:@"invalid"];
[aDictionary setObject:[NSNumber numberWithBool:[self hasBeenRevoked]] forKey:@"revoked"];
[aDictionary setObject:[self userID] forKey:@"raw"];
aString = [self name];
if(aString != nil)
[aDictionary setObject:aString forKey:@"name"];
aString = [self email];
if(aString != nil)
[aDictionary setObject:aString forKey:@"email"];
aString = [self comment];
if(aString != nil)
[aDictionary setObject:aString forKey:@"comment"];
[aDictionary setObject:[NSNumber numberWithInt:[self validity]] forKey:@"validity"];
return aDictionary;
}
@end