VLCDropboxTableViewController.m 6.2 KB

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