VLCServerListViewController.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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_iOS-Swift.h"
  36. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate, VLCNetworkLoginViewControllerDelegate, VLCRemoteNetworkDataSourceDelegate, VLCFileServerSectionTableHeaderViewDelegate>
  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. - (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.separatorStyle = UITableViewCellSeparatorStyleNone;
  73. [_localNetworkTableView registerClass:[VLCFileServerSectionTableHeaderView class] forHeaderFooterViewReuseIdentifier:VLCFileServerSectionTableHeaderView.identifier];
  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. [VLCLocalNetworkServiceBrowserUPnP class],
  119. [VLCLocalNetworkServiceBrowserPlex class],
  120. [VLCLocalNetworkServiceBrowserFTP class],
  121. [VLCLocalNetworkServiceBrowserHTTP class],
  122. #ifndef NDEBUG
  123. [VLCLocalNetworkServiceBrowserSAP class],
  124. #endif
  125. [VLCLocalNetworkServiceBrowserDSM class],
  126. [VLCLocalNetworkServiceBrowserBonjour class],
  127. ];
  128. _discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:browserClasses];
  129. _discoveryController.delegate = self;
  130. }
  131. - (void)viewWillDisappear:(BOOL)animated
  132. {
  133. [super viewWillDisappear:animated];
  134. [_activityIndicator stopAnimating];
  135. [_discoveryController stopDiscovery];
  136. }
  137. - (void)viewWillAppear:(BOOL)animated
  138. {
  139. [super viewWillAppear:animated];
  140. [_discoveryController startDiscovery];
  141. }
  142. - (BOOL)shouldAutorotate
  143. {
  144. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  145. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  146. return NO;
  147. return YES;
  148. }
  149. - (void)contentSizeDidChange
  150. {
  151. [_localNetworkTableView layoutIfNeeded];
  152. _localNetworkHeight.constant = _localNetworkTableView.contentSize.height;
  153. [_remoteNetworkTableView layoutIfNeeded];
  154. _remoteNetworkHeight.constant = _remoteNetworkTableView.contentSize.height;
  155. }
  156. - (void)connectToServer
  157. {
  158. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  159. loginViewController.loginInformation = [[VLCNetworkServerLoginInformation alloc] init];;
  160. loginViewController.delegate = self;
  161. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  162. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  163. [self presentViewController:navCon animated:YES completion:nil];
  164. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  165. loginViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) style:UIBarButtonItemStyleDone target:self action:@selector(_dismissLogin)];
  166. }
  167. #pragma mark - table view handling
  168. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  169. {
  170. return _discoveryController.numberOfSections;
  171. }
  172. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  173. {
  174. return [_discoveryController numberOfItemsInSection:section];
  175. }
  176. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  177. {
  178. if (section == 0) {
  179. VLCFileServerSectionTableHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:VLCFileServerSectionTableHeaderView.identifier];
  180. headerView.delegate = self;
  181. return headerView;
  182. }
  183. return nil;
  184. }
  185. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  186. {
  187. UIColor *color = (indexPath.row % 2 == 0)? PresentationTheme.current.colors.cellBackgroundB : PresentationTheme.current.colors.cellBackgroundA;
  188. cell.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  189. cell.titleLabel.textColor = cell.folderTitleLabel.textColor = cell.subtitleLabel.textColor = cell.thumbnailView.tintColor = PresentationTheme.current.colors.cellTextColor;
  190. }
  191. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  192. {
  193. static NSString *CellIdentifier = @"LocalNetworkCell";
  194. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  195. if (cell == nil)
  196. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  197. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  198. [cell setIsDirectory:YES];
  199. [cell setIcon:service.icon];
  200. [cell setTitle:service.title];
  201. return cell;
  202. }
  203. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  204. {
  205. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  206. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  207. if ([service respondsToSelector:@selector(serverBrowser)]) {
  208. id<VLCNetworkServerBrowser> serverBrowser = [service serverBrowser];
  209. if (serverBrowser) {
  210. VLCNetworkServerBrowserViewController *vc = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  211. [self.navigationController pushViewController:vc animated:YES];
  212. return;
  213. }
  214. }
  215. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  216. NSURL *playbackURL = [service directPlaybackURL];
  217. if (playbackURL) {
  218. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  219. [medialist addMedia:[VLCMedia mediaWithURL:playbackURL]];
  220. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  221. return;
  222. }
  223. }
  224. VLCNetworkServerLoginInformation *login;
  225. if ([service respondsToSelector:@selector(loginInformation)]) {
  226. login = [service loginInformation];
  227. }
  228. [login loadLoginInformationFromKeychainWithError:nil];
  229. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  230. loginViewController.loginInformation = login;
  231. loginViewController.delegate = self;
  232. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  233. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  234. navCon.navigationBarHidden = NO;
  235. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  236. [self presentViewController:navCon animated:YES completion:nil];
  237. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  238. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:self andSelector:@selector(_dismissLogin)];
  239. } else {
  240. [self.navigationController pushViewController:loginViewController animated:YES];
  241. }
  242. }
  243. - (void)showViewController:(UIViewController *)viewController
  244. {
  245. [self.navigationController pushViewController:viewController animated:YES];
  246. }
  247. #pragma mark -
  248. - (void)themeDidChange
  249. {
  250. _localNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  251. _remoteNetworkTableView.backgroundColor = PresentationTheme.current.colors.background;
  252. _scrollView.backgroundColor = PresentationTheme.current.colors.background;
  253. _localNetworkTableView.separatorColor = PresentationTheme.current.colors.background;
  254. _refreshControl.backgroundColor = PresentationTheme.current.colors.background;
  255. [self setNeedsStatusBarAppearanceUpdate];
  256. }
  257. - (void)_dismissLogin
  258. {
  259. if ([self.navigationController presentedViewController]) {
  260. [self dismissViewControllerAnimated:YES completion:nil];
  261. } else {
  262. [self.navigationController popViewControllerAnimated:YES];
  263. }
  264. }
  265. - (UIStatusBarStyle)preferredStatusBarStyle
  266. {
  267. return PresentationTheme.current.colors.statusBarStyle;
  268. }
  269. #pragma mark - Refresh
  270. - (void)handleRefresh
  271. {
  272. //set the title while refreshing
  273. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  274. //set the date and time of refreshing
  275. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  276. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  277. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  278. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  279. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  280. //end the refreshing
  281. if ([_discoveryController refreshDiscoveredData])
  282. [_localNetworkTableView reloadData];
  283. [_refreshControl endRefreshing];
  284. }
  285. #pragma mark - VLCNetworkLoginViewControllerDelegate
  286. - (void)loginWithLoginViewController:(VLCNetworkLoginViewController *)loginViewController loginInfo:(VLCNetworkServerLoginInformation *)loginInformation
  287. {
  288. id<VLCNetworkServerBrowser> serverBrowser = nil;
  289. NSString *identifier = loginInformation.protocolIdentifier;
  290. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  291. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:loginInformation];
  292. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  293. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:loginInformation];
  294. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  295. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:loginInformation];
  296. } else {
  297. APLog(@"Unsupported URL Scheme requested %@", identifier);
  298. }
  299. [self _dismissLogin];
  300. if (serverBrowser) {
  301. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  302. [self.navigationController pushViewController:targetViewController animated:YES];
  303. }
  304. }
  305. - (void)discoveryFoundSomethingNew
  306. {
  307. [_localNetworkTableView reloadData];
  308. [_localNetworkTableView layoutIfNeeded];
  309. _localNetworkHeight.constant = _localNetworkTableView.contentSize.height;
  310. }
  311. #pragma mark - custom table view appearance
  312. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  313. {
  314. if (section == 0)
  315. return 56.;
  316. return 0.;
  317. }
  318. @end