VLCServerListViewController.m 15 KB

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