VLCServerListViewController.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*****************************************************************************
  2. * VLCLocalServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Gleb Pinigin <gpinigin # gmail.com>
  11. * Tobias Conradi <videolan # tobias-conradi.de>
  12. * Vincent L. Cone <vincent.l.cone # tuta.io>
  13. * Carola Nitz <caro # videolan.org>
  14. *
  15. * Refer to the COPYING file of the official project for license.
  16. *****************************************************************************/
  17. #import "VLCServerListViewController.h"
  18. #import "VLCLocalServerDiscoveryController.h"
  19. #import "VLCPlaybackController.h"
  20. #import "VLCNetworkListCell.h"
  21. #import "VLCNetworkLoginViewController.h"
  22. #import "VLCNetworkServerBrowserViewController.h"
  23. #import "VLCNetworkServerLoginInformation+Keychain.h"
  24. #import "VLCNetworkServerBrowserFTP.h"
  25. #import "VLCNetworkServerBrowserVLCMedia.h"
  26. #import "VLCNetworkServerBrowserPlex.h"
  27. #import "VLCLocalNetworkServiceBrowserPlex.h"
  28. #import "VLCLocalNetworkServiceBrowserFTP.h"
  29. #import "VLCLocalNetworkServiceBrowserUPnP.h"
  30. #import "VLCLocalNetworkServiceBrowserHTTP.h"
  31. #import "VLCLocalNetworkServiceBrowserSAP.h"
  32. #import "VLCLocalNetworkServiceBrowserDSM.h"
  33. #import "VLCLocalNetworkServiceBrowserBonjour.h"
  34. #import "VLCWiFiUploadTableViewCell.h"
  35. #import "VLC-Swift.h"
  36. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate, VLCNetworkLoginViewControllerDelegate, VLCRemoteNetworkDataSourceDelegate, VLCFileServerViewDelegate>
  37. {
  38. VLCLocalServerDiscoveryController *_discoveryController;
  39. UIRefreshControl *_refreshControl;
  40. UIActivityIndicatorView *_activityIndicator;
  41. UITableView *_localNetworkTableView;
  42. UITableView *_remoteNetworkTableView;
  43. UIScrollView *_scrollView;
  44. VLCRemoteNetworkDataSourceAndDelegate *_remoteNetworkDataSourceAndDelegate;
  45. NSLayoutConstraint* _localNetworkHeight;
  46. NSLayoutConstraint* _remoteNetworkHeight;
  47. }
  48. @end
  49. @implementation VLCServerListViewController
  50. - (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  51. {
  52. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  53. if (self) {
  54. [self setupUI];
  55. }
  56. return self;
  57. }
  58. - (void)loadView
  59. {
  60. [super loadView];
  61. _scrollView = [[UIScrollView alloc] init];
  62. _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
  63. [self.view addSubview:_scrollView];
  64. [NSLayoutConstraint activateConstraints:@[
  65. [_scrollView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  66. [_scrollView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  67. [_scrollView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor],
  68. [_scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
  69. ]];
  70. _remoteNetworkDataSourceAndDelegate = [VLCRemoteNetworkDataSourceAndDelegate new];
  71. _remoteNetworkDataSourceAndDelegate.delegate = self;
  72. _localNetworkTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  73. _localNetworkTableView.translatesAutoresizingMaskIntoConstraints = NO;
  74. _localNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  75. _localNetworkTableView.delegate = self;
  76. _localNetworkTableView.dataSource = self;
  77. _localNetworkTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  78. _localNetworkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  79. _localNetworkTableView.rowHeight = [VLCNetworkListCell heightOfCell];
  80. _localNetworkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  81. _localNetworkTableView.estimatedRowHeight = [VLCNetworkListCell heightOfCell];
  82. [self.navigationController.navigationBar setTranslucent:NO];
  83. self.navigationController.view.backgroundColor = PresentationTheme.current.colors.background;
  84. _remoteNetworkTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  85. _remoteNetworkTableView.translatesAutoresizingMaskIntoConstraints = NO;
  86. _remoteNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  87. _remoteNetworkTableView.delegate = _remoteNetworkDataSourceAndDelegate;
  88. _remoteNetworkTableView.dataSource = _remoteNetworkDataSourceAndDelegate;
  89. _remoteNetworkTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  90. _remoteNetworkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  91. _remoteNetworkTableView.bounces = NO;
  92. VLCFileServerView *fileServerView = [VLCFileServerView new];
  93. fileServerView.translatesAutoresizingMaskIntoConstraints = NO;
  94. fileServerView.delegate = self;
  95. [_remoteNetworkTableView registerClass:[VLCWiFiUploadTableViewCell class] forCellReuseIdentifier:[VLCWiFiUploadTableViewCell cellIdentifier]];
  96. [_remoteNetworkTableView registerClass:[VLCRemoteNetworkCell class] forCellReuseIdentifier:VLCRemoteNetworkCell.cellIdentifier];
  97. _refreshControl = [[UIRefreshControl alloc] init];
  98. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  99. _refreshControl.tintColor = [UIColor whiteColor];
  100. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  101. [_localNetworkTableView addSubview:_refreshControl];
  102. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  103. _activityIndicator.center = _localNetworkTableView.center;
  104. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  105. _activityIndicator.hidesWhenStopped = YES;
  106. [_localNetworkTableView addSubview:_activityIndicator];
  107. [_scrollView addSubview:_localNetworkTableView];
  108. [_scrollView addSubview:fileServerView];
  109. [_scrollView addSubview:_remoteNetworkTableView];
  110. [_remoteNetworkTableView layoutIfNeeded];
  111. _localNetworkHeight = [_localNetworkTableView.heightAnchor constraintEqualToConstant:_localNetworkTableView.contentSize.height];
  112. _remoteNetworkHeight = [_remoteNetworkTableView.heightAnchor constraintEqualToConstant:_remoteNetworkTableView.contentSize.height];
  113. [NSLayoutConstraint activateConstraints:@[
  114. [_remoteNetworkTableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  115. [_remoteNetworkTableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  116. [_remoteNetworkTableView.topAnchor constraintEqualToAnchor:_scrollView.topAnchor],
  117. [fileServerView.topAnchor constraintEqualToAnchor:_remoteNetworkTableView.bottomAnchor],
  118. [fileServerView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  119. [fileServerView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  120. [_localNetworkTableView.topAnchor constraintEqualToAnchor:fileServerView.bottomAnchor],
  121. [_localNetworkTableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  122. [_localNetworkTableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  123. [_localNetworkTableView.bottomAnchor constraintEqualToAnchor:_scrollView.bottomAnchor],
  124. _localNetworkHeight,
  125. _remoteNetworkHeight
  126. ]];
  127. _scrollView.backgroundColor = PresentationTheme.current.colors.background;
  128. }
  129. - (void)setupUI
  130. {
  131. self.title = NSLocalizedString(@"NETWORK", nil);
  132. self.tabBarItem = [[UITabBarItem alloc] initWithTitle: NSLocalizedString(@"NETWORK", nil)
  133. image: [UIImage imageNamed:@"Network"]
  134. selectedImage: [UIImage imageNamed:@"Network"]];
  135. self.tabBarItem.accessibilityIdentifier = VLCAccessibilityIdentifier.localNetwork;
  136. }
  137. - (void)viewDidLoad
  138. {
  139. [super viewDidLoad];
  140. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChange) name:kVLCThemeDidChangeNotification object:nil];
  141. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(contentSizeDidChange) name:UIContentSizeCategoryDidChangeNotification object:nil];
  142. NSArray *browserClasses = @[
  143. [VLCLocalNetworkServiceBrowserUPnP class],
  144. [VLCLocalNetworkServiceBrowserPlex class],
  145. [VLCLocalNetworkServiceBrowserFTP class],
  146. [VLCLocalNetworkServiceBrowserHTTP class],
  147. #ifndef NDEBUG
  148. [VLCLocalNetworkServiceBrowserSAP class],
  149. #endif
  150. [VLCLocalNetworkServiceBrowserDSM class],
  151. [VLCLocalNetworkServiceBrowserBonjour class],
  152. ];
  153. _discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:browserClasses];
  154. _discoveryController.delegate = self;
  155. }
  156. - (void)viewWillDisappear:(BOOL)animated
  157. {
  158. [super viewWillDisappear:animated];
  159. [_activityIndicator stopAnimating];
  160. [_discoveryController stopDiscovery];
  161. }
  162. - (void)viewWillAppear:(BOOL)animated
  163. {
  164. [super viewWillAppear:animated];
  165. [_discoveryController startDiscovery];
  166. if (@available(iOS 11.0, *)) {
  167. self.navigationController.navigationBar.prefersLargeTitles = YES;
  168. }
  169. }
  170. - (BOOL)shouldAutorotate
  171. {
  172. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  173. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  174. return NO;
  175. return YES;
  176. }
  177. - (void)contentSizeDidChange
  178. {
  179. [_localNetworkTableView layoutIfNeeded];
  180. _localNetworkHeight.constant = _localNetworkTableView.contentSize.height;
  181. [_remoteNetworkTableView layoutIfNeeded];
  182. _remoteNetworkHeight.constant = _remoteNetworkTableView.contentSize.height;
  183. }
  184. - (void)connectToServer
  185. {
  186. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  187. loginViewController.loginInformation = [[VLCNetworkServerLoginInformation alloc] init];;
  188. loginViewController.delegate = self;
  189. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  190. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  191. [navCon.navigationBar setTranslucent:NO];
  192. [self presentViewController:navCon animated:YES completion:nil];
  193. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  194. loginViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) style:UIBarButtonItemStylePlain target:self action:@selector(_dismissLogin)];
  195. }
  196. #pragma mark - table view handling
  197. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  198. {
  199. return _discoveryController.numberOfSections;
  200. }
  201. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  202. {
  203. return [_discoveryController numberOfItemsInSection:section];
  204. }
  205. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  206. {
  207. UIColor *color = PresentationTheme.current.colors.cellBackgroundA;
  208. cell.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  209. cell.titleLabel.textColor = cell.folderTitleLabel.textColor = cell.thumbnailView.tintColor = PresentationTheme.current.colors.cellTextColor;
  210. cell.subtitleLabel.textColor = PresentationTheme.current.colors.cellDetailTextColor;
  211. }
  212. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  213. {
  214. static NSString *CellIdentifier = @"LocalNetworkCell";
  215. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  216. if (cell == nil)
  217. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  218. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  219. [cell setIsDirectory:YES];
  220. [cell setIcon:service.icon];
  221. [cell setTitle:service.title];
  222. [cell setTitleLabelCentered:NO];
  223. [cell setSubtitle:service.serviceName];
  224. return cell;
  225. }
  226. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  227. {
  228. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  229. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  230. if ([service respondsToSelector:@selector(serverBrowser)]) {
  231. id<VLCNetworkServerBrowser> serverBrowser = [service serverBrowser];
  232. if (serverBrowser) {
  233. VLCNetworkServerBrowserViewController *vc = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  234. [self.navigationController pushViewController:vc animated:YES];
  235. return;
  236. }
  237. }
  238. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  239. NSURL *playbackURL = [service directPlaybackURL];
  240. if (playbackURL) {
  241. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  242. [medialist addMedia:[VLCMedia mediaWithURL:playbackURL]];
  243. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  244. return;
  245. }
  246. }
  247. VLCNetworkServerLoginInformation *login;
  248. if ([service respondsToSelector:@selector(loginInformation)]) {
  249. login = [service loginInformation];
  250. }
  251. [login loadLoginInformationFromKeychainWithError:nil];
  252. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  253. loginViewController.loginInformation = login;
  254. loginViewController.delegate = self;
  255. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  256. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  257. navCon.navigationBarHidden = NO;
  258. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  259. [self presentViewController:navCon animated:YES completion:nil];
  260. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  261. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) target:self andSelector:@selector(_dismissLogin)];
  262. } else {
  263. [self.navigationController pushViewController:loginViewController animated:YES];
  264. }
  265. }
  266. - (void)showViewController:(UIViewController *)viewController
  267. {
  268. [self.navigationController pushViewController:viewController animated:YES];
  269. }
  270. #pragma mark -
  271. - (void)themeDidChange
  272. {
  273. _localNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  274. _remoteNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  275. _scrollView.backgroundColor = PresentationTheme.current.colors.background;
  276. _localNetworkTableView.separatorColor = PresentationTheme.current.colors.background;
  277. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  278. self.navigationController.view.backgroundColor = PresentationTheme.current.colors.background;
  279. [self setNeedsStatusBarAppearanceUpdate];
  280. }
  281. - (void)_dismissLogin
  282. {
  283. if ([self.navigationController presentedViewController]) {
  284. [self dismissViewControllerAnimated:YES completion:nil];
  285. } else {
  286. [self.navigationController popViewControllerAnimated:YES];
  287. }
  288. }
  289. - (UIStatusBarStyle)preferredStatusBarStyle
  290. {
  291. return PresentationTheme.current.colors.statusBarStyle;
  292. }
  293. #pragma mark - Refresh
  294. - (void)handleRefresh
  295. {
  296. //set the title while refreshing
  297. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  298. //set the date and time of refreshing
  299. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  300. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  301. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  302. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  303. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  304. //end the refreshing
  305. if ([_discoveryController refreshDiscoveredData])
  306. [_localNetworkTableView reloadData];
  307. [_refreshControl endRefreshing];
  308. }
  309. #pragma mark - VLCNetworkLoginViewControllerDelegate
  310. - (void)loginWithLoginViewController:(VLCNetworkLoginViewController *)loginViewController loginInfo:(VLCNetworkServerLoginInformation *)loginInformation
  311. {
  312. id<VLCNetworkServerBrowser> serverBrowser = nil;
  313. NSString *identifier = loginInformation.protocolIdentifier;
  314. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  315. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:loginInformation];
  316. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  317. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:loginInformation];
  318. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  319. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:loginInformation];
  320. } else {
  321. APLog(@"Unsupported URL Scheme requested %@", identifier);
  322. }
  323. [self _dismissLogin];
  324. if (serverBrowser) {
  325. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  326. [self.navigationController pushViewController:targetViewController animated:YES];
  327. }
  328. }
  329. - (void)discoveryFoundSomethingNew
  330. {
  331. [_localNetworkTableView reloadData];
  332. [_localNetworkTableView layoutIfNeeded];
  333. _localNetworkHeight.constant = _localNetworkTableView.contentSize.height;
  334. }
  335. @end