VLCGoogleDriveTableViewController.m 7.1 KB

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