VLCGoogleDriveTableViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. * VLCGoogleDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCGoogleDriveTableViewController.h"
  14. #import "VLCAppDelegate.h"
  15. #import "VLCGoogleDriveController.h"
  16. #import "UIDevice+VLC.h"
  17. #import "VLCCloudStorageTableViewCell.h"
  18. #import "GTMOAuth2ViewControllerTouch.h"
  19. #import "GTMOAuth2SignIn.h"
  20. @interface VLCGoogleDriveTableViewController () <VLCCloudStorageTableViewCell>
  21. {
  22. VLCGoogleDriveController *_googleDriveController;
  23. GTLDriveFile *_selectedFile;
  24. GTMOAuth2ViewControllerTouch *_authController;
  25. }
  26. @end
  27. @implementation VLCGoogleDriveTableViewController
  28. - (void)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. _googleDriveController = [VLCGoogleDriveController sharedInstance];
  32. _googleDriveController.delegate = self;
  33. self.controller = _googleDriveController;
  34. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"DriveWhite"]];
  35. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"DriveWhite"]];
  36. [self.cloudStorageLogo sizeToFit];
  37. self.cloudStorageLogo.center = self.view.center;
  38. }
  39. - (void)viewWillAppear:(BOOL)animated
  40. {
  41. [super viewWillAppear:animated];
  42. [self updateViewAfterSessionChange];
  43. }
  44. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  45. {
  46. NSInteger currentOffset = scrollView.contentOffset.y;
  47. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  48. if (maximumOffset - currentOffset <= - self.tableView.rowHeight) {
  49. if (_googleDriveController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  50. [self requestInformationForCurrentPath];
  51. }
  52. }
  53. }
  54. - (GTMOAuth2ViewControllerTouch *)createAuthController
  55. {
  56. _authController = [GTMOAuth2ViewControllerTouch controllerWithScope:kGTLAuthScopeDrive
  57. clientID:kVLCGoogleDriveClientID
  58. clientSecret:kVLCGoogleDriveClientSecret
  59. keychainItemName:kKeychainItemName
  60. delegate:self
  61. finishedSelector:@selector(viewController:finishedWithAuth:error:)];
  62. return _authController;
  63. }
  64. - (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)authResult error:(NSError *)error
  65. {
  66. self.authorizationInProgress = NO;
  67. if (error != nil) {
  68. APLog(@"%s: error: %@", __PRETTY_FUNCTION__, error);
  69. _googleDriveController.driveService.authorizer = nil;
  70. } else {
  71. _googleDriveController.driveService.authorizer = authResult;
  72. }
  73. [self updateViewAfterSessionChange];
  74. [self.activityIndicator startAnimating];
  75. }
  76. - (void)viewWillDisappear:(BOOL)animated
  77. {
  78. [super viewWillDisappear:animated];
  79. if ((VLCAppDelegate *)[UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  80. [_googleDriveController stopSession];
  81. [self.tableView reloadData];
  82. }
  83. }
  84. #pragma mark - Table view data source
  85. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  86. {
  87. static NSString *CellIdentifier = @"GoogleDriveCell";
  88. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  89. if (cell == nil)
  90. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  91. NSArray *listOfFiles = _googleDriveController.currentListFiles;
  92. NSInteger row = indexPath.row;
  93. if (row < listOfFiles.count) {
  94. cell.driveFile = listOfFiles[row];
  95. if ([cell.driveFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"])
  96. [cell setIsDownloadable:NO];
  97. else
  98. [cell setIsDownloadable:YES];
  99. }
  100. cell.delegate = self;
  101. return cell;
  102. }
  103. #pragma mark - Table view delegate
  104. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  107. if (indexPath.row >= _googleDriveController.currentListFiles.count)
  108. return;
  109. _selectedFile = _googleDriveController.currentListFiles[indexPath.row];
  110. if (![_selectedFile.mimeType isEqualToString:@"application/vnd.google-apps.folder"]) {
  111. [_googleDriveController streamFile:_selectedFile];
  112. } else {
  113. /* dive into subdirectory */
  114. if (![self.currentPath isEqualToString:@""])
  115. self.currentPath = [self.currentPath stringByAppendingString:@"/"];
  116. self.currentPath = [self.currentPath stringByAppendingString:_selectedFile.identifier];
  117. [self requestInformationForCurrentPath];
  118. }
  119. }
  120. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  121. {
  122. _selectedFile = _googleDriveController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  123. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  124. /* selected item is a proper file, ask the user if s/he wants to download it */
  125. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  126. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  127. delegate:self
  128. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  129. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  130. [alert show];
  131. } else {
  132. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  133. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  134. delegate:self
  135. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  136. otherButtonTitles:nil];
  137. [alert show];
  138. }
  139. }
  140. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  141. {
  142. if (buttonIndex == 1)
  143. [_googleDriveController downloadFileToDocumentFolder:_selectedFile];
  144. _selectedFile = nil;
  145. }
  146. #pragma mark - login dialog
  147. - (IBAction)loginAction:(id)sender
  148. {
  149. if (![_googleDriveController isAuthorized]) {
  150. self.authorizationInProgress = YES;
  151. [self.navigationController pushViewController:[self createAuthController] animated:YES];
  152. } else {
  153. [_googleDriveController logout];
  154. }
  155. }
  156. @end