VLCServerListViewController.m 12 KB

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