VLCLocalServerListViewController.m 16 KB

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