2007-03-07 01:26:50 +00:00
|
|
|
//
|
|
|
|
// PlaylistLoader.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/05/07.
|
|
|
|
// Copyright 2007 Vincent Spader All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "PlaylistLoader.h"
|
|
|
|
#import "PlaylistController.h"
|
|
|
|
#import "PlaylistEntry.h"
|
|
|
|
|
2007-03-14 02:28:30 +00:00
|
|
|
#import "NSFileHandle+CreateFile.h"
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
#import "CogAudio/AudioPlayer.h"
|
|
|
|
|
2007-03-07 01:26:50 +00:00
|
|
|
@implementation PlaylistLoader
|
|
|
|
|
|
|
|
//load/save playlist auto-determines type to be either pls or m3u.
|
|
|
|
- (BOOL)load:(NSString *)filename
|
|
|
|
{
|
|
|
|
NSString *ext = [filename pathExtension];
|
|
|
|
if ([ext isEqualToString:@"m3u"])
|
|
|
|
{
|
|
|
|
return [self loadM3u:filename];
|
|
|
|
}
|
|
|
|
else if ([ext isEqualToString:@"pls"])
|
|
|
|
{
|
|
|
|
return [self loadPls:filename];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [self loadPls:filename] || [self loadM3u:filename];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)save
|
|
|
|
{
|
2007-03-09 01:16:06 +00:00
|
|
|
return [self save:currentFile asType:currentType];
|
2007-03-07 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)save:(NSString *)filename
|
|
|
|
{
|
|
|
|
NSString *ext = [filename pathExtension];
|
|
|
|
if ([ext isEqualToString:@"pls"])
|
|
|
|
{
|
|
|
|
return [self save:filename asType:kPlaylistPls];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return [self save:filename asType:kPlaylistM3u];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)save:(NSString *)filename asType:(PlaylistType)type
|
|
|
|
{
|
|
|
|
if (type == kPlaylistM3u)
|
|
|
|
{
|
|
|
|
return [self saveM3u:filename];
|
|
|
|
}
|
|
|
|
else if (type == kPlaylistPls)
|
|
|
|
{
|
|
|
|
return [self savePls:filename];
|
|
|
|
}
|
|
|
|
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
- (NSString *)relativePathFrom:(NSString *)filename toURL:(NSURL *)entryURL
|
2007-03-07 01:26:50 +00:00
|
|
|
{
|
|
|
|
NSString *basePath = [[[filename stringByStandardizingPath] stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
if ([entryURL isFileURL]) {
|
2007-03-07 01:26:50 +00:00
|
|
|
//We want relative paths.
|
|
|
|
NSMutableString *entryPath = [[[[entryURL path] stringByStandardizingPath] mutableCopy] autorelease];
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
[entryPath replaceOccurrencesOfString:basePath withString:@"" options:(NSAnchoredSearch | NSLiteralSearch | NSCaseInsensitiveSearch) range:NSMakeRange(0, [entryPath length])];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
return entryPath;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
//Write [entryURL absoluteString] to file
|
|
|
|
return [entryURL absoluteString];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
- (NSURL *)urlForPath:(NSString *)path relativeTo:(NSString *)baseFilename
|
|
|
|
{
|
|
|
|
if ([path hasPrefix:@"/"]) {
|
|
|
|
return [NSURL fileURLWithPath:path];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSEnumerator *e = [[AudioPlayer schemes] objectEnumerator];
|
|
|
|
NSString *scheme;
|
|
|
|
while (scheme = [e nextObject])
|
|
|
|
{
|
|
|
|
if ([path hasPrefix:[scheme stringByAppendingString:@"://"]])
|
|
|
|
{
|
|
|
|
return [NSURL URLWithString:path];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *basePath = [[[baseFilename stringByStandardizingPath] stringByDeletingLastPathComponent] stringByAppendingString:@"/"];
|
|
|
|
|
|
|
|
return [NSURL fileURLWithPath:[basePath stringByAppendingString:path]];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (BOOL)loadM3u:(NSString *)filename
|
|
|
|
{
|
|
|
|
NSLog(@"Loading playlist: %@", filename);
|
|
|
|
|
|
|
|
NSError *error = nil;
|
|
|
|
NSString *contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error];
|
|
|
|
if (error || !contents) {
|
|
|
|
NSLog(@"Could not open file...%@ %@", contents, error);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *entry;
|
|
|
|
NSEnumerator *e = [[contents componentsSeparatedByString:@"\n"] objectEnumerator];
|
|
|
|
NSMutableArray *entries = [NSMutableArray array];
|
|
|
|
|
2007-03-21 01:36:31 +00:00
|
|
|
while (entry = [[e nextObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])
|
2007-03-09 01:16:06 +00:00
|
|
|
{
|
|
|
|
if ([entry hasPrefix:@"#"] || [entry isEqualToString:@""]) //Ignore extra info
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//Need to add basePath, and convert to URL
|
|
|
|
[entries addObject:[self urlForPath:entry relativeTo:filename]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self addURLs:entries sort:NO];
|
|
|
|
|
|
|
|
return YES;
|
2007-03-07 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)saveM3u:(NSString *)filename
|
|
|
|
{
|
2007-03-14 02:28:30 +00:00
|
|
|
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filename createFile:YES];
|
2007-03-07 01:26:50 +00:00
|
|
|
if (!fileHandle) {
|
2007-03-14 02:28:30 +00:00
|
|
|
NSLog(@"Error!");
|
2007-03-07 01:26:50 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2007-03-09 01:16:06 +00:00
|
|
|
[fileHandle truncateFileAtOffset:0];
|
|
|
|
|
|
|
|
NSLog(@"Saving: %@", filename);
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
PlaylistEntry *pe;
|
|
|
|
NSEnumerator *e = [[playlistController content] objectEnumerator];
|
|
|
|
|
|
|
|
while (pe = [e nextObject])
|
|
|
|
{
|
2007-03-09 01:16:06 +00:00
|
|
|
NSString *path = [self relativePathFrom:filename toURL:[pe url]];
|
2007-03-07 01:26:50 +00:00
|
|
|
[fileHandle writeData:[[path stringByAppendingString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
[fileHandle closeFile];
|
|
|
|
|
2007-03-07 01:26:50 +00:00
|
|
|
[self setCurrentFile:filename];
|
2007-03-09 01:16:06 +00:00
|
|
|
[self setCurrentType:kPlaylistM3u];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)loadPls:(NSString *)filename
|
|
|
|
{
|
|
|
|
NSError *error;
|
2007-03-09 01:16:06 +00:00
|
|
|
NSString *contents = [NSString stringWithContentsOfFile:filename encoding:NSUTF8StringEncoding error:&error];
|
2007-03-07 01:26:50 +00:00
|
|
|
if (error || !contents) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *entry;
|
2007-03-07 01:45:45 +00:00
|
|
|
NSEnumerator *e = [[contents componentsSeparatedByString:@"\n"] objectEnumerator];
|
2007-03-09 01:16:06 +00:00
|
|
|
NSMutableArray *entries = [NSMutableArray array];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
2007-03-21 01:36:31 +00:00
|
|
|
while (entry = [[e nextObject] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]])
|
2007-03-07 01:26:50 +00:00
|
|
|
{
|
2007-03-09 01:16:06 +00:00
|
|
|
NSScanner *scanner = [[NSScanner alloc] initWithString:entry];
|
2007-03-07 01:45:45 +00:00
|
|
|
NSString *lhs = nil;
|
2007-03-09 01:16:06 +00:00
|
|
|
NSString *rhs = nil;
|
2007-03-07 01:45:45 +00:00
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
if (![scanner scanUpToString:@"=" intoString:&lhs] || // get LHS
|
|
|
|
![scanner scanString:@"=" intoString:nil] || // skip the =
|
|
|
|
![scanner scanUpToString:@"" intoString:&rhs] || // get RHS
|
|
|
|
![lhs isEqualToString:@"File"]) // We only want file entries
|
|
|
|
{
|
|
|
|
[scanner release];
|
2007-03-07 01:45:45 +00:00
|
|
|
continue;
|
2007-03-09 01:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//need to add basepath if its a file, and convert to URL
|
|
|
|
[entries addObject:[self urlForPath:rhs relativeTo:filename]];
|
|
|
|
|
|
|
|
[scanner release];
|
2007-03-07 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
[self addURLs:entries sort:NO];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)savePls:(NSString *)filename
|
|
|
|
{
|
2007-03-14 02:28:30 +00:00
|
|
|
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filename createFile:YES];
|
2007-03-07 01:26:50 +00:00
|
|
|
if (!fileHandle) {
|
|
|
|
return NO;
|
|
|
|
}
|
2007-03-09 01:16:06 +00:00
|
|
|
[fileHandle truncateFileAtOffset:0];
|
|
|
|
|
|
|
|
[fileHandle writeData:[[NSString stringWithFormat:@"[playlist]\nnumberOfEntries=%i\n\n",[[playlistController content] count]] dataUsingEncoding:NSUTF8StringEncoding]];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
NSEnumerator *e = [[playlistController content] objectEnumerator];
|
|
|
|
PlaylistEntry *pe;
|
2007-03-07 01:45:45 +00:00
|
|
|
int i = 1;
|
2007-03-07 01:26:50 +00:00
|
|
|
while (pe = [e nextObject])
|
|
|
|
{
|
2007-03-09 01:16:06 +00:00
|
|
|
NSString *path = [self relativePathFrom:filename toURL:[pe url]];
|
2007-03-07 01:26:50 +00:00
|
|
|
NSString *entry = [NSString stringWithFormat:@"File%i=%@\n",i,path];
|
|
|
|
|
|
|
|
[fileHandle writeData:[entry dataUsingEncoding:NSUTF8StringEncoding]];
|
2007-03-07 01:45:45 +00:00
|
|
|
i++;
|
2007-03-07 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
[fileHandle writeData:[@"\nVERSION=2" dataUsingEncoding:NSUTF8StringEncoding]];
|
|
|
|
[fileHandle closeFile];
|
|
|
|
|
2007-03-07 01:26:50 +00:00
|
|
|
[self setCurrentFile:filename];
|
2007-03-09 01:16:06 +00:00
|
|
|
[self setCurrentType:kPlaylistM3u];
|
2007-03-07 01:26:50 +00:00
|
|
|
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2007-03-09 01:16:06 +00:00
|
|
|
- (NSArray *)fileURLsAtPath:(NSString *)path
|
|
|
|
{
|
|
|
|
NSFileManager *manager = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
NSMutableArray *urls = [NSMutableArray array];
|
|
|
|
|
|
|
|
NSString *subpath;
|
|
|
|
NSArray *subpaths = [manager subpathsAtPath:path];
|
|
|
|
NSEnumerator *e = [subpaths objectEnumerator];
|
|
|
|
|
|
|
|
while(subpath = [e nextObject])
|
|
|
|
{
|
|
|
|
NSString *absoluteSubpath = [NSString pathWithComponents:[NSArray arrayWithObjects:path,subpath,nil]];
|
|
|
|
|
|
|
|
BOOL isDir;
|
|
|
|
if ( [manager fileExistsAtPath:absoluteSubpath isDirectory:&isDir] && isDir == NO)
|
|
|
|
{
|
|
|
|
[urls addObject:[NSURL fileURLWithPath:absoluteSubpath]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return urls;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)insertURLs:(NSArray *)urls atIndex:(int)index sort:(BOOL)sort
|
|
|
|
{
|
|
|
|
NSMutableArray *allURLs = [[NSMutableArray alloc] init];
|
|
|
|
NSMutableArray *validURLs = [[NSMutableArray alloc] init];
|
|
|
|
NSArray *finalURLs;
|
|
|
|
|
|
|
|
if (!urls)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (index < 0)
|
|
|
|
index = 0;
|
|
|
|
|
|
|
|
NSLog(@"URLS: %@", urls);
|
|
|
|
NSEnumerator *urlEnumerator = [urls objectEnumerator];
|
|
|
|
NSURL *url;
|
|
|
|
while (url = [urlEnumerator nextObject])
|
|
|
|
{
|
|
|
|
if ([url isFileURL]) {
|
|
|
|
BOOL isDir;
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:[url path] isDirectory:&isDir])
|
|
|
|
{
|
|
|
|
if (isDir == YES)
|
|
|
|
{
|
|
|
|
//Get subpaths
|
|
|
|
[allURLs addObjectsFromArray:[self fileURLsAtPath:[url path]]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//File url
|
|
|
|
[allURLs addObject:url];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//Non-file URL..
|
|
|
|
[allURLs addObject:url];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
urlEnumerator = [allURLs objectEnumerator];
|
|
|
|
while (url = [urlEnumerator nextObject])
|
|
|
|
{
|
|
|
|
if (![[AudioPlayer schemes] containsObject:[url scheme]])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
//Need a better way to determine acceptable file types than basing it on extensions.
|
|
|
|
if (![[self acceptableFileTypes] containsObject:[[[url path] pathExtension] lowercaseString]])
|
|
|
|
continue;
|
|
|
|
|
|
|
|
[validURLs addObject:url];
|
|
|
|
}
|
|
|
|
|
|
|
|
finalURLs = validURLs;
|
|
|
|
if (sort == YES)
|
|
|
|
{
|
|
|
|
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"absoluteString" ascending:YES];
|
|
|
|
|
|
|
|
finalURLs = [validURLs sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
|
|
|
|
|
|
|
|
[sortDescriptor release];
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create actual entries
|
|
|
|
int i;
|
|
|
|
NSMutableArray *entries = [NSMutableArray array];
|
|
|
|
for (i = 0; i < [finalURLs count]; i++)
|
|
|
|
{
|
|
|
|
PlaylistEntry *pe = [[PlaylistEntry alloc] init];
|
|
|
|
NSURL *url = [finalURLs objectAtIndex:i];
|
|
|
|
|
|
|
|
[pe setURL:url];
|
2007-03-14 02:28:30 +00:00
|
|
|
[pe setIndex:[NSNumber numberWithInt:(index+i)]];
|
2007-03-09 01:16:06 +00:00
|
|
|
[pe setTitle:[[url path] lastPathComponent]];
|
|
|
|
|
|
|
|
[entries addObject:pe];
|
|
|
|
|
|
|
|
[pe release];
|
|
|
|
}
|
|
|
|
|
|
|
|
NSIndexSet *is = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(index, [entries count])];
|
|
|
|
|
|
|
|
[playlistController insertObjects:entries atArrangedObjectIndexes:is];
|
|
|
|
|
|
|
|
//Select the first entry in the group that was just added
|
|
|
|
[playlistController setSelectionIndex:index];
|
|
|
|
|
|
|
|
//Other thread for reading things...
|
|
|
|
[NSThread detachNewThreadSelector:@selector(readEntriesInfoThread:) toTarget:self withObject:entries];
|
|
|
|
|
|
|
|
[allURLs release];
|
|
|
|
[validURLs release];
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)readEntriesInfoThread:(NSArray *)entries
|
|
|
|
{
|
|
|
|
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
|
|
|
|
|
|
|
|
NSEnumerator *e = [entries objectEnumerator];
|
|
|
|
PlaylistEntry *pe;
|
|
|
|
while (pe = [e nextObject])
|
|
|
|
{
|
|
|
|
[pe readPropertiesThread];
|
|
|
|
|
|
|
|
[pe readMetadataThread];
|
|
|
|
|
|
|
|
//Hack so the display gets updated
|
|
|
|
if (pe == [playlistController currentEntry])
|
|
|
|
[playlistController performSelectorOnMainThread:@selector(setCurrentEntry:) withObject:[playlistController currentEntry] waitUntilDone:YES];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[playlistController performSelectorOnMainThread:@selector(updateTotalTime) withObject:nil waitUntilDone:NO];
|
|
|
|
|
|
|
|
[pool release];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)addURLs:(NSArray *)urls sort:(BOOL)sort
|
|
|
|
{
|
|
|
|
[self insertURLs:urls atIndex:[[playlistController content] count] sort:sort];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSArray *)acceptableFileTypes
|
|
|
|
{
|
|
|
|
return [AudioPlayer fileTypes];
|
|
|
|
}
|
|
|
|
|
2007-03-07 01:26:50 +00:00
|
|
|
- (NSArray *)acceptablePlaylistTypes
|
|
|
|
{
|
2007-03-09 01:16:06 +00:00
|
|
|
return [NSArray arrayWithObjects:@"m3u",@"pls",nil];
|
2007-03-07 01:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (PlaylistType)currentType
|
|
|
|
{
|
|
|
|
return currentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCurrentType:(PlaylistType)type
|
|
|
|
{
|
|
|
|
currentType = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)currentFile
|
|
|
|
{
|
|
|
|
return currentFile;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setCurrentFile:(NSString *)file
|
|
|
|
{
|
|
|
|
[file retain];
|
|
|
|
[currentFile release];
|
|
|
|
currentFile = file;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|