VLCNetworkLoginViewController.m 17 KB

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