/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. #import "GTMNSImage+SearchCache.h"
  21. #import "GTMDefines.h"
  22. @implementation NSImage (GTMNSImageSearchCache)
  23. + (NSImage *)gtm_imageWithPath:(NSString *)path {
  24. return [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
  25. }
  26. + (NSImage *)gtm_imageNamed:(NSString *)name {
  27. return [self gtm_imageNamed:name forBundle:nil];
  28. }
  29. + (NSImage *)gtm_imageNamed:(NSString *)name forBundle:(NSBundle *)bundle {
  30. NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
  31. NSImage *image = nil;
  32. // Check our specified bundle first
  33. if (!image) {
  34. NSString *path = [bundle pathForImageResource:name];
  35. if (path) image = [self gtm_imageWithPath:path];
  36. }
  37. // Check the main bundle and the existing NSImage namespace
  38. if (!image) {
  39. image = [NSImage imageNamed:name];
  40. }
  41. // Search for an image with that path
  42. if (!image && ([name isAbsolutePath] || [name hasPrefix:@"~"])) {
  43. NSString *path = [name stringByStandardizingPath];
  44. if ([[NSFileManager defaultManager]
  45. fileExistsAtPath:path]) {
  46. image = [self gtm_imageWithPath:path];
  47. if (!image) {
  48. image = [workspace iconForFile:path];
  49. }
  50. }
  51. }
  52. // Search for a matching bundle id
  53. if (!image) {
  54. NSString *path = [workspace absolutePathForAppBundleWithIdentifier:name];
  55. if (path) image = [workspace iconForFile:path]; ;
  56. }
  57. // Search for a file .extension or 'TYPE'
  58. // TODO(alcor): This ALWAYS returns an image for items with ' or . as prefix
  59. // We might not want this
  60. if ([name hasPrefix:@"'"] || [name hasPrefix:@"."]) {
  61. image = [workspace iconForFileType:name];
  62. }
  63. // Search for a UTI
  64. if ([name rangeOfString:@"."].location != NSNotFound) {
  65. NSDictionary *dict
  66. = GTMCFAutorelease(UTTypeCopyDeclaration((CFStringRef)name));
  67. NSURL *url
  68. = GTMCFAutorelease(UTTypeCopyDeclaringBundleURL((CFStringRef)name));
  69. NSString *iconName = [dict objectForKey:(NSString *)kUTTypeIconFileKey];
  70. if (url && name) {
  71. NSString *path
  72. = [[NSBundle bundleWithPath:[url path]] pathForImageResource:iconName];
  73. if (path)
  74. image = [[[NSImage alloc] initWithContentsOfFile:path] autorelease];
  75. }
  76. }
  77. return image;
  78. }
  79. @end