VLCServerListViewController.m 17 KB

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