VLCDropboxTableViewController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  46. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  47. [self.cloudStorageLogo sizeToFit];
  48. self.cloudStorageLogo.center = self.view.center;
  49. #else
  50. self.title = @"Dropbox";
  51. #endif
  52. }
  53. - (void)viewWillAppear:(BOOL)animated
  54. {
  55. [super viewWillAppear:animated];
  56. self.controller = [VLCDropboxController sharedInstance];
  57. self.controller.delegate = self;
  58. if (self.currentPath != nil)
  59. self.title = self.currentPath.lastPathComponent;
  60. [self updateViewAfterSessionChange];
  61. [self.tableView reloadData];
  62. }
  63. #pragma mark - interface interaction
  64. - (BOOL)shouldAutorotate
  65. {
  66. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  67. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  68. return NO;
  69. return YES;
  70. }
  71. #pragma mark - Table view data source
  72. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  73. {
  74. static NSString *CellIdentifier = @"DropboxCell";
  75. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  76. if (cell == nil)
  77. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  78. NSUInteger index = indexPath.row;
  79. if (_mediaList) {
  80. if (index < _mediaList.count) {
  81. cell.dropboxFile = _mediaList[index];
  82. cell.delegate = self;
  83. }
  84. }
  85. return cell;
  86. }
  87. - (void)mediaListUpdated
  88. {
  89. _mediaList = [self.controller.currentListFiles copy];
  90. [super mediaListUpdated];
  91. }
  92. #pragma mark - Table view delegate
  93. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  94. {
  95. _selectedFile = _mediaList[indexPath.row];
  96. if (![_selectedFile isKindOfClass:[DBFILESFolderMetadata class]])
  97. [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
  98. else {
  99. /* dive into subdirectory */
  100. NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.name];
  101. self.currentPath = futurePath;
  102. [self requestInformationForCurrentPath];
  103. }
  104. _selectedFile = nil;
  105. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  106. }
  107. #pragma mark - login dialog
  108. - (IBAction)loginAction:(id)sender
  109. {
  110. if (!_dropboxController.isAuthorized) {
  111. self.authorizationInProgress = YES;
  112. [DBClientsManager authorizeFromController:[UIApplication sharedApplication]
  113. controller:self
  114. openURL:^(NSURL *url) {
  115. [[UIApplication sharedApplication] openURL:url];
  116. }];
  117. } else
  118. [_dropboxController logout];
  119. }
  120. - (void)sessionWasUpdated:(NSNotification *)aNotification
  121. {
  122. self.authorizationInProgress = YES;
  123. [self updateViewAfterSessionChange];
  124. [_dropboxController shareCredentials];
  125. }
  126. #pragma mark - VLCCloudStorageTableViewCell delegation
  127. #if TARGET_OS_IOS
  128. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  129. {
  130. _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
  131. if (((DBFILESFileMetadata *)_selectedFile).size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  132. /* selected item is a proper file, ask the user if s/he wants to download it */
  133. NSArray<VLCAlertButton *> *buttonsAction = @[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_CANCEL", nil)
  134. action: ^(UIAlertAction* action){
  135. self->_selectedFile = nil;
  136. }],
  137. [[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  138. action: ^(UIAlertAction* action){
  139. [self->_dropboxController downloadFileToDocumentFolder:self->_selectedFile];
  140. self->_selectedFile = nil;
  141. }]
  142. ];
  143. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  144. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  145. viewController:self
  146. buttonsAction:buttonsAction];
  147. } else {
  148. [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  149. errorMessage:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  150. viewController:self];
  151. }
  152. }
  153. #endif
  154. @end