PlaylistLoader now handles playlist files like directories, and simply expands them. Makes sure only unique URL's are added!

CQTexperiment
vspader 2007-05-27 16:14:29 +00:00
parent b5e85a9a3c
commit 5b5ce493e3
3 changed files with 40 additions and 29 deletions

View File

@ -242,7 +242,7 @@ increase/decrease as long as the user holds the left/right, plus/minus button */
NSString *filename = @"~/Library/Application Support/Cog/Default.m3u"; NSString *filename = @"~/Library/Application Support/Cog/Default.m3u";
[playlistLoader loadM3u:[filename stringByExpandingTildeInPath]]; [playlistLoader addURL:[NSURL fileURLWithPath:[filename stringByExpandingTildeInPath]]];
} }
- (void)applicationWillTerminate:(NSNotification *)aNotification - (void)applicationWillTerminate:(NSNotification *)aNotification

View File

@ -21,15 +21,14 @@ typedef enum {
//load arrays of urls... //load arrays of urls...
- (void)addURLs:(NSArray *)urls sort:(BOOL)sort; - (void)addURLs:(NSArray *)urls sort:(BOOL)sort;
- (void)addURL:(NSURL *)url;
- (void)insertURLs:(NSArray *)urls atIndex:(int)index sort:(BOOL)sort; - (void)insertURLs:(NSArray *)urls atIndex:(int)index sort:(BOOL)sort;
//load playlist auto-determines type to be either pls or m3u. //load playlist auto-determines type to be either pls or m3u.
- (BOOL)load:(NSString *)filename; - (NSArray *)urlsFromPlaylist:(NSString *)filename;
- (BOOL)loadM3u:(NSString *)filename; - (NSArray *)urlsFromM3u:(NSString *)filename;
- (BOOL)loadPls:(NSString *)filename; - (NSArray *)urlsFromPls:(NSString *)filename;
//save playlist, uses current info, will fail if there is no current info.
- (BOOL)save;
//save playlist, auto-determines type based on extension. Uses m3u if it cannot be determined. //save playlist, auto-determines type based on extension. Uses m3u if it cannot be determined.
- (BOOL)save:(NSString *)filename; - (BOOL)save:(NSString *)filename;
- (BOOL)save:(NSString *)filename asType:(PlaylistType)type; - (BOOL)save:(NSString *)filename asType:(PlaylistType)type;
@ -39,10 +38,4 @@ typedef enum {
- (NSArray *)acceptableFileTypes; - (NSArray *)acceptableFileTypes;
- (NSArray *)acceptablePlaylistTypes; - (NSArray *)acceptablePlaylistTypes;
- (PlaylistType)currentType;
- (void)setCurrentType:(PlaylistType)type;
- (NSString *)currentFile;
- (void)setCurrentFile:(NSString *)currentFile;
@end @end

View File

@ -17,20 +17,24 @@
@implementation PlaylistLoader @implementation PlaylistLoader
//load/save playlist auto-determines type to be either pls or m3u. //load/save playlist auto-determines type to be either pls or m3u.
- (BOOL)load:(NSString *)filename - (NSArray *)urlsFromPlaylist:(NSString *)filename
{ {
NSString *ext = [filename pathExtension]; NSString *ext = [filename pathExtension];
if ([ext isEqualToString:@"m3u"]) if ([ext isEqualToString:@"m3u"])
{ {
return [self loadM3u:filename]; return [self urlsFromM3u:filename];
} }
else if ([ext isEqualToString:@"pls"]) else if ([ext isEqualToString:@"pls"])
{ {
return [self loadPls:filename]; return [self urlsFromPls:filename];
} }
else else
{ {
return [self loadPls:filename] || [self loadM3u:filename]; NSArray *entries = [self urlsFromPls:filename];
if (entries == nil)
return [self urlsFromM3u:filename];
return entries;
} }
} }
@ -105,7 +109,7 @@
} }
- (BOOL)loadM3u:(NSString *)filename - (NSArray *)urlsFromM3u:(NSString *)filename
{ {
NSLog(@"Loading playlist: %@", filename); NSLog(@"Loading playlist: %@", filename);
@ -129,9 +133,7 @@
[entries addObject:[self urlForPath:entry relativeTo:filename]]; [entries addObject:[self urlForPath:entry relativeTo:filename]];
} }
[self addURLs:entries sort:NO]; return entries;
return YES;
} }
- (BOOL)saveM3u:(NSString *)filename - (BOOL)saveM3u:(NSString *)filename
@ -139,7 +141,7 @@
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filename createFile:YES]; NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filename createFile:YES];
if (!fileHandle) { if (!fileHandle) {
NSLog(@"Error!"); NSLog(@"Error!");
return NO; return nil;
} }
[fileHandle truncateFileAtOffset:0]; [fileHandle truncateFileAtOffset:0];
@ -159,12 +161,12 @@
return YES; return YES;
} }
- (BOOL)loadPls:(NSString *)filename - (NSArray *)urlsFromPls:(NSString *)filename
{ {
NSError *error; NSError *error;
NSString *contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error]; NSString *contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error];
if (error || !contents) { if (error || !contents) {
return NO; return nil;
} }
NSString *entry; NSString *entry;
@ -192,9 +194,7 @@
[scanner release]; [scanner release];
} }
[self addURLs:entries sort:NO]; return entries;
return YES;
} }
- (BOOL)savePls:(NSString *)filename - (BOOL)savePls:(NSString *)filename
@ -251,6 +251,7 @@
- (void)insertURLs:(NSArray *)urls atIndex:(int)index sort:(BOOL)sort - (void)insertURLs:(NSArray *)urls atIndex:(int)index sort:(BOOL)sort
{ {
NSMutableSet *uniqueURLs = [[NSMutableSet alloc] init];
NSMutableArray *allURLs = [[NSMutableArray alloc] init]; NSMutableArray *allURLs = [[NSMutableArray alloc] init];
NSMutableArray *validURLs = [[NSMutableArray alloc] init]; NSMutableArray *validURLs = [[NSMutableArray alloc] init];
NSArray *finalURLs; NSArray *finalURLs;
@ -278,10 +279,16 @@
else else
{ {
//File url //File url
if ([[self acceptablePlaylistTypes] containsObject:[[url path] pathExtension]]) {
[allURLs addObjectsFromArray:[self urlsFromPlaylist:[url path]]];
}
else {
//Not a playlist!
[allURLs addObject:url]; [allURLs addObject:url];
} }
} }
} }
}
else else
{ {
//Non-file URL.. //Non-file URL..
@ -300,7 +307,12 @@
if (![[self acceptableFileTypes] containsObject:[[[url path] pathExtension] lowercaseString]]) if (![[self acceptableFileTypes] containsObject:[[[url path] pathExtension] lowercaseString]])
continue; continue;
if (![uniqueURLs containsObject:url])
{
[validURLs addObject:url]; [validURLs addObject:url];
[uniqueURLs addObject:url];
}
} }
finalURLs = validURLs; finalURLs = validURLs;
@ -342,6 +354,7 @@
[allURLs release]; [allURLs release];
[validURLs release]; [validURLs release];
[uniqueURLs release];
return; return;
} }
@ -374,9 +387,14 @@
[self insertURLs:urls atIndex:[[playlistController content] count] sort:sort]; [self insertURLs:urls atIndex:[[playlistController content] count] sort:sort];
} }
- (void)addURL:(NSURL *)url
{
[self insertURLs:[NSArray arrayWithObject:url] atIndex:[[playlistController content] count] sort:NO];
}
- (NSArray *)acceptableFileTypes - (NSArray *)acceptableFileTypes
{ {
return [AudioPlayer fileTypes]; return [[self acceptablePlaylistTypes] arrayByAddingObjectsFromArray:[AudioPlayer fileTypes]];
} }
- (NSArray *)acceptablePlaylistTypes - (NSArray *)acceptablePlaylistTypes