VLCDropboxTableViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 "DBKeychain.h"
  21. #if TARGET_OS_IOS
  22. #import "VLCAppDelegate.h"
  23. #endif
  24. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
  25. {
  26. VLCDropboxController *_dropboxController;
  27. DBMetadata *_selectedFile;
  28. NSArray *_mediaList;
  29. }
  30. @end
  31. @implementation VLCDropboxTableViewController
  32. - (instancetype)initWithPath:(NSString *)path
  33. {
  34. NSLog(@"%s", __PRETTY_FUNCTION__);
  35. self = [super init];
  36. if (self) {
  37. self.currentPath = path;
  38. }
  39. return self;
  40. }
  41. - (void)dealloc
  42. {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. _dropboxController = [VLCDropboxController sharedInstance];
  49. self.controller = _dropboxController;
  50. self.controller.delegate = self;
  51. #if TARGET_OS_IOS
  52. [[NSNotificationCenter defaultCenter] addObserver:self
  53. selector:@selector(sessionWasUpdated:)
  54. name:VLCDropboxSessionWasAuthorized
  55. object:nil];
  56. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  57. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  58. [self.cloudStorageLogo sizeToFit];
  59. self.cloudStorageLogo.center = self.view.center;
  60. #else
  61. self.title = @"Dropbox";
  62. #endif
  63. }
  64. - (void)viewWillAppear:(BOOL)animated
  65. {
  66. [super viewWillAppear:animated];
  67. self.controller = [VLCDropboxController sharedInstance];
  68. self.controller.delegate = self;
  69. if (self.currentPath != nil)
  70. self.title = self.currentPath.lastPathComponent;
  71. [self updateViewAfterSessionChange];
  72. }
  73. #pragma mark - interface interaction
  74. - (BOOL)shouldAutorotate
  75. {
  76. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  77. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  78. return NO;
  79. return YES;
  80. }
  81. #pragma mark - Table view data source
  82. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  83. {
  84. static NSString *CellIdentifier = @"DropboxCell";
  85. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  86. if (cell == nil)
  87. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  88. NSUInteger index = indexPath.row;
  89. if (_mediaList) {
  90. if (index < _mediaList.count) {
  91. cell.dropboxFile = _mediaList[index];
  92. cell.delegate = self;
  93. }
  94. }
  95. return cell;
  96. }
  97. - (void)mediaListUpdated
  98. {
  99. _mediaList = [self.controller.currentListFiles copy];
  100. [self.tableView reloadData];
  101. }
  102. #pragma mark - Table view delegate
  103. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  104. {
  105. _selectedFile = _mediaList[indexPath.row];
  106. if (!_selectedFile.isDirectory)
  107. [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
  108. else {
  109. /* dive into subdirectory */
  110. NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  111. #if TARGET_OS_TV
  112. [_dropboxController reset];
  113. VLCDropboxTableViewController *targetViewController = [[VLCDropboxTableViewController alloc] initWithPath:futurePath];
  114. [self.navigationController pushViewController:targetViewController animated:YES];
  115. #else
  116. self.currentPath = futurePath;
  117. [self requestInformationForCurrentPath];
  118. #endif
  119. }
  120. _selectedFile = nil;
  121. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  122. }
  123. #pragma mark - login dialog
  124. - (IBAction)loginAction:(id)sender
  125. {
  126. if (!_dropboxController.isAuthorized) {
  127. self.authorizationInProgress = YES;
  128. [[DBSession sharedSession] linkFromController:self];
  129. } else
  130. [_dropboxController logout];
  131. }
  132. - (void)sessionWasUpdated:(NSNotification *)aNotification
  133. {
  134. self.authorizationInProgress = YES;
  135. [self updateViewAfterSessionChange];
  136. [_dropboxController shareCredentials];
  137. }
  138. #pragma mark - VLCCloudStorageTableViewCell delegation
  139. #if TARGET_OS_IOS
  140. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  141. {
  142. _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
  143. if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  144. /* selected item is a proper file, ask the user if s/he wants to download it */
  145. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  146. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.filename, [[UIDevice currentDevice] model]]
  147. delegate:self
  148. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  149. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  150. [alert show];
  151. } else {
  152. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  153. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.filename, [[UIDevice currentDevice] model]]
  154. delegate:self
  155. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  156. otherButtonTitles:nil];
  157. [alert show];
  158. }
  159. }
  160. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  161. {
  162. if (buttonIndex == 1)
  163. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  164. _selectedFile = nil;
  165. }
  166. #endif
  167. @end