VLCServerListViewController.m 13 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 "VLCSharedLibraryListViewController.h"
  23. #import "VLCDiscoveryListViewController.h"
  24. #import "VLCFTPServerListViewController.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. switch (protocol) {
  172. case VLCServerProtocolFTP:
  173. {
  174. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc]
  175. initWithFTPServer:server
  176. userName:username
  177. andPassword:password atPath:@"/"];
  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