PageRenderTime 40ms CodeModel.GetById 17ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/core/externals/google-toolbox-for-mac/AppKit/GTMNSImage+SearchCache.m

http://macfuse.googlecode.com/
Objective C | 90 lines | 52 code | 11 blank | 27 comment | 17 complexity | 7f00dc1aee207a8b542991c158cd7fc6 MD5 | raw file
 1//
 2//  GTMNSImage+SearchCache.m
 3//
 4//  Finds NSImages using a variety of techniques
 5//
 6//  Copyright 2009 Google Inc.
 7//
 8//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
 9//  use this file except in compliance with the License.  You may obtain a copy
10//  of the License at
11// 
12//  http://www.apache.org/licenses/LICENSE-2.0
13// 
14//  Unless required by applicable law or agreed to in writing, software
15//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17//  License for the specific language governing permissions and limitations under
18//  the License.
19//
20
21#import "GTMNSImage+SearchCache.h"
22#import "GTMDefines.h"
23
24@implementation NSImage (GTMNSImageSearchCache)
25+ (NSImage *)gtm_imageWithPath:(NSString *)path {
26  return [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
27}
28
29+ (NSImage *)gtm_imageNamed:(NSString *)name {
30  return [self gtm_imageNamed:name forBundle:nil];
31}
32
33+ (NSImage *)gtm_imageNamed:(NSString *)name forBundle:(NSBundle *)bundle {
34  NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
35  NSImage *image = nil;
36  
37  // Check our specified bundle first
38  if (!image) {
39    NSString *path = [bundle pathForImageResource:name];
40    if (path) image = [self gtm_imageWithPath:path];
41  }
42  
43  // Check the main bundle and the existing NSImage namespace
44  if (!image) {
45    image = [NSImage imageNamed:name];
46  }
47  
48  // Search for an image with that path
49  if (!image && ([name isAbsolutePath] || [name hasPrefix:@"~"])) {
50    NSString *path = [name stringByStandardizingPath];
51    if ([[NSFileManager defaultManager]
52          fileExistsAtPath:path]) {
53      image = [self gtm_imageWithPath:path];
54      if (!image) {
55        image = [workspace iconForFile:path]; 
56      }
57    }
58  }  
59  // Search for a matching bundle id
60  if (!image) {
61    NSString *path = [workspace absolutePathForAppBundleWithIdentifier:name];
62    if (path) image = [workspace iconForFile:path]; ;
63  }
64  
65  // Search for a file .extension or 'TYPE'
66  // TODO(alcor): This ALWAYS returns an image for items with ' or . as prefix
67  // We might not want this
68  if ([name hasPrefix:@"'"] || [name hasPrefix:@"."]) {
69    image = [workspace iconForFileType:name];
70  }
71  
72  // Search for a UTI
73  if ([name rangeOfString:@"."].location != NSNotFound) {    
74    NSDictionary *dict
75      = GTMCFAutorelease(UTTypeCopyDeclaration((CFStringRef)name));
76    NSURL *url
77      = GTMCFAutorelease(UTTypeCopyDeclaringBundleURL((CFStringRef)name));
78    NSString *iconName = [dict objectForKey:(NSString *)kUTTypeIconFileKey];
79    
80    if (url && name) {
81      NSString *path
82        = [[NSBundle bundleWithPath:[url path]] pathForImageResource:iconName];
83      if (path)
84        image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
85    }
86  }
87  
88  return image;
89}
90@end