VLCServerListTVViewController.m 14 KB

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