Commit 862b0df7 authored by Felix Paul Kühne's avatar Felix Paul Kühne

Dropbox: prevent concurrent downloads

parent 79397cbb
......@@ -18,6 +18,8 @@
- (void)operationWithProgressInformationStarted;
- (void)currentProgressInformation:(float)progress;
- (void)operationWithProgressInformationStopped;
- (void)numberOfFilesWaitingToBeDownloadedChanged;
@end
@interface VLCDropboxController : NSObject <DBRestClientDelegate, DBSessionDelegate, DBNetworkRequestDelegate>
......@@ -25,6 +27,7 @@
@property (nonatomic, retain) id delegate;
@property (nonatomic, readonly) NSArray *currentListFiles;
@property (nonatomic, readonly) BOOL sessionIsLinked;
@property (nonatomic, readonly) NSInteger numberOfFilesWaitingToBeDownloaded;
- (void)startSession;
- (void)logout;
......
......@@ -17,6 +17,9 @@
DBRestClient *_restClient;
NSArray *_currentFileList;
NSMutableArray *_listOfDropboxFilesToDownload;
BOOL _downloadInProgress;
NSInteger _outstandingNetworkRequests;
}
......@@ -58,16 +61,41 @@
- (void)downloadFileToDocumentFolder:(DBMetadata *)file
{
if (!file.isDirectory) {
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
if (!_listOfDropboxFilesToDownload)
_listOfDropboxFilesToDownload = [[NSMutableArray alloc] init];
[_listOfDropboxFilesToDownload addObject:file];
[[self restClient] loadFile:file.path intoPath:filePath];
if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
[self.delegate numberOfFilesWaitingToBeDownloadedChanged];
if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
[self.delegate operationWithProgressInformationStarted];
[self _triggerNextDownload];
}
}
- (void)_triggerNextDownload
{
if (_listOfDropboxFilesToDownload.count > 0 && !_downloadInProgress) {
[self _reallyDownloadFileToDocumentFolder:_listOfDropboxFilesToDownload[0]];
[_listOfDropboxFilesToDownload removeObjectAtIndex:0];
if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
[self.delegate numberOfFilesWaitingToBeDownloadedChanged];
}
}
- (void)_reallyDownloadFileToDocumentFolder:(DBMetadata *)file
{
NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
[[self restClient] loadFile:file.path intoPath:filePath];
if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
[self.delegate operationWithProgressInformationStarted];
_downloadInProgress = YES;
}
#pragma mark - restClient delegate
- (BOOL)_supportedFileExtension:(NSString *)filename
{
......@@ -110,6 +138,9 @@
if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
[self.delegate operationWithProgressInformationStopped];
_downloadInProgress = NO;
[self _triggerNextDownload];
}
- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
......@@ -117,6 +148,9 @@
APLog(@"DBFile download failed with error %i", error.code);
if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
[self.delegate operationWithProgressInformationStopped];
_downloadInProgress = NO;
[self _triggerNextDownload];
}
- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
......@@ -154,4 +188,12 @@
return _currentFileList;
}
- (NSInteger)numberOfFilesWaitingToBeDownloaded
{
if (_listOfDropboxFilesToDownload)
return _listOfDropboxFilesToDownload.count;
return 0;
}
@end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment