VLCDropboxController.m 7.4 KB

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