-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProfileReader.m
82 lines (57 loc) · 2.05 KB
/
ProfileReader.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
//
// ProfileReader.m
// SSHLauncher
//
// Created by Martin Jansen on 23.09.10.
// Copyright 2010 Martin Jansen. All rights reserved.
//
#import "ProfileReader.h"
#import "Profile.h"
#import "Section.h"
#import "NSFileManager+MJAdditions.h"
@interface ProfileReader (PRIVATE)
- (Section*) parseProfilesFrom:(NSArray*)anArray;
- (Profile*) parseProfileFrom:(NSDictionary *)aDictionary;
@end
@implementation ProfileReader
- (Section*) getProfiles {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [fileManager applicationSupportDirectory];
NSString* filename = [NSString stringWithFormat:@"%@/Profiles.plist", path];
if (![fileManager fileExistsAtPath:filename]) {
return nil;
}
NSArray* plist = [NSArray arrayWithContentsOfFile:filename];
return [self parseProfilesFrom:plist];
}
- (Section*) parseProfilesFrom: (NSArray*) anArray {
Section* result = [[Section alloc] init];
result.profiles = [NSMutableArray arrayWithCapacity:0];
result.subsections = [NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < [anArray count]; i++) {
id element = [anArray objectAtIndex:i];
if ([element isKindOfClass: [NSDictionary class]]) {
Profile* connection = [self parseProfileFrom:element];
[result.profiles addObject:connection];
continue;
}
if ([element isKindOfClass: [NSString class]]) {
result.title = (NSString*)element;
continue;
}
if ([element isKindOfClass: [NSArray class]]) {
[result.subsections addObject:[self parseProfilesFrom:element]];
}
}
return result;
}
- (Profile*) parseProfileFrom: (NSDictionary*) aDictionary {
Profile* connection = [[Profile alloc] init];
NSString* key;
NSEnumerator *enumerator = [aDictionary keyEnumerator];
while (key = [enumerator nextObject]) {
[connection setValue:[aDictionary valueForKey:key] forKey:key];
}
return connection;
}
@end