VLCServerListTVViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCServerListTVViewController.h"
  12. #import "VLCLocalNetworkServerTVCell.h"
  13. #import "VLCServerBrowsingTVViewController.h"
  14. #import "VLCNetworkServerLoginInformation.h"
  15. #import "VLCNetworkServerBrowserPlex.h"
  16. #import "VLCNetworkServerBrowserVLCMedia.h"
  17. #import "VLCNetworkServerBrowserFTP.h"
  18. #import <SSKeychain/SSKeychain.h>
  19. #import "VLCLocalNetworkServiceBrowserManualConnect.h"
  20. #import "VLCLocalNetworkServiceBrowserPlex.h"
  21. #import "VLCLocalNetworkServiceBrowserFTP.h"
  22. #import "VLCLocalNetworkServiceBrowserUPnP.h"
  23. #ifndef NDEBUG
  24. #import "VLCLocalNetworkServiceBrowserSAP.h"
  25. #endif
  26. #import "VLCLocalNetworkServiceBrowserDSM.h"
  27. #import "VLCLocalNetworkServiceBrowserHTTP.h"
  28. #import "VLCRemoteBrowsingTVCell.h"
  29. @interface VLCServerListTVViewController ()
  30. {
  31. UILabel *_nothingFoundLabel;
  32. }
  33. @property (nonatomic) NSArray <NSIndexPath *> *indexPaths;
  34. @end
  35. @implementation VLCServerListTVViewController
  36. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  37. {
  38. return [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  39. }
  40. - (void)viewDidLoad {
  41. [super viewDidLoad];
  42. self.automaticallyAdjustsScrollViewInsets = NO;
  43. self.edgesForExtendedLayout = UIRectEdgeAll ^ UIRectEdgeTop;
  44. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
  45. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  46. flowLayout.minimumInteritemSpacing = 48.0;
  47. flowLayout.minimumLineSpacing = 100.0;
  48. _nothingFoundLabel = [[UILabel alloc] init];
  49. _nothingFoundLabel.text = NSLocalizedString(@"NO_SERVER_FOUND", nil);
  50. _nothingFoundLabel.textAlignment = NSTextAlignmentCenter;
  51. _nothingFoundLabel.textColor = [UIColor VLCLightTextColor];
  52. _nothingFoundLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleTitle3];
  53. [_nothingFoundLabel sizeToFit];
  54. [_nothingFoundLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
  55. [self.view addSubview:_nothingFoundLabel];
  56. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:_nothingFoundLabel
  57. attribute:NSLayoutAttributeCenterY
  58. relatedBy:NSLayoutRelationEqual
  59. toItem:self.view
  60. attribute:NSLayoutAttributeCenterY
  61. multiplier:1.0
  62. constant:0.0];
  63. [self.view addConstraint:yConstraint];
  64. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:_nothingFoundLabel
  65. attribute:NSLayoutAttributeCenterX
  66. relatedBy:NSLayoutRelationEqual
  67. toItem:self.view
  68. attribute:NSLayoutAttributeCenterX
  69. multiplier:1.0
  70. constant:0.0];
  71. [self.view addConstraint:xConstraint];
  72. NSArray *classes = @[
  73. // [VLCLocalNetworkServiceBrowserManualConnect class],
  74. [VLCLocalNetworkServiceBrowserHTTP class],
  75. [VLCLocalNetworkServiceBrowserUPnP class],
  76. [VLCLocalNetworkServiceBrowserDSM class],
  77. [VLCLocalNetworkServiceBrowserPlex class],
  78. [VLCLocalNetworkServiceBrowserFTP class],
  79. #ifndef NDEBUG
  80. [VLCLocalNetworkServiceBrowserSAP class],
  81. #endif
  82. ];
  83. self.discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:classes];
  84. self.discoveryController.delegate = self;
  85. }
  86. - (NSString *)title {
  87. return NSLocalizedString(@"LOCAL_NETWORK", nil);
  88. }
  89. - (void)viewDidAppear:(BOOL)animated
  90. {
  91. [super viewDidAppear:animated];
  92. [self.discoveryController startDiscovery];
  93. }
  94. - (void)viewDidDisappear:(BOOL)animated
  95. {
  96. [super viewDidDisappear:animated];
  97. [self.discoveryController stopDiscovery];
  98. }
  99. #pragma mark - Collection view data source
  100. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  101. {
  102. return self.indexPaths.count;
  103. }
  104. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
  105. {
  106. VLCRemoteBrowsingTVCell *browsingCell = (VLCRemoteBrowsingTVCell *) [collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  107. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  108. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  109. browsingCell.isDirectory = YES;
  110. browsingCell.title = service.title;
  111. browsingCell.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
  112. browsingCell.subtitle = [self.discoveryController titleForSection:discoveryIndexPath.section];
  113. browsingCell.subtitleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
  114. browsingCell.thumbnailImage = service.icon ? service.icon : [UIImage imageNamed:@"serverIcon"];
  115. return browsingCell;
  116. }
  117. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  118. {
  119. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  120. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  121. [self didSelectService:service];
  122. }
  123. #pragma mark - Service specific stuff
  124. - (void)didSelectService:(id<VLCLocalNetworkService>)service
  125. {
  126. if ([service respondsToSelector:@selector(serverBrowser)]) {
  127. id <VLCNetworkServerBrowser> browser = [service serverBrowser];
  128. if (browser) {
  129. VLCServerBrowsingTVViewController *browsingViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:browser];
  130. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:browsingViewController]
  131. animated:YES
  132. completion:nil];
  133. return;
  134. }
  135. }
  136. if ([service respondsToSelector:@selector(loginInformation)]) {
  137. VLCNetworkServerLoginInformation *login = service.loginInformation;
  138. if (!login) return;
  139. [self showLoginAlertWithLogin:login];
  140. return;
  141. }
  142. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  143. NSURL *url = service.directPlaybackURL;
  144. if (!url) return;
  145. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  146. [vpc playURL:url subtitlesFilePath:nil];
  147. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  148. animated:YES
  149. completion:nil];
  150. return;
  151. }
  152. }
  153. - (void)showLoginAlertWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  154. {
  155. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"CONNECT_TO_SERVER", nil)
  156. message:login.address preferredStyle:UIAlertControllerStyleAlert];
  157. NSURLComponents *components = [[NSURLComponents alloc] init];
  158. components.scheme = login.protocolIdentifier;
  159. components.host = login.address;
  160. components.port = login.port;
  161. NSString *serviceIdentifier = components.URL.absoluteString;
  162. NSString *accountName = [SSKeychain accountsForService:serviceIdentifier].firstObject[kSSKeychainAccountKey];
  163. NSString *password = [SSKeychain passwordForService:serviceIdentifier account:accountName];
  164. __block UITextField *usernameField = nil;
  165. __block UITextField *passwordField = nil;
  166. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  167. textField.placeholder = NSLocalizedString(@"USER_LABEL", nil);
  168. textField.text = accountName;
  169. usernameField = textField;
  170. }];
  171. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  172. textField.secureTextEntry = YES;
  173. textField.placeholder = NSLocalizedString(@"PASSWORD_LABEL", nil);
  174. textField.text = password;
  175. passwordField = textField;
  176. }];
  177. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"LOGIN", nil)
  178. style:UIAlertActionStyleDefault
  179. handler:^(UIAlertAction * _Nonnull action) {
  180. login.username = usernameField.text;
  181. login.password = passwordField.text;
  182. [self showBrowserWithLogin:login];
  183. }]];
  184. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_SAVE", nil)
  185. style:UIAlertActionStyleDefault
  186. handler:^(UIAlertAction * _Nonnull action) {
  187. NSString *accountName = usernameField.text;
  188. NSString *password = passwordField.text;
  189. [SSKeychain setPassword:password forService:serviceIdentifier account:accountName];
  190. login.username = accountName;
  191. login.password = password;
  192. [self showBrowserWithLogin:login];
  193. }]];
  194. if (accountName.length && password.length) {
  195. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  196. style:UIAlertActionStyleDestructive
  197. handler:^(UIAlertAction * _Nonnull action) {
  198. [SSKeychain deletePasswordForService:serviceIdentifier account:accountName];
  199. }]];
  200. }
  201. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  202. style:UIAlertActionStyleCancel
  203. handler:nil]];
  204. [self presentViewController:alertController animated:YES completion:nil];
  205. }
  206. - (void)showBrowserWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  207. {
  208. id<VLCNetworkServerBrowser> serverBrowser = nil;
  209. NSString *identifier = login.protocolIdentifier;
  210. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  211. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:login];
  212. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  213. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:login];
  214. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  215. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:login];
  216. }
  217. if (serverBrowser) {
  218. VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:serverBrowser];
  219. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:targetViewController]
  220. animated:YES
  221. completion:nil];
  222. }
  223. }
  224. #pragma mark - VLCLocalServerDiscoveryController
  225. - (void)discoveryFoundSomethingNew
  226. {
  227. NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
  228. VLCLocalServerDiscoveryController *discoveryController = self.discoveryController;
  229. NSUInteger sectionCount = [discoveryController numberOfSections];
  230. for (NSUInteger section = 0; section < sectionCount; ++section) {
  231. NSUInteger itemsCount = [discoveryController numberOfItemsInSection:section];
  232. for (NSUInteger index = 0; index < itemsCount; ++index) {
  233. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:section];
  234. [indexPaths addObject:indexPath];
  235. }
  236. }
  237. self.indexPaths = [indexPaths copy];
  238. [self.collectionView reloadData];
  239. _nothingFoundLabel.hidden = self.discoveryController.foundAnythingAtAll;
  240. }
  241. @end