VLCDropboxTableViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "VLCAppDelegate.h"
  19. #import "VLCDropboxConstants.h"
  20. #import "UIDevice+VLC.h"
  21. @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell>
  22. {
  23. VLCDropboxController *_dropboxController;
  24. DBMetadata *_selectedFile;
  25. }
  26. @end
  27. @implementation VLCDropboxTableViewController
  28. - (void)dealloc
  29. {
  30. [[NSNotificationCenter defaultCenter] removeObserver:self];
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. _dropboxController = [VLCDropboxController sharedInstance];
  36. self.controller = _dropboxController;
  37. self.controller.delegate = self;
  38. [[NSNotificationCenter defaultCenter] addObserver:self
  39. selector:@selector(sessionWasUpdated:)
  40. name:VLCDropboxSessionWasAuthorized
  41. object:nil];
  42. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  43. [DBSession setSharedSession:dbSession];
  44. [DBRequest setNetworkRequestDelegate:_dropboxController];
  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. }
  50. - (void)viewWillAppear:(BOOL)animated
  51. {
  52. [super viewWillAppear:animated];
  53. self.controller = _dropboxController;
  54. self.controller.delegate = self;
  55. [self updateViewAfterSessionChange];
  56. }
  57. #pragma mark - interface interaction
  58. - (BOOL)shouldAutorotate
  59. {
  60. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  61. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  62. return NO;
  63. return YES;
  64. }
  65. #pragma mark - Table view data source
  66. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  67. {
  68. static NSString *CellIdentifier = @"DropboxCell";
  69. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  70. if (cell == nil)
  71. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  72. cell.dropboxFile = _dropboxController.currentListFiles[indexPath.row];
  73. cell.delegate = self;
  74. return cell;
  75. }
  76. #pragma mark - Table view delegate
  77. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  78. {
  79. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  80. if (!_selectedFile.isDirectory)
  81. [_dropboxController streamFile:_selectedFile];
  82. else {
  83. /* dive into subdirectory */
  84. self.currentPath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  85. [self _requestInformationForCurrentPath];
  86. }
  87. _selectedFile = nil;
  88. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  89. }
  90. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  91. {
  92. if (buttonIndex == 1)
  93. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  94. _selectedFile = nil;
  95. }
  96. #pragma mark - login dialog
  97. - (IBAction)loginAction:(id)sender
  98. {
  99. if (!_dropboxController.isAuthorized) {
  100. self.authorizationInProgress = YES;
  101. [[DBSession sharedSession] linkFromController:self];
  102. } else
  103. [_dropboxController logout];
  104. }
  105. - (void)sessionWasUpdated:(NSNotification *)aNotification
  106. {
  107. self.authorizationInProgress = YES;
  108. [self updateViewAfterSessionChange];
  109. }
  110. #pragma mark - VLCCloudStorageTableViewCell delegation
  111. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  112. {
  113. _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  114. if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  115. /* selected item is a proper file, ask the user if s/he wants to download it */
  116. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  117. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.filename, [[UIDevice currentDevice] model]]
  118. delegate:self
  119. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  120. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  121. [alert show];
  122. } else {
  123. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  124. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.filename, [[UIDevice currentDevice] model]]
  125. delegate:self
  126. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  127. otherButtonTitles:nil];
  128. [alert show];
  129. }
  130. }
  131. @end