VLCNetworkLoginViewController.m 16 KB

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