VLCServerListViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  156. [medialist addMedia:[VLCMedia mediaWithURL:playbackURL]];
  157. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:nil];
  158. return;
  159. }
  160. }
  161. VLCNetworkServerLoginInformation *login;
  162. if ([service respondsToSelector:@selector(loginInformation)]) {
  163. login = [service loginInformation];
  164. }
  165. [login loadLoginInformationFromKeychainWithError:nil];
  166. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  167. loginViewController.loginInformation = login;
  168. loginViewController.delegate = self;
  169. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  170. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginViewController];
  171. navCon.navigationBarHidden = NO;
  172. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  173. [self presentViewController:navCon animated:YES completion:nil];
  174. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  175. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:self andSelector:@selector(_dismissLogin)];
  176. } else {
  177. [self.navigationController pushViewController:loginViewController animated:YES];
  178. }
  179. }
  180. - (void)_dismissLogin
  181. {
  182. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  183. [self.navigationController popViewControllerAnimated:YES];
  184. else
  185. [self dismissViewControllerAnimated:YES completion:nil];
  186. }
  187. #pragma mark - Refresh
  188. -(void)handleRefresh
  189. {
  190. //set the title while refreshing
  191. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  192. //set the date and time of refreshing
  193. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  194. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  195. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  196. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  197. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  198. //end the refreshing
  199. if ([_discoveryController refreshDiscoveredData])
  200. [self.tableView reloadData];
  201. [_refreshControl endRefreshing];
  202. }
  203. #pragma mark - VLCNetworkLoginViewControllerDelegate
  204. - (void)loginWithLoginViewController:(VLCNetworkLoginViewController *)loginViewController loginInfo:(VLCNetworkServerLoginInformation *)loginInformation
  205. {
  206. id<VLCNetworkServerBrowser> serverBrowser = nil;
  207. NSString *identifier = loginInformation.protocolIdentifier;
  208. if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  209. serverBrowser = [[VLCNetworkServerBrowserFTP alloc] initWithLogin:loginInformation];
  210. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  211. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithLogin:loginInformation];
  212. } else if ([identifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  213. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithLogin:loginInformation];
  214. } else {
  215. APLog(@"Unsupported URL Scheme requested %@", identifier);
  216. }
  217. [self _dismissLogin];
  218. if (serverBrowser) {
  219. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  220. [self.navigationController pushViewController:targetViewController animated:YES];
  221. }
  222. }
  223. - (void)discoveryFoundSomethingNew
  224. {
  225. [self.tableView reloadData];
  226. }
  227. #pragma mark - custom table view appearance
  228. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  229. {
  230. // always hide the header of the first section
  231. if (section == 0)
  232. return 0.;
  233. if ([_discoveryController numberOfItemsInSection:section] == 0)
  234. return 0.;
  235. return 21.f;
  236. }
  237. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  238. {
  239. NSString *headerText = [_discoveryController titleForSection:section];
  240. UIView *headerView = nil;
  241. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  242. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  243. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  244. textLabel.text = (NSString *) headerText;
  245. textLabel.font = [UIFont boldSystemFontOfSize:([UIFont systemFontSize] * 0.8f)];
  246. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  247. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  248. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  249. textLabel.backgroundColor = [UIColor clearColor];
  250. [headerView addSubview:textLabel];
  251. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  252. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  253. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  254. [headerView addSubview:topLine];
  255. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  256. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  257. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  258. [headerView addSubview:bottomLine];
  259. return headerView;
  260. }
  261. @end