Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 252 lines (210 sloc) 7.109 kb
26849ca1 »
2012-04-01 Split out KSUniformType.
1 //
2 // KSUniformType.m
3 // Sandvox
4 //
5 // Created by Mike Abdullah on 01/04/2012.
6 // Copyright (c) 2012 Karelia Software. All rights reserved.
7 //
8
9 #import "KSUniformType.h"
10
11 @implementation KSUniformType
12
13 + (NSString *)MIMETypeForType:(NSString *)aUTI;
14 {
15 NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)aUTI, kUTTagClassMIMEType));
16 [result autorelease];
17
18 // BUGSID:37340 OS X doesn't know the MIME type for .m4a files, so we're hardcoding it here.
19 if (!result)
20 {
21 if ([aUTI isEqualToString:(NSString *)kUTTypeMPEG4Audio])
22 {
23 result = @"audio/x-m4a";
24 }
25 else if ([aUTI isEqualToString:(NSString *)kUTTypeICO])
26 {
27 result = @"image/vnd.microsoft.icon";
28 }
29 // Apparently .m4v com.apple.protected-mpeg-4-video is also not known.
30 else if ([aUTI isEqualToString:@"com.apple.protected-mpeg-4-video"])
31 {
32 result = @"video/x-m4v";
33 }
34 else
35 {
36 result = @"application/octet-stream";
37 }
38 }
39
40 NSAssert(result, @"Should always fall back to raw data MIME type at the least");
41 return result;
42 }
43
44 + (NSString *)OSTypeStringForType:(NSString *)aUTI
45 {
46 NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass(
47 (CFStringRef)aUTI,
48 kUTTagClassOSType
49 ));
50 return [result autorelease];
51 }
52
53 + (OSType)OSTypeForType:(NSString *)aUTI
54 {
55 return UTGetOSTypeFromString((CFStringRef)[self OSTypeStringForType:aUTI]);
56 }
57
58 + (NSString *)typeOfFileAtURL:(NSURL *)url;
59 {
60 NSString *result = nil;
61 FSRef fileRef;
62 Boolean isDir;
63
64 if (FSPathMakeRef((const UInt8 *)[[url path] fileSystemRepresentation], &fileRef, &isDir) == noErr)
65 {
66 // get the content type (UTI) of this file
67 CFStringRef uti;
68 if (LSCopyItemAttribute(&fileRef, kLSRolesViewer, kLSItemContentType, (CFTypeRef*)&uti)==noErr)
69 {
70 result = [NSMakeCollectable(uti) autorelease]; // I want an autoreleased copy of this.
71 }
72 }
73
74 // check extension if we can't find the actual file
75 if (nil == result)
76 {
77 NSString *extension = [url pathExtension];
78 if ( (nil != extension) && ![extension isEqualToString:@""] )
79 {
80 result = [self typeForFilenameExtension:extension];
81 }
82 }
83
84 // if no extension or no result, check file type
85 if ( nil == result || [result isEqualToString:(NSString *)kUTTypeData])
86 {
87 NSString *fileType = NSHFSTypeOfFile([url path]);
88 if (6 == [fileType length])
89 {
90 fileType = [fileType substringWithRange:NSMakeRange(1,4)];
91 }
92 result = [self typeForOSTypeString:fileType];
93 if ([result hasPrefix:@"dyn."])
94 {
95 result = nil; // reject a dynamic type if it tries that.
96 }
97 }
98
99 if (nil == result) // not found, figure out if it's a directory or not
100 {
101 BOOL isDirectory;
102 if ( [[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory] )
103 {
104 result = isDirectory ? (NSString *)kUTTypeDirectory : (NSString *)kUTTypeData;
105 }
106 }
107
108 // Will return nil if file doesn't exist.
109
110 return result;
111 }
112
113 + (NSString *)typeForFilenameExtension:(NSString *)anExtension;
114 {
115 NSString *UTI = nil;
116
117 if ([anExtension isEqualToString:@"m4v"])
118 {
119 // Hack, since we already have this UTI defined in the system, I don't think I can add it to the plist.
120 UTI = (NSString *)kUTTypeMPEG4;
121 }
122 else
123 {
124 UTI = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
125 kUTTagClassFilenameExtension,
126 (CFStringRef)anExtension,
127 NULL
128 ));
129 [UTI autorelease];
130 }
131
132 // If we don't find it, add an entry to the info.plist of the APP,
133 // along the lines of what is documented here:
134 // http://developer.apple.com/documentation/Carbon/Conceptual/understanding_utis/understand_utis_conc/chapter_2_section_4.html
135 // A good starting point for informal ones is:
136 // http://www.huw.id.au/code/fileTypeIDs.html
137
138 return UTI;
139 }
140
141 + (NSString *)typeForMIMEType:(NSString *)aMIMEType
142 {
143 if ([aMIMEType isEqualToString:@"image/vnd.microsoft.icon"])
144 {
145 return (NSString *)kUTTypeICO;
146 }
147 else
148 {
149 NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
150 kUTTagClassMIMEType,
151 (CFStringRef)aMIMEType,
152 kUTTypeData
153 ));
154 return [result autorelease];
155 }
156 }
157
158 + (NSString *)typeForOSTypeString:(NSString *)aFileType;
159 {
160 NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
161 kUTTagClassOSType,
162 (CFStringRef)aFileType,
163 NULL
164 ));
165 return [result autorelease];
166 }
167
168 + (NSString *)typeForOSType:(OSType)anOSType;
169 {
170 NSString *OSTypeAsString = NSMakeCollectable(UTCreateStringForOSType(anOSType));
171 NSString *result = [self typeForOSTypeString:OSTypeAsString];
172 [OSTypeAsString release];
173 return result;
174 }
175
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
176 #pragma mark Creating a KSUniformType Instance
177
ff43ae0e »
2012-04-02 +uniformTypeWithFilenameExtension:
178 + (id)uniformTypeWithFilenameExtension:(NSString *)extension;
179 {
180 return [[[self alloc] initWithIdentifier:[self typeForFilenameExtension:extension]] autorelease];
181 }
182
a9050a58 »
2012-04-02 +uniformTypeWithIdentifier:
183 + (id)uniformTypeWithIdentifier:(NSString *)identifier;
184 {
fbbfa5e6 »
2012-04-02 Be lenient of nil.
185 if (!identifier) return nil;
a9050a58 »
2012-04-02 +uniformTypeWithIdentifier:
186 return [[[self alloc] initWithIdentifier:identifier] autorelease];
187 }
188
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
189 + (id)bestGuessUniformTypeForURL:(NSURL *)url;
190 {
6567d82c »
2012-04-29 +bestGuessUniformTypeForURL: returns nil for URLs where type can't be…
191 return [self uniformTypeWithIdentifier:[self typeOfFileAtURL:url]];
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
192 }
193
194 - (id)initWithIdentifier:(NSString *)uti;
26849ca1 »
2012-04-01 Split out KSUniformType.
195 {
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
196 NSParameterAssert(uti);
197 if (self = [self init])
198 {
199 _identifier = [uti copy];
200 }
201
202 return self;
26849ca1 »
2012-04-01 Split out KSUniformType.
203 }
204
f8afbf62 »
2012-05-16 Well look which dimwit forgot to write a dealloc method :(
205 - (void)dealloc;
206 {
207 [_identifier release];
208 [super dealloc];
209 }
210
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
211 #pragma mark Properties
212
213 @synthesize identifier = _identifier;
214
e5e96186 »
2012-04-02 -MIMEType
215 - (NSString *)MIMEType; { return [[self class] MIMETypeForType:[self identifier]]; }
216
feabda5e »
2012-04-02 Begin the KSUniformType instance API.
217 #pragma mark Testing Uniform Type Identifiers
218
219 - (BOOL)isEqualToType:(NSString *)type; { return [[self class] type:[self identifier] isEqualToType:type]; }
220
221 - (BOOL)isEqual:(id)object;
222 {
223 if (self == object) return YES;
224 if (![object isKindOfClass:[KSUniformType class]]) return NO;
225
226 return [self isEqualToType:[object identifier]];
227 }
228
229 - (NSUInteger)hash; { return 0; } // see header
230
231 - (BOOL)conformsToType:(NSString *)type;
232 {
233 return [[NSWorkspace sharedWorkspace] type:[self identifier] conformsToType:type];
234 }
235
236 + (BOOL)type:(NSString *)type1 isEqualToType:(NSString *)anotherUTI;
237 {
238 return UTTypeEqual((CFStringRef)type1, (CFStringRef)anotherUTI);
239 }
26849ca1 »
2012-04-01 Split out KSUniformType.
240
241 + (BOOL)type:(NSString *)type conformsToOneOfTypes:(NSArray *)types;
242 {
243 for (NSString *aType in types)
244 {
245 if ([[NSWorkspace sharedWorkspace] type:type conformsToType:aType]) return YES;
246 }
247
248 return NO;
249 }
250
251 @end
Something went wrong with that request. Please try again.