VLCLocalServerListViewController.m 17 KB

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