diff options
author | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2009-09-04 21:15:20 +0000 |
---|---|---|
committer | gtm.daemon <gtm.daemon@7dc7ac4e-7543-0410-b95c-c1676fc8e2a3> | 2009-09-04 21:15:20 +0000 |
commit | 0cc6f9d00b5006a2b1ef2016048e0815a49f2574 (patch) | |
tree | 1563ba9751515ac5f64d657fc19c1fe13c3c64fe /AppKit | |
parent | 080c554c4901491976eda408c394b0e34c33295d (diff) |
[Author: alcor]
Add the NSImage Search category
R=dmaclach
DELTA=203 (203 added, 0 deleted, 0 changed)
Diffstat (limited to 'AppKit')
-rw-r--r-- | AppKit/GTMNSImage+SearchCache.h | 39 | ||||
-rw-r--r-- | AppKit/GTMNSImage+SearchCache.m | 90 | ||||
-rw-r--r-- | AppKit/GTMNSImage+SearchCacheTest.m | 57 |
3 files changed, 186 insertions, 0 deletions
diff --git a/AppKit/GTMNSImage+SearchCache.h b/AppKit/GTMNSImage+SearchCache.h new file mode 100644 index 0000000..b0999bf --- /dev/null +++ b/AppKit/GTMNSImage+SearchCache.h @@ -0,0 +1,39 @@ +// +// GTMNSImage+SearchCache.h +// +// Finds NSImages using a variety of techniques +// +// Copyright 2009 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +// + +// This category provides convenience methods for image initialization based +// on creating an image by searching the following locations: +// +// * A specified bundle +// * Main bundle / +[NSImage imageNamed] +// * An exact path for an image +// * An exact path for any file (using the icon) +// * An app bundle id (using the icon) +// * A file type as .extension, 'OSTYPE' (in single quotes), or UTI +// * An icon in the system icon bundle +// +// TODO(alcor): this class should have basic MRU cache +// + +@interface NSImage (GTMNSImageSearchCache) ++ (NSImage *)gtm_imageWithPath:(NSString *)path; ++ (NSImage *)gtm_imageNamed:(NSString *)name; ++ (NSImage *)gtm_imageNamed:(NSString *)name forBundle:(NSBundle *)bundle; +@end diff --git a/AppKit/GTMNSImage+SearchCache.m b/AppKit/GTMNSImage+SearchCache.m new file mode 100644 index 0000000..99c2af4 --- /dev/null +++ b/AppKit/GTMNSImage+SearchCache.m @@ -0,0 +1,90 @@ +// +// GTMNSImage+SearchCache.m +// +// Finds NSImages using a variety of techniques +// +// Copyright 2009 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +// + +#import "GTMNSImage+SearchCache.h" +#import "GTMGarbageCollection.h" + +@implementation NSImage (GTMNSImageSearchCache) ++ (NSImage *)gtm_imageWithPath:(NSString *)path { + return [[[NSImage alloc] initWithContentsOfFile:path] autorelease]; +} + ++ (NSImage *)gtm_imageNamed:(NSString *)name { + return [self gtm_imageNamed:name forBundle:nil]; +} + ++ (NSImage *)gtm_imageNamed:(NSString *)name forBundle:(NSBundle *)bundle { + NSWorkspace *workspace = [NSWorkspace sharedWorkspace]; + NSImage *image = nil; + + // Check our specified bundle first + if (!image) { + NSString *path = [bundle pathForImageResource:name]; + if (path) image = [self gtm_imageWithPath:path]; + } + + // Check the main bundle and the existing NSImage namespace + if (!image) { + image = [NSImage imageNamed:name]; + } + + // Search for an image with that path + if (!image && ([name isAbsolutePath] || [name hasPrefix:@"~"])) { + NSString *path = [name stringByStandardizingPath]; + if ([[NSFileManager defaultManager] + fileExistsAtPath:path]) { + image = [self gtm_imageWithPath:path]; + if (!image) { + image = [workspace iconForFile:path]; + } + } + } + // Search for a matching bundle id + if (!image) { + NSString *path = [workspace absolutePathForAppBundleWithIdentifier:name]; + if (path) image = [workspace iconForFile:path]; ; + } + + // Search for a file .extension or 'TYPE' + // TODO(alcor): This ALWAYS returns an image for items with ' or . as prefix + // We might not want this + if ([name hasPrefix:@"'"] || [name hasPrefix:@"."]) { + image = [workspace iconForFileType:name]; + } + + // Search for a UTI + if ([name rangeOfString:@"."].location != NSNotFound) { + NSDictionary *dict + = GTMCFAutorelease(UTTypeCopyDeclaration((CFStringRef)name)); + NSURL *url + = GTMCFAutorelease(UTTypeCopyDeclaringBundleURL((CFStringRef)name)); + NSString *iconName = [dict objectForKey:(NSString *)kUTTypeIconFileKey]; + + if (url && name) { + NSString *path + = [[NSBundle bundleWithPath:[url path]] pathForImageResource:iconName]; + if (path) + image = [[NSImage alloc] initWithContentsOfFile:path]; + } + } + + return image; +} +@end diff --git a/AppKit/GTMNSImage+SearchCacheTest.m b/AppKit/GTMNSImage+SearchCacheTest.m new file mode 100644 index 0000000..51c2693 --- /dev/null +++ b/AppKit/GTMNSImage+SearchCacheTest.m @@ -0,0 +1,57 @@ +// +// GTMNSImage+SearchCacheTest.m +// +// Copyright 2006-2008 Google Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy +// of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +// + +#import <Cocoa/Cocoa.h> + +#import "GTMSenTestCase.h" + +#import "GTMNSImage+SearchCache.h" +#import "GTMGeometryUtils.h" + +@interface GTMNSImage_SearchCacheTest : GTMTestCase +@end + +@implementation GTMNSImage_SearchCacheTest + +- (void)testSearchCache { + NSImage *testImage = [NSImage gtm_imageNamed:@"NSApplicationIcon"]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:@"com.apple.Xcode"]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:NSImageNameBonjour]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:(NSString *)kUTTypeFolder]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:@"~/Library"]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:@"'APPL'"]; + STAssertNotNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:@"ponies for sale"]; + STAssertNil(testImage, nil); + + testImage = [NSImage gtm_imageNamed:@"/An/Invalid/Path"]; + STAssertNil(testImage, nil); +} + +@end |