VLCDropboxTableViewController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*****************************************************************************
  2. * VLCDropboxTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Carola Nitz <nitz.carola # googlemail.com>
  11. * Fabio Ritrovato <sephiroth87 # videolan.org>
  12. * Tamas Timar <ttimar.vlc # gmail.com>
  13. *
  14. * Refer to the COPYING file of the official project for license.
  15. *****************************************************************************/
  16. #import "VLCDropboxTableViewController.h"
  17. #import "VLCDropboxController.h"
  18. #import "VLCCloudStorageTableViewCell.h"
  19. #import "UIDevice+VLC.h"
  20. #import "VLCAppDelegate.h"
  21. #import "VLC_iOS-Swift.h"
  22. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
  23. {
  24. VLCDropboxController *_dropboxController;
  25. DBFILESMetadata *_selectedFile;
  26. NSArray *_mediaList;
  27. }
  28. @end
  29. @implementation VLCDropboxTableViewController
  30. - (instancetype)initWithPath:(NSString *)path
  31. {
  32. self = [super init];
  33. if (self) {
  34. self.currentPath = path;
  35. }
  36. return self;
  37. }
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. _dropboxController = [VLCDropboxController sharedInstance];
  42. self.controller = _dropboxController;
  43. self.controller.delegate = self;
  44. #if TARGET_OS_IOS
  45. [[NSNotificationCenter defaultCenter] addObserver:self
  46. selector:@selector(sessionWasUpdated:)
  47. name:VLCDropboxSessionWasAuthorized
  48. object:nil];
  49. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  50. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  51. [self.cloudStorageLogo sizeToFit];
  52. self.cloudStorageLogo.center = self.view.center;
  53. #else
  54. self.title = @"Dropbox";
  55. #endif
  56. }
  57. - (void)viewWillAppear:(BOOL)animated
  58. {
  59. [super viewWillAppear:animated];
  60. self.controller = [VLCDropboxController sharedInstance];
  61. self.controller.delegate = self;
  62. if (self.currentPath != nil)
  63. self.title = self.currentPath.lastPathComponent;
  64. [self updateViewAfterSessionChange];
  65. [self.tableView reloadData];
  66. }
  67. #pragma mark - interface interaction
  68. - (BOOL)shouldAutorotate
  69. {
  70. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  71. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  72. return NO;
  73. return YES;
  74. }
  75. #pragma mark - Table view data source
  76. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. static NSString *CellIdentifier = @"DropboxCell";
  79. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  80. if (cell == nil)
  81. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  82. NSUInteger index = indexPath.row;
  83. if (_mediaList) {
  84. if (index < _mediaList.count) {
  85. cell.dropboxFile = _mediaList[index];
  86. cell.delegate = self;
  87. }
  88. }
  89. return cell;
  90. }
  91. - (void)mediaListUpdated
  92. {
  93. _mediaList = [self.controller.currentListFiles copy];
  94. [super mediaListUpdated];
  95. }
  96. #pragma mark - Table view delegate
  97. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  98. {
  99. _selectedFile = _mediaList[indexPath.row];
  100. if (![_selectedFile isKindOfClass:[DBFILESFolderMetadata class]])
  101. [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
  102. else {
  103. /* dive into subdirectory */
  104. NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.name];
  105. self.currentPath = futurePath;
  106. [self requestInformationForCurrentPath];
  107. }
  108. _selectedFile = nil;
  109. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  110. }
  111. #pragma mark - login dialog
  112. - (IBAction)loginAction:(id)sender
  113. {
  114. if (!_dropboxController.isAuthorized) {
  115. self.authorizationInProgress = YES;
  116. [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
  117. controller:self
  118. openURL:^(NSURL *url) {
  119. [[UIApplication sharedApplication] openURL:url];
  120. }];
  121. } else
  122. [_dropboxController logout];
  123. }
  124. - (void)sessionWasUpdated:(NSNotification *)aNotification
  125. {
  126. self.authorizationInProgress = YES;
  127. [self updateViewAfterSessionChange];
  128. [_dropboxController shareCredentials];
  129. }
  130. #pragma mark - VLCCloudStorageTableViewCell delegation
  131. #if TARGET_OS_IOS
  132. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  133. {
  134. _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
  135. if (((DBFILESFileMetadata *)_selectedFile).size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  136. /* selected item is a proper file, ask the user if s/he wants to download it */
  137. NSMutableArray<ButtonAction *> *buttonsAction = [[NSMutableArray alloc] init];
  138. ButtonAction *cancelAction = [[ButtonAction alloc] initWithButtonTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  139. buttonAction: ^(UIAlertAction* action){}];
  140. ButtonAction *downloadAction = [[ButtonAction alloc] initWithButtonTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  141. buttonAction: ^(UIAlertAction* action){
  142. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  143. _selectedFile = nil;
  144. }];
  145. [buttonsAction addObject: cancelAction];
  146. [buttonsAction addObject: downloadAction];
  147. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  148. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  149. viewController:self
  150. buttonsAction:buttonsAction];
  151. } else {
  152. NSMutableArray<ButtonAction *> *buttonsAction = [[NSMutableArray alloc] init];
  153. ButtonAction *cancelAction = [[ButtonAction alloc] initWithButtonTitle: NSLocalizedString(@"BUTTON_OK", nil)
  154. buttonAction: ^(UIAlertAction* action){}];
  155. [buttonsAction addObject: cancelAction];
  156. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  157. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  158. viewController:self
  159. buttonsAction:buttonsAction];
  160. }
  161. }
  162. #endif
  163. @end