Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 247 lines (208 sloc) 6.895 kB
a3b7edf @mikeabdullah KSWorkspaceUtilities
mikeabdullah authored
1 //
2 // KSWorkspaceUtilities.m
3 // Sandvox
4 //
5 // Created by Mike on 28/04/2011.
6 // Copyright 2011 Karelia Software. All rights reserved.
7 //
8
9 #import "KSWorkspaceUtilities.h"
10
ba68e07 @mikeabdullah UTI handling methods
mikeabdullah authored
11 #import "KSURLUtilities.h"
12
a3b7edf @mikeabdullah KSWorkspaceUtilities
mikeabdullah authored
13
14 @implementation NSWorkspace (KSWorkspaceUtilities)
15
28831d8 @mikeabdullah -ks_iconForType:
mikeabdullah authored
16 #pragma mark Manipulating Uniform Type Identifier Information
17
ba68e07 @mikeabdullah UTI handling methods
mikeabdullah authored
18 - (NSString *)ks_MIMETypeForType:(NSString *)aUTI;
19 {
20 NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass((CFStringRef)aUTI, kUTTagClassMIMEType));
21 result = [result autorelease];
22
23 // BUGSID:37340 OS X doesn't know the MIME type for .m4a files, so we're hardcoding it here.
24 if (!result)
25 {
26 if ([aUTI isEqualToString:(NSString *)kUTTypeMPEG4Audio])
27 {
28 result = @"audio/x-m4a";
29 }
30 else if ([aUTI isEqualToString:(NSString *)kUTTypeICO])
31 {
32 result = @"image/vnd.microsoft.icon";
33 }
34 // Apparently .m4v com.apple.protected-mpeg-4-video is also not known.
35 else if ([aUTI isEqualToString:@"com.apple.protected-mpeg-4-video"])
36 {
37 result = @"video/x-m4v";
38 }
39 else
40 {
41 result = @"application/octet-stream";
42 }
43 }
44
45 OBPOSTCONDITION(result);
46 return result;
47 }
48
49 - (NSString *)ks_OSTypeStringForType:(NSString *)aUTI
50 {
51 NSString *result = NSMakeCollectable(UTTypeCopyPreferredTagWithClass(
52 (CFStringRef)aUTI,
53 kUTTagClassOSType
54 ));
55 result = [result autorelease];
56 return result;
57 }
58
59 - (OSType)ks_OSTypeForType:(NSString *)aUTI
60 {
61 return UTGetOSTypeFromString((CFStringRef)[self ks_OSTypeStringForType:aUTI]);
62 }
63
64 - (NSString *)ks_typeOfFileAtURL:(NSURL *)url;
65 {
66 NSString *result = nil;
67 FSRef fileRef;
68 Boolean isDir;
69
70 if (FSPathMakeRef((const UInt8 *)[[url path] fileSystemRepresentation], &fileRef, &isDir) == noErr)
71 {
72 // get the content type (UTI) of this file
73 CFStringRef uti;
74 if (LSCopyItemAttribute(&fileRef, kLSRolesViewer, kLSItemContentType, (CFTypeRef*)&uti)==noErr)
75 {
76 result = [NSMakeCollectable(uti) autorelease]; // I want an autoreleased copy of this.
77 }
78 }
79
80 // check extension if we can't find the actual file
81 if (nil == result)
82 {
83 NSString *extension = [url ks_pathExtension];
84 if ( (nil != extension) && ![extension isEqualToString:@""] )
85 {
86 result = [self ks_typeForFilenameExtension:extension];
87 }
88 }
89
90 // if no extension or no result, check file type
91 if ( nil == result || [result isEqualToString:(NSString *)kUTTypeData])
92 {
93 NSString *fileType = NSHFSTypeOfFile([url path]);
94 if (6 == [fileType length])
95 {
96 fileType = [fileType substringWithRange:NSMakeRange(1,4)];
97 }
98 result = [self ks_typeForOSTypeString:fileType];
99 if ([result hasPrefix:@"dyn."])
100 {
101 result = nil; // reject a dynamic type if it tries that.
102 }
103 }
104
105 if (nil == result) // not found, figure out if it's a directory or not
106 {
107 BOOL isDirectory;
108 if ( [[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDirectory] )
109 {
110 result = isDirectory ? (NSString *)kUTTypeDirectory : (NSString *)kUTTypeData;
111 }
112 }
113
114 // Will return nil if file doesn't exist.
115
116 return result;
117 }
118
119 - (NSString *)ks_typeForFilenameExtension:(NSString *)anExtension;
120 {
121 NSString *UTI = nil;
122
123 if ([anExtension isEqualToString:@"m4v"])
124 {
125 // Hack, since we already have this UTI defined in the system, I don't think I can add it to the plist.
126 UTI = (NSString *)kUTTypeMPEG4;
127 }
128 else
129 {
130 UTI = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
131 kUTTagClassFilenameExtension,
132 (CFStringRef)anExtension,
133 NULL
134 ));
135 UTI = [UTI autorelease];
136 }
137
138 // If we don't find it, add an entry to the info.plist of the APP,
139 // along the lines of what is documented here:
140 // http://developer.apple.com/documentation/Carbon/Conceptual/understanding_utis/understand_utis_conc/chapter_2_section_4.html
141 // A good starting point for informal ones is:
142 // http://www.huw.id.au/code/fileTypeIDs.html
143
144 return UTI;
145 }
146
147 - (NSString *)ks_typeForMIMEType:(NSString *)aMIMEType
148 {
149 if ([aMIMEType isEqualToString:@"image/vnd.microsoft.icon"])
150 {
151 return (NSString *)kUTTypeICO;
152 }
153 else
154 {
155 NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
156 kUTTagClassMIMEType,
157 (CFStringRef)aMIMEType,
158 kUTTypeData
159 ));
160 result = [result autorelease];
161 return result;
162
163 }
164 }
165
166 - (NSString *)ks_typeForOSTypeString:(NSString *)aFileType;
167 {
168 NSString *result = NSMakeCollectable(UTTypeCreatePreferredIdentifierForTag(
169 kUTTagClassOSType,
170 (CFStringRef)aFileType,
171 NULL
172 ));
173 result = [result autorelease];
174 return result;
175 }
176
177 - (NSString *)ks_typeForOSType:(OSType)anOSType;
178 {
179 NSString *OSTypeAsString = NSMakeCollectable(UTCreateStringForOSType(anOSType));
180 NSString *result = [self ks_typeForOSTypeString:OSTypeAsString];
181 [OSTypeAsString release];
182 return result;
183 }
184
185 - (BOOL)ks_type:(NSString *)type1 isEqualToType:(NSString *)anotherUTI;
186 {
187 return UTTypeEqual (
188 (CFStringRef)type1,
189 (CFStringRef)anotherUTI
190 );
191 }
192
193
194 - (BOOL)ks_type:(NSString *)type conformsToOneOfTypes:(NSArray *)types;
195 {
196 for (NSString *aType in types)
197 {
198 if ([self type:type conformsToType:aType]) return YES;
199 }
200
201 return NO;
202 }
203
28831d8 @mikeabdullah -ks_iconForType:
mikeabdullah authored
204 #pragma mark Requesting Information
205
206 - (NSImage *)ks_iconForType:(NSString *)aUTI;
207 {
208 NSString *extension = [self preferredFilenameExtensionForType:aUTI];
209 NSImage *result = [self iconForFileType:extension];
210 //[result normalizeSize];
211 return result;
212 }
213
241f907 @mikeabdullah -ks_setBundleBit:forFileAtURL:
mikeabdullah authored
214 #pragma mark
215
216 - (void)ks_setBundleBit:(BOOL)flag forFileAtURL:(NSURL *)url;
217 {
218 FSRef fileRef;
219 OSErr error = FSPathMakeRef((UInt8 *)[[url path] fileSystemRepresentation], &fileRef, NULL);
220
221 // Get the file's current info
222 FSCatalogInfo fileInfo;
223 if (!error)
224 {
225 error = FSGetCatalogInfo(&fileRef, kFSCatInfoFinderInfo, &fileInfo, NULL, NULL, NULL);
226 }
227
228 if (!error)
229 {
230 // Adjust the bundle bit
231 FolderInfo *finderInfo = (FolderInfo *)fileInfo.finderInfo;
232 if (flag) {
233 finderInfo->finderFlags |= kHasBundle;
234 }
235 else {
236 finderInfo->finderFlags &= ~kHasBundle;
237 }
238
239 // Set the altered flags of the file
240 error = FSSetCatalogInfo(&fileRef, kFSCatInfoFinderInfo, &fileInfo);
241 }
242
243 if (error) NSLog(@"OSError %i in -[NSWorkspace setBundleBit:forFile:]", error);
244 }
245
a3b7edf @mikeabdullah KSWorkspaceUtilities
mikeabdullah authored
246 @end
Something went wrong with that request. Please try again.