VLCNetworkLoginViewController.m 8.66 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * VLCNetworkLoginViewController.m
 * VLC for iOS
 *****************************************************************************
5
 * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
6 7 8 9
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *          Pierre SAGASPE <pierre.sagaspe # me.com>
10
 *          Vincent L. Cone <vincent.l.cone # tuta.io>
11 12 13
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/
14 15

#import "VLCNetworkLoginViewController.h"
16
#import "VLCPlexWebAPI.h"
17

18 19 20 21 22
#import "VLCNetworkLoginDataSource.h"
#import "VLCNetworkLoginDataSourceProtocol.h"
#import "VLCNetworkLoginDataSourceLogin.h"
#import "VLCNetworkLoginDataSourceSavedLogins.h"
#import "VLCNetworkServerLoginInformation.h"
23
#import "VLC_iOS-Swift.h"
24 25 26 27 28 29 30 31 32


// for protocol identifier
#import "VLCLocalNetworkServiceBrowserPlex.h"
#import "VLCLocalNetworkServiceBrowserFTP.h"
#import "VLCLocalNetworkServiceBrowserDSM.h"


@interface VLCNetworkLoginViewController () <UITextFieldDelegate, VLCNetworkLoginDataSourceProtocolDelegate, VLCNetworkLoginDataSourceLoginDelegate, VLCNetworkLoginDataSourceSavedLoginsDelegate>
Pierre SAGASPE's avatar
Pierre SAGASPE committed
33
{
34 35
    UIActivityIndicatorView *_activityIndicator;
    UIView *_activityBackgroundView;
Pierre SAGASPE's avatar
Pierre SAGASPE committed
36
}
37 38 39 40 41 42 43 44

@property (nonatomic) VLCNetworkLoginDataSource *dataSource;
@property (nonatomic) VLCNetworkLoginDataSourceProtocol *protocolDataSource;
@property (nonatomic) VLCNetworkLoginDataSourceLogin *loginDataSource;
@property (nonatomic) VLCNetworkLoginDataSourceSavedLogins *savedLoginsDataSource;

@property (nonatomic, weak) IBOutlet UITableView *tableView;

45 46 47 48 49 50 51 52 53 54
@end

@implementation VLCNetworkLoginViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.modalPresentationStyle = UIModalPresentationFormSheet;

55
    self.title = NSLocalizedString(@"CONNECT_TO_SERVER", nil);
56 57

    self.tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
58
    self.tableView.separatorColor = [UIColor blackColor];
59 60 61 62 63 64 65 66 67 68 69 70 71 72

    self.protocolDataSource = [[VLCNetworkLoginDataSourceProtocol alloc] init];
    self.protocolDataSource.delegate = self;
    self.protocolDataSource.protocol = [self protocolForProtocolIdentifier:self.loginInformation.protocolIdentifier];
    self.loginDataSource = [[VLCNetworkLoginDataSourceLogin alloc] init];
    self.loginDataSource.loginInformation = self.loginInformation;
    self.loginDataSource.delegate = self;
    self.savedLoginsDataSource = [[VLCNetworkLoginDataSourceSavedLogins alloc] init];
    self.savedLoginsDataSource.delegate = self;

    VLCNetworkLoginDataSource *dataSource = [[VLCNetworkLoginDataSource alloc] init];
    dataSource.dataSources = @[self.protocolDataSource, self.loginDataSource, self.savedLoginsDataSource];
    [dataSource configureWithTableView:self.tableView];
    self.dataSource = dataSource;
73

74 75 76 77 78 79 80 81 82 83 84 85 86
    _activityBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
    _activityBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _activityBackgroundView.hidden = YES;
    _activityBackgroundView.backgroundColor = [UIColor VLCDarkBackgroundColor];
    [self.view addSubview:_activityBackgroundView];

    _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    _activityIndicator.hidesWhenStopped = YES;
    _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;

    [_activityBackgroundView addSubview:_activityIndicator];
    [_activityIndicator setCenter:_activityBackgroundView.center];

87
//    self.edgesForExtendedLayout = UIRectEdgeNone;
88 89
}

90 91 92
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
93
    [self.tableView reloadData];
94 95
}

96
#pragma mark -
97

98
- (VLCServerProtocol)protocolForProtocolIdentifier:(NSString *)protocolIdentifier
99
{
100 101 102 103 104 105 106
    VLCServerProtocol protocol = VLCServerProtocolUndefined;
    if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
        protocol = VLCServerProtocolFTP;
    } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
        protocol = VLCServerProtocolSMB;
    } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
        protocol = VLCServerProtocolPLEX;
107
    }
108
    return protocol;
109 110
}

