VLCLocalServerListViewController.m 16 KB

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