VLCServerListViewController.m 13 KB

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