VLCServerListViewController.m 13 KB

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