VLCServerListViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*****************************************************************************
  2. * VLCLocalServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 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. *
  14. * Refer to the COPYING file of the official project for license.
  15. *****************************************************************************/
  16. #import "VLCServerListViewController.h"
  17. #import "VLCLocalServerDiscoveryController.h"
  18. #import "VLCPlaybackController.h"
  19. #import "VLCNetworkListCell.h"
  20. #import "VLCNetworkLoginViewController.h"
  21. #import "VLCNetworkServerBrowserViewController.h"
  22. #import "VLCNetworkServerLoginInformation+Keychain.h"
  23. #import "VLCNetworkServerBrowserFTP.h"
  24. #import "VLCNetworkServerBrowserVLCMedia.h"
  25. #import "VLCNetworkServerBrowserPlex.h"
  26. #import "VLCLocalNetworkServiceBrowserManualConnect.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. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate, VLCNetworkLoginViewControllerDelegate>
  35. {
  36. VLCLocalServerDiscoveryController *_discoveryController;
  37. UIBarButtonItem *_backToMenuButton;
  38. UIRefreshControl *_refreshControl;
  39. UIActivityIndicatorView *_activityIndicator;
  40. }
  41. @end
  42. @implementation VLCServerListViewController
  43. - (void)dealloc
  44. {
  45. [[NSNotificationCenter defaultCenter] removeObserver:self];
  46. }
  47. - (void)loadView
  48. {
  49. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  50. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  51. _tableView.delegate = self;
  52. _tableView.dataSource = self;
  53. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  54. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  55. self.view = _tableView;
  56. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  57. _activityIndicator.center = _tableView.center;
  58. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  59. _activityIndicator.hidesWhenStopped = YES;
  60. [self.view addSubview:_activityIndicator];
  61. }
  62. - (void)viewDidLoad
  63. {
  64. [super viewDidLoad];
  65. NSArray *browserClasses = @[
  66. [VLCLocalNetworkServiceBrowserManualConnect class],
  67. [VLCLocalNetworkServiceBrowserUPnP class],
  68. [VLCLocalNetworkServiceBrowserPlex class],
  69. [VLCLocalNetworkServiceBrowserFTP class],
  70. [VLCLocalNetworkServiceBrowserHTTP class],
  71. #ifndef NDEBUG
  72. [VLCLocalNetworkServiceBrowserSAP class],
  73. #endif
  74. [VLCLocalNetworkServiceBrowserDSM class],
  75. [VLCLocalNetworkServiceBrowserBonjour class],
  76. ];
  77. _discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:browserClasses];
  78. _discoveryController.delegate = self;
  79. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  80. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  81. self.tableView.rowHeight = [VLCNetworkListCell heightOfCell];
  82. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  83. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  84. self.title = NSLocalizedString(@"LOCAL_NETWORK", nil);
  85. _refreshControl = [[UIRefreshControl alloc] init];
  86. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  87. _refreshControl.tintColor = [UIColor whiteColor];
  88. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  89. [self.tableView addSubview:_refreshControl];
  90. }
  91. - (void)viewWillDisappear:(BOOL)animated
  92. {
  93. [super viewWillDisappear:animated];
  94. [_activityIndicator stopAnimating];
  95. [_discoveryController stopDiscovery];
  96. }
  97. - (void)viewWillAppear:(BOOL)animated
  98. {
  99. [super viewWillAppear:animated];
  100. [_discoveryController startDiscovery];
  101. }
  102. - (IBAction)goBack:(id)sender
  103. {
  104. [_discoveryController stopDiscovery];
  105. [[VLCSidebarController sharedInstance] toggleSidebar];
  106. }
  107. - (BOOL)shouldAutorotate
  108. {
  109. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  110. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  111. return NO;
  112. return YES;
  113. }
  114. #pragma mark - table view handling
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  116. {
  117. return _discoveryController.numberOfSections;
  118. }
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  120. {
  121. return [_discoveryController numberOfItemsInSection:section];
  122. }
  123. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  126. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  127. }
  128. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  129. {
  130. static NSString *CellIdentifier = @"LocalNetworkCell";
  131. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  132. if (cell == nil)
  133. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  134. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  135. [cell setIsDirectory:YES];
  136. [cell setIcon:service.icon];
  137. [cell setTitle:service.title];
  138. return cell;
  139. }
  140. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  143. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  144. if ([service respondsToSelector:@selector(serverBrowser)]) {
  145. id<VLCNetworkServerBrowser> serverBrowser = [service serverBrowser];
  146. if (serverBrowser) {
  147. VLCNetworkServerBrowserViewController *vc = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  148. [self.navigationController pushViewController:vc animated:YES];
  149. return;
  150. }
  151. }
  152. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  153. NSURL *playbackURL = [service directPlaybackURL];
  154. if (playbackURL) {
  155. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  156. [vpc playURL:playbackURL successCallback:nil errorCallback:nil];
  157. return;
  158. }
  159. }
  160. VLCNetworkServerLoginInformation *login;
  161. if ([service respondsToSelector:@selector(loginInformation)]) {
  162. login = [service loginInformation];
  163. }
  164. [login loadLoginInformationFromKeychainWithError:nil];
  165. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  166. loginViewController.loginInformation = login;
  167. loginViewController.delegate = self;
  168. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  169. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  170. navCon.navigationBarHidden = NO;
  171. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  172. [self presentViewController:navCon animated:YES completion:nil];
  173. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  174. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:self andSelector:@selector(_dismissLogin)];
  175. } else {
  176. [self.navigationController pushViewController:loginViewController animated:YES];
  177. }
  178. }
  179. - (void)_dismissLogin
  180. {
  181. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  182. [self.navigationController popViewControllerAnimated:YES];
  183. else
  184. [self dismissViewControllerAnimated:YES completion:nil];
  185. }
  186. #pragma mark - Refresh
  187. -(void)handleRefresh
  188. {
  189. //set the title while refreshing
  190. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  191. //set the date and time of refreshing
  192. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  193. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  194. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  195. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  196. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  197. //end the refreshing
  198. if ([_discoveryController refreshDiscoveredData])
  199. [self.tableView reloadData];
  200. [_refreshControl endRefreshing];
  201. }
  202. #pragma mark - VLCNetworkLoginViewControllerDelegate
  203. - (void)loginWithLoginViewController:(VLCNetworkLoginViewController *)loginViewController loginInfo:(VLCNetworkServerLoginInformation *)loginInformation
  204. {
  205. id<VLCNetworkServerBrowser> serverBrowser = nil;
  206. NSString *identifier = loginInformation.protocolIdentifier;
  207. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  208. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:loginInformation];
  209. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  210. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:loginInformation];
  211. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  212. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:loginInformation];
  213. } else {
  214. APLog(@"Unsupported URL Scheme requested %@", identifier);
  215. }
  216. [self _dismissLogin];
  217. if (serverBrowser) {
  218. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  219. [self.navigationController pushViewController:targetViewController animated:YES];
  220. }
  221. }
  222. - (void)discoveryFoundSomethingNew
  223. {
  224. [self.tableView reloadData];
  225. }
  226. #pragma mark - custom table view appearance
  227. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  228. {
  229. // always hide the header of the first section
  230. if (section == 0)
  231. return 0.;
  232. if ([_discoveryController numberOfItemsInSection:section] == 0)
  233. return 0.;
  234. return 21.f;
  235. }
  236. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  237. {
  238. NSString *headerText = [_discoveryController titleForSection:section];
  239. UIView *headerView = nil;
  240. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  241. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  242. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  243. textLabel.text = (NSString *) headerText;
  244. textLabel.font = [UIFont boldSystemFontOfSize:([UIFont systemFontSize] * 0.8f)];
  245. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  246. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  247. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  248. textLabel.backgroundColor = [UIColor clearColor];
  249. [headerView addSubview:textLabel];
  250. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  251. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  252. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  253. [headerView addSubview:topLine];
  254. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  255. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  256. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  257. [headerView addSubview:bottomLine];
  258. return headerView;
  259. }
  260. @end