VLCDropboxTableViewController.m 4.5 KB

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