VLCServerListTVViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 "VLCSearchableServerBrowsingTVViewController.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 "VLCLocalNetworkServiceBrowserBonjour.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. self.nothingFoundLabel.text = NSLocalizedString(@"NO_SERVER_FOUND", nil);
  49. [self.nothingFoundLabel sizeToFit];
  50. UIView *nothingFoundView = self.nothingFoundView;
  51. [nothingFoundView sizeToFit];
  52. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  53. [self.view addSubview:nothingFoundView];
  54. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  55. attribute:NSLayoutAttributeCenterY
  56. relatedBy:NSLayoutRelationEqual
  57. toItem:self.view
  58. attribute:NSLayoutAttributeCenterY
  59. multiplier:1.0
  60. constant:0.0];
  61. [self.view addConstraint:yConstraint];
  62. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  63. attribute:NSLayoutAttributeCenterX
  64. relatedBy:NSLayoutRelationEqual
  65. toItem:self.view
  66. attribute:NSLayoutAttributeCenterX
  67. multiplier:1.0
  68. constant:0.0];
  69. [self.view addConstraint:xConstraint];
  70. NSArray *classes = @[
  71. // [VLCLocalNetworkServiceBrowserManualConnect class],
  72. [VLCLocalNetworkServiceBrowserHTTP class],
  73. [VLCLocalNetworkServiceBrowserUPnP class],
  74. [VLCLocalNetworkServiceBrowserDSM class],
  75. [VLCLocalNetworkServiceBrowserPlex class],
  76. [VLCLocalNetworkServiceBrowserFTP class],
  77. [VLCLocalNetworkServiceBrowserBonjour 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. NSInteger count = self.indexPaths.count;
  102. self.nothingFoundView.hidden = count > 0;
  103. return count;
  104. }
  105. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
  106. {
  107. VLCRemoteBrowsingTVCell *browsingCell = (VLCRemoteBrowsingTVCell *) [collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  108. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  109. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  110. if (service == nil)
  111. return browsingCell;
  112. browsingCell.isDirectory = YES;
  113. browsingCell.title = service.title;
  114. browsingCell.titleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption2];
  115. browsingCell.subtitle = [self.discoveryController titleForSection:discoveryIndexPath.section];
  116. browsingCell.subtitleLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCaption1];
  117. UIImage *serviceIcon = service.icon;
  118. browsingCell.thumbnailImage = serviceIcon ? serviceIcon : [UIImage imageNamed:@"serverIcon"];
  119. return browsingCell;
  120. }
  121. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  122. {
  123. NSIndexPath *discoveryIndexPath = self.indexPaths[indexPath.row];
  124. id<VLCLocalNetworkService> service = [self.discoveryController networkServiceForIndexPath:discoveryIndexPath];
  125. [self didSelectService:service];
  126. }
  127. #pragma mark - Service specific stuff
  128. - (void)didSelectService:(id<VLCLocalNetworkService>)service
  129. {
  130. if ([service respondsToSelector:@selector(serverBrowser)]) {
  131. id <VLCNetworkServerBrowser> browser = [service serverBrowser];
  132. if (browser) {
  133. VLCServerBrowsingTVViewController *browsingViewController = [[VLCSearchableServerBrowsingTVViewController alloc] initWithServerBrowser:browser];
  134. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:browsingViewController]
  135. animated:YES
  136. completion:nil];
  137. return;
  138. }
  139. }
  140. if ([service respondsToSelector:@selector(loginInformation)]) {
  141. VLCNetworkServerLoginInformation *login = service.loginInformation;
  142. if (!login) return;
  143. [self showLoginAlertWithLogin:login];
  144. return;
  145. }
  146. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  147. NSURL *url = service.directPlaybackURL;
  148. if (!url) return;
  149. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  150. [vpc playURL:url subtitlesFilePath:nil];
  151. [self presentViewController:[VLCFullscreenMovieTVViewController fullscreenMovieTVViewController]
  152. animated:YES
  153. completion:nil];
  154. return;
  155. }
  156. }
  157. - (void)showLoginAlertWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  158. {
  159. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"CONNECT_TO_SERVER", nil)
  160. message:login.address preferredStyle:UIAlertControllerStyleAlert];
  161. NSURLComponents *components = [[NSURLComponents alloc] init];
  162. components.scheme = login.protocolIdentifier;
  163. components.host = login.address;
  164. components.port = login.port;
  165. NSString *serviceIdentifier = components.URL.absoluteString;
  166. NSString *accountName = [SSKeychain accountsForService:serviceIdentifier].firstObject[kSSKeychainAccountKey];
  167. NSString *password = [SSKeychain passwordForService:serviceIdentifier account:accountName];
  168. __block UITextField *usernameField = nil;
  169. __block UITextField *passwordField = nil;
  170. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  171. textField.placeholder = NSLocalizedString(@"USER_LABEL", nil);
  172. textField.text = accountName;
  173. usernameField = textField;
  174. }];
  175. [alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  176. textField.secureTextEntry = YES;
  177. textField.placeholder = NSLocalizedString(@"PASSWORD_LABEL", nil);
  178. textField.text = password;
  179. passwordField = textField;
  180. }];
  181. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"LOGIN", nil)
  182. style:UIAlertActionStyleDefault
  183. handler:^(UIAlertAction * _Nonnull action) {
  184. login.username = usernameField.text;
  185. login.password = passwordField.text;
  186. [self showBrowserWithLogin:login];
  187. }]];
  188. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_SAVE", nil)
  189. style:UIAlertActionStyleDefault
  190. handler:^(UIAlertAction * _Nonnull action) {
  191. NSString *accountName = usernameField.text;
  192. NSString *password = passwordField.text;
  193. [SSKeychain setPassword:password forService:serviceIdentifier account:accountName];
  194. login.username = accountName;
  195. login.password = password;
  196. [self showBrowserWithLogin:login];
  197. }]];
  198. if (accountName.length && password.length) {
  199. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DELETE", nil)
  200. style:UIAlertActionStyleDestructive
  201. handler:^(UIAlertAction * _Nonnull action) {
  202. [SSKeychain deletePasswordForService:serviceIdentifier account:accountName];
  203. }]];
  204. } else {
  205. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_ANONYMOUS_LOGIN", nil)
  206. style:UIAlertActionStyleDefault
  207. handler:^(UIAlertAction * _Nonnull action) {
  208. login.username = nil;
  209. login.password = nil;
  210. [self showBrowserWithLogin:login];
  211. }]];
  212. }
  213. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  214. style:UIAlertActionStyleCancel
  215. handler:nil]];
  216. [self presentViewController:alertController animated:YES completion:nil];
  217. }
  218. - (void)showBrowserWithLogin:(nonnull VLCNetworkServerLoginInformation *)login
  219. {
  220. id<VLCNetworkServerBrowser> serverBrowser = nil;
  221. NSString *identifier = login.protocolIdentifier;
  222. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  223. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:login];
  224. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  225. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:login];
  226. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  227. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:login];
  228. }
  229. if (serverBrowser) {
  230. VLCServerBrowsingTVViewController *targetViewController = [[VLCSearchableServerBrowsingTVViewController alloc] initWithServerBrowser:serverBrowser];
  231. [self presentViewController:[[UINavigationController alloc] initWithRootViewController:targetViewController]
  232. animated:YES
  233. completion:nil];
  234. }
  235. }
  236. #pragma mark - VLCLocalServerDiscoveryController
  237. - (void)discoveryFoundSomethingNew
  238. {
  239. NSMutableArray<NSIndexPath *> *indexPaths = [NSMutableArray array];
  240. VLCLocalServerDiscoveryController *discoveryController = self.discoveryController;
  241. NSUInteger sectionCount = [discoveryController numberOfSections];
  242. for (NSUInteger section = 0; section < sectionCount; ++section) {
  243. NSUInteger itemsCount = [discoveryController numberOfItemsInSection:section];
  244. for (NSUInteger index = 0; index < itemsCount; ++index) {
  245. NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index inSection:section];
  246. [indexPaths addObject:indexPath];
  247. }
  248. }
  249. self.indexPaths = [indexPaths copy];
  250. [self.collectionView reloadData];
  251. _nothingFoundLabel.hidden = self.discoveryController.foundAnythingAtAll;
  252. }
  253. @end