VLCNetworkLoginViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. #import "SSKeychain.h"
  16. @interface VLCNetworkLoginViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
  17. {
  18. NSString *_hostname;
  19. NSString *_username;
  20. NSString *_password;
  21. UIActivityIndicatorView *_activityIndicator;
  22. UIView *_activityBackgroundView;
  23. NSMutableArray *_serverList;
  24. }
  25. @end
  26. @implementation VLCNetworkLoginViewController
  27. - (void)viewDidLoad
  28. {
  29. [super viewDidLoad];
  30. self.modalPresentationStyle = UIModalPresentationFormSheet;
  31. self.title = NSLocalizedString(@"CONNECT_TO_SERVER", nil);
  32. [self.connectButton setTitle:NSLocalizedString(@"BUTTON_CONNECT", nil) forState:UIControlStateNormal];
  33. self.serverLabel.text = NSLocalizedString(@"SERVER", nil);
  34. self.portLabel.text = NSLocalizedString(@"SERVER_PORT", nil);
  35. self.loginHelpLabel.text = NSLocalizedString(@"ENTER_SERVER_CREDS_HELP", nil);
  36. [self.saveButton setTitle:NSLocalizedString(@"BUTTON_SAVE", nil) forState:UIControlStateNormal];
  37. self.serverField.delegate = self;
  38. self.serverField.returnKeyType = UIReturnKeyNext;
  39. self.serverField.clearButtonMode = UITextFieldViewModeWhileEditing;
  40. self.portField.delegate = self;
  41. self.portField.returnKeyType = UIReturnKeyNext;
  42. self.portField.clearButtonMode = UITextFieldViewModeWhileEditing;
  43. self.usernameField.delegate = self;
  44. self.usernameField.returnKeyType = UIReturnKeyNext;
  45. self.usernameField.clearButtonMode = UITextFieldViewModeWhileEditing;
  46. self.passwordField.delegate = self;
  47. self.passwordField.returnKeyType = UIReturnKeyDone;
  48. self.passwordField.clearButtonMode = UITextFieldViewModeWhileEditing;
  49. self.storedServersTableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  50. _activityBackgroundView = [[UIView alloc] initWithFrame:self.view.frame];
  51. _activityBackgroundView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
  52. _activityBackgroundView.hidden = YES;
  53. _activityBackgroundView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  54. [self.view addSubview:_activityBackgroundView];
  55. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  56. _activityIndicator.hidesWhenStopped = YES;
  57. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin;
  58. [_activityBackgroundView addSubview:_activityIndicator];
  59. [_activityIndicator setCenter:_activityBackgroundView.center];
  60. UIColor *color = [UIColor VLCLightTextColor];
  61. self.serverField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"yourserver.local" attributes:@{NSForegroundColorAttributeName: color}];
  62. self.usernameField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"USER_LABEL", nil) attributes:@{NSForegroundColorAttributeName: color}];
  63. self.passwordField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"PASSWORD_LABEL", nil) attributes:@{NSForegroundColorAttributeName: color}];
  64. self.edgesForExtendedLayout = UIRectEdgeNone;
  65. }
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. [super viewWillAppear:animated];
  69. if (_hostname.length > 0)
  70. self.serverField.text = _hostname;
  71. if (_port.length > 0)
  72. self.portField.text = _port;
  73. if (_username.length > 0)
  74. self.usernameField.text = _username;
  75. if (_password.length > 0)
  76. self.passwordField.text = _password;
  77. if (self.serverProtocol != VLCServerProtocolUndefined) {
  78. self.protocolSegmentedControl.selectedSegmentIndex = self.serverProtocol;
  79. self.protocolSegmentedControl.enabled = NO;
  80. } else {
  81. self.protocolSegmentedControl.selectedSegmentIndex = VLCServerProtocolSMB;
  82. self.protocolSegmentedControl.enabled = YES;
  83. [self protocolSelectionChanged:nil];
  84. }
  85. // persistent state
  86. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  87. _serverList = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCStoredServerList]];
  88. if (_serverList.count == 0) {
  89. /* we need to migrate from previous, insecure storage fields */
  90. NSArray *ftpServerList = [defaults objectForKey:kVLCFTPServer];
  91. NSArray *ftpLoginList = [defaults objectForKey:kVLCFTPLogin];
  92. NSArray *ftpPasswordList = [defaults objectForKey:kVLCFTPPassword];
  93. NSUInteger count = ftpServerList.count;
  94. if (count > 0) {
  95. for (NSUInteger i = 0; i < count; i++) {
  96. [SSKeychain setPassword:ftpPasswordList[i] forService:ftpServerList[i] account:ftpLoginList[i]];
  97. [_serverList addObject:ftpServerList[i]];
  98. }
  99. }
  100. NSArray *plexServerList = [defaults objectForKey:kVLCPLEXServer];
  101. NSArray *plexPortList = [defaults objectForKey:kVLCPLEXPort];
  102. count = plexServerList.count;
  103. if (count > 0) {
  104. for (NSUInteger i = 0; i < count; i++) {
  105. [_serverList addObject:[NSString stringWithFormat:@"plex://%@:%@", plexServerList[i], plexPortList[i]]];
  106. }
  107. }
  108. [defaults setObject:_serverList forKey:kVLCStoredServerList];
  109. [defaults synchronize];
  110. }
  111. [self.storedServersTableView reloadData];
  112. }
  113. - (void)viewWillDisappear:(BOOL)animated
  114. {
  115. [super viewWillDisappear:animated];
  116. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  117. [defaults setObject:_serverList forKey:kVLCStoredServerList];
  118. }
  119. - (IBAction)connectToServer:(id)sender
  120. {
  121. if (self.delegate) {
  122. if ([self.delegate respondsToSelector:@selector(loginToServer:port:protocol:confirmedWithUsername:andPassword:)]) {
  123. VLCServerProtocol protocol = self.protocolSegmentedControl.selectedSegmentIndex;
  124. NSString *username = self.usernameField.text;
  125. NSString *password = self.passwordField.text;
  126. if ((username.length > 0 || password.length > 0) && protocol == VLCServerProtocolPLEX) {
  127. _activityBackgroundView.hidden = NO;
  128. [_activityIndicator startAnimating];
  129. [self performSelectorInBackground:@selector(_plexLogin)
  130. withObject:nil];
  131. return;
  132. }
  133. [self.navigationController popViewControllerAnimated:YES];
  134. [self.delegate loginToServer:self.serverField.text
  135. port:self.portField.text
  136. protocol:protocol
  137. confirmedWithUsername:username
  138. andPassword:password];
  139. }
  140. }
  141. }
  142. - (void)_plexLogin
  143. {
  144. VLCPlexWebAPI *PlexWebAPI = [[VLCPlexWebAPI alloc] init];
  145. NSString *auth = [PlexWebAPI PlexAuthentification:self.usernameField.text password:self.passwordField.text];
  146. if ([auth isEqualToString:@""]) {
  147. [self performSelectorOnMainThread:@selector(_stopActivity) withObject:nil waitUntilDone:YES];
  148. VLCAlertView *alertView = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"PLEX_ERROR_ACCOUNT", nil)
  149. message:NSLocalizedString(@"PLEX_CHECK_ACCOUNT", nil)
  150. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  151. otherButtonTitles:nil];
  152. [alertView performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:NO];
  153. return;
  154. }
  155. [self performSelectorOnMainThread:@selector(_dismiss) withObject:nil waitUntilDone:YES];
  156. [self.delegate loginToServer:self.serverField.text
  157. port:self.portField.text
  158. protocol:VLCServerProtocolPLEX
  159. confirmedWithUsername:auth
  160. andPassword:nil];
  161. }
  162. - (void)_stopActivity
  163. {
  164. _activityBackgroundView.hidden = YES;
  165. [_activityIndicator stopAnimating];
  166. }
  167. - (void)_dismiss
  168. {
  169. _activityBackgroundView.hidden = YES;
  170. [_activityIndicator stopAnimating];
  171. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  172. [self.navigationController popViewControllerAnimated:YES];
  173. else
  174. [self dismissViewControllerAnimated:YES completion:nil];
  175. }
  176. - (IBAction)saveServer:(id)sender
  177. {
  178. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  179. NSString *server = self.serverField.text;
  180. if (!server)
  181. return;
  182. VLCServerProtocol protocol = self.protocolSegmentedControl.selectedSegmentIndex;
  183. NSString *scheme;
  184. switch (protocol) {
  185. case VLCServerProtocolFTP:
  186. scheme = @"ftp";
  187. break;
  188. case VLCServerProtocolSMB:
  189. scheme = @"smb";
  190. break;
  191. case VLCServerProtocolPLEX:
  192. scheme = @"plex";
  193. break;
  194. default:
  195. break;
  196. }
  197. NSString *port = self.portField.text;
  198. NSString *service;
  199. if (port.length > 0)
  200. service = [NSString stringWithFormat:@"%@://%@:%@",
  201. scheme, server, port];
  202. else
  203. service = [NSString stringWithFormat:@"%@://%@",
  204. scheme, server];
  205. [_serverList addObject:service];
  206. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  207. [defaults setObject:_serverList forKey:kVLCStoredServerList];
  208. [defaults synchronize];
  209. NSString *username = self.usernameField.text;
  210. NSString *password = self.passwordField.text;
  211. if (username || password)
  212. [SSKeychain setPassword:password forService:service account:username];
  213. [self.storedServersTableView reloadData];
  214. }
  215. - (IBAction)protocolSelectionChanged:(id)sender
  216. {
  217. UIColor *color = [UIColor VLCLightTextColor];
  218. switch (self.protocolSegmentedControl.selectedSegmentIndex) {
  219. case VLCServerProtocolFTP:
  220. {
  221. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"21" attributes:@{NSForegroundColorAttributeName: color}];
  222. self.portField.enabled = YES;
  223. break;
  224. }
  225. case VLCServerProtocolPLEX:
  226. {
  227. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"32400" attributes:@{NSForegroundColorAttributeName: color}];
  228. self.portField.enabled = YES;
  229. break;
  230. }
  231. case VLCServerProtocolSMB:
  232. {
  233. self.portField.placeholder = @"";
  234. self.portField.text = @"";
  235. self.portField.enabled = NO;
  236. }
  237. default:
  238. break;
  239. }
  240. }
  241. #pragma mark - text view delegate
  242. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  243. {
  244. if ([self.serverField isFirstResponder]) {
  245. [self.serverField resignFirstResponder];
  246. [self.usernameField becomeFirstResponder];
  247. } else if ([self.usernameField isFirstResponder]) {
  248. [self.usernameField resignFirstResponder];
  249. [self.passwordField becomeFirstResponder];
  250. } else if ([self.passwordField isFirstResponder]) {
  251. [self.passwordField resignFirstResponder];
  252. }
  253. return NO;
  254. }
  255. #pragma mark - table view data source
  256. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  257. {
  258. return 1;
  259. }
  260. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  261. {
  262. return _serverList.count;
  263. }
  264. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  265. {
  266. static NSString *CellIdentifier = @"StoredServerListCell";
  267. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  268. if (cell == nil) {
  269. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  270. cell.textLabel.textColor = [UIColor whiteColor];
  271. cell.detailTextLabel.textColor = [UIColor VLCLightTextColor];
  272. }
  273. NSInteger row = indexPath.row;
  274. NSString *serviceString = _serverList[row];
  275. NSURL *service = [NSURL URLWithString:serviceString];
  276. cell.textLabel.text = [NSString stringWithFormat:@"%@ [%@]", service.host, [service.scheme uppercaseString]];
  277. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  278. if (accounts.count > 0) {
  279. NSDictionary *account = [accounts firstObject];
  280. cell.detailTextLabel.text = [account objectForKey:@"acct"];
  281. } else
  282. cell.detailTextLabel.text = @"";
  283. return cell;
  284. }
  285. #pragma mark - table view delegate
  286. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  287. {
  288. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  289. }
  290. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  291. {
  292. return YES;
  293. }
  294. - (void)tableView:(UITableView *)tableView
  295. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  296. forRowAtIndexPath:(NSIndexPath *)indexPath
  297. {
  298. if (editingStyle == UITableViewCellEditingStyleDelete) {
  299. NSString *serviceString = _serverList[indexPath.row];
  300. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  301. NSUInteger count = accounts.count;
  302. for (NSUInteger i = 0; i < count; i++) {
  303. NSString *username = [accounts[i] objectForKey:@"acct"];
  304. [SSKeychain deletePasswordForService:serviceString account:username];
  305. }
  306. [_serverList removeObject:serviceString];
  307. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  308. [defaults removeObjectForKey:serviceString];
  309. [tableView reloadData];
  310. }
  311. }
  312. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  313. {
  314. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  315. NSString *serviceString = _serverList[indexPath.row];
  316. NSURL *service = [NSURL URLWithString:serviceString];
  317. NSString *scheme = service.scheme;
  318. if ([scheme isEqualToString:@"smb"])
  319. self.serverProtocol = VLCServerProtocolSMB;
  320. else if ([scheme isEqualToString:@"ftp"])
  321. self.serverProtocol = VLCServerProtocolFTP;
  322. else if ([scheme isEqualToString:@"plex"])
  323. self.serverProtocol = VLCServerProtocolPLEX;
  324. self.protocolSegmentedControl.selectedSegmentIndex = self.serverProtocol;
  325. [self protocolSelectionChanged:nil];
  326. self.serverField.text = service.host;
  327. self.portField.text = [service.port stringValue];
  328. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  329. if (!accounts) {
  330. self.usernameField.text = self.passwordField.text = @"";
  331. return;
  332. }
  333. NSDictionary *account = [accounts firstObject];
  334. NSString *username = [account objectForKey:@"acct"];
  335. self.usernameField.text = username;
  336. self.passwordField.text = [SSKeychain passwordForService:serviceString account:username];
  337. }
  338. - (void)setHostname:(NSString *)theHostname
  339. {
  340. _hostname = theHostname;
  341. self.serverField.text = theHostname;
  342. }
  343. - (NSString *)hostname
  344. {
  345. return self.serverField.text;
  346. }
  347. - (void)setUsername:(NSString *)theUsername
  348. {
  349. _username = theUsername;
  350. self.usernameField.text = theUsername;
  351. }
  352. - (NSString *)username
  353. {
  354. return self.usernameField.text;
  355. }
  356. - (void)setPassword:(NSString *)thePassword
  357. {
  358. _password = thePassword;
  359. self.passwordField.text = thePassword;
  360. }
  361. - (NSString *)password
  362. {
  363. return self.passwordField.text;
  364. }
  365. @end