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.
CQTexperiment
mscott 2010-04-05 12:40:17 -05:00
parent f1b965bf12
commit 1269de06f9
4 changed files with 93 additions and 21 deletions

View File

@ -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

View File

@ -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;
}

View File

@ -15,4 +15,7 @@
}
+ (BOOL)isCoverFile:(NSString *)fileName;
+ (NSArray *)coverNames;
@end

View File

@ -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