VLCServerListTVViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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. #import "GRKArrayDiff+UICollectionView.h"
  30. @interface VLCServerListTVViewController ()
  31. {
  32. UILabel *_nothingFoundLabel;
  33. }
  34. @property (nonatomic, copy) NSMutableArray<id<VLCLocalNetworkService>> *networkServices;
  35. @end
  36. @implementation VLCServerListTVViewController
  37. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  38. {
  39. return [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. self.automaticallyAdjustsScrollViewInsets = NO;
  44. self.edgesForExtendedLayout = UIRectEdgeAll ^ UIRectEdgeTop;
  45. UICollectionViewFlowLayout *flowLayout = (UICollectionViewFlowLayout *)self.collectionViewLayout;
  46. flowLayout.itemSize = CGSizeMake(250.0, 300.0);
  47. flowLayout.minimumInteritemSpacing = 48.0;
  48. flowLayout.minimumLineSpacing = 100.0;
  49. self.nothingFoundLabel.text = NSLocalizedString(@"NO_SERVER_FOUND", nil);
  50. [self.nothingFoundLabel sizeToFit];
  51. UIView *nothingFoundView = self.nothingFoundView;
  52. [nothingFoundView sizeToFit];
  53. [nothingFoundView setTranslatesAutoresizingMaskIntoConstraints:NO];
  54. [self.view addSubview:nothingFoundView];
  55. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:nothingFoundView
  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:nothingFoundView
  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. [VLCLocalNetworkServiceBrowserBonjour 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. NSInteger count = self.networkServices.count;
  103. self.nothingFoundView.hidden = count > 0;
  104. return count;
  105. }
  106. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
  107. {
  108. VLCRemoteBrowsingTVCell *browsingCell = (VLCRemoteBrowsingTVCell *) [collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  109. id<VLCLocalNetworkService> service = self.networkServices[indexPath.row];
  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 = service.serviceName;
  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. id<VLCLocalNetworkService> service = self.networkServices[indexPath.row];
  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 = [[VLCSearchableServerBrowsingTVViewController 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 = [[VLCSearchableServerBrowsingTVViewController 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<id<VLCLocalNetworkService>> *newNetworkServices = [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. id<VLCLocalNetworkService> service = [discoveryController networkServiceForIndexPath:indexPath];
  246. [newNetworkServices addObject:service];
  247. }
  248. }
  249. NSArray *oldNetworkServices = self.networkServices;
  250. GRKArrayDiff *diff = [[GRKArrayDiff alloc] initWithPreviousArray:oldNetworkServices
  251. currentArray:newNetworkServices
  252. identityBlock:^NSString * _Nullable(id <VLCLocalNetworkService> service) {
  253. return [NSString stringWithFormat:@"%@: %@", service.serviceName, service.title];
  254. }
  255. modifiedBlock:nil];
  256. [diff performBatchUpdatesWithCollectionView:self.collectionView
  257. section:0
  258. dataSourceUpdate:^{
  259. self.networkServices = newNetworkServices;
  260. } completion:nil];
  261. _nothingFoundLabel.hidden = self.discoveryController.foundAnythingAtAll;
  262. }
  263. @end