VLCNetworkLoginViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*****************************************************************************
  2. * VLCNetworkLoginViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Vincent L. Cone <vincent.l.cone # tuta.io>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCNetworkLoginViewController.h"
  15. #import "VLCPlexWebAPI.h"
  16. #import <XKKeychain/XKKeychainGenericPasswordItem.h>
  17. #import "VLCNetworkLoginDataSource.h"
  18. #import "VLCNetworkLoginDataSourceProtocol.h"
  19. #import "VLCNetworkLoginDataSourceLogin.h"
  20. #import "VLCNetworkLoginDataSourceSavedLogins.h"
  21. #import "VLCNetworkServerLoginInformation.h"
  22. // for protocol identifier
  23. #import "VLCLocalNetworkServiceBrowserPlex.h"
  24. #import "VLCLocalNetworkServiceBrowserFTP.h"
  25. #import "VLCLocalNetworkServiceBrowserDSM.h"
  26. @interface VLCNetworkLoginViewController () <UITextFieldDelegate, VLCNetworkLoginDataSourceProtocolDelegate, VLCNetworkLoginDataSourceLoginDelegate, VLCNetworkLoginDataSourceSavedLoginsDelegate>
  27. {
  28. UIActivityIndicatorView *_activityIndicator;
  29. UIView *_activityBackgroundView;
  30. }
  31. @property (nonatomic) VLCNetworkLoginDataSource *dataSource;
  32. @property (nonatomic) VLCNetworkLoginDataSourceProtocol *protocolDataSource;
  33. @property (nonatomic) VLCNetworkLoginDataSourceLogin *loginDataSource;
  34. @property (nonatomic) VLCNetworkLoginDataSourceSavedLogins *savedLoginsDataSource;
  35. @property (nonatomic, weak) IBOutlet UITableView *tableView;
  36. @end
  37. @implementation VLCNetworkLoginViewController
  38. - (void)viewDidLoad
  39. {
  40. [super viewDidLoad];
  41. self.modalPresentationStyle = UIModalPresentationFormSheet;
  42. self.title = NSLocalizedString(@"CONNECT_TO_SERVER", nil);
  43. self.tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  44. self.tableView.separatorColor = [UIColor VLCLightTextColor];
  45. self.protocolDataSource = [[VLCNetworkLoginDataSourceProtocol alloc] init];
  46. self.protocolDataSource.delegate = self;
  47. self.protocolDataSource.protocol = [self protocolForProtocolIdentifier:self.loginInformation.protocolIdentifier];
  48. self.loginDataSource = [[VLCNetworkLoginDataSourceLogin alloc] init];
  49. self.loginDataSource.loginInformation = self.loginInformation;
  50. self.loginDataSource.delegate = self;
  51. self.savedLoginsDataSource = [[VLCNetworkLoginDataSourceSavedLogins alloc] init];
  52. self.savedLoginsDataSource.delegate = self;
  53. VLCNetworkLoginDataSource *dataSource = [[VLCNetworkLoginDataSource alloc] init];
  54. dataSource.dataSources = @[self.protocolDataSource, self.loginDataSource, self.savedLoginsDataSource];
  55. [dataSource configureWithTableView:self.tableView];
  56. self.dataSource = dataSource;
  57. _activityBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
  58. _activityBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  59. _activityBackgroundView.hidden = YES;
  60. _activityBackgroundView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  61. [self.view addSubview:_activityBackgroundView];
  62. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  63. _activityIndicator.hidesWhenStopped = YES;
  64. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  65. [_activityBackgroundView addSubview:_activityIndicator];
  66. [_activityIndicator setCenter:_activityBackgroundView.center];
  67. // self.edgesForExtendedLayout = UIRectEdgeNone;
  68. }
  69. - (void)dealloc
  70. {
  71. [[NSNotificationCenter defaultCenter] removeObserver:self];
  72. }
  73. - (void)viewWillAppear:(BOOL)animated
  74. {
  75. [super viewWillAppear:animated];
  76. [self.tableView reloadData];
  77. }
  78. #pragma mark - PLEX stuff
  79. // TODO: can we move this Plex stuff to the Plex server browser?
  80. - (void)_plexLogin
  81. {
  82. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  83. NSString *auth = [PlexWebAPI PlexAuthentification:self.loginInformation.username password:self.loginInformation.password];
  84. [self performSelectorOnMainThread:@selector(_stopActivity) withObject:nil waitUntilDone:YES];
  85. if ([auth isEqualToString:@""]) {
  86. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil)
  87. message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil)
  88. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  89. otherButtonTitles:nil];
  90. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  91. return;
  92. }
  93. [self.delegate loginWithLoginViewController:self loginInfo:self.loginInformation];
  94. }
  95. - (void)_stopActivity
  96. {
  97. _activityBackgroundView.hidden = YES;
  98. [_activityIndicator stopAnimating];
  99. }
  100. #pragma mark -
  101. - (VLCServerProtocol)protocolForProtocolIdentifier:(NSString *)protocolIdentifier
  102. {
  103. VLCServerProtocol protocol = VLCServerProtocolUndefined;
  104. if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  105. protocol = VLCServerProtocolFTP;
  106. } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  107. protocol = VLCServerProtocolSMB;
  108. } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  109. protocol = VLCServerProtocolPLEX;
  110. }
  111. return protocol;
  112. }
  113. - (nullable NSString *)protocolIdentifierForProtocol:(VLCServerProtocol)protocol
  114. {
  115. NSString *protocolIdentifier = nil;
  116. switch (protocol) {
  117. case VLCServerProtocolFTP:
  118. {
  119. protocolIdentifier = VLCNetworkServerProtocolIdentifierFTP;
  120. break;
  121. }
  122. case VLCServerProtocolPLEX:
  123. {
  124. protocolIdentifier = VLCNetworkServerProtocolIdentifierPlex;
  125. break;
  126. }
  127. case VLCServerProtocolSMB:
  128. {
  129. protocolIdentifier = VLCNetworkServerProtocolIdentifierSMB;
  130. }
  131. default:
  132. break;
  133. }
  134. return protocolIdentifier;
  135. }
  136. - (void)setLoginInformation:(VLCNetworkServerLoginInformation *)loginInformation
  137. {
  138. _loginInformation = loginInformation;
  139. self.protocolDataSource.protocol = [self protocolForProtocolIdentifier:loginInformation.protocolIdentifier];
  140. self.loginDataSource.loginInformation = loginInformation;
  141. }
  142. #pragma mark - VLCNetworkLoginDataSourceProtocolDelegate
  143. - (void)protocolDidChange:(VLCNetworkLoginDataSourceProtocol *)protocolSection
  144. {
  145. NSString *protocolIdentifier = [self protocolIdentifierForProtocol:protocolSection.protocol];
  146. VLCNetworkServerLoginInformation *login = [VLCNetworkServerLoginInformation newLoginInformationForProtocol:protocolIdentifier];
  147. login.address = self.loginInformation.address;
  148. login.username = self.loginInformation.username;
  149. login.password = self.loginInformation.password;
  150. self.loginDataSource.loginInformation = login;
  151. }
  152. #pragma mark - VLCNetworkLoginDataSourceLoginDelegate
  153. - (void)saveLoginDataSource:(VLCNetworkLoginDataSourceLogin *)dataSource
  154. {
  155. if (!self.protocolSelected)
  156. return;
  157. VLCNetworkServerLoginInformation *login = dataSource.loginInformation;
  158. // TODO: move somewere else?
  159. // Normalize Plex login
  160. if ([login.protocolIdentifier isEqualToString:@"plex"]) {
  161. if (!login.address.length) {
  162. login.address = @"Account";
  163. }
  164. if (!login.port) {
  165. login.port = @32400;
  166. }
  167. }
  168. self.loginInformation = login;
  169. NSError *error = nil;
  170. if (![self.savedLoginsDataSource saveLogin:login error:&error]) {
  171. [[[VLCAlertView alloc] initWithTitle:error.localizedDescription
  172. message:error.localizedFailureReason
  173. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil] show];
  174. }
  175. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  176. }
  177. - (void)connectLoginDataSource:(VLCNetworkLoginDataSourceLogin *)dataSource
  178. {
  179. if (!self.protocolSelected)
  180. return;
  181. VLCNetworkServerLoginInformation *loginInformation = dataSource.loginInformation;
  182. self.loginInformation = loginInformation;
  183. // TODO: can we move this Plex stuff to the Plex server browser?
  184. if ([loginInformation.protocolIdentifier isEqualToString:@"plex"] &&
  185. (loginInformation.username.length > 0 || loginInformation.password.length > 0)) {
  186. _activityBackgroundView.hidden = NO;
  187. [_activityIndicator startAnimating];
  188. [self performSelectorInBackground:@selector(_plexLogin) withObject:nil];
  189. } else {
  190. [self.delegate loginWithLoginViewController:self loginInfo:dataSource.loginInformation];
  191. }
  192. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  193. }
  194. - (BOOL)protocolSelected
  195. {
  196. if (self.protocolDataSource.protocol == VLCServerProtocolUndefined) {
  197. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PROTOCOL_NOT_SELECTED", nil)
  198. message:NSLocalizedString(@"PROTOCOL_NOT_SELECTED", nil)
  199. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  200. otherButtonTitles:nil];
  201. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  202. [self.tableView deselectRowAtIndexPath:self.tableView.indexPathForSelectedRow animated:YES];
  203. return NO;
  204. }
  205. return YES;
  206. }
  207. #pragma mark - VLCNetworkLoginDataSourceSavedLoginsDelegate
  208. - (void)loginsDataSource:(VLCNetworkLoginDataSourceSavedLogins *)dataSource selectedLogin:(VLCNetworkServerLoginInformation *)login
  209. {
  210. self.loginInformation = login;
  211. }
  212. @end