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