VLCServerListTVViewController.m 14 KB

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