From 1269de06f915408f545aeeda0e672626b97555ce Mon Sep 17 00:00:00 2001 From: mscott Date: Mon, 5 Apr 2010 12:40:17 -0500 Subject: [PATCH] Add artwork fetch from image file in directory when no embedded image is found (looks for image files named or ending with "cover", "folder", "album" or "front"). Clean up playlist popup menu validation. --- FileTree/FileTreeController.m | 22 +++++++++ FileTree/FileTreeOutlineView.m | 21 --------- Plugins/TagLib/TagLibMetadataReader.h | 3 ++ Plugins/TagLib/TagLibMetadataReader.m | 68 +++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 21 deletions(-) diff --git a/FileTree/FileTreeController.m b/FileTree/FileTreeController.m index baf030bcd..0d0312e96 100644 --- a/FileTree/FileTreeController.m +++ b/FileTree/FileTreeController.m @@ -63,4 +63,26 @@ } } +-(BOOL)validateMenuItem:(NSMenuItem*)menuItem +{ + SEL action = [menuItem action]; + + if ([outlineView numberOfSelectedRows] == 0) + return NO; + + if (action == @selector(setAsRoot:)) + { + BOOL isDir; + NSInteger row = [outlineView selectedRow]; + + if ([outlineView numberOfSelectedRows] > 1) + return NO; + + // Only let directories be Set as Root + [[NSFileManager defaultManager] fileExistsAtPath:[[[outlineView itemAtRow:row] URL] path] isDirectory:&isDir]; + return isDir; + } + + return YES; +} @end diff --git a/FileTree/FileTreeOutlineView.m b/FileTree/FileTreeOutlineView.m index 52ccbab1c..154b2cafc 100644 --- a/FileTree/FileTreeOutlineView.m +++ b/FileTree/FileTreeOutlineView.m @@ -14,7 +14,6 @@ - (void)awakeFromNib { - [[self menu] setAutoenablesItems:NO]; [self setDoubleAction:@selector(addToPlaylist:)]; [self setTarget:[self delegate]]; } @@ -52,7 +51,6 @@ { //Find which row is under the cursor [[self window] makeFirstResponder:self]; - BOOL isDir; NSPoint menuPoint = [self convertPoint:[event locationInWindow] fromView:nil]; NSInteger iRow = [self rowAtPoint:menuPoint]; NSMenu* contextMenu = [self menu]; @@ -71,25 +69,6 @@ [self selectRow:iRow byExtendingSelection:NO]; } - if ([self numberOfSelectedRows] > 0) - { - [[contextMenu itemWithTag:1] setEnabled:YES]; // Add to Playlist - [[contextMenu itemWithTag:2] setEnabled:YES]; // Set as Playlist - [[contextMenu itemWithTag:3] setEnabled:YES]; // Show in Finder - - // Only let directories be Set as Root - [[NSFileManager defaultManager] fileExistsAtPath:[[[self itemAtRow:iRow] URL] path] isDirectory:&isDir]; - [[contextMenu itemWithTag:4] setEnabled:(isDir? YES : NO)]; - } - else - { - //No rows are selected, so the menu should be displayed with all items disabled - int i; - for (i=0;i<[contextMenu numberOfItems];i++) { - [[contextMenu itemAtIndex:i] setEnabled:NO]; - } - } - return contextMenu; } diff --git a/Plugins/TagLib/TagLibMetadataReader.h b/Plugins/TagLib/TagLibMetadataReader.h index 0068fcd37..a9b8a575b 100644 --- a/Plugins/TagLib/TagLibMetadataReader.h +++ b/Plugins/TagLib/TagLibMetadataReader.h @@ -15,4 +15,7 @@ } ++ (BOOL)isCoverFile:(NSString *)fileName; ++ (NSArray *)coverNames; + @end diff --git a/Plugins/TagLib/TagLibMetadataReader.m b/Plugins/TagLib/TagLibMetadataReader.m index 4a49b5a16..cb6a1a97d 100644 --- a/Plugins/TagLib/TagLibMetadataReader.m +++ b/Plugins/TagLib/TagLibMetadataReader.m @@ -85,6 +85,55 @@ } } + if (nil == image) { + // Try to load image from external file + + // If we find an appropriately-named image in this directory, it will + // be tagged with the first image cache tag. Subsequent directory entries + // may have a different tag, but an image search would result in the same + // artwork. + + static NSString *lastImagePath = nil; + static NSString *lastCacheTag = nil; + + NSString *path = [[url path] stringByDeletingLastPathComponent]; + + if ([path isEqualToString:lastImagePath]) { + // Use whatever image may have been stored with the initial tag for the path + // (might be nil but no point scanning again) + + image = [NSImage imageNamed:lastCacheTag]; + } else { + // Book-keeping... + + if (nil != lastImagePath) + [lastImagePath release]; + + lastImagePath = [path retain]; + + if (nil != lastCacheTag) + [lastCacheTag release]; + + lastCacheTag = [imageCacheTag retain]; + + // Gather list of candidate image files + + NSArray *fileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; + NSArray *imageFileNames = [fileNames pathsMatchingExtensions:[NSImage imageFileTypes]]; + + NSEnumerator *imageEnumerator = [imageFileNames objectEnumerator]; + NSString *fileName; + + while (fileName = [imageEnumerator nextObject]) { + if ([TagLibMetadataReader isCoverFile:fileName]) { + image = [[[NSImage alloc] initByReferencingFile:[path stringByAppendingPathComponent:fileName]] autorelease]; + [image setName:imageCacheTag]; + break; + } + } + } + } + if (nil != image) { [dict setObject:image forKey:@"albumArt"]; } @@ -93,6 +142,25 @@ return [dict autorelease]; } ++ (BOOL)isCoverFile:(NSString *)fileName +{ + NSEnumerator *coverEnumerator = [[TagLibMetadataReader coverNames] objectEnumerator]; + NSString *coverFileName; + + while (coverFileName = [coverEnumerator nextObject]) { + if ([[[[fileName lastPathComponent] stringByDeletingPathExtension] lowercaseString] hasSuffix:coverFileName]) { + return true; + } + } + return false; + +} + ++ (NSArray *)coverNames +{ + return [NSArray arrayWithObjects:@"cover", @"folder", @"album", @"front", nil]; +} + + (NSArray *)fileTypes { //May be a way to get a list of supported formats