VLCDropboxController.m 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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)streamFile:(DBMetadata *)file
  60. {
  61. if (!file.isDirectory)
  62. [[self restClient] loadStreamableURLForFile:file.path];
  63. }
  64. - (void)_triggerNextDownload
  65. {
  66. if (_listOfDropboxFilesToDownload.count > 0 && !_downloadInProgress) {
  67. [self _reallyDownloadFileToDocumentFolder:_listOfDropboxFilesToDownload[0]];
  68. [_listOfDropboxFilesToDownload removeObjectAtIndex:0];
  69. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  70. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  71. }
  72. }
  73. - (void)_reallyDownloadFileToDocumentFolder:(DBMetadata *)file
  74. {
  75. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  76. NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
  77. [[self restClient] loadFile:file.path intoPath:filePath];
  78. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  79. [self.delegate operationWithProgressInformationStarted];
  80. _downloadInProgress = YES;
  81. }
  82. #pragma mark - restClient delegate
  83. - (BOOL)_supportedFileExtension:(NSString *)filename
  84. {
  85. if ([filename isSupportedMediaFormat] || [filename isSupportedAudioMediaFormat] || [filename isSupportedSubtitleFormat])
  86. return YES;
  87. return NO;
  88. }
  89. - (void)restClient:(DBRestClient *)client loadedMetadata:(DBMetadata *)metadata {
  90. NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
  91. if (metadata.isDirectory) {
  92. NSArray *contents = metadata.contents;
  93. NSUInteger metaDataCount = metadata.contents.count;
  94. for (NSUInteger x = 0; x < metaDataCount; x++) {
  95. DBMetadata *file = contents[x];
  96. if ([file isDirectory] || [self _supportedFileExtension:file.filename])
  97. [listOfGoodFilesAndFolders addObject:file];
  98. }
  99. }
  100. _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
  101. APLog(@"found filtered metadata for %i files", _currentFileList.count);
  102. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  103. [self.delegate mediaListUpdated];
  104. }
  105. - (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error
  106. {
  107. APLog(@"DBMetadata download failed with error %i", error.code);
  108. [self _handleError:error];
  109. }
  110. - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  111. {
  112. /* update library now that we got a file */
  113. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  114. [appDelegate updateMediaList];
  115. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  116. [self.delegate operationWithProgressInformationStopped];
  117. _downloadInProgress = NO;
  118. [self _triggerNextDownload];
  119. }
  120. - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  121. {
  122. APLog(@"DBFile download failed with error %i", error.code);
  123. [self _handleError:error];
  124. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  125. [self.delegate operationWithProgressInformationStopped];
  126. _downloadInProgress = NO;
  127. [self _triggerNextDownload];
  128. }
  129. - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  130. {
  131. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  132. [self.delegate currentProgressInformation:progress];
  133. }
  134. - (void)restClient:(DBRestClient*)restClient loadedStreamableURL:(NSURL*)url forFile:(NSString*)path
  135. {
  136. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  137. /* DBX returns a https URL which we don't support yet. in turn, we fall back on http */
  138. [appDelegate openMovieFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@%@", url.host, url.path]]];
  139. }
  140. - (void)restClient:(DBRestClient*)restClient loadStreamableURLFailedWithError:(NSError*)error
  141. {
  142. APLog(@"loadStreamableURL failed with error %i", error.code);
  143. [self _handleError:error];
  144. }
  145. #pragma mark - DBSession delegate
  146. - (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  147. {
  148. APLog(@"DBSession received authorization failure with user ID %@", userId);
  149. }
  150. #pragma mark - DBNetworkRequest delegate
  151. - (void)networkRequestStarted
  152. {
  153. _outstandingNetworkRequests++;
  154. if (_outstandingNetworkRequests == 1) {
  155. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  156. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  157. }
  158. }
  159. - (void)networkRequestStopped
  160. {
  161. _outstandingNetworkRequests--;
  162. if (_outstandingNetworkRequests == 0) {
  163. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  164. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  165. }
  166. }
  167. #pragma mark - VLC internal communication and delegate
  168. - (NSArray *)currentListFiles
  169. {
  170. return _currentFileList;
  171. }
  172. - (NSInteger)numberOfFilesWaitingToBeDownloaded
  173. {
  174. if (_listOfDropboxFilesToDownload)
  175. return _listOfDropboxFilesToDownload.count;
  176. return 0;
  177. }
  178. #pragma mark - user feedback
  179. - (void)_handleError:(NSError *)error
  180. {
  181. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"ERROR_NUMBER", @""), error.code] message:error.localizedDescription delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  182. [alert show];
  183. }
  184. @end