VLCServerListViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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(action)]) {
  123. service.action();
  124. }
  125. if ([service respondsToSelector:@selector(detailViewController)]) {
  126. UIViewController *controller = [service detailViewController];
  127. // TODO: refactor this out
  128. if ([controller isKindOfClass:[VLCNetworkLoginViewController class]]) {
  129. VLCNetworkLoginViewController *loginViewController = (id)controller;
  130. loginViewController.delegate = self;
  131. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  132. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  133. navCon.navigationBarHidden = NO;
  134. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  135. [self presentViewController:navCon animated:YES completion:nil];
  136. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  137. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(_dismiss)];
  138. } else
  139. [self.navigationController pushViewController:loginViewController animated:YES];
  140. }
  141. // end TODO
  142. else if (controller) {
  143. [self.navigationController pushViewController:controller animated:YES];
  144. }
  145. }
  146. }
  147. #pragma mark - Refresh
  148. -(void)handleRefresh
  149. {
  150. //set the title while refreshing
  151. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  152. //set the date and time of refreshing
  153. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  154. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  155. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  156. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  157. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  158. //end the refreshing
  159. if ([_discoveryController refreshDiscoveredData])
  160. [self.tableView reloadData];
  161. [_refreshControl endRefreshing];
  162. }
  163. #pragma mark - login panel protocol
  164. - (void)loginToServer:(NSString *)server
  165. port:(NSString *)port
  166. protocol:(VLCServerProtocol)protocol
  167. confirmedWithUsername:(NSString *)username
  168. andPassword:(NSString *)password
  169. {
  170. id<VLCNetworkServerBrowser> serverBrowser = nil;
  171. switch (protocol) {
  172. case VLCServerProtocolFTP:
  173. {
  174. serverBrowser = [[VLCNetworkServerBrowserFTP alloc]initWithFTPServer:server
  175. userName:username
  176. andPassword:password atPath:@"/"];
  177. break;
  178. }
  179. case VLCServerProtocolPLEX:
  180. {
  181. NSNumber *portNumber;
  182. NSUInteger portInteger = 0;
  183. if (port.length) {
  184. portInteger = [port integerValue];
  185. }
  186. portNumber = portInteger != 0 ? @(portInteger) : @(32400);
  187. serverBrowser = [[VLCNetworkServerBrowserPlex alloc] initWithName:server
  188. host:server
  189. portNumber:@([port integerValue])
  190. path:@""
  191. authentificication:@""];
  192. break;
  193. }
  194. case VLCServerProtocolSMB:
  195. {
  196. NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"smb://%@", server]];
  197. serverBrowser = [VLCNetworkServerBrowserVLCMedia SMBNetworkServerBrowserWithURL:url
  198. username:username
  199. password:password
  200. workgroup:nil];
  201. }
  202. default:
  203. APLog(@"Unsupported URL Scheme requested %ld", (long)protocol);
  204. break;
  205. }
  206. if (serverBrowser) {
  207. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:serverBrowser];
  208. [self.navigationController pushViewController:targetViewController animated:YES];
  209. }
  210. }
  211. - (void)discoveryFoundSomethingNew
  212. {
  213. [self.tableView reloadData];
  214. }
  215. #pragma mark - custom table view appearance
  216. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  217. {
  218. // always hide the header of the first section
  219. if (section == 0)
  220. return 0.;
  221. if ([_discoveryController numberOfItemsInSection:section] == 0)
  222. return 0.;
  223. return 21.f;
  224. }
  225. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  226. {
  227. NSString *headerText = [_discoveryController titleForSection:section];
  228. UIView *headerView = nil;
  229. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  230. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  231. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  232. textLabel.text = (NSString *) headerText;
  233. textLabel.font = [UIFont boldSystemFontOfSize:([UIFont systemFontSize] * 0.8f)];
  234. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  235. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  236. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  237. textLabel.backgroundColor = [UIColor clearColor];
  238. [headerView addSubview:textLabel];
  239. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  240. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  241. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  242. [headerView addSubview:topLine];
  243. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  244. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  245. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  246. [headerView addSubview:bottomLine];
  247. return headerView;
  248. }
  249. @end