123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207 |
- /*****************************************************************************
- * VLCDropboxTableViewController.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- * Gleb Pinigin <gpinigin # gmail.com>
- * Carola Nitz <nitz.carola # googlemail.com>
- * Fabio Ritrovato <sephiroth87 # videolan.org>
- * Tamas Timar <ttimar.vlc # gmail.com>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCDropboxTableViewController.h"
- #import "VLCDropboxController.h"
- #import "VLCCloudStorageTableViewCell.h"
- #import "UIDevice+VLC.h"
- #import "DBKeychain.h"
- #if TARGET_OS_IOS
- #import "VLCAppDelegate.h"
- #endif
- @interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
- {
- VLCDropboxController *_dropboxController;
- DBMetadata *_selectedFile;
- NSArray *_mediaList;
- }
- @end
- @implementation VLCDropboxTableViewController
- - (instancetype)initWithPath:(NSString *)path
- {
- NSLog(@"%s", __PRETTY_FUNCTION__);
- self = [super init];
- if (self) {
- self.currentPath = path;
- }
- return self;
- }
- - (void)dealloc
- {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- _dropboxController = [VLCDropboxController sharedInstance];
- self.controller = _dropboxController;
- self.controller.delegate = self;
- #if TARGET_OS_IOS
- [[NSNotificationCenter defaultCenter] addObserver:self
- selector:@selector(sessionWasUpdated:)
- name:VLCDropboxSessionWasAuthorized
- object:nil];
- self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
- [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
- [self.cloudStorageLogo sizeToFit];
- self.cloudStorageLogo.center = self.view.center;
- #else
- self.title = @"Dropbox";
- #endif
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- self.controller = [VLCDropboxController sharedInstance];
- self.controller.delegate = self;
- if (self.currentPath != nil)
- self.title = self.currentPath.lastPathComponent;
- [self updateViewAfterSessionChange];
- }
- #pragma mark - interface interaction
- - (BOOL)shouldAutorotate
- {
- UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
- if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
- return NO;
- return YES;
- }
- #pragma mark - Table view data source
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"DropboxCell";
- VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
- NSUInteger index = indexPath.row;
- if (_mediaList) {
- if (index < _mediaList.count) {
- cell.dropboxFile = _mediaList[index];
- cell.delegate = self;
- }
- }
- return cell;
- }
- - (void)mediaListUpdated
- {
- _mediaList = [self.controller.currentListFiles copy];
- [self.tableView reloadData];
- }
- #pragma mark - Table view delegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- _selectedFile = _mediaList[indexPath.row];
- if (!_selectedFile.isDirectory)
- [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
- else {
- /* dive into subdirectory */
- NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
- #if TARGET_OS_TV
- [_dropboxController reset];
- VLCDropboxTableViewController *targetViewController = [[VLCDropboxTableViewController alloc] initWithPath:futurePath];
- [self.navigationController pushViewController:targetViewController animated:YES];
- #else
- self.currentPath = futurePath;
- [self requestInformationForCurrentPath];
- #endif
- }
- _selectedFile = nil;
- [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
- }
- #pragma mark - login dialog
- - (IBAction)loginAction:(id)sender
- {
- if (!_dropboxController.isAuthorized) {
- self.authorizationInProgress = YES;
- [[DBSession sharedSession] linkFromController:self];
- } else
- [_dropboxController logout];
- }
- - (void)sessionWasUpdated:(NSNotification *)aNotification
- {
- self.authorizationInProgress = YES;
- [self updateViewAfterSessionChange];
- [_dropboxController shareCredentials];
- }
- #pragma mark - VLCCloudStorageTableViewCell delegation
- #if TARGET_OS_IOS
- - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
- {
- _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
- if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
- /* selected item is a proper file, ask the user if s/he wants to download it */
- VLCAlertView *alert = [[VLCAlertView 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];
- [alert show];
- } else {
- VLCAlertView *alert = [[VLCAlertView 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];
- [alert show];
- }
- }
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1)
- [_dropboxController downloadFileToDocumentFolder:_selectedFile];
- _selectedFile = nil;
- }
- #endif
- @end
|