VLCGoogleDriveController.m 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. //
  2. // VLCGoogleDriveController.m
  3. // VLC for iOS
  4. //
  5. // Created by Carola Nitz on 21.09.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 "VLCGoogleDriveController.h"
  11. #import "NSString+SupportedMedia.h"
  12. #import "VLCAppDelegate.h"
  13. #import "HTTPMessage.h"
  14. #import "VLCGoogleDriveConstants.h"
  15. static NSString *const kKeychainItemName = @"Google Drive Quickstart #2";
  16. @interface VLCGoogleDriveController ()
  17. {
  18. GTLDriveFileList *_fileList;
  19. NSError *_fileListFetchError;
  20. GTLServiceTicket *_fileListTicket;
  21. NSArray *_currentFileList;
  22. NSMutableArray *_listOfGoogleDriveFilesToDownload;
  23. BOOL _downloadInProgress;
  24. NSInteger _outstandingNetworkRequests;
  25. }
  26. @end
  27. @implementation VLCGoogleDriveController
  28. #pragma mark - session handling
  29. - (void)startSession
  30. {
  31. self.driveService = [[GTLServiceDrive alloc] init];
  32. self.driveService.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kVLCGoogleDriveClientID clientSecret:kVLCGoogleDriveClientSecret];
  33. }
  34. - (void)logout
  35. {
  36. }
  37. - (BOOL)isAuthorized
  38. {
  39. return [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];;
  40. }
  41. - (void)showAlert:(NSString *)title message:(NSString *)message
  42. {
  43. UIAlertView *alert;
  44. alert = [[UIAlertView alloc] initWithTitle: title
  45. message: message
  46. delegate: nil
  47. cancelButtonTitle: @"OK"
  48. otherButtonTitles: nil];
  49. [alert show];
  50. }
  51. #pragma mark - file management
  52. - (void)requestDirectoryListingAtPath:(NSString *)path
  53. {
  54. if (self.isAuthorized)
  55. [self listFiles];
  56. }
  57. - (void)downloadFileToDocumentFolder:(DBMetadata *)file
  58. {
  59. if (!file.isDirectory) {
  60. if (!_listOfGoogleDriveFilesToDownload)
  61. _listOfGoogleDriveFilesToDownload = [[NSMutableArray alloc] init];
  62. [_listOfGoogleDriveFilesToDownload addObject:file];
  63. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  64. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  65. [self _triggerNextDownload];
  66. }
  67. }
  68. - (void)listFiles
  69. {
  70. _fileList = nil;
  71. _fileListFetchError = nil;
  72. GTLServiceDrive *service = self.driveService;
  73. GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
  74. // maxResults specifies the number of results per page. Since we earlier
  75. // specified shouldFetchNextPages=YES, all results should be fetched,
  76. // though specifying a larger maxResults will reduce the number of fetches
  77. // needed to retrieve all pages.
  78. query.maxResults = 150;
  79. // The Drive API's file entries are chock full of data that the app may not
  80. // care about. Specifying the fields we want here reduces the network
  81. // bandwidth and memory needed for the collection.
  82. //
  83. // For example, leave query.fields as nil during development.
  84. // When ready to test and optimize your app, specify just the fields needed.
  85. // For example, this sample app might use
  86. //
  87. // query.fields = @"kind,etag,items(id,downloadUrl,editable,etag,exportLinks,kind,labels,originalFilename,title)";
  88. //TODO:specify query.fields
  89. _fileListTicket = [service executeQuery:query
  90. completionHandler:^(GTLServiceTicket *ticket,
  91. GTLDriveFileList *fileList,
  92. NSError *error) {
  93. // Callback
  94. _fileList = fileList;
  95. _fileListFetchError = error;
  96. _fileListTicket = nil;
  97. [self listOfGoodFiles];
  98. }];
  99. // [self updateUI];
  100. }
  101. - (void)_triggerNextDownload
  102. {
  103. if (_listOfGoogleDriveFilesToDownload.count > 0 && !_downloadInProgress) {
  104. [self _reallyDownloadFileToDocumentFolder:_listOfGoogleDriveFilesToDownload[0]];
  105. [_listOfGoogleDriveFilesToDownload removeObjectAtIndex:0];
  106. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  107. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  108. }
  109. }
  110. - (void)_reallyDownloadFileToDocumentFolder:(DBMetadata *)file
  111. {
  112. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  113. NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.filename];
  114. //[[self restClient] loadFile:file.path intoPath:filePath];
  115. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  116. [self.delegate operationWithProgressInformationStarted];
  117. _downloadInProgress = YES;
  118. }
  119. #pragma mark - restClient delegate
  120. - (BOOL)_supportedFileExtension:(NSString *)filename
  121. {
  122. if ([filename isSupportedMediaFormat] || [filename isSupportedAudioMediaFormat] || [filename isSupportedSubtitleFormat])
  123. return YES;
  124. return NO;
  125. }
  126. - (void)listOfGoodFiles
  127. {
  128. NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
  129. for (GTLDriveFile *driveFile in _fileList.items)
  130. {
  131. BOOL isDirectory = [driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
  132. if (isDirectory || [self _supportedFileExtension:driveFile.fileExtension]) {
  133. [listOfGoodFilesAndFolders addObject:driveFile];
  134. }
  135. }
  136. _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
  137. APLog(@"found filtered metadata for %i files", _currentFileList.count);
  138. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  139. [self.delegate mediaListUpdated];
  140. }
  141. //- (void)restClient:(DBRestClient *)client loadMetadataFailedWithError:(NSError *)error
  142. //{
  143. // APLog(@"DBMetadata download failed with error %i", error.code);
  144. //}
  145. //
  146. //- (void)restClient:(DBRestClient*)client loadedFile:(NSString*)localPath
  147. //{
  148. // /* update library now that we got a file */
  149. // VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  150. // [appDelegate updateMediaList];
  151. //
  152. // if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  153. // [self.delegate operationWithProgressInformationStopped];
  154. // _downloadInProgress = NO;
  155. //
  156. // [self _triggerNextDownload];
  157. //}
  158. //
  159. //- (void)restClient:(DBRestClient*)client loadFileFailedWithError:(NSError*)error
  160. //{
  161. // APLog(@"DBFile download failed with error %i", error.code);
  162. // if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  163. // [self.delegate operationWithProgressInformationStopped];
  164. // _downloadInProgress = NO;
  165. //
  166. // [self _triggerNextDownload];
  167. //}
  168. //
  169. //- (void)restClient:(DBRestClient*)client loadProgress:(CGFloat)progress forFile:(NSString*)destPath
  170. //{
  171. // if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  172. // [self.delegate currentProgressInformation:progress];
  173. //}
  174. //
  175. //#pragma mark - DBSession delegate
  176. //
  177. //- (void)sessionDidReceiveAuthorizationFailure:(DBSession *)session userId:(NSString *)userId
  178. //{
  179. // APLog(@"DriveSession received authorization failure with user ID %@", userId);
  180. //}
  181. //
  182. //#pragma mark - DBNetworkRequest delegate
  183. //- (void)networkRequestStarted
  184. //{
  185. // _outstandingNetworkRequests++;
  186. // if (_outstandingNetworkRequests == 1) {
  187. // [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  188. // [(VLCAppDelegate*)[UIApplication sharedApplication].delegate disableIdleTimer];
  189. // }
  190. //}
  191. - (void)networkRequestStopped
  192. {
  193. _outstandingNetworkRequests--;
  194. if (_outstandingNetworkRequests == 0) {
  195. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  196. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate activateIdleTimer];
  197. }
  198. }
  199. #pragma mark - VLC internal communication and delegate
  200. - (NSArray *)currentListFiles
  201. {
  202. return _currentFileList;
  203. }
  204. - (NSInteger)numberOfFilesWaitingToBeDownloaded
  205. {
  206. if (_listOfGoogleDriveFilesToDownload)
  207. return _listOfGoogleDriveFilesToDownload.count;
  208. return 0;
  209. }
  210. @end