111
- (nullable NSString *)protocolIdentifierForProtocol:(VLCServerProtocol)protocol
112
{
113 114
    NSString *protocolIdentifier = nil;
    switch (protocol) {
115 116
        case VLCServerProtocolFTP:
        {
117
            protocolIdentifier = VLCNetworkServerProtocolIdentifierFTP;
118 119 120 121
            break;
        }
        case VLCServerProtocolPLEX:
        {
122
            protocolIdentifier = VLCNetworkServerProtocolIdentifierPlex;
123 124 125 126
            break;
        }
        case VLCServerProtocolSMB:
        {
127
            protocolIdentifier = VLCNetworkServerProtocolIdentifierSMB;
128 129 130 131
        }
        default:
            break;
    }
132
    return protocolIdentifier;
Pierre SAGASPE's avatar
Pierre SAGASPE committed
133 134
}

135
- (void)setLoginInformation:(VLCNetworkServerLoginInformation *)loginInformation
Pierre SAGASPE's avatar
Pierre SAGASPE committed
136
{
137 138 139
    _loginInformation = loginInformation;
    self.protocolDataSource.protocol = [self protocolForProtocolIdentifier:loginInformation.protocolIdentifier];
    self.loginDataSource.loginInformation = loginInformation;
Pierre SAGASPE's avatar
Pierre SAGASPE committed
140 141
}

142 143
#pragma mark - VLCNetworkLoginDataSourceProtocolDelegate
- (void)protocolDidChange:(VLCNetworkLoginDataSourceProtocol *)protocolSection
Pierre SAGASPE's avatar
Pierre SAGASPE committed
144
{
145 146 147 148 149 150
    NSString *protocolIdentifier = [self protocolIdentifierForProtocol:protocolSection.protocol];
    VLCNetworkServerLoginInformation *login = [VLCNetworkServerLoginInformation newLoginInformationForProtocol:protocolIdentifier];
    login.address = self.loginInformation.address;
    login.username = self.loginInformation.username;
    login.password = self.loginInformation.password;
    self.loginDataSource.loginInformation = login;
Pierre SAGASPE's avatar
Pierre SAGASPE committed
151 152
}

153
#pragma mark - VLCNetworkLoginDataSourceLoginDelegate
Pierre SAGASPE's avatar
Pierre SAGASPE committed
154

155
- (void)saveLoginDataSource:(VLCNetworkLoginDataSourceLogin *)dataSource
Pierre SAGASPE's avatar
Pierre SAGASPE committed
156
{
157 158 159
    if (!self.protocolSelected)
        return;

160 161 162 163
    VLCNetworkServerLoginInformation *login = dataSource.loginInformation;
    // TODO: move somewere else?
    // Normalize Plex login
    if ([login.protocolIdentifier isEqualToString:@"plex"]) {
164
        if (!login.address.length) {
165 166
            login.address = @"Account";
        }
167
        if (!login.port) {
168 169
            login.port = @32400;
        }
Pierre SAGASPE's avatar
Pierre SAGASPE committed
170 171
    }

172 173 174
    self.loginInformation = login;
    NSError *error = nil;
    if (![self.savedLoginsDataSource saveLogin:login error:&error]) {
175 176 177 178 179 180
        [VLCAlertViewController alertViewManagerWithTitle:error.localizedDescription
                                             errorMessage: error.localizedFailureReason
                                           viewController:self
                                            buttonsAction:@[[[VLCAlertButton alloc]
                                                             initWithTitle: NSLocalizedString(@"BUTTON_OK", nil)
                                                             action: ^(UIAlertAction* action){}]]];
181 182
    }

183
    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
Pierre SAGASPE's avatar
Pierre SAGASPE committed
184
}
185

186
- (void)connectLoginDataSource:(VLCNetworkLoginDataSourceLogin *)dataSource
187
{
188 189 190
    if (!self.protocolSelected)
        return;

191 192
    VLCNetworkServerLoginInformation *loginInformation = dataSource.loginInformation;
    self.loginInformation = loginInformation;
193

194
    [self.delegate loginWithLoginViewController:self loginInfo:dataSource.loginInformation];
195

196
    [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
197 198
}

199 200 201
- (BOOL)protocolSelected
{
    if (self.protocolDataSource.protocol == VLCServerProtocolUndefined) {
202 203 204
        [VLCAlertViewController alertViewManagerWithTitle:NSLocalizedString(@"PROTOCOL_NOT_SELECTED", nil)
                                             errorMessage: NSLocalizedString(@"PROTOCOL_NOT_SELECTED", nil)
                                           viewController:self
205 206
                                            buttonsAction:@[[[VLCAlertButton alloc] initWithTitle: NSLocalizedString(@"BUTTON_OK", nil)
                                                                                         action: ^(UIAlertAction* action){}]]];
207 208 209 210 211 212
        [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
        return NO;
    }
    return YES;
}

213 214
#pragma mark - VLCNetworkLoginDataSourceSavedLoginsDelegate
- (void)loginsDataSource:(VLCNetworkLoginDataSourceSavedLogins *)dataSource selectedLogin:(VLCNetworkServerLoginInformation *)login
215
{
216
    self.loginInformation = login;
217 218
}

219
@end