VLCDropboxTableViewController.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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)viewDidLoad
  29. {
  30. [super viewDidLoad];
  31. _dropboxController = [[VLCDropboxController alloc] init];
  32. self.controller = _dropboxController;
  33. self.controller.delegate = self;
  34. DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
  35. [DBSession setSharedSession:dbSession];
  36. [DBRequest setNetworkRequestDelegate:_dropboxController];
  37. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
  38. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
  39. [self.cloudStorageLogo sizeToFit];
  40. self.cloudStorageLogo.center = self.view.center;
  41. }
  42. - (void)viewWillAppear:(BOOL)animated
  43. {
  44. [super viewWillAppear:animated];
  45. [self updateViewAfterSessionChange];
  46. }
  47. #pragma mark - interface interaction
  48. - (BOOL)shouldAutorotate
  49. {
  50. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  51. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  52. return NO;
  53. return YES;
  54. }
  55. #pragma mark - Table view data source
  56. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  57. {
  58. static NSString *CellIdentifier = @"DropboxCell";
  59. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  60. if (cell == nil)
  61. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  62. cell.fileMetadata = _dropboxController.currentListFiles[indexPath.row];
  63. cell.delegate = self;
  64. return cell;
  65. }
  66. #pragma mark - Table view delegate
  67. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  68. {
  69. _selectedFile = _dropboxController.currentListFiles[indexPath.row];
  70. if (!_selectedFile.isDirectory)
  71. [_dropboxController streamFile:_selectedFile];
  72. else {
  73. /* dive into subdirectory */
  74. self.currentPath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
  75. [self _requestInformationForCurrentPath];
  76. }
  77. _selectedFile = nil;
  78. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  79. }
  80. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  81. {
  82. if (buttonIndex == 1)
  83. [_dropboxController downloadFileToDocumentFolder:_selectedFile];
  84. _selectedFile = nil;
  85. }
  86. #pragma mark - login dialog
  87. - (IBAction)loginAction:(id)sender
  88. {
  89. if (!_dropboxController.isAuthorized) {
  90. self.authorizationInProgress = YES;
  91. [[DBSession sharedSession] linkFromController:self];
  92. } else
  93. [_dropboxController logout];
  94. }
  95. #pragma mark - VLCCloudStorageTableViewCell delegation
  96. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  97. {
  98. _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];
  99. if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  100. /* selected item is a proper file, ask the user if s/he wants to download it */
  101. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  102. [alert show];
  103. } else {
  104. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.filename, [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  105. [alert show];
  106. }
  107. }
  108. @end