-
Notifications
You must be signed in to change notification settings - Fork 20
/
KSUniformType.m
269 lines (228 loc) · 8.01 KB
/
KSUniformType.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
//
// KSUniformType.m
// Sandvox
//
// Created by Mike Abdullah on 01/04/2012.
// Copyright © 2012 Karelia Software
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "KSUniformType.h"
@implementation KSUniformType
+ (NSString *)MIMETypeForType:(NSString *)aUTI;
{
NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)aUTI, kUTTagClassMIMEType));
[result autorelease];
// BUGSID:37340 OS X doesn't know the MIME type for .m4a files, so we're hardcoding it here.
if (!result)
{
if ([aUTI isEqualToString:(NSString *)kUTTypeMPEG4Audio])
{
result = @"audio/x-m4a";
}
else if ([aUTI isEqualToString:(NSString *)kUTTypeICO])
{
result = @"image/vnd.microsoft.icon";
}
// Apparently .m4v com.apple.protected-mpeg-4-video is also not known.
else if ([aUTI isEqualToString:@"com.apple.protected-mpeg-4-video"])
{
result = @"video/x-m4v";
}
else
{
result = @"application/octet-stream";
}
}
NSAssert(result, @"Should always fall back to raw data MIME type at the least");
return result;
}
+ (NSString *)OSTypeStringForType:(NSString *)aUTI
{
NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass(
(CFStringRef)aUTI,
kUTTagClassOSType
));
return [result autorelease];
}
+ (OSType)OSTypeForType:(NSString *)aUTI
{
return UTGetOSTypeFromString((CFStringRef)[self OSTypeStringForType:aUTI]);
}
+ (NSString *)typeOfFileAtURL:(NSURL *)url;
{
NSString *result = nil;
FSRef fileRef;
Boolean isDir;
if (FSPathMakeRef((const UInt8 *)[[url path] fileSystemRepresentation], &fileRef, &isDir) == noErr)
{
// get the content type (UTI) of this file
CFStringRef uti;
if (LSCopyItemAttribute(&fileRef, kLSRolesViewer, kLSItemContentType, (CFTypeRef*)&uti)==noErr)
{
result = [NSMakeCollectable(uti) autorelease]; // I want an autoreleased copy of this.
}
}
// check extension if we can't find the actual file
if (nil == result)
{
NSString *extension = [url pathExtension];
if ( (nil != extension) && ![extension isEqualToString:@""] )
{
result = [self typeForFilenameExtension:extension];
}
}
// if no extension or no result, check file type
if ( nil == result || [result isEqualToString:(NSString *)kUTTypeData])
{
NSString *fileType = NSHFSTypeOfFile([url path]);
if (6 == [fileType length])
{
fileType = [fileType substringWithRange:NSMakeRange(1,4)];
}
result = [self typeForOSTypeString:fileType];
if ([result hasPrefix:@"dyn."])
{
result = nil; // reject a dynamic type if it tries that.
}
}
if (nil == result) // not found, figure out if it's a directory or not
{
BOOL isDirectory;
if ( [[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory] )
{
result = isDirectory ? (NSString *)kUTTypeDirectory : (NSString *)kUTTypeData;
}
}
// Will return nil if file doesn't exist.
return result;
}
+ (NSString *)typeForFilenameExtension:(NSString *)anExtension;
{
NSString *UTI = nil;
if ([anExtension isEqualToString:@"m4v"])
{
// Hack, since we already have this UTI defined in the system, I don't think I can add it to the plist.
UTI = (NSString *)kUTTypeMPEG4;
}
else
{
UTI = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
kUTTagClassFilenameExtension,
(CFStringRef)anExtension,
NULL
));
[UTI autorelease];
}
// If we don't find it, add an entry to the info.plist of the APP,
// along the lines of what is documented here:
// http://developer.apple.com/documentation/Carbon/Conceptual/understanding_utis/understand_utis_conc/chapter_2_section_4.html
// A good starting point for informal ones is:
// http://www.huw.id.au/code/fileTypeIDs.html
return UTI;
}
+ (NSString *)typeForMIMEType:(NSString *)aMIMEType
{
if ([aMIMEType isEqualToString:@"image/vnd.microsoft.icon"])
{
return (NSString *)kUTTypeICO;
}
else
{
NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
kUTTagClassMIMEType,
(CFStringRef)aMIMEType,
kUTTypeData
));
return [result autorelease];
}
}
+ (NSString *)typeForOSTypeString:(NSString *)aFileType;
{
NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
kUTTagClassOSType,
(CFStringRef)aFileType,
NULL
));
return [result autorelease];
}
+ (NSString *)typeForOSType:(OSType)anOSType;
{
NSString *OSTypeAsString = NSMakeCollectable(UTCreateStringForOSType(anOSType));
NSString *result = [self typeForOSTypeString:OSTypeAsString];
[OSTypeAsString release];
return result;
}
#pragma mark Creating a KSUniformType Instance
+ (instancetype)uniformTypeWithFilenameExtension:(NSString *)extension;
{
return [[[self alloc] initWithIdentifier:[self typeForFilenameExtension:extension]] autorelease];
}
+ (instancetype)uniformTypeWithIdentifier:(NSString *)identifier;
{
if (!identifier) return nil;
return [[[self alloc] initWithIdentifier:identifier] autorelease];
}
+ (instancetype)bestGuessUniformTypeForURL:(NSURL *)url;
{
return [self uniformTypeWithIdentifier:[self typeOfFileAtURL:url]];
}
- (id)initWithIdentifier:(NSString *)uti;
{
NSParameterAssert(uti);
if (self = [self init])
{
_identifier = [uti copy];
}
return self;
}
- (void)dealloc;
{
[_identifier release];
[super dealloc];
}
#pragma mark Properties
@synthesize identifier = _identifier;
- (NSString *)MIMEType; { return [[self class] MIMETypeForType:[self identifier]]; }
#pragma mark Testing Uniform Type Identifiers
- (BOOL)isEqualToType:(NSString *)type; { return [[self class] type:[self identifier] isEqualToType:type]; }
- (BOOL)isEqual:(id)object;
{
if (self == object) return YES;
if (![object isKindOfClass:[KSUniformType class]]) return NO;
return [self isEqualToType:[object identifier]];
}
- (NSUInteger)hash; { return 0; } // see header
- (BOOL)conformsToType:(NSString *)type;
{
return [[NSWorkspace sharedWorkspace] type:[self identifier] conformsToType:type];
}
+ (BOOL)type:(NSString *)type1 isEqualToType:(NSString *)anotherUTI;
{
return UTTypeEqual((CFStringRef)type1, (CFStringRef)anotherUTI);
}
+ (BOOL)type:(NSString *)type conformsToOneOfTypes:(NSArray *)types;
{
for (NSString *aType in types)
{
if ([[NSWorkspace sharedWorkspace] type:type conformsToType:aType]) return YES;
}
return NO;
}
@end