VLCDropboxTableViewController.m 6.8 KB

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