VLCDropboxTableViewController.m 4.97 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCDropboxTableViewController.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
6 7 8 9 10 11 12 13 14 15
 * $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.
 *****************************************************************************/
16 17 18 19

#import "VLCDropboxTableViewController.h"
#import "VLCDropboxController.h"
#import "VLCAppDelegate.h"
20
#import "VLCDropboxConstants.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
21
#import "UIDevice+VLC.h"
22

23
@interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell>
24 25
{
    VLCDropboxController *_dropboxController;
26
    DBMetadata *_selectedFile;
27 28 29 30 31 32 33 34 35
}

@end

@implementation VLCDropboxTableViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
36

37
    _dropboxController = [[VLCDropboxController alloc] init];
38 39
    self.controller = _dropboxController;
    self.controller.delegate = self;
40

41
    DBSession* dbSession = [[DBSession alloc] initWithAppKey:kVLCDropboxAppKey appSecret:kVLCDropboxPrivateKey root:kDBRootDropbox];
42 43 44 45
    [DBSession setSharedSession:dbSession];
    [DBRequest setNetworkRequestDelegate:_dropboxController];

    self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dropbox-white"]];
46

47
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
48 49 50

    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
51 52
}

53 54 55
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
56
    [self updateViewAfterSessionChange];
57 58
}

59 60
#pragma mark - interface interaction

61
- (BOOL)shouldAutorotate
62
{
63
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
64 65 66 67 68
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    return YES;
}

69 70 71 72 73 74
#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"DropboxCell";

75
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
76
    if (cell == nil)
77
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
78

Felix Paul Kühne's avatar
Felix Paul Kühne committed
79
    cell.dropboxFile = _dropboxController.currentListFiles[indexPath.row];
80
    cell.delegate = self;
81 82 83 84 85 86 87 88

    return cell;
}

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
89
    _selectedFile = _dropboxController.currentListFiles[indexPath.row];
90 91 92
    if (!_selectedFile.isDirectory)
        [_dropboxController streamFile:_selectedFile];
    else {
93
        /* dive into subdirectory */
94
        self.currentPath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
95
        [self _requestInformationForCurrentPath];
96
    }
97
    _selectedFile = nil;
98 99 100 101

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}

102 103
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
104 105 106 107
    if (buttonIndex == 1)
        [_dropboxController downloadFileToDocumentFolder:_selectedFile];

    _selectedFile = nil;
108 109
}

110
#pragma mark - login dialog
111

112
- (IBAction)loginAction:(id)sender
113
{
114 115
    if (!_dropboxController.isAuthorized) {
        self.authorizationInProgress = YES;
116
        [[DBSession sharedSession] linkFromController:self];
117
    } else
118 119 120
        [_dropboxController logout];
}

121

122
#pragma mark - VLCCloudStorageTableViewCell delegation
Felix Paul Kühne's avatar
Felix Paul Kühne committed
123

124
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
125 126 127
{
    _selectedFile = _dropboxController.currentListFiles[[self.tableView indexPathForCell:cell].row];

128 129 130 131 132 133 134 135
    if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
        /* selected item is a proper file, ask the user if s/he wants to download it */
        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];
        [alert show];
    } else {
        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];
        [alert show];
    }
136 137
}

138
@end