Skip to content
This repository has been archived by the owner on Jun 6, 2018. It is now read-only.

Commit

Permalink
Additional interface implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
exalted committed Apr 18, 2012
1 parent 95fdd33 commit 3800b39
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
4 changes: 4 additions & 0 deletions PTDownloadManager/PTDownloadManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@
- (void)changeDefaultsWithDiskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path;

- (PTFile *)addFileWithName:(NSString *)name date:(NSDate *)date request:(NSURLRequest *)request;
- (void)removeFile:(PTFile *)file;
- (PTFile *)fileWithName:(NSString *)name;

@property(nonatomic, readonly) NSArray *files;

@end
41 changes: 41 additions & 0 deletions PTDownloadManager/PTDownloadManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#import "PTDownloadManager.h"

#import "ASINetworkQueue.h"
#import "ASIHTTPRequest.h"

#define kPTLibraryInfoFileName @"libraryInfo.plist"
#define kPTLibraryInfoFilesKey @"files"
Expand Down Expand Up @@ -91,6 +92,18 @@ - (id)init
return self;
}

- (NSArray *)files
{
NSMutableArray *result = [NSMutableArray array];

NSArray *allKeys = [[self.libraryInfo objectForKey:kPTLibraryInfoFilesKey] allKeys];
for (int i = 0; i < allKeys.count; i++) {
[result addObject:[self fileWithName:[allKeys objectAtIndex:i]]];
}

return result;
}

- (void)changeDefaultsWithDiskCapacity:(NSUInteger)diskCapacity diskPath:(NSString *)path
{
// TODO missing implementation
Expand Down Expand Up @@ -154,4 +167,32 @@ - (PTFile *)addFileWithName:(NSString *)name date:(NSDate *)date request:(NSURLR
return [[PTFile alloc] initWithName:name date:date];
}

- (void)removeFile:(PTFile *)file
{
NSMutableDictionary *files = [self.libraryInfo objectForKey:kPTLibraryInfoFilesKey];
NSMutableDictionary *urls = [self.libraryInfo objectForKey:kPTLibraryInfoRequestURLStringsKey];

for (int i = 0; i < self.downloadQueue.requestsCount; i++) {
ASIHTTPRequest *request = [self.downloadQueue.operations objectAtIndex:i];
if ([request.originalURL.absoluteString isEqualToString:[urls objectForKey:file.name]]) {
[request cancel];
[request removeTemporaryDownloadFile];
}
}

[files removeObjectForKey:file.name];
[urls removeObjectForKey:file.name];

[self saveLibraryInfo];

NSFileManager *fileManager = [[NSFileManager alloc] init];
[fileManager removeItemAtURL:file.contentURL error:NULL];
}

- (PTFile *)fileWithName:(NSString *)name
{
NSDictionary *files = [self.libraryInfo objectForKey:kPTLibraryInfoFilesKey];
return [files objectForKey:name] ? [[PTFile alloc] initWithName:name date:[files objectForKey:name]] : nil;
}

@end
3 changes: 3 additions & 0 deletions PTDownloadManager/PTFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

@interface PTFile : NSObject

// the location where file content should be stored
@property(nonatomic, readonly) NSURL *contentURL;

@property (nonatomic, readonly) NSString *name;
@property (nonatomic, readonly) NSDate *date;

Expand Down
5 changes: 5 additions & 0 deletions PTDownloadManager/PTFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ - (id)initWithName:(NSString *)name date:(NSDate *)date
return self;
}

- (NSURL *)contentURL
{
return [NSURL fileURLWithPath:[[[PTDownloadManager sharedManager] diskPath] stringByAppendingPathComponent:self.name]];
}

- (NSOperation *)download
{
return [self downloadWithProgressOnView:nil];
Expand Down

0 comments on commit 3800b39

Please sign in to comment.