123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // VLCGoogleDriveController.m
- // VLC for iOS
- //
- // Created by Carola Nitz on 21.09.13.
- // Copyright (c) 2013 VideoLAN. All rights reserved.
- //
- // Refer to the COPYING file of the official project for license.
- //
- #import "VLCGoogleDriveController.h"
- #import "NSString+SupportedMedia.h"
- #import "VLCAppDelegate.h"
- #import "HTTPMessage.h"
- @interface VLCGoogleDriveController ()
- {
- GTLDriveFileList *_fileList;
- GTLServiceTicket *_fileListTicket;
- NSError *_fileListFetchError;
- NSArray *_currentFileList;
- NSMutableArray *_listOfGoogleDriveFilesToDownload;
- BOOL _downloadInProgress;
- NSInteger _outstandingNetworkRequests;
- }
- @end
- @implementation VLCGoogleDriveController
- #pragma mark - session handling
- - (void)startSession
- {
- self.driveService = [[GTLServiceDrive alloc] init];
- self.driveService.authorizer = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:kKeychainItemName clientID:kVLCGoogleDriveClientID clientSecret:kVLCGoogleDriveClientSecret];
- }
- - (void)logout
- {
- [GTMOAuth2ViewControllerTouch removeAuthFromKeychainForName:kKeychainItemName];
- self.driveService.authorizer = nil;
- }
- - (BOOL)isAuthorized
- {
- return [((GTMOAuth2Authentication *)self.driveService.authorizer) canAuthorize];;
- }
- - (void)showAlert:(NSString *)title message:(NSString *)message
- {
- UIAlertView *alert;
- alert = [[UIAlertView alloc] initWithTitle: title
- message: message
- delegate: nil
- cancelButtonTitle: @"OK"
- otherButtonTitles: nil];
- [alert show];
- }
- #pragma mark - file management
- - (void)requestDirectoryListingAtPath:(NSString *)path
- {
- if (self.isAuthorized)
- [self listFiles];
- }
- - (void)downloadFileToDocumentFolder:(GTLDriveFile *)file
- {
- if (![file.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
- if (!_listOfGoogleDriveFilesToDownload)
- _listOfGoogleDriveFilesToDownload = [[NSMutableArray alloc] init];
- [_listOfGoogleDriveFilesToDownload addObject:file];
- if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
- [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
- [self _triggerNextDownload];
- }
- }
- - (void)listFiles
- {
- _fileList = nil;
- _fileListFetchError = nil;
- GTLServiceDrive *service = self.driveService;
- GTLQueryDrive *query = [GTLQueryDrive queryForFilesList];
- query.maxResults = 150;
- query.fields = @"items(originalFilename,title,mimeType,fileExtension,fileSize,iconLink)";
- //+ (id)queryForChildrenListWithFolderId:(NSString *)folderId;
- _fileListTicket = [service executeQuery:query
- completionHandler:^(GTLServiceTicket *ticket,
- GTLDriveFileList *fileList,
- NSError *error) {
- // Callback
- _fileList = fileList;
-
- _fileListFetchError = error;
- _fileListTicket = nil;
- [self listOfGoodFilesAndFolders];
- }];
- }
- - (void)_triggerNextDownload
- {
- if (_listOfGoogleDriveFilesToDownload.count > 0 && !_downloadInProgress) {
- [self _reallyDownloadFileToDocumentFolder:_listOfGoogleDriveFilesToDownload[0]];
- [_listOfGoogleDriveFilesToDownload removeObjectAtIndex:0];
- if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
- [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
- }
- }
- - (void)_reallyDownloadFileToDocumentFolder:(GTLDriveFile *)file
- {
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *filePath = [searchPaths[0] stringByAppendingFormat:@"/%@", file.originalFilename];
- [self loadFile:file intoPath:filePath];
- if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
- [self.delegate operationWithProgressInformationStarted];
- _downloadInProgress = YES;
- }
- - (BOOL)_supportedFileExtension:(NSString *)filename
- {
- if ([filename isSupportedMediaFormat] || [filename isSupportedAudioMediaFormat] || [filename isSupportedSubtitleFormat])
- return YES;
- return NO;
- }
- - (void)listOfGoodFilesAndFolders
- {
- NSMutableArray *listOfGoodFilesAndFolders = [[NSMutableArray alloc] init];
-
- for (GTLDriveFile *driveFile in _fileList.items)
- {
- BOOL isDirectory = [driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"];
- if (isDirectory || [self _supportedFileExtension:[NSString stringWithFormat:@".%@",driveFile.fileExtension ]]) {
- [listOfGoodFilesAndFolders addObject:driveFile];
- }
- }
- _currentFileList = [NSArray arrayWithArray:listOfGoodFilesAndFolders];
- APLog(@"found filtered metadata for %i files", _currentFileList.count);
- if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
- [self.delegate mediaListUpdated];
- }
- - (void)loadFile:(GTLDriveFile*)file intoPath:(NSString*)destinationPath
- {
- NSString *exportURLStr = file.downloadUrl;
- if ([exportURLStr length] > 0) {
- NSString *suggestedName = file.originalFilename;
- if ([suggestedName length] == 0) {
- suggestedName = file.title;
- }
- NSURL *url = [NSURL URLWithString:exportURLStr];
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
- GTMHTTPFetcher *fetcher = [GTMHTTPFetcher fetcherWithRequest:request];
- // Requests of user data from Google services must be authorized.
- fetcher.authorizer = self.driveService.authorizer;
- // The fetcher can save data directly to a file.
- fetcher.downloadPath = destinationPath;
- // Fetcher logging can include comments.
- [fetcher setCommentWithFormat:@"Downloading \"%@\"", file.title];
- __weak GTMHTTPFetcher *weakFetcher = fetcher;
- fetcher.receivedDataBlock = ^(NSData *receivedData) {
- float progress = (float)weakFetcher.downloadedLength / (float)[file.fileSize longLongValue];
- if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
- [self.delegate currentProgressInformation:progress];
- };
- [fetcher beginFetchWithCompletionHandler:^(NSData *data, NSError *error) {
- // Callback
- //TODO:localize Strings
- if (error == nil) {
- UIAlertView *alert;
- alert = [[UIAlertView alloc] initWithTitle: @"Downloaded"
- message: @"your file has been sucessfully downloaded"
- delegate: nil
- cancelButtonTitle: @"OK"
- otherButtonTitles: nil];
- [alert show];
- [self downloadSucessfull];
- } else {
- UIAlertView *alert;
- alert = [[UIAlertView alloc] initWithTitle: @"Error"
- message: @"AN Error occured while downloading"
- delegate: nil
- cancelButtonTitle: @"OK"
- otherButtonTitles: nil];
- [alert show];
- [self downloadFailedWithError:error];
- }
- }];
- }
- }
- - (void)downloadSucessfull
- {
- /* update library now that we got a file */
- APLog(@"DriveFile download was sucessful");
- VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
- [appDelegate updateMediaList];
- if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
- [self.delegate operationWithProgressInformationStopped];
- _downloadInProgress = NO;
- [self _triggerNextDownload];
- }
- - (void)downloadFailedWithError:(NSError*)error
- {
- APLog(@"DriveFile download failed with error %i", error.code);
- if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
- [self.delegate operationWithProgressInformationStopped];
- _downloadInProgress = NO;
- [self _triggerNextDownload];
- }
- #pragma mark - VLC internal communication and delegate
- - (NSArray *)currentListFiles
- {
- return _currentFileList;
- }
- - (NSInteger)numberOfFilesWaitingToBeDownloaded
- {
- if (_listOfGoogleDriveFilesToDownload)
- return _listOfGoogleDriveFilesToDownload.count;
- return 0;
- }
- @end
|