cog/Playlist/PlaylistController.m

804 lines
18 KiB
Matlab
Raw Normal View History

2005-06-02 18:16:43 +00:00
//
// PlaylistController.m
// Cog
//
// Created by Vincent Spader on 3/18/05.
2005-07-02 21:02:06 +00:00
// Copyright 2005 Vincent Spader All rights reserved.
2005-06-02 18:16:43 +00:00
//
#import "PlaylistController.h"
#import "PlaylistEntry.h"
2006-01-20 15:22:03 +00:00
#import "Shuffle.h"
2005-06-02 18:16:43 +00:00
#import "CogAudio/AudioPlayer.h"
2006-05-07 13:19:23 +00:00
2005-06-02 18:16:43 +00:00
@implementation PlaylistController
2006-01-20 15:22:03 +00:00
#define SHUFFLE_HISTORY_SIZE 100
2005-06-02 18:16:43 +00:00
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super initWithCoder:decoder];
if (self)
{
acceptableFileTypes = [[AudioPlayer fileTypes] retain];
2005-06-02 18:16:43 +00:00
acceptablePlaylistTypes = [[NSArray alloc] initWithObjects:@"playlist",nil];
2005-06-02 18:16:43 +00:00
shuffleList = [[NSMutableArray alloc] init];
// DBLog(@"DAH BUTTER CHORNAR: %@", history);
}
return self;
}
- (void)awakeFromNib
{
[super awakeFromNib];
2006-05-29 22:23:56 +00:00
NSNotificationCenter* ns = [NSNotificationCenter defaultCenter];
[ns addObserver:self selector:@selector(handlePlaylistViewHeaderNotification:) name:@"PlaylistViewColumnSeparatorDoubleClick" object:nil];
2005-06-02 18:16:43 +00:00
}
- (NSArray *)filesAtPath:(NSString *)path
2005-06-02 18:16:43 +00:00
{
BOOL isDir;
NSFileManager *manager;
manager = [NSFileManager defaultManager];
NSLog(@"Checking if path is a directory: %@", path);
2005-06-02 18:16:43 +00:00
if ([manager fileExistsAtPath:path isDirectory:&isDir] && isDir == YES)
{
DBLog(@"path is directory");
2005-06-02 18:16:43 +00:00
int j;
NSArray *subpaths;
NSMutableArray *validPaths = [[NSMutableArray alloc] init];
2005-06-02 18:16:43 +00:00
subpaths = [manager subpathsAtPath:path];
DBLog(@"Subpaths: %@", subpaths);
2005-06-02 18:16:43 +00:00
for (j = 0; j < [subpaths count]; j++)
{
NSString *filepath;
filepath = [NSString pathWithComponents:[NSArray arrayWithObjects:path,[subpaths objectAtIndex:j],nil]];
if ([manager fileExistsAtPath:filepath isDirectory:&isDir] && isDir == NO)
{
if ([acceptableFileTypes containsObject:[[filepath pathExtension] lowercaseString]] && [[NSFileManager defaultManager] fileExistsAtPath:filepath])
{
[validPaths addObject:filepath];
}
}
2005-06-02 18:16:43 +00:00
}
return [validPaths autorelease];
2005-06-02 18:16:43 +00:00
}
else
{
NSLog(@"path is a file");
if ([acceptableFileTypes containsObject:[[path pathExtension] lowercaseString]] && [[NSFileManager defaultManager] fileExistsAtPath:path])
{
NSLog(@"RETURNING THING");
return [NSArray arrayWithObject:path];
}
else
{
return nil;
}
2005-06-02 18:16:43 +00:00
}
}
- (void)insertPaths:(NSArray *)paths atIndex:(int)index sort:(BOOL)sort
2005-06-02 18:16:43 +00:00
{
NSArray *sortedFiles;
NSMutableArray *files = [[NSMutableArray alloc] init];
NSMutableArray *entries= [[NSMutableArray alloc] init];
2005-06-02 18:16:43 +00:00
int i;
if (!paths)
return;
2005-06-02 18:16:43 +00:00
if (index < 0)
index = 0;
for(i=0; i < [paths count]; i++)
{
[files addObjectsFromArray:[self filesAtPath:[paths objectAtIndex:i]]];
NSLog(@"files is: %i", [files count]);
}
2005-06-02 18:16:43 +00:00
DBLog(@"Sorting paths");
2005-06-02 18:16:43 +00:00
if (sort == YES)
2005-06-29 15:28:20 +00:00
{
sortedFiles = [files sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
2005-06-29 15:28:20 +00:00
}
2005-06-02 18:16:43 +00:00
else
2005-06-29 15:28:20 +00:00
{
2006-05-12 21:40:46 +00:00
sortedFiles = files;
2005-06-29 15:28:20 +00:00
}
for (i = 0; i < [sortedFiles count]; i++)
2005-06-02 18:16:43 +00:00
{
PlaylistEntry *pe = [[PlaylistEntry alloc] init];
[pe setFilename:[sortedFiles objectAtIndex:i]];
[pe setIndex:index+i];
[pe setTitle:[[sortedFiles objectAtIndex:i] lastPathComponent]];
// [pe performSelectorOnMainThread:@selector(readTags) withObject:nil waitUntilDone:NO];
// [pe performSelectorOnMainThread:@selector(readInfo) withObject:nil waitUntilDone:NO];
2005-06-02 18:16:43 +00:00
[entries addObject:pe];
[pe release];
2005-06-02 18:16:43 +00:00
}
NSRange r = NSMakeRange(index, [entries count]);
NSLog(@"MAking range from: %i to %i", index, index + [entries count]);
NSIndexSet *is = [NSIndexSet indexSetWithIndexesInRange:r];
NSLog(@"INDex set: %@", is);
NSLog(@"Adding: %i files", [entries count]);
[self insertObjects:entries atArrangedObjectIndexes:is];
2005-06-02 18:16:43 +00:00
if (shuffle == YES)
2006-01-20 15:22:03 +00:00
[self resetShuffleList];
2005-06-02 18:16:43 +00:00
2005-06-30 17:46:07 +00:00
[self setSelectionIndex:index];
//Other thread will release entries....crazy crazy bad idea...whatever
[NSThread detachNewThreadSelector:@selector(readMetaData:) toTarget:self withObject:entries];
2006-05-12 21:40:46 +00:00
[files release];
return;
2005-06-02 18:16:43 +00:00
}
- (void)readMetaData:(id)entries
2005-06-02 18:16:43 +00:00
{
2006-05-12 19:08:39 +00:00
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int i;
for (i = 0; i < [entries count]; i++)
{
PlaylistEntry *pe =[entries objectAtIndex:i];
2007-02-18 21:48:37 +00:00
[pe readInfoThreaded];
[pe readTagsThreaded];
//Hack so the display gets updated
if (pe == [self currentEntry])
[self performSelectorOnMainThread:@selector(setCurrentEntry:) withObject:[self currentEntry] waitUntilDone:YES];
}
2005-06-02 18:16:43 +00:00
[self performSelectorOnMainThread:@selector(updateTotalTime) withObject:nil waitUntilDone:NO];
[entries release];
2006-05-12 19:08:39 +00:00
[pool release];
2005-06-02 18:16:43 +00:00
}
- (void)addPaths:(NSArray *)paths sort:(BOOL)sort
2005-06-02 18:16:43 +00:00
{
[self insertPaths:paths atIndex:[[self arrangedObjects] count] sort:sort];
2005-06-02 18:16:43 +00:00
}
- (NSArray *)acceptableFileTypes
{
return acceptableFileTypes;
}
2006-04-29 00:03:28 +00:00
- (void)tableView:(NSTableView *)tableView
didClickTableColumn:(NSTableColumn *)tableColumn
{
2006-04-30 13:01:33 +00:00
if (shuffle == YES)
[self resetShuffleList];
2006-04-29 00:03:28 +00:00
[self updateIndexesFromRow:0];
}
2005-06-02 18:16:43 +00:00
- (BOOL)tableView:(NSTableView*)tv
acceptDrop:(id <NSDraggingInfo>)info
row:(int)row
dropOperation:(NSTableViewDropOperation)op
{
int i;
2006-09-02 16:09:20 +00:00
NSLog(@"DROPPED");
2005-06-02 18:16:43 +00:00
[super tableView:tv acceptDrop:info row:row dropOperation:op];
if ([info draggingSource] == tableView)
{
//DNDArrayController handles moving...still need to update the uhm...indices
NSLog(@"Archive stuff");
2005-06-02 18:16:43 +00:00
NSArray *rows = [NSKeyedUnarchiver unarchiveObjectWithData:[[info draggingPasteboard] dataForType: MovedRowsType]];
NSLog(@"Whatever");
2005-06-02 18:16:43 +00:00
NSIndexSet *indexSet = [self indexSetFromRows:rows];
int firstIndex = [indexSet firstIndex];
if (firstIndex > row)
{
i = row;
}
else
{
i = firstIndex;
}
2006-04-29 00:03:28 +00:00
NSLog(@"Updating indexes: %i", i);
2005-06-02 18:16:43 +00:00
[self updateIndexesFromRow:i];
return YES;
}
if (row < 0)
row = 0;
// Determine the type of object that was dropped
NSArray *supportedtypes = [NSArray arrayWithObjects:NSFilenamesPboardType, iTunesDropType, nil];
NSPasteboard *pboard = [info draggingPasteboard];
NSString *bestType = [pboard availableTypeFromArray:supportedtypes];
// Get files from a normal file drop (such as from Finder)
if ([bestType isEqualToString:NSFilenamesPboardType]) {
NSArray *files = [[info draggingPasteboard] propertyListForType:NSFilenamesPboardType];
NSLog(@"INSERTING PATHS: %@", files);
[self insertPaths:files atIndex:row sort:YES];
}
// Get files from an iTunes drop
if ([bestType isEqualToString:iTunesDropType]) {
NSDictionary *iTunesDict = [pboard propertyListForType:iTunesDropType];
NSDictionary *tracks = [iTunesDict valueForKey:@"Tracks"];
2005-06-02 18:16:43 +00:00
// Convert the iTunes URLs to filenames
NSMutableArray *files = [[NSMutableArray alloc] init];
NSEnumerator *e = [[tracks allValues] objectEnumerator];
NSDictionary *trackInfo;
NSURL *url;
while (trackInfo = [e nextObject]) {
url = [[NSURL alloc] initWithString:[trackInfo valueForKey:@"Location"]];
if ([url isFileURL]) {
[files addObject:[url path]];
}
[url release];
}
NSLog(@"INSERTING ITUNES PATHS: %@", files);
[self insertPaths:files atIndex:row sort:YES];
[files release];
}
2006-05-12 21:40:46 +00:00
NSLog(@"UPDATING");
2005-06-29 22:41:49 +00:00
[self updateIndexesFromRow:row];
2006-04-13 18:40:23 +00:00
[self updateTotalTime];
2005-06-02 18:16:43 +00:00
if (shuffle == YES)
2006-01-20 15:22:03 +00:00
[self resetShuffleList];
2005-06-02 18:16:43 +00:00
return YES;
}
2006-04-13 18:40:23 +00:00
- (void)updateTotalTime
{
double tt=0;
2006-04-29 00:03:28 +00:00
NSEnumerator *enumerator = [[self arrangedObjects] objectEnumerator];
2006-04-13 18:40:23 +00:00
PlaylistEntry* pe;
while (pe = [enumerator nextObject]) {
tt += [pe length];
}
int sec = (int)(tt/1000.0);
[self setTotalTimeDisplay:[NSString stringWithFormat:@"%i:%02i",sec/60, sec%60]];
}
- (void)setTotalTimeDisplay:(NSString *)ttd
{
[ttd retain];
[totalTimeDisplay release];
totalTimeDisplay = ttd;
NSLog(@"Displaying: %@", ttd);
}
- (NSString *)totalTimeDisplay;
{
return totalTimeDisplay;
}
2005-06-02 18:16:43 +00:00
- (void)updateIndexesFromRow:(int) row
{
// DBLog(@"UPDATE INDEXES: %i", row);
int j;
2006-04-29 00:03:28 +00:00
for (j = row; j < [[self arrangedObjects] count]; j++)
2005-06-02 18:16:43 +00:00
{
PlaylistEntry *p;
2006-04-29 00:03:28 +00:00
p = [[self arrangedObjects] objectAtIndex:j];
2005-06-02 18:16:43 +00:00
2006-04-15 14:17:46 +00:00
[p setIndex:j];
2005-06-02 18:16:43 +00:00
}
}
- (void)removeObjectsAtArrangedObjectIndexes:(NSIndexSet *)indexes
{
2005-06-06 04:37:35 +00:00
unsigned int *indexBuffer;
NSMutableArray *a = [[NSMutableArray alloc] init];
int i;
//10.3 fix
indexBuffer = malloc([indexes count]*sizeof(unsigned int));
[indexes getIndexes:indexBuffer maxCount:[indexes count] inIndexRange:nil];
for (i = 0; i < [indexes count]; i++)
{
NSLog(@"REMOVING FROM INDEX: %i", indexBuffer[i]);
2005-06-06 04:37:35 +00:00
[a addObject:[[self arrangedObjects] objectAtIndex:(indexBuffer[i])]];
}
// a = [[self arrangedObjects] objectsAtIndexes:indexes]; //10.4 only
2005-06-02 18:16:43 +00:00
if ([a containsObject:currentEntry])
{
[currentEntry setIndex:-1];
}
[super removeObjectsAtArrangedObjectIndexes:indexes];
[self updateIndexesFromRow:[indexes firstIndex]];
2006-04-13 18:40:23 +00:00
[self updateTotalTime];
2005-06-02 18:16:43 +00:00
if (shuffle == YES)
2006-01-20 15:22:03 +00:00
[self resetShuffleList];
2005-06-06 04:37:35 +00:00
[a release];
2005-06-02 18:16:43 +00:00
}
2006-05-12 19:23:17 +00:00
- (void)setSortDescriptors:(NSArray *)sortDescriptors
{
//Cheap hack so the index column isn't sorted
2006-05-29 23:03:58 +00:00
if (([sortDescriptors count] != 0) && [[[sortDescriptors objectAtIndex:0] key] caseInsensitiveCompare:@"displayIndex"] == NSOrderedSame)
2006-05-12 19:23:17 +00:00
{
//Remove the sort descriptors
[super setSortDescriptors:nil];
[self rearrangeObjects];
2006-05-29 23:03:58 +00:00
return;
2006-05-12 19:23:17 +00:00
}
2006-05-29 23:03:58 +00:00
[super setSortDescriptors:sortDescriptors];
2006-05-12 19:23:17 +00:00
}
- (IBAction)sortByPath:(id)sender
2006-04-30 13:01:33 +00:00
{
NSSortDescriptor *s = [[NSSortDescriptor alloc] initWithKey:@"filename" ascending:YES selector:@selector(compare:)];
[self setSortDescriptors:[NSArray arrayWithObject:s]];
[self rearrangeObjects];
2006-04-30 13:01:33 +00:00
[s release];
if (shuffle == YES)
[self resetShuffleList];
[self updateIndexesFromRow:0];
}
- (void)randomizeList
{
[self setSortDescriptors:nil];
2006-05-12 19:32:01 +00:00
2006-04-30 13:01:33 +00:00
[self setContent:[Shuffle shuffleList:[self content]]];
if (shuffle == YES)
[self resetShuffleList];
[self updateIndexesFromRow:0];
}
2005-06-02 18:16:43 +00:00
- (IBAction)takeShuffleFromObject:(id)sender
{
if( [sender respondsToSelector: @selector(boolValue)] )
[self setShuffle: [sender boolValue]];
else
[self setShuffle: [sender state]];
}
- (IBAction)takeRepeatFromObject:(id)sender
{
if( [sender respondsToSelector: @selector(boolValue)] )
[self setRepeat: [sender boolValue]];
else
[self setRepeat: [sender state]];
}
2006-04-13 02:51:22 +00:00
- (PlaylistEntry *)entryAtIndex:(int)i
{
2006-04-15 13:51:40 +00:00
if (i < 0)
2006-04-13 02:51:22 +00:00
{
2006-04-15 13:51:40 +00:00
if (repeat == YES)
i += [[self arrangedObjects] count];
else
return nil;
}
else if (i >= [[self arrangedObjects] count])
{
if (repeat == YES)
i -= [[self arrangedObjects] count];
else
return nil;
}
return [[self arrangedObjects] objectAtIndex:i];
}
- (PlaylistEntry *)shuffledEntryAtIndex:(int)i
{
// NSLog(@"SHUFFLE: %i %i %i %i", i, shuffleIndex, offset, [shuffleList count]);
while (i < 0)
{
if (repeat == YES)
{
2006-04-15 13:51:40 +00:00
[self addShuffledListToFront];
//change i appropriately
i += [[self arrangedObjects] count];
}
2006-04-15 13:51:40 +00:00
else
{
2006-04-15 13:51:40 +00:00
return nil;
}
2006-04-13 02:51:22 +00:00
}
2006-04-15 13:51:40 +00:00
while (i >= [shuffleList count])
2006-04-13 02:51:22 +00:00
{
2006-04-15 13:51:40 +00:00
if (repeat == YES)
{
2006-04-15 13:51:40 +00:00
NSLog(@"Adding shuffled list to back!");
[self addShuffledListToBack];
}
2006-04-15 13:51:40 +00:00
else
{
2006-04-15 13:51:40 +00:00
return nil;
}
2006-04-15 13:51:40 +00:00
}
return [shuffleList objectAtIndex:i];
2006-04-13 02:51:22 +00:00
}
/*- (PlaylistEntry *)entryAtOffset:(int)offset
2006-01-20 15:22:03 +00:00
{
if (shuffle == YES)
{
int i = shuffleIndex;
i += offset;
2006-04-05 17:25:51 +00:00
// NSLog(@"SHUFFLE: %i %i %i %i", i, shuffleIndex, offset, [shuffleList count]);
2006-01-20 15:22:03 +00:00
while (i < 0)
{
if (repeat == YES)
{
[self addShuffledListToFront];
//change i appropriately
i += [[self arrangedObjects] count];
}
else
{
return nil;
}
}
while (i >= [shuffleList count])
{
if (repeat == YES)
{
2006-04-05 17:25:51 +00:00
NSLog(@"Adding shuffled list to back!");
2006-01-20 15:22:03 +00:00
[self addShuffledListToBack];
}
else
{
return nil;
}
}
return [shuffleList objectAtIndex:i];
}
else
{
int i;
i = [currentEntry index];
i += (offset-1);
if (i < 0)
{
if (repeat == YES)
i += [[self arrangedObjects] count];
else
return nil;
}
else if (i >= [[self arrangedObjects] count])
{
if (repeat == YES)
i -= [[self arrangedObjects] count];
else
return nil;
}
return [[self arrangedObjects] objectAtIndex:i];
}
}
*/
2006-04-15 13:51:40 +00:00
- (PlaylistEntry *)getNextEntry:(PlaylistEntry *)pe
{
if (shuffle == YES)
{
return [self shuffledEntryAtIndex:[pe shuffleIndex] + 1];
}
else
{
return [self entryAtIndex:[pe index] + 1];
}
}
- (PlaylistEntry *)getPrevEntry:(PlaylistEntry *)pe
{
if (shuffle == YES)
{
return [self shuffledEntryAtIndex:[pe shuffleIndex] - 1];
}
else
{
2006-04-15 14:17:46 +00:00
//Fix for removing a track, then pressing prev with repeat turned on
if ([pe index] == -1)
{
return [self entryAtIndex:[pe index]];
}
else
{
return [self entryAtIndex:[pe index] - 1];
}
2006-04-15 13:51:40 +00:00
}
}
2006-01-29 14:57:48 +00:00
- (BOOL)next
2005-06-02 18:16:43 +00:00
{
PlaylistEntry *pe;
2006-04-15 13:51:40 +00:00
pe = [self getNextEntry:[self currentEntry]];
2005-06-02 18:16:43 +00:00
if (pe == nil)
2006-01-29 14:57:48 +00:00
return NO;
2005-06-02 18:16:43 +00:00
2006-01-20 15:22:03 +00:00
[self setCurrentEntry:pe];
2006-01-29 14:57:48 +00:00
return YES;
2005-06-02 18:16:43 +00:00
}
2006-01-29 14:57:48 +00:00
- (BOOL)prev
2005-06-02 18:16:43 +00:00
{
PlaylistEntry *pe;
2006-04-15 13:51:40 +00:00
pe = [self getPrevEntry:[self currentEntry]];
2005-06-02 18:16:43 +00:00
if (pe == nil)
2006-01-29 14:57:48 +00:00
return NO;
2005-06-02 18:16:43 +00:00
2006-01-20 15:22:03 +00:00
[self setCurrentEntry:pe];
2006-01-29 14:57:48 +00:00
return YES;
2005-06-02 18:16:43 +00:00
}
2006-04-14 20:34:14 +00:00
2006-04-15 13:51:40 +00:00
//Need to do...when first generated, need to have current entry at the beginning of the list.
- (void)addShuffledListToFront
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
NSArray *newList = [Shuffle shuffleList:[self arrangedObjects]];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, [newList count])];
2005-06-02 18:16:43 +00:00
2006-04-05 17:25:51 +00:00
[shuffleList insertObjects:newList atIndexes:indexSet];
2006-04-15 13:51:40 +00:00
int i;
for (i = 0; i < [shuffleList count]; i++)
{
[[shuffleList objectAtIndex:i] setShuffleIndex:i];
}
2005-06-02 18:16:43 +00:00
}
2006-04-15 13:51:40 +00:00
- (void)addShuffledListToBack
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
NSArray *newList = [Shuffle shuffleList:[self arrangedObjects]];
2006-04-05 17:25:51 +00:00
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange([shuffleList count], [newList count])];
2006-04-15 13:51:40 +00:00
2006-04-05 17:25:51 +00:00
[shuffleList insertObjects:newList atIndexes:indexSet];
2006-04-15 13:51:40 +00:00
int i;
for (i = ([shuffleList count] - [newList count]); i < [shuffleList count]; i++)
{
[[shuffleList objectAtIndex:i] setShuffleIndex:i];
}
2005-06-02 18:16:43 +00:00
}
2006-01-20 15:22:03 +00:00
- (void)resetShuffleList
2005-06-02 18:16:43 +00:00
{
2006-01-20 15:22:03 +00:00
[shuffleList removeAllObjects];
2006-04-15 13:51:40 +00:00
2006-04-05 17:25:51 +00:00
[self addShuffledListToFront];
2006-04-15 13:51:40 +00:00
2006-04-15 14:17:46 +00:00
if (currentEntry && [currentEntry index] != -1)
2006-04-15 13:51:40 +00:00
{
2006-04-15 14:17:46 +00:00
[shuffleList insertObject:currentEntry atIndex:0];
[currentEntry setShuffleIndex:0];
//Need to rejigger so the current entry is at the start now...
int i;
BOOL found = NO;
for (i = 1; i < [shuffleList count]; i++)
2006-04-15 13:51:40 +00:00
{
2006-04-15 14:17:46 +00:00
if (found == NO && [[shuffleList objectAtIndex:i] filename] == [currentEntry filename])
{
found = YES;
[shuffleList removeObjectAtIndex:i];
}
2006-04-15 13:51:40 +00:00
[[shuffleList objectAtIndex:i] setShuffleIndex:i];
2006-04-15 14:17:46 +00:00
}
2006-04-15 13:51:40 +00:00
}
2005-06-02 18:16:43 +00:00
}
- (id)currentEntry
{
return currentEntry;
}
- (void)setCurrentEntry:(PlaylistEntry *)pe
2005-06-02 18:16:43 +00:00
{
[currentEntry setCurrent:NO];
[pe setCurrent:YES];
[tableView scrollRowToVisible:[pe index]];
2005-06-02 18:16:43 +00:00
[pe retain];
[currentEntry release];
currentEntry = pe;
}
- (void)setShuffle:(BOOL)s
{
shuffle = s;
if (shuffle == YES)
2006-01-20 15:22:03 +00:00
[self resetShuffleList];
2005-06-02 18:16:43 +00:00
}
- (BOOL)shuffle
{
return shuffle;
}
- (void)setRepeat:(BOOL)r
{
repeat = r;
}
- (BOOL)repeat
{
return repeat;
}
2006-04-30 15:31:57 +00:00
- (void)setFilterPredicate:(NSPredicate *)filterPredicate
{
[super setFilterPredicate:filterPredicate];
2006-04-30 16:05:39 +00:00
int j;
for (j = 0; j < [[self content] count]; j++)
{
PlaylistEntry *p;
p = [[self content] objectAtIndex:j];
[p setIndex:-1];
}
[self updateIndexesFromRow:0];
2006-04-30 15:31:57 +00:00
}
2005-06-02 18:16:43 +00:00
- (void)savePlaylist:(NSString *)filename
{
// DBLog(@"SAVING PLAYLIST: %@", filename);
NSString *fileContents;
NSMutableArray *filenames = [NSMutableArray array];
NSEnumerator *enumerator;
PlaylistEntry *entry;
2006-04-30 14:05:07 +00:00
enumerator = [[self content] objectEnumerator];
2005-06-02 18:16:43 +00:00
while (entry = [enumerator nextObject])
{
[filenames addObject:[entry filename]];
}
fileContents = [filenames componentsJoinedByString:@"\n"];
[fileContents writeToFile:filename atomically:NO];
}
- (void)loadPlaylist:(NSString *)filename
{
NSString *fileContents;
[self removeObjects:[self arrangedObjects]];
fileContents = [NSString stringWithContentsOfFile:filename];
if (fileContents)
{
NSArray *filenames = [fileContents componentsSeparatedByString:@"\n"];
// DBLog(@"filenames: %@", filenames);
[self addPaths:filenames sort:NO];
}
}
- (NSArray *)acceptablePlaylistTypes
{
return acceptablePlaylistTypes;
}
- (NSString *)playlistFilename
{
return playlistFilename;
}
- (void)setPlaylistFilename:(NSString *)pf
{
[pf retain];
[playlistFilename release];
playlistFilename = pf;
}
- (IBAction)showFileInFinder:(id)sender
{
NSWorkspace* ws = [NSWorkspace sharedWorkspace];
if ([self selectionIndex] < 0)
return;
PlaylistEntry* curr = [self entryAtIndex:[self selectionIndex]];
[ws selectFile:[curr filename] inFileViewerRootedAtPath:[curr filename]];
}
2006-05-29 22:23:56 +00:00
- (void)handlePlaylistViewHeaderNotification:(NSNotification*)notif
{
NSTableView *tv = [notif object];
NSNumber *colIdx = [[notif userInfo] objectForKey:@"column"];
NSTableColumn *col = [[tv tableColumns] objectAtIndex:[colIdx intValue]];
// find which function to call on PlaylistEntry*
SEL sel;
NSString* identifier = [col identifier];
BOOL isNumeric = NO;
2006-06-04 19:44:06 +00:00
if ([identifier compare:@"title"] == NSOrderedSame)
2006-05-29 22:23:56 +00:00
sel = @selector(title);
else if ([identifier compare:@"length"] == NSOrderedSame)
sel = @selector(lengthString);
else if ([identifier compare:@"index"] == NSOrderedSame) {
sel = @selector(index);
isNumeric = YES;
}
else if ([identifier compare:@"artist"] == NSOrderedSame)
sel = @selector(artist);
else if ([identifier compare:@"album"] == NSOrderedSame)
sel = @selector(album);
else if ([identifier compare:@"year"] == NSOrderedSame)
sel = @selector(year);
else if ([identifier compare:@"genre"] == NSOrderedSame)
sel = @selector(genre);
else if ([identifier compare:@"track"] == NSOrderedSame) {
sel = @selector(track);
isNumeric = YES;
}
else
return;
NSCell *cell = [col dataCell];
NSAttributedString * as = [cell attributedStringValue];
// find the longest string display length in that column
NSArray *entries = [self arrangedObjects];
NSEnumerator *enumerator = [entries objectEnumerator];
PlaylistEntry *entry;
float maxlength = -1;
NSString *ret;
while (entry = [enumerator nextObject]) {
if (isNumeric)
ret = [NSString stringWithFormat:@"%d", objc_msgSend(entry, sel)];
else
ret = objc_msgSend(entry, sel);
if ([ret sizeWithAttributes:[as attributesAtIndex:0 effectiveRange:nil]].width > maxlength)
maxlength = [ret sizeWithAttributes:[as attributesAtIndex:0 effectiveRange:nil]].width;
}
// set the new width (plus a 5 pixel extra to avoid "..." string substitution)
[col setWidth:maxlength+5];
}
2005-06-02 18:16:43 +00:00
@end