VLCNetworkLoginViewController.m 18 KB

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