VLCServerListViewController.m 16 KB

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