VLCServerListViewController.m 13 KB

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