VLCLocalServerListViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /*****************************************************************************
  2. * VLCLocalServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 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 "VLCLocalServerListViewController.h"
  15. #import "UIBarButtonItem+Theme.h"
  16. #import "VLCAppDelegate.h"
  17. #import "UPnPManager.h"
  18. #import "VLCLocalNetworkListCell.h"
  19. #import "VLCLocalServerFolderListViewController.h"
  20. #import <QuartzCore/QuartzCore.h>
  21. #import "GHRevealViewController.h"
  22. #import "VLCNetworkLoginViewController.h"
  23. #import "UINavigationController+Theme.h"
  24. #import "VLCPlaylistViewController.h"
  25. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate, VLCNetworkLoginViewController, NSNetServiceDelegate, VLCMediaListDelegate>
  26. {
  27. UIBarButtonItem *_backToMenuButton;
  28. NSArray *_sectionHeaderTexts;
  29. NSNetServiceBrowser *_ftpNetServiceBrowser;
  30. NSMutableArray *_rawNetServices;
  31. NSMutableArray *_ftpServices;
  32. NSArray *_filteredUPNPDevices;
  33. NSArray *_UPNPdevices;
  34. VLCMediaDiscoverer * _sapDiscoverer;
  35. VLCNetworkLoginViewController *_loginViewController;
  36. UIRefreshControl *_refreshControl;
  37. UIActivityIndicatorView *_activityIndicator;
  38. }
  39. @end
  40. @implementation VLCLocalServerListViewController
  41. - (void)dealloc
  42. {
  43. [_ftpNetServiceBrowser stop];
  44. }
  45. - (void)loadView
  46. {
  47. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  48. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  49. _tableView.delegate = self;
  50. _tableView.dataSource = self;
  51. self.view = _tableView;
  52. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  53. _activityIndicator.center = _tableView.center;
  54. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  55. _activityIndicator.hidesWhenStopped = YES;
  56. [self.view addSubview:_activityIndicator];
  57. }
  58. - (void)viewDidLoad
  59. {
  60. [super viewDidLoad];
  61. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)", @"Network Streams (SAP)"];
  62. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  63. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  64. self.tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  65. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  66. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  67. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  68. _ftpServices = [[NSMutableArray alloc] init];
  69. [_ftpServices addObject:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  70. _rawNetServices = [[NSMutableArray alloc] init];
  71. _ftpNetServiceBrowser = [[NSNetServiceBrowser alloc] init];
  72. _ftpNetServiceBrowser.delegate = self;
  73. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  74. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  75. _refreshControl = [[UIRefreshControl alloc] init];
  76. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  77. [self.tableView addSubview:_refreshControl];
  78. if (SYSTEM_RUNS_IOS7_OR_LATER)
  79. _loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCFutureNetworkLoginViewController" bundle:nil];
  80. else
  81. _loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  82. _loginViewController.delegate = self;
  83. }
  84. - (void)viewWillDisappear:(BOOL)animated
  85. {
  86. [super viewWillDisappear:animated];
  87. [_activityIndicator stopAnimating];
  88. [_ftpNetServiceBrowser stop];
  89. }
  90. - (void)viewWillAppear:(BOOL)animated
  91. {
  92. [_activityIndicator stopAnimating];
  93. [super viewWillAppear:animated];
  94. [self _triggerNetServiceBrowser];
  95. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  96. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  97. }
  98. - (void)_triggerNetServiceBrowser
  99. {
  100. [_ftpNetServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  101. }
  102. - (void)_startUPNPDiscovery
  103. {
  104. UPnPManager *managerInstance = [UPnPManager GetInstance];
  105. _UPNPdevices = [[managerInstance DB] rootDevices];
  106. if (_UPNPdevices.count > 0)
  107. [self UPnPDBUpdated:nil];
  108. [[managerInstance DB] addObserver:(UPnPDBObserver*)self];
  109. //Optional; set User Agent
  110. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  111. //Search for UPnP Devices
  112. [[managerInstance SSDP] startSSDP];
  113. [[managerInstance SSDP] searchSSDP];
  114. [[managerInstance SSDP] SSDPDBUpdate];
  115. }
  116. - (IBAction)goBack:(id)sender
  117. {
  118. UPnPManager *managerInstance = [UPnPManager GetInstance];
  119. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  120. [[managerInstance SSDP] stopSSDP];
  121. [self _stopSAPDiscovery];
  122. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  123. }
  124. - (BOOL)shouldAutorotate
  125. {
  126. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  127. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  128. return NO;
  129. return YES;
  130. }
  131. #pragma mark - table view handling
  132. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  133. {
  134. return _sectionHeaderTexts.count;
  135. }
  136. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  137. {
  138. if (section == 0)
  139. return _filteredUPNPDevices.count;
  140. else if (section == 1)
  141. return _ftpServices.count;
  142. else if (section == 2)
  143. return _sapDiscoverer.discoveredMedia.count;
  144. return 0;
  145. }
  146. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  149. cell.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  150. }
  151. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  152. {
  153. static NSString *CellIdentifier = @"LocalNetworkCell";
  154. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  155. if (cell == nil)
  156. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  157. NSUInteger row = indexPath.row;
  158. NSUInteger section = indexPath.section;
  159. [cell setIsDirectory:YES];
  160. [cell setIcon:nil];
  161. if (section == 0) {
  162. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  163. [cell setTitle:[device friendlyName]];
  164. UIImage *icon = [device smallIcon];
  165. [cell setIcon:icon != nil ? icon : [UIImage imageNamed:@"serverIcon"]];
  166. } else if (section == 1) {
  167. if (row == 0)
  168. [cell setTitle:_ftpServices[row]];
  169. else {
  170. [cell setTitle:[_ftpServices[row] name]];
  171. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  172. }
  173. } else if (section == 2)
  174. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  175. return cell;
  176. }
  177. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  180. NSUInteger row = indexPath.row;
  181. NSUInteger section = indexPath.section;
  182. if (section == 0) {
  183. [_activityIndicator startAnimating];
  184. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  185. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  186. MediaServer1Device *server = (MediaServer1Device*)device;
  187. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  188. [self.navigationController pushViewController:targetViewController animated:YES];
  189. }
  190. } else if (section == 1) {
  191. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_loginViewController];
  192. [navCon loadTheme];
  193. navCon.navigationBarHidden = NO;
  194. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  195. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  196. [self presentViewController:navCon animated:YES completion:nil];
  197. if (_loginViewController.navigationItem.leftBarButtonItem == nil) {
  198. UIBarButtonItem *doneButton = [UIBarButtonItem themedDoneButtonWithTarget:_loginViewController andSelector:@selector(dismissWithAnimation:)];
  199. _loginViewController.navigationItem.leftBarButtonItem = doneButton;
  200. }
  201. } else
  202. [self.navigationController pushViewController:_loginViewController animated:YES];
  203. if (row != 0 && [_ftpServices[row] hostName].length > 0) // FTP Connect To Server Special Item and hostname is long enough
  204. _loginViewController.hostname = [_ftpServices[row] hostName];
  205. else
  206. _loginViewController.hostname = @"";
  207. } else if (section == 2) {
  208. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  209. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  210. }
  211. }
  212. #pragma mark - Refresh
  213. -(void)handleRefresh
  214. {
  215. UPnPManager *managerInstance = [UPnPManager GetInstance];
  216. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  217. [[managerInstance SSDP] stopSSDP];
  218. //set the title while refreshing
  219. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  220. //set the date and time of refreshing
  221. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  222. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  223. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  224. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
  225. //end the refreshing
  226. [_refreshControl endRefreshing];
  227. [self.tableView reloadData];
  228. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  229. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  230. }
  231. #pragma mark - login panel protocol
  232. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  233. {
  234. if ([url.scheme isEqualToString:@"ftp"]) {
  235. if (url.host.length > 0) {
  236. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  237. [self.navigationController pushViewController:targetViewController animated:YES];
  238. }
  239. } else
  240. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  241. }
  242. #pragma mark - custom table view appearance
  243. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  244. return 21.f;
  245. }
  246. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  247. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  248. UIView *headerView = nil;
  249. if (headerText != [NSNull null]) {
  250. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  251. CAGradientLayer *gradient = [CAGradientLayer layer];
  252. gradient.frame = headerView.bounds;
  253. gradient.colors = @[
  254. (id)[UIColor colorWithRed:(66.0f/255.0f) green:(66.0f/255.0f) blue:(66.0f/255.0f) alpha:1.0f].CGColor,
  255. (id)[UIColor colorWithRed:(56.0f/255.0f) green:(56.0f/255.0f) blue:(56.0f/255.0f) alpha:1.0f].CGColor,
  256. ];
  257. [headerView.layer insertSublayer:gradient atIndex:0];
  258. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  259. textLabel.text = (NSString *) headerText;
  260. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  261. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  262. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  263. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  264. textLabel.backgroundColor = [UIColor clearColor];
  265. [headerView addSubview:textLabel];
  266. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  267. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  268. [headerView addSubview:topLine];
  269. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  270. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  271. [headerView addSubview:bottomLine];
  272. }
  273. return headerView;
  274. }
  275. #pragma mark - bonjour discovery
  276. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  277. {
  278. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  279. [_rawNetServices addObject:aNetService];
  280. aNetService.delegate = self;
  281. [aNetService resolveWithTimeout:5.];
  282. }
  283. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  284. {
  285. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  286. if ([_rawNetServices containsObject:aNetService])
  287. [_rawNetServices removeObject:aNetService];
  288. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  289. [_ftpServices removeObject:aNetService];
  290. if (!moreComing)
  291. [self.tableView reloadData];
  292. }
  293. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  294. {
  295. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  296. if (![_ftpServices containsObject:aNetService])
  297. [_ftpServices addObject:aNetService];
  298. }
  299. [_rawNetServices removeObject:aNetService];
  300. [self.tableView reloadData];
  301. }
  302. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  303. {
  304. APLog(@"failed to resolve: %@", aNetService.name);
  305. [_rawNetServices removeObject:aNetService];
  306. }
  307. #pragma mark - UPNP details
  308. //protocol UPnPDBObserver
  309. - (void)UPnPDBWillUpdate:(UPnPDB*)sender{
  310. }
  311. - (void)UPnPDBUpdated:(UPnPDB*)sender{
  312. NSUInteger count = _UPNPdevices.count;
  313. BasicUPnPDevice *device;
  314. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  315. for (NSUInteger x = 0; x < count; x++) {
  316. device = _UPNPdevices[x];
  317. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  318. [mutArray addObject:device];
  319. else
  320. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  321. }
  322. _filteredUPNPDevices = nil;
  323. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  324. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  325. }
  326. #pragma mark SAP discovery
  327. - (void)_startSAPDiscovery
  328. {
  329. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  330. _sapDiscoverer.discoveredMedia.delegate = self;
  331. }
  332. - (void)_stopSAPDiscovery
  333. {
  334. _sapDiscoverer = nil;
  335. }
  336. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  337. {
  338. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  339. }
  340. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  341. {
  342. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  343. }
  344. @end