-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGKeyGroup.m
124 lines (101 loc) · 3.53 KB
/
GPGKeyGroup.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
//
// GPGKeyGroup.m
// MacGPGME
//
// Created by davelopper at users.sourceforge.net on Wed Oct 6 2004.
//
//
// 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 "GPGKeyGroup.h"
#include "GPGContext.h"
#include "GPGOptions.h"
#include "GPGInternals.h"
@implementation GPGKeyGroup
- (void) dealloc
{
[_name release];
[_keys release];
[super dealloc];
}
- (NSString *) name
{
return _name;
}
- (NSArray *) keys
{
return _keys;
}
- (NSString *) debugDescription
{
return [NSString stringWithFormat:@"<%@: %p> name = %@, keys = %@", NSStringFromClass([self class]), self, [self name], [self keys]];
}
+ (id) createKeyGroupNamed:(NSString *)name withKeys:(NSArray *)keys
{
GPGOptions *options;
NSArray *optionNames;
NSArray *optionValues;
NSCharacterSet *forbiddenChars = [NSCharacterSet characterSetWithCharactersInString:@"=\n"];
NSString *newGroupDefValue;
GPGKeyGroup *newGroup;
unsigned i, aCount;
BOOL foundGroup = NO;
name = [name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
NSParameterAssert(name != nil && [name length] > 0 && [name rangeOfCharacterFromSet:forbiddenChars].location == NSNotFound);
newGroupDefValue = [NSString stringWithFormat:@"%@=%@", name, [[keys valueForKey:@"keyID"] componentsJoinedByString:@" "]];
options = [[GPGOptions alloc] init];
optionNames = [options optionNames];
optionValues = [options optionValues];
aCount = [optionNames count];
for(i = 0; i < aCount; i++){
if([[optionNames objectAtIndex:i] isEqualToString:@"group"]){
NSDictionary *aDict = [GPGContext parsedGroupDefinitionLine:[optionValues objectAtIndex:i]];
if([[aDict objectForKey:@"name"] isEqualToString:name]){
if(!foundGroup){
[options setOptionValue:newGroupDefValue atIndex:i];
foundGroup = YES;
}
else{
[options removeOptionAtIndex:i];
aCount--;
i--;
}
}
}
}
if(!foundGroup)
[options addOptionNamed:@"group" value:newGroupDefValue state:YES];
[options saveOptions];
[options release];
newGroup = [[GPGKeyGroup alloc] initWithName:name keys:keys];
return [newGroup autorelease];
}
@end
@implementation GPGKeyGroup(GPGInternals)
- (id) initWithName:(NSString *)name keys:(NSArray *)keys
{
if((self = [super init]) != nil){
NSZone *aZone = [self zone];
_name = [name copyWithZone:aZone];
_keys = [keys copyWithZone:aZone];
}
return self;
}
@end