VLCDropboxController.m 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. }
  109. - (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  110. {
  111. /* update library now that we got a file */
  112. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  113. [appDelegate updateMediaList];
  114. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  115. [self.delegate operationWithProgressInformationStopped];
  116. _downloadInProgress = NO;
  117. [self _triggerNextDownload];
  118. }
  119. - (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  120. {
  121. APLog(@"DBFile download failed with error %i", error.code);
  122. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  123. [self.delegate operationWithProgressInformationStopped];
  124. _downloadInProgress = NO;
  125. [self _triggerNextDownload];
  126. }
  127. - (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  128. {
  129. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  130. [self.delegate currentProgressInformation:progress];
  131. }
  132. - (void)restClient:(DBRestClient*)restClient loadedStreamableURL:(NSURL*)url forFile:(NSString*)path
  133. {
  134. VLCAppDelegate *appDelegate = (VLCAppDelegate *)[UIApplication sharedApplication].delegate;
  135. /* DBX returns a https URL which we don't support yet. in turn, we fall back on http */
  136. [appDelegate openMovieFromURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@%@", url.host, url.path]]];
  137. }
  138. - (void)restClient:(DBRestClient*)restClient loadStreamableURLFailedWithError:(NSError*)error
  139. {
  140. APLog(@"loadStreamableURL failed with error %i", error.code);
  141. }
  142. #pragma mark - DBSession delegate
  143. - (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  144. {
  145. APLog(@"DBSession received authorization failure with user ID %@", userId);
  146. }
  147. #pragma mark - DBNetworkRequest delegate
  148. - (void)networkRequestStarted
  149. {
  150. _outstandingNetworkRequests++;
  151. if (_outstandingNetworkRequests == 1) {
  152. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  153. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  154. }
  155. }
  156. - (void)networkRequestStopped
  157. {
  158. _outstandingNetworkRequests--;
  159. if (_outstandingNetworkRequests == 0) {
  160. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  161. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  162. }
  163. }
  164. #pragma mark - VLC internal communication and delegate
  165. - (NSArray *)currentListFiles
  166. {
  167. return _currentFileList;
  168. }
  169. - (NSInteger)numberOfFilesWaitingToBeDownloaded
  170. {
  171. if (_listOfDropboxFilesToDownload)
  172. return _listOfDropboxFilesToDownload.count;
  173. return 0;
  174. }
  175. @end