VLCServerListViewController.m 14 KB

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