VLCServerListViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. *
  13. * Refer to the COPYING file of the official project for license.
  14. *****************************************************************************/
  15. #import "VLCServerListViewController.h"
  16. #import "VLCLocalServerDiscoveryController.h"
  17. #import "VLCPlaybackController.h"
  18. #import "VLCNetworkListCell.h"
  19. #import "VLCNetworkLoginViewController.h"
  20. #import "VLCNetworkServerBrowserViewController.h"
  21. #import "VLCNetworkServerLoginInformation.h"
  22. #import "VLCNetworkServerBrowserFTP.h"
  23. #import "VLCNetworkServerBrowserVLCMedia.h"
  24. #import "VLCNetworkServerBrowserPlex.h"
  25. #import "VLCLocalNetworkServiceBrowserManualConnect.h"
  26. #import "VLCLocalNetworkServiceBrowserPlex.h"
  27. #import "VLCLocalNetworkServiceBrowserFTP.h"
  28. #import "VLCLocalNetworkServiceBrowserUPnP.h"
  29. #import "VLCLocalNetworkServiceBrowserHTTP.h"
  30. #import "VLCLocalNetworkServiceBrowserSAP.h"
  31. #import "VLCLocalNetworkServiceBrowserDSM.h"
  32. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate>
  33. {
  34. VLCLocalServerDiscoveryController *_discoveryController;
  35. UIBarButtonItem *_backToMenuButton;
  36. UIRefreshControl *_refreshControl;
  37. UIActivityIndicatorView *_activityIndicator;
  38. }
  39. @end
  40. @implementation VLCServerListViewController
  41. - (void)dealloc
  42. {
  43. [[NSNotificationCenter defaultCenter] removeObserver:self];
  44. }
  45. - (void)loadView
  46. {
  47. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  48. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  49. _tableView.delegate = self;
  50. _tableView.dataSource = self;
  51. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  52. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  53. self.view = _tableView;
  54. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  55. _activityIndicator.center = _tableView.center;
  56. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  57. _activityIndicator.hidesWhenStopped = YES;
  58. [self.view addSubview:_activityIndicator];
  59. }
  60. - (void)viewDidLoad
  61. {
  62. [super viewDidLoad];
  63. NSArray *browserClasses = @[
  64. [VLCLocalNetworkServiceBrowserManualConnect class],
  65. [VLCLocalNetworkServiceBrowserUPnP class],
  66. [VLCLocalNetworkServiceBrowserPlex class],
  67. [VLCLocalNetworkServiceBrowserFTP class],
  68. [VLCLocalNetworkServiceBrowserHTTP class],
  69. [VLCLocalNetworkServiceBrowserSAP class],
  70. [VLCLocalNetworkServiceBrowserDSM class],
  71. ];
  72. _discoveryController = [[VLCLocalServerDiscoveryController alloc] initWithServiceBrowserClasses:browserClasses];
  73. _discoveryController.delegate = self;
  74. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  75. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  76. self.tableView.rowHeight = [VLCNetworkListCell heightOfCell];
  77. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  78. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  79. self.title = NSLocalizedString(@"LOCAL_NETWORK", nil);
  80. _refreshControl = [[UIRefreshControl alloc] init];
  81. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  82. _refreshControl.tintColor = [UIColor whiteColor];
  83. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  84. [self.tableView addSubview:_refreshControl];
  85. }
  86. - (void)viewWillDisappear:(BOOL)animated
  87. {
  88. [super viewWillDisappear:animated];
  89. [_activityIndicator stopAnimating];
  90. [_discoveryController stopDiscovery];
  91. }
  92. - (void)viewWillAppear:(BOOL)animated
  93. {
  94. [super viewWillAppear:animated];
  95. [_discoveryController startDiscovery];
  96. }
  97. - (IBAction)goBack:(id)sender
  98. {
  99. [_discoveryController stopDiscovery];
  100. [[VLCSidebarController sharedInstance] toggleSidebar];
  101. }
  102. - (BOOL)shouldAutorotate
  103. {
  104. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  105. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  106. return NO;
  107. return YES;
  108. }
  109. #pragma mark - table view handling
  110. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  111. {
  112. return _discoveryController.numberOfSections;
  113. }
  114. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  115. {
  116. return [_discoveryController numberOfItemsInSection:section];
  117. }
  118. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  119. {
  120. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  121. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  122. }
  123. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  124. {
  125. static NSString *CellIdentifier = @"LocalNetworkCell";
  126. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  127. if (cell == nil)
  128. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  129. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  130. [cell setIsDirectory:YES];
  131. [cell setIcon:service.icon];
  132. [cell setTitle:service.title];
  133. return cell;
  134. }
  135. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  138. id<VLCLocalNetworkService> service = [_discoveryController networkServiceForIndexPath:indexPath];
  139. if ([service respondsToSelector:@selector(serverBrowser)]) {
  140. id<VLCNetworkServerBrowser> serverBrowser = [service serverBrowser];
  141. if (serverBrowser) {
  142. VLCNetworkServerBrowserViewController *vc = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  143. [self.navigationController pushViewController:vc animated:YES];
  144. return;
  145. }
  146. }
  147. if ([service respondsToSelector:@selector(directPlaybackURL)]) {
  148. NSURL *playbackURL = [service directPlaybackURL];
  149. if (playbackURL) {
  150. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  151. [vpc playURL:playbackURL successCallback:nil errorCallback:nil];
  152. return;
  153. }
  154. }
  155. VLCNetworkServerLoginInformation *login;
  156. if ([service respondsToSelector:@selector(loginInformation)]) {
  157. login = [service loginInformation];
  158. }
  159. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  160. VLCServerProtocol protocol = VLCServerProtocolUndefined;
  161. NSString *protocolIdentifier = login.protocolIdentifier;
  162. if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierFTP]) {
  163. protocol = VLCServerProtocolFTP;
  164. } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierPlex]) {
  165. protocol = VLCServerProtocolPLEX;
  166. } else if ([protocolIdentifier isEqualToString:VLCNetworkServerProtocolIdentifierSMB]) {
  167. protocol = VLCServerProtocolSMB;
  168. }
  169. loginViewController.serverProtocol = protocol;
  170. loginViewController.hostname = login.address;
  171. loginViewController.username = login.username;
  172. loginViewController.password = login.password;
  173. loginViewController.port = login.port.stringValue;
  174. loginViewController.delegate = self;
  175. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  176. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  177. navCon.navigationBarHidden = NO;
  178. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  179. [self presentViewController:navCon animated:YES completion:nil];
  180. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  181. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(_dismiss)];
  182. } else {
  183. [self.navigationController pushViewController:loginViewController animated:YES];
  184. }
  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 - login panel protocol
  203. - (void)loginToServer:(NSString *)server
  204. port:(NSString *)port
  205. protocol:(VLCServerProtocol)protocol
  206. confirmedWithUsername:(NSString *)username
  207. andPassword:(NSString *)password
  208. {
  209. id<VLCNetworkServerBrowser> serverBrowser = nil;
  210. switch (protocol) {
  211. case VLCServerProtocolFTP:
  212. {
  213. serverBrowser = [[VLCNetworkServerBrowserFTP alloc]initWithFTPServer:server
  214. userName:username
  215. andPassword:password atPath:@"/"];
  216. break;
  217. }
  218. case VLCServerProtocolPLEX:
  219. {
  220. NSNumber *portNumber;
  221. NSUInteger portInteger = 0;
  222. if (port.length) {
  223. portInteger = [port integerValue];
  224. }
  225. portNumber = portInteger != 0 ? @(portInteger) : @(32400);
  226. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithName:server
  227. host:server
  228. portNumber:@([port integerValue])
  229. path:@""
  230. authentificication:@""];
  231. break;
  232. }
  233. case VLCServerProtocolSMB:
  234. {
  235. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"smb://%@", server]];
  236. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithURL:url
  237. username:username
  238. password:password
  239. workgroup:nil];
  240. }
  241. default:
  242. APLog(@"Unsupported URL Scheme requested %ld", (long)protocol);
  243. break;
  244. }
  245. if (serverBrowser) {
  246. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  247. [self.navigationController pushViewController:targetViewController animated:YES];
  248. }
  249. }
  250. - (void)discoveryFoundSomethingNew
  251. {
  252. [self.tableView reloadData];
  253. }
  254. #pragma mark - custom table view appearance
  255. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  256. {
  257. // always hide the header of the first section
  258. if (section == 0)
  259. return 0.;
  260. if ([_discoveryController numberOfItemsInSection:section] == 0)
  261. return 0.;
  262. return 21.f;
  263. }
  264. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  265. {
  266. NSString *headerText = [_discoveryController titleForSection:section];
  267. UIView *headerView = nil;
  268. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  269. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  270. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  271. textLabel.text = (NSString *) headerText;
  272. textLabel.font = [UIFont boldSystemFontOfSize:([UIFont systemFontSize] * 0.8f)];
  273. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  274. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  275. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  276. textLabel.backgroundColor = [UIColor clearColor];
  277. [headerView addSubview:textLabel];
  278. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  279. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  280. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  281. [headerView addSubview:topLine];
  282. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  283. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  284. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  285. [headerView addSubview:bottomLine];
  286. return headerView;
  287. }
  288. @end