VLCDropboxController.m 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // VLCDropboxController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 23.05.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCDropboxController.h"
  11. #import "NSString+SupportedMedia.h"
  12. #import "VLCAppDelegate.h"
  13. @interface VLCDropboxController ()
  14. {
  15. DBRestClient *_restClient;
  16. NSArray *_currentFileList;
  17. NSMutableArray *_listOfDropboxFilesToDownload;
  18. BOOL _downloadInProgress;
  19. NSInteger _outstandingNetworkRequests;
  20. }
  21. @end
  22. @implementation VLCDropboxController
  23. #pragma mark - session handling
  24. - (void)startSession
  25. {
  26. }
  27. - (void)logout
  28. {
  29. [[DBSession sharedSession] unlinkAll];
  30. }
  31. - (BOOL)sessionIsLinked
  32. {
  33. return [[DBSession sharedSession] isLinked];
  34. }
  35. - (DBRestClient *)restClient {
  36. if (!_restClient) {
  37. _restClient = [[DBRestClient alloc] initWithSession:[DBSession sharedSession]];
  38. _restClient.delegate = self;
  39. }
  40. return _restClient;
  41. }
  42. #pragma mark - file management
  43. - (void)requestDirectoryListingAtPath:(NSString *)path
  44. {
  45. if (self.sessionIsLinked)
  46. [[self restClient] loadMetadata:path];
  47. }
  48. - (void)downloadFileToDocumentFolder:(DBMetadata *)file
  49. {
  50. if (!file.isDirectory) {
  51. if (!_listOfDropboxFilesToDownload)
  52. _listOfDropboxFilesToDownload = [[NSMutableArray alloc] init];
  53. [_listOfDropboxFilesToDownload addObject:file];
  54. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  55. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  56. [self _triggerNextDownload];
  57. }
  58. }
  59. - (void)_triggerNextDownload
  60. {
  61. if (_listOfDropboxFilesToDownload.count > 0 && !_downloadInProgress) {
  62. [self _reallyDownloadFileToDocumentFolder:_listOfDropboxFilesToDownload[0]];
  63. [_listOfDropboxFilesToDownload removeObjectAtIndex:0];
  64. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  65. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  66. }
  67. }
  68. - (void)_reallyDownloadFileToDocumentFolder:(DBMetadata *)file
  69. {
  70. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  71. NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
  72. [[self restClient] loadFile:file.path intoPath:filePath];
  73. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  74. [self.delegate operationWithProgressInformationStarted];
  75. _downloadInProgress = YES;
  76. }
  77. #pragma mark - restClient delegate
  78. - (BOOL)_supportedFileExtension:(NSString *)filename
  79. {
  80. if ([filename isSupportedMediaFormat] || [filename isSupportedAudioMediaFormat] || [filename isSupportedSubtitleFormat])
  81. return YES;
  82. return NO;
  83. }
  84. - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
  85. NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
  86. if (metadata.isDirectory) {
  87. NSArray *contents = metadata.contents;
  88. NSUInteger metaDataCount = metadata.contents.count;
  89. for (NSUInteger x = 0; x < metaDataCount; x++) {
  90. DBMetadata *file = contents[x];
  91. if ([file isDirectory] || [self _supportedFileExtension:file.filename])
  92. [listOfGoodFilesAndFolders addObject:file];
  93. }
  94. }
  95. _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
  96. APLog(@"found filtered metadata for %i files", _currentFileList.count);
  97. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  98. [self.delegate mediaListUpdated];
  99. }
  100. - (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error
  101. {
  102. APLog(@"DBMetadata download failed with error %i", error.code);
  103. }
  104. - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  105. {
  106. /* update library now that we got a file */
  107. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  108. [appDelegate updateMediaList];
  109. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  110. [self.delegate operationWithProgressInformationStopped];
  111. _downloadInProgress = NO;
  112. [self _triggerNextDownload];
  113. }
  114. - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  115. {
  116. APLog(@"DBFile download failed with error %i", error.code);
  117. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  118. [self.delegate operationWithProgressInformationStopped];
  119. _downloadInProgress = NO;
  120. [self _triggerNextDownload];
  121. }
  122. - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  123. {
  124. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  125. [self.delegate currentProgressInformation:progress];
  126. }
  127. #pragma mark - DBSession delegate
  128. - (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  129. {
  130. APLog(@"DBSession received authorization failure with user ID %@", userId);
  131. }
  132. #pragma mark - DBNetworkRequest delegate
  133. - (void)networkRequestStarted
  134. {
  135. _outstandingNetworkRequests++;
  136. if (_outstandingNetworkRequests == 1) {
  137. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  138. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  139. }
  140. }
  141. - (void)networkRequestStopped
  142. {
  143. _outstandingNetworkRequests--;
  144. if (_outstandingNetworkRequests == 0) {
  145. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  146. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  147. }
  148. }
  149. #pragma mark - VLC internal communication and delegate
  150. - (NSArray *)currentListFiles
  151. {
  152. return _currentFileList;
  153. }
  154. - (NSInteger)numberOfFilesWaitingToBeDownloaded
  155. {
  156. if (_listOfDropboxFilesToDownload)
  157. return _listOfDropboxFilesToDownload.count;
  158. return 0;
  159. }
  160. @end