VLCNetworkLoginViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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. [self.navigationController popViewControllerAnimated:YES];
  172. }
  173. - (IBAction)saveServer:(id)sender
  174. {
  175. [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
  176. NSString *server = self.serverField.text;
  177. if (!server)
  178. return;
  179. VLCServerProtocol protocol = self.protocolSegmentedControl.selectedSegmentIndex;
  180. NSString *scheme;
  181. switch (protocol) {
  182. case VLCServerProtocolFTP:
  183. scheme = @"ftp";
  184. break;
  185. case VLCServerProtocolSMB:
  186. scheme = @"smb";
  187. break;
  188. case VLCServerProtocolPLEX:
  189. scheme = @"plex";
  190. break;
  191. default:
  192. break;
  193. }
  194. NSString *port = self.portField.text;
  195. NSString *service;
  196. if (port.length > 0)
  197. service = [NSString stringWithFormat:@"%@://%@:%@",
  198. scheme, server, port];
  199. else
  200. service = [NSString stringWithFormat:@"%@://%@",
  201. scheme, server];
  202. [_serverList addObject:service];
  203. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  204. [defaults setObject:_serverList forKey:kVLCStoredServerList];
  205. [defaults synchronize];
  206. NSString *username = self.usernameField.text;
  207. NSString *password = self.passwordField.text;
  208. if (username || password)
  209. [SSKeychain setPassword:password forService:service account:username];
  210. [self.storedServersTableView reloadData];
  211. }
  212. - (IBAction)protocolSelectionChanged:(id)sender
  213. {
  214. UIColor *color = [UIColor VLCLightTextColor];
  215. switch (self.protocolSegmentedControl.selectedSegmentIndex) {
  216. case VLCServerProtocolFTP:
  217. {
  218. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"21" attributes:@{NSForegroundColorAttributeName: color}];
  219. self.portField.enabled = YES;
  220. break;
  221. }
  222. case VLCServerProtocolPLEX:
  223. {
  224. self.portField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"32400" attributes:@{NSForegroundColorAttributeName: color}];
  225. self.portField.enabled = YES;
  226. break;
  227. }
  228. case VLCServerProtocolSMB:
  229. {
  230. self.portField.placeholder = @"";
  231. self.portField.text = @"";
  232. self.portField.enabled = NO;
  233. }
  234. default:
  235. break;
  236. }
  237. }
  238. #pragma mark - text view delegate
  239. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  240. {
  241. if ([self.serverField isFirstResponder]) {
  242. [self.serverField resignFirstResponder];
  243. [self.usernameField becomeFirstResponder];
  244. } else if ([self.usernameField isFirstResponder]) {
  245. [self.usernameField resignFirstResponder];
  246. [self.passwordField becomeFirstResponder];
  247. } else if ([self.passwordField isFirstResponder]) {
  248. [self.passwordField resignFirstResponder];
  249. }
  250. return NO;
  251. }
  252. #pragma mark - table view data source
  253. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  254. {
  255. return 1;
  256. }
  257. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  258. {
  259. return _serverList.count;
  260. }
  261. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  262. {
  263. static NSString *CellIdentifier = @"StoredServerListCell";
  264. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  265. if (cell == nil) {
  266. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  267. cell.textLabel.textColor = [UIColor whiteColor];
  268. cell.detailTextLabel.textColor = [UIColor VLCLightTextColor];
  269. }
  270. NSInteger row = indexPath.row;
  271. NSString *serviceString = _serverList[row];
  272. NSURL *service = [NSURL URLWithString:serviceString];
  273. cell.textLabel.text = [NSString stringWithFormat:@"%@ [%@]", service.host, [service.scheme uppercaseString]];
  274. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  275. if (accounts.count > 0) {
  276. NSDictionary *account = [accounts firstObject];
  277. cell.detailTextLabel.text = [account objectForKey:@"acct"];
  278. } else
  279. cell.detailTextLabel.text = @"";
  280. return cell;
  281. }
  282. #pragma mark - table view delegate
  283. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  284. {
  285. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  286. }
  287. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  288. {
  289. return YES;
  290. }
  291. - (void)tableView:(UITableView *)tableView
  292. commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
  293. forRowAtIndexPath:(NSIndexPath *)indexPath
  294. {
  295. if (editingStyle == UITableViewCellEditingStyleDelete) {
  296. NSString *serviceString = _serverList[indexPath.row];
  297. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  298. NSUInteger count = accounts.count;
  299. for (NSUInteger i = 0; i < count; i++) {
  300. NSString *username = [accounts[i] objectForKey:@"acct"];
  301. [SSKeychain deletePasswordForService:serviceString account:username];
  302. }
  303. [_serverList removeObject:serviceString];
  304. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  305. [defaults removeObjectForKey:serviceString];
  306. [tableView reloadData];
  307. }
  308. }
  309. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  310. {
  311. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  312. NSString *serviceString = _serverList[indexPath.row];
  313. NSURL *service = [NSURL URLWithString:serviceString];
  314. NSString *scheme = service.scheme;
  315. if ([scheme isEqualToString:@"smb"])
  316. self.serverProtocol = VLCServerProtocolSMB;
  317. else if ([scheme isEqualToString:@"ftp"])
  318. self.serverProtocol = VLCServerProtocolFTP;
  319. else if ([scheme isEqualToString:@"plex"])
  320. self.serverProtocol = VLCServerProtocolPLEX;
  321. self.protocolSegmentedControl.selectedSegmentIndex = self.serverProtocol;
  322. [self protocolSelectionChanged:nil];
  323. self.serverField.text = service.host;
  324. self.portField.text = [service.port stringValue];
  325. NSArray *accounts = [SSKeychain accountsForService:serviceString];
  326. if (!accounts) {
  327. self.usernameField.text = self.passwordField.text = @"";
  328. return;
  329. }
  330. NSDictionary *account = [accounts firstObject];
  331. NSString *username = [account objectForKey:@"acct"];
  332. self.usernameField.text = username;
  333. self.passwordField.text = [SSKeychain passwordForService:serviceString account:username];
  334. }
  335. - (void)setHostname:(NSString *)theHostname
  336. {
  337. _hostname = theHostname;
  338. self.serverField.text = theHostname;
  339. }
  340. - (NSString *)hostname
  341. {
  342. return self.serverField.text;
  343. }
  344. - (void)setUsername:(NSString *)theUsername
  345. {
  346. _username = theUsername;
  347. self.usernameField.text = theUsername;
  348. }
  349. - (NSString *)username
  350. {
  351. return self.usernameField.text;
  352. }
  353. - (void)setPassword:(NSString *)thePassword
  354. {
  355. _password = thePassword;
  356. self.passwordField.text = thePassword;
  357. }
  358. - (NSString *)password
  359. {
  360. return self.passwordField.text;
  361. }
  362. @end