VLCDropboxTableViewController.m 6.48 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

#import "VLCDropboxTableViewController.h"
#import "VLCDropboxController.h"
19
#import "VLCCloudStorageTableViewCell.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
20
#import "UIDevice+VLC.h"
21 22 23 24 25
#import "DBKeychain.h"
#if TARGET_OS_IOS
#import "VLCAppDelegate.h"
#endif

26

27
@interface VLCDropboxTableViewController () <VLCCloudStorageTableViewCell, VLCCloudStorageDelegate>
28 29
{
    VLCDropboxController *_dropboxController;
30
    DBMetadata *_selectedFile;
31
    NSArray *_mediaList;
32 33 34 35 36 37
}

@end

@implementation VLCDropboxTableViewController

38 39 40 41 42 43 44 45 46
- (instancetype)initWithPath:(NSString *)path
{
    self = [super init];
    if (self) {
        self.currentPath = path;
    }
    return self;
}

47 48 49 50 51
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

52 53 54
- (void)viewDidLoad
{
    [super viewDidLoad];
55

56
    _dropboxController = [VLCDropboxController sharedInstance];
57 58
    self.controller = _dropboxController;
    self.controller.delegate = self;
59

60
#if TARGET_OS_IOS
61 62 63 64
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(sessionWasUpdated:)
                                                 name:VLCDropboxSessionWasAuthorized
                                               object:nil];
65 66

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

68
    [self.cloudStorageLogo setImage:[UIImage imageNamed:@"dropbox-white.png"]];
69 70 71

    [self.cloudStorageLogo sizeToFit];
    self.cloudStorageLogo.center = self.view.center;
72 73
#else
    self.title = @"Dropbox";
74
#endif
75 76
}

77 78 79
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
80

81
    self.controller = [VLCDropboxController sharedInstance];
82 83
    self.controller.delegate = self;

84 85 86
    if (self.currentPath != nil)
        self.title = self.currentPath.lastPathComponent;

87
    [self updateViewAfterSessionChange];
88 89
}

90 91
#pragma mark - interface interaction

92
- (BOOL)shouldAutorotate
93
{
94
    UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
95 96 97 98 99
    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        return NO;
    return YES;
}

100 101 102 103 104 105
#pragma mark - Table view data source

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

106
    VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
107
    if (cell == nil)
108
        cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
109

110 111 112 113 114 115 116
    NSUInteger index = indexPath.row;
    if (_mediaList) {
        if (index < _mediaList.count) {
            cell.dropboxFile = _mediaList[index];
            cell.delegate = self;
        }
    }
117 118 119 120

    return cell;
}

121 122 123
- (void)mediaListUpdated
{
    _mediaList = [self.controller.currentListFiles copy];
124 125

    [super mediaListUpdated];
126 127
}

128 129 130 131
#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
132
    _selectedFile = _mediaList[indexPath.row];
133
    if (!_selectedFile.isDirectory)
134
        [_dropboxController streamFile:_selectedFile currentNavigationController:self.navigationController];
135
    else {
136
        /* dive into subdirectory */
137 138
        NSString *futurePath = [self.currentPath stringByAppendingFormat:@"/%@", _selectedFile.filename];
        self.currentPath = futurePath;
139
        [self requestInformationForCurrentPath];
140
    }
141
    _selectedFile = nil;
142 143 144 145 146

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

#pragma mark - login dialog
147

148
- (IBAction)loginAction:(id)sender
149
{
150 151
    if (!_dropboxController.isAuthorized) {
        self.authorizationInProgress = YES;
152
        [[DBSession sharedSession] linkFromController:self];
153
    } else
154 155 156
        [_dropboxController logout];
}

157 158 159 160
- (void)sessionWasUpdated:(NSNotification *)aNotification
{
    self.authorizationInProgress = YES;
    [self updateViewAfterSessionChange];
161 162

    [_dropboxController shareCredentials];
163
}
164

165
#pragma mark - VLCCloudStorageTableViewCell delegation
Felix Paul Kühne's avatar
Felix Paul Kühne committed
166

167
#if TARGET_OS_IOS
168
- (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
169
{
170
    _selectedFile = _mediaList[[self.tableView indexPathForCell:cell].row];
171

172 173
    if (_selectedFile.totalBytes < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
        /* selected item is a proper file, ask the user if s/he wants to download it */
174 175 176 177 178
        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];
179 180
        [alert show];
    } else {
181 182 183 184 185
        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];
186 187
        [alert show];
    }
188 189
}

190 191 192 193 194 195 196 197 198 199
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
        [_dropboxController downloadFileToDocumentFolder:_selectedFile];

    _selectedFile = nil;
}

#endif

200
@end