VLCServerListTVViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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.subtitle = [self.discoveryController titleForSection:discoveryIndexPath.section];
  112. browsingCell.subtitleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
  113. browsingCell.thumbnailImage = service.icon ? service.icon : [UIImage imageNamed:@"serverIcon"];
  114. return browsingCell;
  115. }
  116. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  117. {
  118. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  119. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  120. [self didSelectService:service];
  121. }
  122. #pragma mark - Service specific stuff
  123. - (void)didSelectService:(id<VLCLocalNetworkService>)service
  124. {
  125. if ([service respondsToSelector:@selector(serverBrowser)]) {
  126. id <VLCNetworkServerBrowser> browser = [service serverBrowser];
  127. if (browser) {
  128. VLCServerBrowsingTVViewController *browsingViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:browser];
  129. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:browsingViewController]
  130. animated:YES
  131. completion:nil];
  132. return;
  133. }
  134. }
  135. if ([service respondsToSelector:@selector(loginInformation)]) {
  136. VLCNetworkServerLoginInformation *login = service.loginInformation;
  137. if (!login) return;
  138. [self showLoginAlertWithLogin:login];
  139. return;
  140. }
  141. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  142. NSURL *url = service.directPlaybackURL;
  143. if (!url) return;
  144. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  145. [vpc playURL:url subtitlesFilePath:nil];
  146. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  147. animated:YES
  148. completion:nil];
  149. return;
  150. }
  151. }
  152. - (void)showLoginAlertWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  153. {
  154. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"CONNECT_TO_SERVER", nil)
  155. message:login.address preferredStyle:UIAlertControllerStyleAlert];
  156. NSURLComponents *components = [[NSURLComponents alloc] init];
  157. components.scheme = login.protocolIdentifier;
  158. components.host = login.address;
  159. components.port = login.port;
  160. NSString *serviceIdentifier = components.URL.absoluteString;
  161. NSString *accountName = [SSKeychain accountsForService:serviceIdentifier].firstObject[kSSKeychainAccountKey];
  162. NSString *password = [SSKeychain passwordForService:serviceIdentifier account:accountName];
  163. __block UITextField *usernameField = nil;
  164. __block UITextField *passwordField = nil;
  165. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  166. textField.placeholder = NSLocalizedString(@"USER_LABEL", nil);
  167. textField.text = accountName;
  168. usernameField = textField;
  169. }];
  170. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  171. textField.secureTextEntry = YES;
  172. textField.placeholder = NSLocalizedString(@"PASSWORD_LABEL", nil);
  173. textField.text = password;
  174. passwordField = textField;
  175. }];
  176. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"LOGIN", nil)
  177. style:UIAlertActionStyleDefault
  178. handler:^(UIAlertAction * _Nonnull action) {
  179. login.username = usernameField.text;
  180. login.password = passwordField.text;
  181. [self showBrowserWithLogin:login];
  182. }]];
  183. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_SAVE", nil)
  184. style:UIAlertActionStyleDefault
  185. handler:^(UIAlertAction * _Nonnull action) {
  186. NSString *accountName = usernameField.text;
  187. NSString *password = passwordField.text;
  188. [SSKeychain setPassword:password forService:serviceIdentifier account:accountName];
  189. login.username = accountName;
  190. login.password = password;
  191. [self showBrowserWithLogin:login];
  192. }]];
  193. if (accountName.length && password.length) {
  194. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  195. style:UIAlertActionStyleDestructive
  196. handler:^(UIAlertAction * _Nonnull action) {
  197. [SSKeychain deletePasswordForService:serviceIdentifier account:accountName];
  198. }]];
  199. }
  200. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  201. style:UIAlertActionStyleCancel
  202. handler:nil]];
  203. [self presentViewController:alertController animated:YES completion:nil];
  204. }
  205. - (void)showBrowserWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  206. {
  207. id<VLCNetworkServerBrowser> serverBrowser = nil;
  208. NSString *identifier = login.protocolIdentifier;
  209. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  210. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:login];
  211. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  212. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:login];
  213. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  214. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:login];
  215. }
  216. if (serverBrowser) {
  217. VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:serverBrowser];
  218. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:targetViewController]
  219. animated:YES
  220. completion:nil];
  221. }
  222. }
  223. #pragma mark - VLCLocalServerDiscoveryController
  224. - (void)discoveryFoundSomethingNew
  225. {
  226. NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
  227. VLCLocalServerDiscoveryController *discoveryController = self.discoveryController;
  228. NSUInteger sectionCount = [discoveryController numberOfSections];
  229. for (NSUInteger section = 0; section < sectionCount; ++section) {
  230. NSUInteger itemsCount = [discoveryController numberOfItemsInSection:section];
  231. for (NSUInteger index = 0; index < itemsCount; ++index) {
  232. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:section];
  233. [indexPaths addObject:indexPath];
  234. }
  235. }
  236. self.indexPaths = [indexPaths copy];
  237. [self.collectionView reloadData];
  238. _nothingFoundLabel.hidden = self.discoveryController.foundAnythingAtAll;
  239. }
  240. @end