VLCNetworkLoginViewController.m 17 KB

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