-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVolumeInfo.m
More file actions
149 lines (125 loc) · 3.44 KB
/
VolumeInfo.m
File metadata and controls
149 lines (125 loc) · 3.44 KB
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
//
// VolumeInfo.m
// MapSelect
//
// Created by Michael Klein on 2011-05-23.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "VolumeInfo.h"
@implementation VolumeInfo
@synthesize volumePath;
@synthesize currentMap;
@synthesize maps;
+ (id)volumeInfoWithPath:(NSString*)p
{
return [[[VolumeInfo alloc] initWithPath:p] autorelease];
}
- (id)initWithPath:(NSString*)p
{
if ((self = [super init]) != nil)
{
[self setVolumePath:p];
if (![self findMaps])
{
[self release];
return nil;
}
}
return self;
}
- (BOOL)findMaps
{
NSFileManager * fm = [NSFileManager defaultManager];
NSMutableArray *dirs = [NSMutableArray array];
NSString * path = [volumePath stringByAppendingPathComponent:@"GARMIN"];
NSArray * dc = [fm directoryContentsAtPath:path];
BOOL isDir;
if (!([fm fileExistsAtPath:path isDirectory:&isDir] && isDir))
{
return NO;
}
NSEnumerator * e = [dc objectEnumerator];
NSString * o;
BOOL gmapsupp_found = NO;
NSString * current_map = nil;
while ((o = [e nextObject]) != nil)
{
NSString * dir = [path stringByAppendingPathComponent:o];
if ([o caseInsensitiveCompare:@"gmapsupp.img"] == NSOrderedSame)
{
gmapsupp_found = YES;
}
else if ([fm fileExistsAtPath:dir isDirectory:&isDir] && isDir)
{
if ([fm fileExistsAtPath:[dir stringByAppendingPathComponent:@"gmapsupp.img"]])
{
[dirs addObject:[NSDictionary dictionaryWithObjectsAndKeys:o, @"mapName", nil]];
}
else
{
if (current_map == nil)
{
current_map = [NSString stringWithString:o];
}
else
{
NSLog(@"skipping directory: %@", o);
}
}
}
}
prevMap = current_map;
if (gmapsupp_found)
{
if (current_map != nil)
{
[dirs addObject:[NSDictionary dictionaryWithObjectsAndKeys:current_map, @"mapName", nil]];
[self setCurrentMap:current_map];
}
else
{
NSLog(@"no empty directory for active map");
}
}
else
{
[self setCurrentMap:nil];
}
[self setMaps:dirs];
return YES;
}
- (void)activateMap:(NSString*)map
{
NSString * path = [volumePath stringByAppendingPathComponent:@"GARMIN"];
NSFileManager * fm = [NSFileManager defaultManager];
NSError * error;
if (prevMap != nil)
{
if (![fm moveItemAtPath:[path stringByAppendingPathComponent:@"gmapsupp.img"]
toPath:[[path stringByAppendingPathComponent:prevMap] stringByAppendingPathComponent:@"gmapsupp.img"]
error:&error])
{
[NSApp presentError:error];
return;
}
}
prevMap = map;
if (map != nil)
{
if (![fm moveItemAtPath:[[path stringByAppendingPathComponent:map] stringByAppendingPathComponent:@"gmapsupp.img"]
toPath:[path stringByAppendingPathComponent:@"gmapsupp.img"]
error:&error])
{
[NSApp presentError:error];
}
else
{
prevMap = map;
currentMap = map;
return;
}
}
// FIXME
currentMap = nil;
}
@end