-
Notifications
You must be signed in to change notification settings - Fork 4
/
GPGOptions.m
765 lines (644 loc) · 26.6 KB
/
GPGOptions.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
//
// GPGOptions.m
// GPGPreferences and MacGPGME
//
// Created by davelopper at users.sourceforge.net on Sun Feb 03 2002.
//
//
// Copyright (C) 2002-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/GPGOptions.h>
#include <MacGPGME/GPGEngine.h>
static NSString *gnupgVersion = nil;
NSString * const GPGUserDefaultsSuiteName = @"net.sourceforge.macgpg";
NSString * const GPGOpenPGPExecutablePathKey = @"GPGOpenPGPExecutablePath";
NSString * const GPGDefaultsDidChangeNotification = @"GPGDefaultsDidChangeNotification";
@interface GPGOptions(Private)
- (void) doSaveOptions;
@end
@interface NSMutableArray(GPGOptions)
- (unsigned) gpgMoveObjectsAtIndexes:(NSArray *)indexes toIndex:(unsigned)index;
@end
@implementation GPGOptions
+ (NSString *) activeEnvironmentVariableValueForName:(NSString *)name
{
return [[[NSProcessInfo processInfo] environment] objectForKey:name];
}
+ (NSString *) futureEnvironmentVariableValueForName:(NSString *)name
{
NSString *aDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@".MacOSX"];
NSString *filename = [aDirectory stringByAppendingPathComponent:@"environment.plist"];
NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
if(environment == nil)
return nil;
else
return [environment objectForKey:name];
}
+ (void) setFutureEnvironmentVariableValue:(NSString *)value forName:(NSString *)name
{
// We modify ~/.MacOSX/environment.plist
NSString *aDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@".MacOSX"];
NSString *filename = [aDirectory stringByAppendingPathComponent:@"environment.plist"];
NSMutableDictionary *environment = [NSMutableDictionary dictionaryWithContentsOfFile:filename];
NSString *activeValue;
BOOL isModified;
if(environment == nil){
environment = [NSMutableDictionary dictionary];
activeValue = nil;
}
else
activeValue = [environment objectForKey:name];
if(activeValue == nil){
if(value == nil)
isModified = NO;
else
isModified = YES;
}
else{
if(value == nil)
isModified = YES;
else
isModified = ![activeValue isEqualToString:value];
}
if(isModified){
NSFileManager *defaultManager = [NSFileManager defaultManager];
BOOL isDirectory;
if(value == nil)
[environment removeObjectForKey:name];
else
[environment setObject:value forKey:name];
if([defaultManager fileExistsAtPath:aDirectory isDirectory:&isDirectory])
NSAssert1(isDirectory, @"'%@' is not a directory.", aDirectory);
else
NSAssert1([defaultManager createDirectoryAtPath:aDirectory attributes:nil], @"Unable to create directory '%@'", aDirectory);
NSAssert1([environment writeToFile:filename atomically:YES], @"Unable to write file '%@'", filename);
}
}
+ (NSString *) defaultHomeDirectory
{
#warning Use +[GPGEngine defaultHomeDirectory]
return [NSHomeDirectory() stringByAppendingPathComponent:@".gnupg"];
}
+ (NSString *) activeHomeDirectory
{
#warning Use -[GPGEngine homeDirectory]
NSString *homeDirectory = [self activeEnvironmentVariableValueForName:@"GNUPGHOME"];
if(homeDirectory == nil)
return [self defaultHomeDirectory];
else
return homeDirectory;
}
+ (NSString *) homeDirectory
{
NSString *homeDirectory = [self futureEnvironmentVariableValueForName:@"GNUPGHOME"];
if(homeDirectory == nil)
return [self defaultHomeDirectory];
else
return homeDirectory;
}
+ (void) setHomeDirectory:(NSString *)homeDirectory
{
if(homeDirectory != nil && [homeDirectory rangeOfCharacterFromSet:[[NSCharacterSet whitespaceCharacterSet] invertedSet]].length > 0)
[self setFutureEnvironmentVariableValue:homeDirectory forName:@"GNUPGHOME"];
else
[self setFutureEnvironmentVariableValue:nil forName:@"GNUPGHOME"];
}
+ (BOOL) homeDirectoryChanged
{
return (![[[self homeDirectory] stringByStandardizingPath] isEqualToString:[[self activeHomeDirectory] stringByStandardizingPath]]);
}
+ (NSString *) gpgPath
{
return [[GPGEngine engineForProtocol:GPGOpenPGPProtocol] executablePath];
}
+ (NSString *) optionsFilename
{
// FIXME: valid only for GPGPreferences; should not be used outside of this context
return [[self homeDirectory] stringByAppendingPathComponent:@"gpg.conf"];
}
/*
* Subscribed in +[GPGObject initialize]
*/
+ (void) defaultsDidChange:(NSNotification *)notification
{
[[NSUserDefaults standardUserDefaults] synchronize];
}
+ (void) setDefaultValue:(id)value forKey:(NSString *)key
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableDictionary *aDict;
[defaults synchronize];
aDict = [NSMutableDictionary dictionaryWithDictionary:[defaults persistentDomainForName:GPGUserDefaultsSuiteName]];
if(value)
[aDict setObject:value forKey:key];
else
[aDict removeObjectForKey:key];
[defaults setPersistentDomain:aDict forName:GPGUserDefaultsSuiteName];
[defaults synchronize];
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:GPGDefaultsDidChangeNotification object:nil];
}
- (void) parseOptionsFromLines:(NSArray *)lines save:(BOOL)save
{
unsigned i, lineCount;
[optionFileLines setArray:lines];
if(save)
[self doSaveOptions];
lineCount = [optionFileLines count];
for(i = 0; i < lineCount; i++){
NSString *aLine = [optionFileLines objectAtIndex:i];
unsigned lineLength = [aLine length];
if(lineLength > 0){
BOOL isCommented;
unsigned startIndex, endIndex;
unsigned j = 0;
NSString *aValue;
// Trim spacers
for(; j < lineLength; j++)
if(!isspace([aLine characterAtIndex:j]))
break;
if(j >= lineLength)
continue;
isCommented = ([aLine characterAtIndex:j] == '#');
if(isCommented && (lineLength - j) == 1)
continue;
if(isCommented && isspace([aLine characterAtIndex:j + 1]))
// A line beginning with a # followed by a spacer is considered as a comment for GPGPreferences
continue;
// else _we_ consider it as a disabled option
// Note that if an option value begins or ends with a space, or contains a carriage return,
// then it must be double-quoted, or \n must be escaped
// Option name terminates at the first non spacer character or at the end of line
// First we skip prepending spaces
for(startIndex = j + (isCommented ? 1:0); startIndex < lineLength; startIndex++){
if(!isspace([aLine characterAtIndex:startIndex]))
break;
}
if(startIndex >= lineLength)
continue;
// and find the end of the word
for(endIndex = startIndex + 1; endIndex < lineLength; endIndex++){
if(isspace([aLine characterAtIndex:endIndex]))
break;
}
[optionNames addObject:[aLine substringWithRange:NSMakeRange(startIndex, endIndex - startIndex)]];
[optionLineNumbers addObject:[NSNumber numberWithUnsignedInt:i]];
[optionStates addObject:[NSNumber numberWithBool:!isCommented]];
// Now we skip spaces between name and value
for(startIndex = endIndex + 1; startIndex < lineLength; startIndex++){
if(!isspace([aLine characterAtIndex:startIndex]))
break;
}
if(startIndex >= lineLength){
// No value, only an option name
[optionValues addObject:@""];
continue;
}
// and find the end of the value, backwards
for(endIndex = lineLength - 1; endIndex > startIndex; endIndex--){
if(!isspace([aLine characterAtIndex:endIndex]))
break;
}
aValue = [aLine substringWithRange:NSMakeRange(startIndex, endIndex - startIndex + 1)];
if([aValue characterAtIndex:0] == '"' && [aValue length] > 1 && [aValue characterAtIndex:[aValue length] - 1] == '"')
aValue = [aValue substringWithRange:NSMakeRange(1, [aValue length] - 2)]; // We unquote it
[optionValues addObject:aValue];
}
}
}
- (void) reloadOptions
{
NSString *filename = path;
NSString *optionsAsString;
NSData *fileData;
BOOL wasInUnicode = NO;
[optionFileLines removeAllObjects];
[optionNames removeAllObjects];
[optionValues removeAllObjects];
[optionStates removeAllObjects];
[optionLineNumbers removeAllObjects];
fileData = [[NSData alloc] initWithContentsOfFile:filename];
// Check whether file has been saved as Unicode (it shouldn't, but who knows...)
if([fileData length] >= 2 && !([fileData length] & 1) && (((short int *)[fileData bytes])[0] == (short int)0xFEFF || ((short int *)[fileData bytes])[0] == (short int)0x0FFFE)){
optionsAsString = [[NSString alloc] initWithData:fileData encoding:NSUnicodeStringEncoding];
wasInUnicode = YES;
}
else
optionsAsString = [[NSString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
[fileData release];
if(optionsAsString == nil){
NSLog(@"%s: Unable to read file %@", __PRETTY_FUNCTION__, filename);
// If we were unable to read it, gpg is probably unable too
optionsAsString = @"";
}
[self parseOptionsFromLines:[optionsAsString componentsSeparatedByString:@"\n"] save:wasInUnicode];
[optionsAsString release];
hasModifications = NO;
}
- (NSArray *) optionLines
{
return optionFileLines;
}
- (id) init
{
return [self initWithPath:[[self class] optionsFilename]];
}
- (id) initWithPath:(NSString *)aPath
{
if(self = [super init]){
NSParameterAssert(aPath != nil);
path = [aPath copy];
optionFileLines = [[NSMutableArray alloc] initWithCapacity:100];
optionNames = [[NSMutableArray alloc] initWithCapacity:20];
optionValues = [[NSMutableArray alloc] initWithCapacity:20];
optionStates = [[NSMutableArray alloc] initWithCapacity:20];
optionLineNumbers = [[NSMutableArray alloc] initWithCapacity:20];
[self reloadOptions];
}
return self;
}
- (void) dealloc
{
[optionFileLines release];
[optionNames release];
[optionValues release];
[optionStates release];
[optionLineNumbers release];
[path release];
[super dealloc];
}
- (void) doSaveOptions
{
NSString *content = [optionFileLines componentsJoinedByString:@"\n"];
if(![content hasSuffix:@"\n"])
content = [content stringByAppendingString:@"\n"];
NSAssert1([[content dataUsingEncoding:NSUTF8StringEncoding] writeToFile:path atomically:YES], @"Unable to save options in %@", path);
}
- (void) saveOptions
{
// TODO: Save only if modified
[self doSaveOptions];
// TODO: Test new options file by running gpg (gpg: /Users/kindov/.gnupg/options:21: invalid option)
[self reloadOptions];
}
- (NSString *) normalizedValue:(NSString *)value
{
// Replace \n occurences by \\n
// Enclose with double-quotes if necessary
if(value == nil)
return value;
else{
NSMutableString *newValue = [NSMutableString stringWithString:value];
int i;
BOOL needsDoubleQuotes = NO;
BOOL isLastChar = YES;
for(i = [newValue length] - 1; i >= 0; i--){
unichar aChar = [newValue characterAtIndex:i];
if(isLastChar){
isLastChar = NO;
needsDoubleQuotes = isspace(aChar);
}
if(aChar == '\n')
[newValue replaceCharactersInRange:NSMakeRange(i, 1) withString:@"\\n"];
else if(i == 0)
needsDoubleQuotes = (needsDoubleQuotes || isspace(aChar));
}
if(needsDoubleQuotes)
newValue = [NSString stringWithFormat:@"\"%@\"", newValue];
return newValue;
}
}
- (void) updateOptionLineAtIndex:(unsigned)index
{
NSString *newLine = [NSString stringWithFormat:@"%@%@ %@", ([[optionStates objectAtIndex:index] boolValue] ? @"":@"#"), [optionNames objectAtIndex:index], [optionValues objectAtIndex:index]];
[optionFileLines replaceObjectAtIndex:[[optionLineNumbers objectAtIndex:index] unsignedIntValue] withObject:newLine];
}
- (void) setOptionValue:(NSString *)value atIndex:(unsigned)index
{
[optionValues replaceObjectAtIndex:index withObject:[self normalizedValue:value]];
[self updateOptionLineAtIndex:index];
}
- (void) setEmptyOptionValueAtIndex:(unsigned)index
{
[self setOptionValue:@"\"\"" atIndex:index];
}
- (void) setOptionName:(NSString *)name atIndex:(unsigned)index
{
[optionNames replaceObjectAtIndex:index withObject:name];
[self updateOptionLineAtIndex:index];
}
- (void) setOptionState:(BOOL)flag atIndex:(unsigned)index
{
[optionStates replaceObjectAtIndex:index withObject:[NSNumber numberWithBool:flag]];
[self updateOptionLineAtIndex:index];
}
- (void) addOptionNamed:(NSString *)name
{
[optionNames addObject:name];
[optionValues addObject:@""];
[optionStates addObject:[NSNumber numberWithBool:NO]];
[optionLineNumbers addObject:[NSNumber numberWithUnsignedInt:[optionFileLines count]]];
[optionFileLines addObject:[@"#" stringByAppendingString:name]];
}
- (void) addOptionNamed:(NSString *)name value:(NSString *)value state:(BOOL)state
{
[optionNames addObject:name];
[optionValues addObject:value];
[optionStates addObject:[NSNumber numberWithBool:state]];
[optionLineNumbers addObject:[NSNumber numberWithUnsignedInt:[optionFileLines count]]];
[optionFileLines addObject:[NSString stringWithFormat:@"%@%@ %@", (state ? @"":@"#"), name, value]];
}
- (void) insertOptionNamed:(NSString *)name atIndex:(unsigned)index
{
unsigned maxIndex = [optionNames count];
[optionNames insertObject:name atIndex:index];
[optionValues insertObject:@"" atIndex:index];
[optionStates insertObject:[NSNumber numberWithBool:NO] atIndex:index];
[optionFileLines insertObject:[@"#" stringByAppendingString:name] atIndex:[[optionLineNumbers objectAtIndex:index] unsignedIntValue]];
[optionLineNumbers insertObject:[optionLineNumbers objectAtIndex:index] atIndex:index];
for(index++, maxIndex++; index < maxIndex; index++)
[optionLineNumbers replaceObjectAtIndex:index withObject:[NSNumber numberWithUnsignedInt:[[optionLineNumbers objectAtIndex:index] unsignedIntValue] + 1]];
}
- (void) removeOptionAtIndex:(unsigned)index
{
unsigned maxIndex = [optionNames count];
[optionNames removeObjectAtIndex:index];
[optionValues removeObjectAtIndex:index];
[optionFileLines removeObjectAtIndex:[[optionLineNumbers objectAtIndex:index] unsignedIntValue]];
[optionLineNumbers removeObjectAtIndex:index];
[optionStates removeObjectAtIndex:index];
for(maxIndex--; index < maxIndex; index++)
[optionLineNumbers replaceObjectAtIndex:index withObject:[NSNumber numberWithUnsignedInt:[[optionLineNumbers objectAtIndex:index] unsignedIntValue] - 1]];
}
- (NSArray *) optionNames
{
return optionNames;
}
- (NSArray *) optionValues
{
return optionValues;
}
- (NSArray *) optionStates
{
return optionStates;
}
- (NSString *) optionValueForName:(NSString *)name
{
int anIndex = [optionNames count] - 1;
NSString *lastValue = nil;
BOOL lastValueIsActive = NO;
for(; anIndex >= 0; anIndex--)
if([[optionNames objectAtIndex:anIndex] isEqualToString:name]){
if(lastValue == nil){
lastValue = [optionValues objectAtIndex:anIndex];
lastValueIsActive = [[optionStates objectAtIndex:anIndex] boolValue];
}
else if(!lastValueIsActive && [[optionStates objectAtIndex:anIndex] boolValue]){
lastValue = [optionValues objectAtIndex:anIndex];
lastValueIsActive = YES;
}
}
return lastValue;
}
- (NSArray *) optionValuesForName:(NSString *)name activeOnly:(BOOL)activeOnly
{
int anIndex = 0;
int max = [optionNames count];
NSMutableArray *values = [NSMutableArray array];
for(; anIndex < max; anIndex++)
if([[optionNames objectAtIndex:anIndex] isEqualToString:name] && (!activeOnly || [[optionStates objectAtIndex:anIndex] boolValue]))
[values addObject:[optionValues objectAtIndex:anIndex]];
return values;
}
- (NSArray *) activeOptionValuesForName:(NSString *)name
{
return [self optionValuesForName:name activeOnly:YES];
}
- (NSArray *) allOptionValuesForName:(NSString *)name
{
return [self optionValuesForName:name activeOnly:NO];
}
- (void) setEmptyOptionValueForName:(NSString *)name
{
[self setOptionValue:@"\"\"" forName:name];
}
- (void) setOptionValue:(NSString *)value forName:(NSString *)name
{
int anIndex = 0, maxIndex = [optionNames count];
int deletedLineNumber = -1;
value = [self normalizedValue:value];
for(; anIndex < maxIndex; anIndex++){
if(deletedLineNumber > 0)
[optionLineNumbers replaceObjectAtIndex:anIndex withObject:[NSNumber numberWithUnsignedInt:[[optionLineNumbers objectAtIndex:anIndex] unsignedIntValue] - deletedLineNumber]];
if([[optionNames objectAtIndex:anIndex] isEqualToString:name]){
deletedLineNumber++;
if(value == nil || deletedLineNumber > 0){
if(deletedLineNumber == 0)
deletedLineNumber++;
[optionFileLines removeObjectAtIndex:[[optionLineNumbers objectAtIndex:anIndex] unsignedIntValue]];
[optionNames removeObjectAtIndex:anIndex];
[optionStates removeObjectAtIndex:anIndex];
[optionValues removeObjectAtIndex:anIndex];
[optionLineNumbers removeObjectAtIndex:anIndex];
anIndex--;
maxIndex--;
}
else{
[optionValues replaceObjectAtIndex:anIndex withObject:value];
[self updateOptionLineAtIndex:anIndex];
}
}
}
if(deletedLineNumber < 0 && value != nil){
[optionNames addObject:name];
[optionStates addObject:[NSNumber numberWithBool:YES]];
[optionValues addObject:value];
[optionLineNumbers addObject:[NSNumber numberWithUnsignedInt:[optionFileLines count]]];
[optionFileLines addObject:[NSString stringWithFormat:@"%@ %@", name, value]];
}
}
- (BOOL) optionStateForName:(NSString *)name
{
int anIndex = [optionNames count] - 1;
for(; anIndex >= 0; anIndex--)
if([[optionNames objectAtIndex:anIndex] isEqualToString:name])
if([[optionStates objectAtIndex:anIndex] boolValue])
return YES;
return NO;
}
- (void) setOptionState:(BOOL)state forName:(NSString *)name
{
int anIndex = 0, maxIndex = [optionNames count];
int deletedLineNumber = -1;
for(; anIndex < maxIndex; anIndex++){
if(deletedLineNumber > 0)
[optionLineNumbers replaceObjectAtIndex:anIndex withObject:[NSNumber numberWithUnsignedInt:[[optionLineNumbers objectAtIndex:anIndex] unsignedIntValue] - deletedLineNumber]];
if([[optionNames objectAtIndex:anIndex] isEqualToString:name]){
deletedLineNumber++;
if(deletedLineNumber > 0){
[optionFileLines removeObjectAtIndex:[[optionLineNumbers objectAtIndex:anIndex] unsignedIntValue]];
[optionNames removeObjectAtIndex:anIndex];
[optionStates removeObjectAtIndex:anIndex];
[optionValues removeObjectAtIndex:anIndex];
[optionLineNumbers removeObjectAtIndex:anIndex];
maxIndex--;
}
else{
[optionStates replaceObjectAtIndex:anIndex withObject:[NSNumber numberWithBool:state]];
[self updateOptionLineAtIndex:anIndex];
}
}
}
if(deletedLineNumber == -1 && state)
[self setOptionValue:@"" forName:name];
}
- (NSArray *) _subOptionsForName:(NSString *)optionName
{
NSString *subOptionsString = [self optionValueForName:optionName];
NSArray *optionParameters = [subOptionsString componentsSeparatedByString:@","];
NSEnumerator *anEnum = [optionParameters objectEnumerator];
NSMutableArray *subOptions = [NSMutableArray array];
NSString *aString;
while(aString = [anEnum nextObject])
[subOptions addObjectsFromArray:[aString componentsSeparatedByString:@" "]];
[subOptions removeObject:@""]; // Removes all occurences
return subOptions;
}
- (BOOL) subOptionState:(NSString *)subOptionName forName:(NSString *)optionName
{
if([self optionStateForName:optionName]){
NSArray *optionParameters;
int setIndex, unsetIndex;
optionParameters = [[[self _subOptionsForName:optionName] reverseObjectEnumerator] allObjects]; // Reversed array
setIndex = [optionParameters indexOfObject:subOptionName];
if(setIndex == NSNotFound){
// TODO: name=value
}
unsetIndex = [optionParameters indexOfObject:[@"no-" stringByAppendingString:subOptionName]];
return (setIndex < unsetIndex);
}
else
// FIXME: In fact we should return the default value
return NO;
}
- (NSString *) subOptionValue:(NSString *)subOptionName state:(BOOL *)statePtr forName:(NSString *)optionName
{
if([self optionStateForName:optionName]){
NSEnumerator *paramEnum = [[self _subOptionsForName:optionName] reverseObjectEnumerator];
NSString *eachParam;
NSString *subOptionNameEquals = [subOptionName stringByAppendingString:@"="];
while(eachParam = [paramEnum nextObject]){
if([eachParam hasPrefix:subOptionNameEquals])
return [eachParam substringFromIndex:[subOptionNameEquals length]];
}
return nil;
}
else
// FIXME: In fact we should return the default value
return nil;
}
- (void) setSubOption:(NSString *)subOptionName state:(BOOL)state forName:(NSString *)optionName
{
NSString *disabledSubOptionName = [@"no-" stringByAppendingString:subOptionName];
NSMutableArray *subOptions = [NSMutableArray arrayWithArray:[self _subOptionsForName:optionName]];
[subOptions removeObject:disabledSubOptionName];
[subOptions removeObject:subOptionName];
[subOptions addObject:(state ? subOptionName:disabledSubOptionName)];
[self setOptionValue:[subOptions componentsJoinedByString:@","] forName:optionName];
[self setOptionState:YES forName:optionName];
}
- (void) setSubOption:(NSString *)subOptionName value:(NSString *)value state:(BOOL)state forName:(NSString *)optionName
{
NSMutableArray *subOptions = [NSMutableArray arrayWithArray:[self _subOptionsForName:optionName]];
NSEnumerator *subOptionEnum = [[NSArray arrayWithArray:subOptions] objectEnumerator];
NSString *eachSubOption;
NSString *subOptionNameEquals = [subOptionName stringByAppendingString:@"="];
while(eachSubOption = [subOptionEnum nextObject])
if([eachSubOption hasPrefix:subOptionNameEquals])
[subOptions removeObject:eachSubOption];
if(state)
[subOptions addObject:[subOptionNameEquals stringByAppendingString:value]];
[self setOptionValue:[subOptions componentsJoinedByString:@","] forName:optionName];
[self setOptionState:YES forName:optionName];
}
- (unsigned) moveOptionsAtIndexes:(NSArray *)indexes toIndex:(unsigned)index
{
NSEnumerator *anEnum = [indexes objectEnumerator];
NSNumber *anIndex;
NSMutableArray *lineIndexes = [NSMutableArray arrayWithCapacity:[indexes count]];
unsigned lineIndex;
while(anIndex = [anEnum nextObject])
[lineIndexes addObject:[optionLineNumbers objectAtIndex:[anIndex unsignedIntValue]]];
if(index == [optionLineNumbers count])
lineIndex = [optionFileLines count];
else
lineIndex = [[optionLineNumbers objectAtIndex:index] unsignedIntValue];
lineIndex = [optionFileLines gpgMoveObjectsAtIndexes:lineIndexes toIndex:lineIndex];
hasModifications = YES;
[self saveOptions];
return [optionLineNumbers indexOfObject:[NSNumber numberWithUnsignedInt:lineIndex]];
}
+ (NSString *) _gnupgVersion
{
// Returns cached value, if any, else get version
if(gnupgVersion == nil)
return [self gnupgVersion];
else
return gnupgVersion;
}
+ (NSString *) gnupgVersion
{
GPGEngine *anEngine = [GPGEngine engineForProtocol:GPGOpenPGPProtocol];
return [anEngine version];
}
@end
@implementation NSMutableArray(GPGOptions)
- (unsigned) gpgMoveObjectsAtIndexes:(NSArray *)indexes toIndex:(unsigned)targetIndex
{
NSEnumerator *anEnum;
NSNumber *anIndex;
NSArray *originalArray = [NSArray arrayWithArray:self];
unsigned lowerOffset = 0, upperOffset = 0;
BOOL adding = (targetIndex == [self count]);
unsigned newIndex = targetIndex;
indexes = [indexes sortedArrayUsingSelector:@selector(compare:)];
anEnum = [indexes objectEnumerator];
while(anIndex = [anEnum nextObject]){
if(adding)
[self addObject:[originalArray objectAtIndex:[anIndex unsignedIntValue]]];
else
[self insertObject:[originalArray objectAtIndex:[anIndex unsignedIntValue]] atIndex:(targetIndex + upperOffset)];
upperOffset++;
}
anEnum = [indexes objectEnumerator];
while(anIndex = [anEnum nextObject]){
unsigned index = [anIndex unsignedIntValue];
if(index < targetIndex){
[self removeObjectAtIndex:index + lowerOffset];
newIndex--;
lowerOffset--;
}
else{
[self removeObjectAtIndex:index + upperOffset + lowerOffset];
upperOffset--;
}
}
return newIndex;
}
@end