VLCNetworkLoginViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCNetworkLoginViewController.h"
  14. #import "VLCPlexWebAPI.h"
  15. @interface VLCNetworkLoginViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
  16. {
  17. NSString *_hostname;
  18. NSString *_username;
  19. NSString *_password;
  20. UIActivityIndicatorView *_activityIndicator;
  21. UIView *_activityBackgroundView;
  22. }
  23. @end
  24. @implementation VLCNetworkLoginViewController
  25. - (void)viewDidLoad
  26. {
  27. [super viewDidLoad];
  28. self.modalPresentationStyle = UIModalPresentationFormSheet;
  29. self.title = NSLocalizedString(@"CONNECT_TO_SERVER", nil);
  30. [self.connectButton setTitle:NSLocalizedString(@"BUTTON_CONNECT", nil) forState:UIControlStateNormal];
  31. self.serverLabel.text = NSLocalizedString(@"SERVER", nil);
  32. self.portLabel.text = NSLocalizedString(@"SERVER_PORT", nil);
  33. self.loginHelpLabel.text = NSLocalizedString(@"ENTER_SERVER_CREDS_HELP", nil);
  34. self.serverField.delegate = self;
  35. self.serverField.returnKeyType = UIReturnKeyNext;
  36. self.serverField.clearButtonMode = UITextFieldViewModeWhileEditing;
  37. self.portField.delegate = self;
  38. self.portField.returnKeyType = UIReturnKeyNext;
  39. self.portField.clearButtonMode = UITextFieldViewModeWhileEditing;
  40. self.usernameField.delegate = self;
  41. self.usernameField.returnKeyType = UIReturnKeyNext;
  42. self.usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
  43. self.passwordField.delegate = self;
  44. self.passwordField.returnKeyType = UIReturnKeyDone;
  45. self.passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
  46. self.historyLogin.backgroundColor = [UIColor VLCDarkBackgroundColor];
  47. _activityBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
  48. _activityBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  49. _activityBackgroundView.hidden = YES;
  50. _activityBackgroundView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  51. [self.view addSubview:_activityBackgroundView];
  52. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  53. _activityIndicator.hidesWhenStopped = YES;
  54. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  55. [_activityBackgroundView addSubview:_activityIndicator];
  56. [_activityIndicator setCenter:_activityBackgroundView.center];
  57. UIColor *color = [UIColor VLCLightTextColor];
  58. self.serverField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"yourserver.local" attributes:@{NSForegroundColorAttributeName: color}];
  59. self.usernameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"USER_LABEL", nil) attributes:@{NSForegroundColorAttributeName: color}];
  60. self.passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"PASSWORD_LABEL", nil) attributes:@{NSForegroundColorAttributeName: color}];
  61. self.edgesForExtendedLayout = UIRectEdgeNone;
  62. }
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [super viewWillAppear:animated];
  66. if (_hostname.length > 0)
  67. self.serverField.text = _hostname;
  68. if (_port.length > 0)
  69. self.portField.text = _port;
  70. if (_username.length > 0)
  71. self.usernameField.text = _username;
  72. if (_password.length > 0)
  73. self.passwordField.text = _password;
  74. if (self.serverProtocol != VLCServerProtocolUndefined) {
  75. self.protocolSegmentedControl.selectedSegmentIndex = self.serverProtocol;
  76. self.protocolSegmentedControl.enabled = NO;
  77. } else {
  78. self.protocolSegmentedControl.selectedSegmentIndex = VLCServerProtocolSMB;
  79. self.protocolSegmentedControl.enabled = YES;
  80. [self protocolSelectionChanged:nil];
  81. }
  82. // FIXME: persistent state
  83. /*
  84. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  85. _bookmarkServer = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCPLEXServer]];
  86. _bookmarkPort = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCPLEXPort]];
  87. [super viewWillAppear:animated];
  88. if ([defaults stringForKey:kVLCLastPLEXServer])
  89. self.serverAddressField.text = [defaults stringForKey:kVLCLastPLEXServer];
  90. if ([defaults stringForKey:kVLCLastPLEXPort])
  91. self.portField.text = [defaults stringForKey:kVLCLastPLEXPort];
  92. if (self.portField.text.length < 1)
  93. self.portField.text = kPlexMediaServerPortDefault;
  94. */
  95. }
  96. - (void)viewWillDisappear:(BOOL)animated
  97. {
  98. // FIXME: persistent state?!
  99. [super viewWillDisappear:animated];
  100. }
  101. - (IBAction)connectToServer:(id)sender
  102. {
  103. if (self.delegate) {
  104. if ([self.delegate respondsToSelector:@selector(loginToServer:port:protocol:confirmedWithUsername:andPassword:)]) {
  105. VLCServerProtocol protocol = self.protocolSegmentedControl.selectedSegmentIndex;
  106. NSString *username = self.usernameField.text;
  107. NSString *password = self.passwordField.text;
  108. if ((username.length > 0 || password.length > 0) && protocol == VLCServerProtocolPLEX) {
  109. _activityBackgroundView.hidden = NO;
  110. [_activityIndicator startAnimating];
  111. [self performSelectorInBackground:@selector(_plexLogin)
  112. withObject:nil];
  113. return;
  114. }
  115. [self.navigationController popViewControllerAnimated:YES];
  116. [self.delegate loginToServer:self.serverField.text
  117. port:self.portField.text
  118. protocol:protocol
  119. confirmedWithUsername:username
  120. andPassword:password];
  121. }
  122. }
  123. }
  124. - (void)_plexLogin
  125. {
  126. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  127. NSString *auth = [PlexWebAPI PlexAuthentification:self.usernameField.text password:self.passwordField.text];
  128. if ([auth isEqualToString:@""]) {
  129. [self performSelectorOnMainThread:@selector(_stopActivity) withObject:nil waitUntilDone:YES];
  130. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil)
  131. message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil)
  132. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  133. otherButtonTitles:nil];
  134. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  135. return;
  136. }
  137. [self performSelectorOnMainThread:@selector(_dismiss) withObject:nil waitUntilDone:YES];
  138. [self.delegate loginToServer:self.serverField.text
  139. port:self.portField.text
  140. protocol:VLCServerProtocolPLEX
  141. confirmedWithUsername:auth
  142. andPassword:nil];
  143. }
  144. - (void)_stopActivity
  145. {
  146. _activityBackgroundView.hidden = YES;
  147. [_activityIndicator stopAnimating];
  148. }
  149. - (void)_dismiss
  150. {
  151. _activityBackgroundView.hidden = YES;
  152. [_activityIndicator stopAnimating];
  153. [self.navigationController popViewControllerAnimated:YES];
  154. }
  155. - (IBAction)saveServer:(id)sender
  156. {
  157. // FIXME:
  158. /*
  159. NSString *serverAddress = self.serverAddressField.text;
  160. if (!serverAddress)
  161. return;
  162. if (serverAddress.length < 1)
  163. return;
  164. [_saveServer addObject:serverAddress];
  165. [_saveLogin addObject:self.usernameField.text];
  166. [_savePass addObject:self.passwordField.text];
  167. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  168. [defaults setObject:[NSArray arrayWithArray:_bookmarkServer] forKey:kVLCPLEXServer];
  169. [defaults setObject:[NSArray arrayWithArray:_bookmarkPort] forKey:kVLCPLEXPort];
  170. [self.historyLogin reloadData];*/
  171. }
  172. - (IBAction)protocolSelectionChanged:(id)sender
  173. {
  174. UIColor *color = [UIColor VLCLightTextColor];
  175. switch (self.protocolSegmentedControl.selectedSegmentIndex) {
  176. case VLCServerProtocolFTP:
  177. {
  178. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"21" attributes:@{NSForegroundColorAttributeName: color}];
  179. break;
  180. }
  181. case VLCServerProtocolPLEX:
  182. {
  183. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"32400" attributes:@{NSForegroundColorAttributeName: color}];
  184. break;
  185. }
  186. case VLCServerProtocolSMB:
  187. {
  188. self.portField.placeholder = @"";
  189. self.portField.enabled = NO;
  190. }
  191. default:
  192. break;
  193. }
  194. }
  195. #pragma mark - text view delegate
  196. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  197. {
  198. if ([self.serverField isFirstResponder]) {
  199. [self.serverField resignFirstResponder];
  200. [self.usernameField becomeFirstResponder];
  201. } else if ([self.usernameField isFirstResponder]) {
  202. [self.usernameField resignFirstResponder];
  203. [self.passwordField becomeFirstResponder];
  204. } else if ([self.passwordField isFirstResponder]) {
  205. [self.passwordField resignFirstResponder];
  206. }
  207. return NO;
  208. }
  209. #pragma mark - table view data source
  210. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  211. {
  212. return 1;
  213. }
  214. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  215. {
  216. return 0; // FIXME: _saveServer.count;
  217. }
  218. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  219. {
  220. static NSString *CellIdentifier = @"FTPHistoryCell";
  221. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  222. if (cell == nil) {
  223. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  224. cell.textLabel.textColor = [UIColor whiteColor];
  225. cell.detailTextLabel.textColor = [UIColor VLCLightTextColor];
  226. }
  227. NSInteger row = indexPath.row;
  228. /* FIXME: fetch from storage
  229. cell.textLabel.text = [_saveServer[row] lastPathComponent];
  230. cell.detailTextLabel.text = _saveLogin[row];*/
  231. return cell;
  232. }
  233. #pragma mark - table view delegate
  234. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  235. {
  236. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  237. }
  238. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  239. {
  240. return YES;
  241. }
  242. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  243. {
  244. if (editingStyle == UITableViewCellEditingStyleDelete) {
  245. // FIXME: remove from storage
  246. [tableView reloadData];
  247. }
  248. }
  249. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  250. {
  251. // FIXME: fetch from storage
  252. [self.historyLogin deselectRowAtIndexPath:indexPath animated:NO];
  253. }
  254. - (void)setHostname:(NSString *)theHostname
  255. {
  256. _hostname = theHostname;
  257. self.serverField.text = theHostname;
  258. }
  259. - (NSString *)hostname
  260. {
  261. return self.serverField.text;
  262. }
  263. - (void)setUsername:(NSString *)theUsername
  264. {
  265. _username = theUsername;
  266. self.usernameField.text = theUsername;
  267. }
  268. - (NSString *)username
  269. {
  270. return self.usernameField.text;
  271. }
  272. - (void)setPassword:(NSString *)thePassword
  273. {
  274. _password = thePassword;
  275. self.passwordField.text = thePassword;
  276. }
  277. - (NSString *)password
  278. {
  279. return self.passwordField.text;
  280. }
  281. @end