VLCServerListTVViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. self.nothingFoundLabel.text = NSLocalizedString(@"NO_SERVER_FOUND", nil);
  48. [self.nothingFoundLabel sizeToFit];
  49. UIView *nothingFoundView = self.nothingFoundView;
  50. [nothingFoundView sizeToFit];
  51. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  52. [self.view addSubview:nothingFoundView];
  53. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  54. attribute:NSLayoutAttributeCenterY
  55. relatedBy:NSLayoutRelationEqual
  56. toItem:self.view
  57. attribute:NSLayoutAttributeCenterY
  58. multiplier:1.0
  59. constant:0.0];
  60. [self.view addConstraint:yConstraint];
  61. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  62. attribute:NSLayoutAttributeCenterX
  63. relatedBy:NSLayoutRelationEqual
  64. toItem:self.view
  65. attribute:NSLayoutAttributeCenterX
  66. multiplier:1.0
  67. constant:0.0];
  68. [self.view addConstraint:xConstraint];
  69. NSArray *classes = @[
  70. // [VLCLocalNetworkServiceBrowserManualConnect class],
  71. [VLCLocalNetworkServiceBrowserHTTP class],
  72. [VLCLocalNetworkServiceBrowserUPnP class],
  73. [VLCLocalNetworkServiceBrowserDSM class],
  74. [VLCLocalNetworkServiceBrowserPlex class],
  75. [VLCLocalNetworkServiceBrowserFTP class],
  76. #ifndef NDEBUG
  77. [VLCLocalNetworkServiceBrowserSAP class],
  78. #endif
  79. ];
  80. self.discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:classes];
  81. self.discoveryController.delegate = self;
  82. }
  83. - (NSString *)title {
  84. return NSLocalizedString(@"LOCAL_NETWORK", nil);
  85. }
  86. - (void)viewDidAppear:(BOOL)animated
  87. {
  88. [super viewDidAppear:animated];
  89. [self.discoveryController startDiscovery];
  90. }
  91. - (void)viewDidDisappear:(BOOL)animated
  92. {
  93. [super viewDidDisappear:animated];
  94. [self.discoveryController stopDiscovery];
  95. }
  96. #pragma mark - Collection view data source
  97. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  98. {
  99. return self.indexPaths.count;
  100. }
  101. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
  102. {
  103. VLCRemoteBrowsingTVCell *browsingCell = (VLCRemoteBrowsingTVCell *) [collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  104. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  105. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  106. if (service == nil)
  107. return browsingCell;
  108. browsingCell.isDirectory = YES;
  109. browsingCell.title = service.title;
  110. browsingCell.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
  111. browsingCell.subtitle = [self.discoveryController titleForSection:discoveryIndexPath.section];
  112. browsingCell.subtitleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
  113. UIImage *serviceIcon = service.icon;
  114. browsingCell.thumbnailImage = serviceIcon ? serviceIcon : [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. } else {
  201. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_ANONYMOUS_LOGIN", nil)
  202. style:UIAlertActionStyleDefault
  203. handler:^(UIAlertAction * _Nonnull action) {
  204. login.username = nil;
  205. login.password = nil;
  206. [self showBrowserWithLogin:login];
  207. }]];
  208. }
  209. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  210. style:UIAlertActionStyleCancel
  211. handler:nil]];
  212. [self presentViewController:alertController animated:YES completion:nil];
  213. }
  214. - (void)showBrowserWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  215. {
  216. id<VLCNetworkServerBrowser> serverBrowser = nil;
  217. NSString *identifier = login.protocolIdentifier;
  218. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  219. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:login];
  220. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  221. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:login];
  222. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  223. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:login];
  224. }
  225. if (serverBrowser) {
  226. VLCServerBrowsingTVViewController *targetViewController = [[VLCServerBrowsingTVViewController alloc] initWithServerBrowser:serverBrowser];
  227. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:targetViewController]
  228. animated:YES
  229. completion:nil];
  230. }
  231. }
  232. #pragma mark - VLCLocalServerDiscoveryController
  233. - (void)discoveryFoundSomethingNew
  234. {
  235. NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
  236. VLCLocalServerDiscoveryController *discoveryController = self.discoveryController;
  237. NSUInteger sectionCount = [discoveryController numberOfSections];
  238. for (NSUInteger section = 0; section < sectionCount; ++section) {
  239. NSUInteger itemsCount = [discoveryController numberOfItemsInSection:section];
  240. for (NSUInteger index = 0; index < itemsCount; ++index) {
  241. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:section];
  242. [indexPaths addObject:indexPath];
  243. }
  244. }
  245. self.indexPaths = [indexPaths copy];
  246. [self.collectionView reloadData];
  247. _nothingFoundLabel.hidden = self.discoveryController.foundAnythingAtAll;
  248. }
  249. @end