VLCDropboxTableViewController.m 6.8 KB

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