-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathL0iPod.m
189 lines (154 loc) · 5.06 KB
/
L0iPod.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
//
// L0iPod.m
#import <SproutedUtilities/L0iPod.h>
@implementation L0iPod
+ (NSString*) deviceRootForPath:(NSString*) path {
NSArray* arr = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];
NSEnumerator* enu = [arr objectEnumerator];
NSString* root;
while (root = [enu nextObject]) {
if ([path hasPrefix:root] && [self hasControlFolder:root])
return root;
}
return nil;
}
+ (BOOL) hasControlFolder:(NSString*) path {
BOOL isDir;
return [[NSFileManager defaultManager] fileExistsAtPath:[path stringByAppendingPathComponent:@"iPod_Control"] isDirectory:&isDir] && isDir;
}
+ (NSArray*) allMountedDevices {
NSArray* arr = [[NSWorkspace sharedWorkspace] mountedRemovableMedia];
NSEnumerator* enu = [arr objectEnumerator];
NSMutableArray* ipods = [NSMutableArray arrayWithCapacity:[arr count]];
NSString* path;
while (path = [enu nextObject]) {
if ([self hasControlFolder:path])
[ipods addObject:[[self alloc] initWithPath:path]];
}
return [NSArray arrayWithArray:ipods];
}
- (id) initWithPath:(NSString*) path {
if (self = [super init]) {
NSString* ipodRoot = [[self class] deviceRootForPath:path];
if (!ipodRoot) {
[self release];
return nil;
}
CFURLGetFSRef((CFURLRef)[NSURL fileURLWithPath:ipodRoot], &iPodRef);
family = kL0iPodUnchecked;
}
return self;
}
- (NSURL*) fileURL {
NSURL* url = (NSURL*) CFURLCreateFromFSRef(NULL, &iPodRef);
return [url autorelease];
}
- (NSString*) path {
return [[self fileURL] path];
}
- (NSImage*) icon {
NSString* path = [self path];
return path == nil? nil : [[NSWorkspace sharedWorkspace] iconForFile:path];
}
- (NSDictionary*) deviceInformation {
NSString* ipod = [self path];
if (!ipod)
return nil;
NSMutableDictionary* dict = [NSMutableDictionary dictionary];
NSString* sysInfo = [NSString stringWithContentsOfFile:[ipod stringByAppendingPathComponent:@"iPod_Control/Device/SysInfo"]];
NSScanner* scanner = [NSScanner scannerWithString:sysInfo];
[scanner setCharactersToBeSkipped:[NSCharacterSet whitespaceCharacterSet]];
while (![scanner isAtEnd]) {
NSString* key = nil, * value = nil;
[scanner scanUpToCharactersFromSet:[NSCharacterSet characterSetWithCharactersInString:@":\n"] intoString:&key];
if ([scanner scanString:@":" intoString:nil]) {
[scanner scanUpToString:@"\n" intoString:&value];
[dict setObject:value forKey:key];
}
[scanner scanString:@"\n" intoString:nil];
}
return [NSDictionary dictionaryWithDictionary:dict];
}
- (L0iPodFamily)family
{
//L0iPodFamily family;
if (family != kL0iPodUnchecked)
return family;
NSDictionary* info = [self deviceInformation];
if (info == nil || [info count] == 0)
{
// it's most likely a shuffle...
family = kL0iPodShuffle;
}
else
{
NSString *boardHwSwInterfaceRev = [info objectForKey:@"boardHwSwInterfaceRev"];
if ([boardHwSwInterfaceRev hasPrefix:@"0x00010000"] || [boardHwSwInterfaceRev hasPrefix:@"0x00020000"])
{
// mechanical/touch wheel (first & second generation)
family = kL0iPodMechanicalOrTouchWheel;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x00030001"])
{
// touch wheel & buttons (third generation)
family = kL0iPodTouchWheelAndButtons;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x00040013"] || [boardHwSwInterfaceRev hasPrefix:@"0x00070002"])
{
// iPod mini (1st and 2nd generations)
family = kL0iPodMini;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x00050013"] || [boardHwSwInterfaceRev hasPrefix:@"0x00050014"])
{
// click wheel (fourth generation)
family = kL0iPodClickWheel;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x00060000"] || [boardHwSwInterfaceRev hasPrefix:@"0x00060004"])
{
// iPod with color display (includes iPod photo)
family = kL0iPodColorDisplay;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x000C0005"])
{
// iPod nano
family = kL0iPodNano;
}
else if ([boardHwSwInterfaceRev hasPrefix:@"0x000B0005"])
{
// iPod (with video playback)
family = kL0iPodVideo;
}
else
{
// Unrecognized iPod
family = kL0iPodGeneric;
}
}
return family;
}
- (BOOL) hasDisplay {
return [self family] != kL0iPodShuffle;
}
- (BOOL) hasColorDisplay {
L0iPodFamily fam = [self family];
return fam == kL0iPodColorDisplay || fam == kL0iPodNano || fam == kL0iPodVideo;
}
- (BOOL) hasNotes {
L0iPodFamily fam = [self family];
return fam != kL0iPodMechanicalOrTouchWheel && fam != kL0iPodShuffle;
}
- (BOOL) hasTVOut {
L0iPodFamily fam = [self family];
return fam == kL0iPodColorDisplay || fam == kL0iPodVideo;
}
- (BOOL) hasVideoPlayback {
return [self family] == kL0iPodVideo;
}
- (BOOL) hasPhotoAlbum {
return [self hasColorDisplay];
}
- (NSString*) displayName {
NSString* path = [self path];
return path? [[NSFileManager defaultManager] displayNameAtPath:path] : nil;
}
@end