VLCServerListViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 "VLCLocalNetworkServiceBrowserManualConnect.h"
  28. #import "VLCLocalNetworkServiceBrowserPlex.h"
  29. #import "VLCLocalNetworkServiceBrowserFTP.h"
  30. #import "VLCLocalNetworkServiceBrowserUPnP.h"
  31. #import "VLCLocalNetworkServiceBrowserHTTP.h"
  32. #import "VLCLocalNetworkServiceBrowserSAP.h"
  33. #import "VLCLocalNetworkServiceBrowserDSM.h"
  34. #import "VLCLocalNetworkServiceBrowserBonjour.h"
  35. #import "VLCWiFiUploadTableViewCell.h"
  36. #import "VLC_iOS-Swift.h"
  37. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate, VLCNetworkLoginViewControllerDelegate, VLCRemoteNetworkDataSourceDelegate>
  38. {
  39. VLCLocalServerDiscoveryController *_discoveryController;
  40. UIRefreshControl *_refreshControl;
  41. UIActivityIndicatorView *_activityIndicator;
  42. UITableView *_localNetworkTableView;
  43. UITableView *_remoteNetworkTableView;
  44. UIScrollView *_scrollView;
  45. VLCRemoteNetworkDataSourceAndDelegate *_remoteNetworkDataSourceAndDelegate;
  46. NSLayoutConstraint* _localNetworkHeight;
  47. }
  48. @end
  49. @implementation VLCServerListViewController
  50. - (void)loadView
  51. {
  52. [super loadView];
  53. _scrollView = [[UIScrollView alloc] init];
  54. _scrollView.translatesAutoresizingMaskIntoConstraints = NO;
  55. [self.view addSubview:_scrollView];
  56. [NSLayoutConstraint activateConstraints:@[
  57. [_scrollView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  58. [_scrollView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  59. [_scrollView.topAnchor constraintEqualToAnchor:self.topLayoutGuide.bottomAnchor],
  60. [_scrollView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor],
  61. ]];
  62. _remoteNetworkDataSourceAndDelegate = [VLCRemoteNetworkDataSourceAndDelegate new];
  63. _remoteNetworkDataSourceAndDelegate.delegate = self;
  64. _localNetworkTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  65. _localNetworkTableView.translatesAutoresizingMaskIntoConstraints = NO;
  66. _localNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  67. _localNetworkTableView.delegate = self;
  68. _localNetworkTableView.dataSource = self;
  69. _localNetworkTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  70. _localNetworkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  71. _localNetworkTableView.rowHeight = [VLCNetworkListCell heightOfCell];
  72. _localNetworkTableView.separatorColor = PresentationTheme.current.colors.background;
  73. _remoteNetworkTableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  74. _remoteNetworkTableView.translatesAutoresizingMaskIntoConstraints = NO;
  75. _remoteNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  76. _remoteNetworkTableView.delegate = _remoteNetworkDataSourceAndDelegate;
  77. _remoteNetworkTableView.dataSource = _remoteNetworkDataSourceAndDelegate;
  78. _remoteNetworkTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  79. _remoteNetworkTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  80. _remoteNetworkTableView.bounces = NO;
  81. [_remoteNetworkTableView registerClass:[VLCWiFiUploadTableViewCell class] forCellReuseIdentifier:[VLCWiFiUploadTableViewCell cellIdentifier]];
  82. [_remoteNetworkTableView registerClass:[VLCRemoteNetworkCell class] forCellReuseIdentifier:VLCRemoteNetworkCell.cellIdentifier];
  83. _refreshControl = [[UIRefreshControl alloc] init];
  84. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  85. _refreshControl.tintColor = [UIColor whiteColor];
  86. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  87. [_localNetworkTableView addSubview:_refreshControl];
  88. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  89. _activityIndicator.center = _localNetworkTableView.center;
  90. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  91. _activityIndicator.hidesWhenStopped = YES;
  92. [_localNetworkTableView addSubview:_activityIndicator];
  93. [_scrollView addSubview:_localNetworkTableView];
  94. [_scrollView addSubview:_remoteNetworkTableView];
  95. [_remoteNetworkTableView layoutIfNeeded];
  96. CGSize contentSize = [_remoteNetworkTableView contentSize];
  97. _localNetworkHeight = [_localNetworkTableView.heightAnchor constraintEqualToConstant:_localNetworkTableView.contentSize.height];
  98. [NSLayoutConstraint activateConstraints:@[
  99. [_remoteNetworkTableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  100. [_remoteNetworkTableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  101. [_remoteNetworkTableView.topAnchor constraintEqualToAnchor:_scrollView.topAnchor],
  102. [_remoteNetworkTableView.heightAnchor constraintEqualToConstant:contentSize.height],
  103. [_localNetworkTableView.topAnchor constraintEqualToAnchor:_remoteNetworkTableView.bottomAnchor],
  104. [_localNetworkTableView.leftAnchor constraintEqualToAnchor:self.view.leftAnchor],
  105. [_localNetworkTableView.rightAnchor constraintEqualToAnchor:self.view.rightAnchor],
  106. [_localNetworkTableView.bottomAnchor constraintEqualToAnchor:_scrollView.bottomAnchor],
  107. _localNetworkHeight
  108. ]];
  109. _scrollView.backgroundColor = PresentationTheme.current.colors.background;
  110. }
  111. - (void)viewDidLoad
  112. {
  113. [super viewDidLoad];
  114. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeDidChange) name:kVLCThemeDidChangeNotification object:nil];
  115. NSArray *browserClasses = @[
  116. [VLCLocalNetworkServiceBrowserManualConnect class],
  117. [VLCLocalNetworkServiceBrowserUPnP class],
  118. [VLCLocalNetworkServiceBrowserPlex class],
  119. [VLCLocalNetworkServiceBrowserFTP class],
  120. [VLCLocalNetworkServiceBrowserHTTP class],
  121. #ifndef NDEBUG
  122. [VLCLocalNetworkServiceBrowserSAP class],
  123. #endif
  124. [VLCLocalNetworkServiceBrowserDSM class],
  125. [VLCLocalNetworkServiceBrowserBonjour class],
  126. ];
  127. _discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:browserClasses];
  128. _discoveryController.delegate = self;
  129. }
  130. - (void)viewWillDisappear:(BOOL)animated
  131. {
  132. [super viewWillDisappear:animated];
  133. [_activityIndicator stopAnimating];
  134. [_discoveryController stopDiscovery];
  135. }
  136. - (void)viewWillAppear:(BOOL)animated
  137. {
  138. [super viewWillAppear:animated];
  139. [_discoveryController startDiscovery];
  140. }
  141. - (BOOL)shouldAutorotate
  142. {
  143. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  144. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  145. return NO;
  146. return YES;
  147. }
  148. #pragma mark - table view handling
  149. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  150. {
  151. return _discoveryController.numberOfSections;
  152. }
  153. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  154. {
  155. return [_discoveryController numberOfItemsInSection:section];
  156. }
  157. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  158. {
  159. if (tableView == _remoteNetworkTableView) return;
  160. UIColor *color = (indexPath.row % 2 == 0)? PresentationTheme.current.colors.cellBackgroundB : PresentationTheme.current.colors.cellBackgroundA;
  161. cell.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  162. cell.titleLabel.textColor = cell.folderTitleLabel.textColor = cell.subtitleLabel.textColor = cell.thumbnailView.tintColor = PresentationTheme.current.colors.cellTextColor;
  163. }
  164. - (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section
  165. {
  166. // Text Color
  167. UITableViewHeaderFooterView *header = (UITableViewHeaderFooterView *)view;
  168. header.textLabel.textColor = PresentationTheme.current.colors.sectionHeaderTextColor;
  169. header.textLabel.font = [UIFont boldSystemFontOfSize:([UIFont systemFontSize] * 0.8f)];
  170. header.tintColor = PresentationTheme.current.colors.sectionHeaderTintColor;
  171. }
  172. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. static NSString *CellIdentifier = @"LocalNetworkCell";
  175. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  176. if (cell == nil)
  177. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  178. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  179. [cell setIsDirectory:YES];
  180. [cell setIcon:service.icon];
  181. [cell setTitle:service.title];
  182. return cell;
  183. }
  184. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  185. {
  186. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  187. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  188. if ([service respondsToSelector:@selector(serverBrowser)]) {
  189. id<VLCNetworkServerBrowser> serverBrowser = [service serverBrowser];
  190. if (serverBrowser) {
  191. VLCNetworkServerBrowserViewController *vc = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  192. [self.navigationController pushViewController:vc animated:YES];
  193. return;
  194. }
  195. }
  196. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  197. NSURL *playbackURL = [service directPlaybackURL];
  198. if (playbackURL) {
  199. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  200. [medialist addMedia:[VLCMedia mediaWithURL:playbackURL]];
  201. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  202. return;
  203. }
  204. }
  205. VLCNetworkServerLoginInformation *login;
  206. if ([service respondsToSelector:@selector(loginInformation)]) {
  207. login = [service loginInformation];
  208. }
  209. [login loadLoginInformationFromKeychainWithError:nil];
  210. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  211. loginViewController.loginInformation = login;
  212. loginViewController.delegate = self;
  213. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  214. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  215. navCon.navigationBarHidden = NO;
  216. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  217. [self presentViewController:navCon animated:YES completion:nil];
  218. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  219. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:self andSelector:@selector(_dismissLogin)];
  220. } else {
  221. [self.navigationController pushViewController:loginViewController animated:YES];
  222. }
  223. }
  224. - (void)showViewController:(UIViewController *)viewController
  225. {
  226. [self.navigationController pushViewController:viewController animated:YES];
  227. }
  228. #pragma mark -
  229. - (void)themeDidChange
  230. {
  231. _localNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  232. _remoteNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  233. _scrollView.backgroundColor = PresentationTheme.current.colors.background;
  234. _localNetworkTableView.separatorColor = PresentationTheme.current.colors.background;
  235. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  236. }
  237. - (void)_dismissLogin
  238. {
  239. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  240. [self.navigationController popViewControllerAnimated:YES];
  241. else
  242. [self dismissViewControllerAnimated:YES completion:nil];
  243. }
  244. #pragma mark - Refresh
  245. - (void)handleRefresh
  246. {
  247. //set the title while refreshing
  248. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  249. //set the date and time of refreshing
  250. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  251. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  252. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  253. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  254. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  255. //end the refreshing
  256. if ([_discoveryController refreshDiscoveredData])
  257. [_localNetworkTableView reloadData];
  258. [_refreshControl endRefreshing];
  259. }
  260. #pragma mark - VLCNetworkLoginViewControllerDelegate
  261. - (void)loginWithLoginViewController:(VLCNetworkLoginViewController *)loginViewController loginInfo:(VLCNetworkServerLoginInformation *)loginInformation
  262. {
  263. id<VLCNetworkServerBrowser> serverBrowser = nil;
  264. NSString *identifier = loginInformation.protocolIdentifier;
  265. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  266. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:loginInformation];
  267. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  268. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:loginInformation];
  269. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  270. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:loginInformation];
  271. } else {
  272. APLog(@"Unsupported URL Scheme requested %@", identifier);
  273. }
  274. [self _dismissLogin];
  275. if (serverBrowser) {
  276. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  277. [self.navigationController pushViewController:targetViewController animated:YES];
  278. }
  279. }
  280. - (void)discoveryFoundSomethingNew
  281. {
  282. [_localNetworkTableView reloadData];
  283. [_localNetworkTableView layoutIfNeeded];
  284. _localNetworkHeight.constant = _localNetworkTableView.contentSize.height;
  285. }
  286. #pragma mark - custom table view appearance
  287. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  288. {
  289. // always hide the header of the first section
  290. if (section == 0)
  291. return 0.;
  292. if ([_discoveryController numberOfItemsInSection:section] == 0)
  293. return 0.;
  294. return 21.f;
  295. }
  296. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  297. {
  298. return [_discoveryController titleForSection:section];
  299. }
  300. @end