VLCServerListViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCServerListViewController.h"
  15. #import "VLCLocalServerDiscoveryController.h"
  16. #import "VLCPlaybackController.h"
  17. #import "VLCNetworkListCell.h"
  18. #import "VLCNetworkLoginViewController.h"
  19. #import "VLCUPnPServerListViewController.h"
  20. #import "VLCLocalPlexFolderListViewController.h"
  21. #import "VLCSharedLibraryListViewController.h"
  22. #import "VLCDiscoveryListViewController.h"
  23. #import "VLCFTPServerListViewController.h"
  24. @interface VLCServerListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalServerDiscoveryControllerDelegate>
  25. {
  26. VLCLocalServerDiscoveryController *_discoveryController;
  27. UIBarButtonItem *_backToMenuButton;
  28. NSArray *_sectionHeaderTexts;
  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. _sectionHeaderTexts = _discoveryController.sectionHeaderTexts;
  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 _sectionHeaderTexts.count;
  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. [cell setIsDirectory:YES];
  115. [cell setIcon:[_discoveryController iconForIndexPath:indexPath]];
  116. [cell setTitle:[_discoveryController titleForIndexPath:indexPath]];
  117. return cell;
  118. }
  119. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  122. NSUInteger section = indexPath.section;
  123. switch (section) {
  124. case 0:
  125. {
  126. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  127. loginViewController.delegate = self;
  128. loginViewController.serverProtocol = VLCServerProtocolUndefined;
  129. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  130. navCon.navigationBarHidden = NO;
  131. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  132. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  133. [self presentViewController:navCon animated:YES completion:nil];
  134. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  135. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(_dismiss)];
  136. } else
  137. [self.navigationController pushViewController:loginViewController animated:YES];
  138. break;
  139. }
  140. case 1:
  141. {
  142. [_activityIndicator startAnimating];
  143. BasicUPnPDevice *device = [_discoveryController upnpDeviceForIndexPath:indexPath];
  144. if (device != nil) {
  145. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  146. MediaServer1Device *server = (MediaServer1Device*)device;
  147. VLCUPnPServerListViewController *targetViewController = [[VLCUPnPServerListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  148. [self.navigationController pushViewController:targetViewController animated:YES];
  149. }
  150. }
  151. break;
  152. }
  153. case 2:
  154. {
  155. NSDictionary *serviceDescription = [_discoveryController plexServiceDescriptionForIndexPath:indexPath];
  156. if (serviceDescription != nil) {
  157. NSString *name = serviceDescription[@"name"];
  158. NSString *hostName = serviceDescription[@"hostName"];
  159. NSString *portNum = serviceDescription[@"port"];
  160. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:name serverAddress:hostName portNumber:portNum atPath:@"" authentification:@""];
  161. [[self navigationController] pushViewController:targetViewController animated:YES];
  162. }
  163. break;
  164. }
  165. case 3:
  166. {
  167. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  168. loginViewController.delegate = self;
  169. loginViewController.serverProtocol = VLCServerProtocolFTP;
  170. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  171. navCon.navigationBarHidden = NO;
  172. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  173. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  174. [self presentViewController:navCon animated:YES completion:nil];
  175. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  176. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(_dismiss)];
  177. } else
  178. [self.navigationController pushViewController:loginViewController animated:YES];
  179. loginViewController.hostname = [_discoveryController ftpHostnameForIndexPath:indexPath];
  180. break;
  181. }
  182. case 4:
  183. {
  184. NSDictionary *serviceDescription = [_discoveryController httpServiceDescriptionForIndexPath:indexPath];
  185. if (serviceDescription != nil) {
  186. NSString *name = serviceDescription[@"name"];
  187. NSString *hostName = serviceDescription[@"hostName"];
  188. NSString *portNum = serviceDescription[@"port"];
  189. VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc]
  190. initWithHttpServer:name
  191. serverAddress:hostName
  192. portNumber:portNum];
  193. [[self navigationController] pushViewController:targetViewController animated:YES];
  194. }
  195. break;
  196. }
  197. case 5:
  198. {
  199. VLCMedia *cellMedia = [_discoveryController dsmDiscoveryForIndexPath:indexPath];
  200. if (cellMedia.mediaType != VLCMediaTypeDirectory)
  201. return;
  202. NSDictionary *mediaOptions = @{@"smb-user" : @"",
  203. @"smb-pwd" : @"",
  204. @"smb-domain" : @""};
  205. [cellMedia addOptions:mediaOptions];
  206. VLCDiscoveryListViewController *targetViewController = [[VLCDiscoveryListViewController alloc]
  207. initWithMedia:cellMedia
  208. options:mediaOptions];
  209. [[self navigationController] pushViewController:targetViewController animated:YES];
  210. break;
  211. }
  212. case 6:
  213. {
  214. VLCMedia *cellMedia = [_discoveryController sapDiscoveryForIndexPath:indexPath];
  215. VLCMediaType mediaType = cellMedia.mediaType;
  216. if (mediaType != VLCMediaTypeDirectory && mediaType != VLCMediaTypeDisc) {
  217. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  218. [vpc playURL:[cellMedia url] successCallback:nil errorCallback:nil];
  219. }
  220. break;
  221. }
  222. default:
  223. break;
  224. }
  225. }
  226. #pragma mark - Refresh
  227. -(void)handleRefresh
  228. {
  229. //set the title while refreshing
  230. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  231. //set the date and time of refreshing
  232. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  233. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  234. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  235. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  236. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  237. //end the refreshing
  238. if ([_discoveryController refreshDiscoveredData])
  239. [self.tableView reloadData];
  240. [_refreshControl endRefreshing];
  241. }
  242. #pragma mark - login panel protocol
  243. - (void)loginToServer:(NSString *)server
  244. port:(NSString *)port
  245. protocol:(VLCServerProtocol)protocol
  246. confirmedWithUsername:(NSString *)username
  247. andPassword:(NSString *)password
  248. {
  249. switch (protocol) {
  250. case VLCServerProtocolFTP:
  251. {
  252. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc]
  253. initWithFTPServer:server
  254. userName:username
  255. andPassword:password atPath:@"/"];
  256. [self.navigationController pushViewController:targetViewController animated:YES];
  257. break;
  258. }
  259. case VLCServerProtocolPLEX:
  260. {
  261. if (port == nil || port.length == 0)
  262. port = @"32400";
  263. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc]
  264. initWithPlexServer:server
  265. serverAddress:server
  266. portNumber:[NSString stringWithFormat:@":%@", port] atPath:@""
  267. authentification:username];
  268. [[self navigationController] pushViewController:targetViewController animated:YES];
  269. break;
  270. }
  271. case VLCServerProtocolSMB:
  272. {
  273. VLCMedia *media = [VLCMedia mediaWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"smb://%@", server]]];
  274. NSDictionary *mediaOptions = @{@"smb-user" : username ? username : @"",
  275. @"smb-pwd" : password ? password : @"",
  276. @"smb-domain" : @"WORKGROUP"};
  277. [media addOptions:mediaOptions];
  278. VLCDiscoveryListViewController *targetViewController = [[VLCDiscoveryListViewController alloc]
  279. initWithMedia:media
  280. options:mediaOptions];
  281. [[self navigationController] pushViewController:targetViewController animated:YES];
  282. }
  283. default:
  284. APLog(@"Unsupported URL Scheme requested %ld", (long)protocol);
  285. break;
  286. }
  287. }
  288. - (void)discoveryFoundSomethingNew
  289. {
  290. [self.tableView reloadData];
  291. }
  292. #pragma mark - custom table view appearance
  293. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  294. {
  295. // always hide the header of the first section
  296. if (section == 0)
  297. return 0.;
  298. if ([_discoveryController numberOfItemsInSection:section] == 0)
  299. return 0.;
  300. return 21.f;
  301. }
  302. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  303. {
  304. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  305. UIView *headerView = nil;
  306. if (headerText != [NSNull null]) {
  307. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  308. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  309. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  310. textLabel.text = (NSString *) headerText;
  311. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  312. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  313. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  314. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  315. textLabel.backgroundColor = [UIColor clearColor];
  316. [headerView addSubview:textLabel];
  317. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  318. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  319. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  320. [headerView addSubview:topLine];
  321. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  322. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  323. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  324. [headerView addSubview:bottomLine];
  325. }
  326. return headerView;
  327. }
  328. @end