Skip to content

Instantly share code, notes, and snippets.

@pjrobertson
Created November 9, 2012 19:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjrobertson/4047584 to your computer and use it in GitHub Desktop.
Save pjrobertson/4047584 to your computer and use it in GitHub Desktop.
- (NSDictionary *)infoRecord {
NSString *path = [self validSingleFilePath];
if (!path)
return nil;
NSDictionary *dict;
if (dict = [self objectForCache:@"QSItemInfoRecord"])
return dict;
/* Try to get information for this file */
LSItemInfoRecord record;
OSStatus status = LSCopyItemInfoForURL((CFURLRef)[NSURL fileURLWithPath:path], kLSRequestAllInfo, &record);
if (status) {
NSLog(@"LSCopyItemInfoForURL error: %ld", (long)status);
return nil;
}
NSString *uti = QSUTIForExtensionOrType((NSString *)record.extension, record.filetype);
NSString *extension = [(NSString *)record.extension copy];
/* local or network volume? does it support Trash? */
struct statfs sfsb;
statfs([path UTF8String], &sfsb);
NSString *device = [NSString stringWithCString:sfsb.f_mntfromname encoding:NSUTF8StringEncoding];
BOOL isLocal = [device hasPrefix:@"/dev/"];
/* Now build a dictionary with that record */
NSMutableDictionary *tempDict = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithUnsignedInt:record.flags], @"flags",
[NSValue valueWithOSType:record.filetype], @"filetype",
[NSValue valueWithOSType:record.creator], @"creator",
[NSNumber numberWithBool:record.flags & kLSItemInfoIsApplication, @"isApplication".
[NSNumber numberWithBool:isLocal], @"localVolume",
nil];
if (uti) {
[tempDict setObject:uti forKey:@"uti"];
}
if (extension) {
[tempDict setObject:extension forKey:@"extension"];
}
dict = [NSDictionary dictionaryWithDictionary:tempDict];
/* Release the file's extension if one was returned */
if (record.extension)
CFRelease(record.extension);
[self setObject:dict forCache:@"QSItemInfoRecord"];
return dict;
}
- (BOOL)isApplication {
NSDictionary *infoRecord = [self objectForCache:@"QSItemInfoRecord"];
if(!infoRecord) {
BOOL isApp = [NFileManager … get is application;
[[self setObject:[NSDictionary dictionaryWithObjectAndKey:[NSNumber numberWithBool:isApp],@"isApplication,nil] forCache:@"QSItemInfoRecord"];
}
return [[[self objectForCache:@"QSItemInfoRecord"] objectForKey:@"isApplication] boolValue];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment