Clean up file downloading.

This commit is contained in:
Paul Chote
2010-11-25 11:24:10 +13:00
parent 2fc88e439d
commit 6f66a19b18
6 changed files with 109 additions and 82 deletions

View File

@@ -30,8 +30,7 @@
- (SidebarEntry *)sidebarModsTree;
- (SidebarEntry *)sidebarOtherTree;
- (BOOL)downloadUrl:(NSString *)url toFile:(NSString *)filename withId:(NSString *)key;
- (void)cancelDownload:(NSString *)key;
- (BOOL)registerDownload:(NSString *)key withURL:(NSString *)url filePath:(NSString *)path;
- (Download *)downloadWithKey:(NSString *)key;
@end

View File

@@ -94,25 +94,16 @@
[game launchMod:mod];
}
- (BOOL)downloadUrl:(NSString *)url toFile:(NSString *)path withId:(NSString *)key
- (BOOL)registerDownload:(NSString *)key withURL:(NSString *)url filePath:(NSString *)path;
{
if ([downloads objectForKey:key] != nil)
{
NSLog(@"Download already in progress for %@",key);
return NO;
}
Download *download = [Download downloadWithURL:url filename:path key:key game:game];
[downloads setObject:download forKey:key];
[downloads setObject:[Download downloadWithURL:url filename:path key:key game:game]
forKey:key];
return YES;
}
- (void)cancelDownload:(NSString *)key
{
[[downloads objectForKey:key] cancel];
[downloads removeObjectForKey:key];
}
- (Download *)downloadWithKey:(NSString *)key
{
return [downloads objectForKey:key];

View File

@@ -20,6 +20,7 @@
int bytesCompleted;
int bytesTotal;
}
@property(readonly) NSString *key;
@property(readonly) NSString *status;
@property(readonly) int bytesCompleted;
@@ -27,6 +28,7 @@
+ (id)downloadWithURL:(NSString *)aURL filename:(NSString *)aFilename key:(NSString *)aKey game:(GameInstall *)aGame;
- (id)initWithURL:(NSString *)aURL filename:(NSString *)aFilename key:(NSString *)aKey game:(GameInstall *)game;
- (void)cancel;
- (BOOL)start;
- (BOOL)cancel;
@end

View File

@@ -32,16 +32,18 @@
filename = [aFilename retain];
key = [aKey retain];
game = [aGame retain];
status = @"Initializing";
bytesCompleted = -1;
bytesTotal = -1;
NSLog(@"Starting download...");
task = [game runAsyncUtilityWithArg:[NSString stringWithFormat:@"--download-url=%@,%@",url,filename]
delegate:self
responseSelector:@selector(utilityResponded:)
terminatedSelector:@selector(utilityTerminated:)];
[task retain];
if ([[NSFileManager defaultManager] fileExistsAtPath:filename])
{
status = @"COMPLETE";
bytesCompleted = bytesTotal = [[[NSFileManager defaultManager] attributesOfItemAtPath:filename error:nil] fileSize];
}
else
{
status = @"AVAILABLE";
bytesCompleted = -1;
bytesTotal = -1;
}
}
return self;
}
@@ -65,17 +67,13 @@
if ([type isEqualToString:@"Error"])
{
status = @"Error";
status = @"ERROR";
}
else if ([type isEqualToString:@"Status"])
{
if ([message isEqualToString:@"Initializing"])
if ([message isEqualToString:@"Completed"])
{
status = @"Initializing";
}
else if ([message isEqualToString:@"Completed"])
{
status = @"Complete";
status = @"COMPLETE";
}
// Parse download status info
@@ -94,15 +92,29 @@
[[n object] readInBackgroundAndNotify];
}
- (void)cancel
- (BOOL)start
{
status = @"DOWNLOADING";
NSLog(@"Starting download...");
task = [game runAsyncUtilityWithArg:[NSString stringWithFormat:@"--download-url=%@,%@",url,filename]
delegate:self
responseSelector:@selector(utilityResponded:)
terminatedSelector:@selector(utilityTerminated:)];
[task retain];
return YES;
}
- (BOOL)cancel
{
NSLog(@"Cancelling");
status = @"Cancelled";
status = @"ERROR";
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:NSFileHandleReadCompletionNotification object:[[task standardOutput] fileHandleForReading]];
[nc removeObserver:self name:NSTaskDidTerminateNotification object:task];
[task terminate];
[task release]; task = nil;
return YES;
}
- (void)utilityTerminated:(NSNotification *)n

View File

@@ -45,8 +45,8 @@ static JSBridge *SharedInstance;
@"existsInMod", NSStringFromSelector(@selector(fileExists:inMod:)),
// File downloading
@"existsInCache", NSStringFromSelector(@selector(existsInCache:)),
@"downloadToCache", NSStringFromSelector(@selector(downloadUrl:withName:key:)),
@"registerDownload", NSStringFromSelector(@selector(registerDownload:withURL:filename:)),
@"startDownload", NSStringFromSelector(@selector(startDownload:)),
@"cancelDownload", NSStringFromSelector(@selector(cancelDownload:)),
@"downloadStatus", NSStringFromSelector(@selector(downloadStatus:)),
@"bytesCompleted", NSStringFromSelector(@selector(bytesCompleted:)),
@@ -103,36 +103,34 @@ static JSBridge *SharedInstance;
return YES;
}
- (BOOL)existsInCache:(NSString *)name
- (BOOL)registerDownload:(NSString *)key withURL:(NSString *)url filename:(NSString *)filename
{
// Disallow traversing directories; take only the last component
id path = [[@"~/Library/Application Support/OpenRA/Downloads/" stringByAppendingPathComponent:[name lastPathComponent]] stringByExpandingTildeInPath];
return [[NSFileManager defaultManager] fileExistsAtPath:path];
}
- (void)downloadUrl:(NSString *)url withName:(NSString *)name key:(NSString *)key
{
NSLog(@"downloadFile:%@ intoCacheWithName:%@ key:%@",url,name,key);
// Disallow traversing directories; take only the last component
id path = [[@"~/Library/Application Support/OpenRA/Downloads/" stringByAppendingPathComponent:[name lastPathComponent]] stringByExpandingTildeInPath];
[controller downloadUrl:url toFile:path withId:key];
}
- (void)cancelDownload:(NSString *)key
{
[controller cancelDownload:key];
id path = [[@"~/Library/Application Support/OpenRA/Downloads/" stringByAppendingPathComponent:[filename lastPathComponent]] stringByExpandingTildeInPath];
return [controller registerDownload:key withURL:url filePath:path];
}
- (NSString *)downloadStatus:(NSString *)key
{
Download *d = [controller downloadWithKey:key];
if (d == nil)
return @"Invalid";
return @"NOT_REGISTERED";
return [d status];
}
- (BOOL)startDownload:(NSString *)key
{
Download *d = [controller downloadWithKey:key];
return (d == nil) ? NO : [d start];
}
- (BOOL)cancelDownload:(NSString *)key
{
Download *d = [controller downloadWithKey:key];
return (d == nil) ? NO : [d cancel];
}
- (int)bytesCompleted:(NSString *)key
{
Download *d = [controller downloadWithKey:key];