VLCLocalServerListViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  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 "VLCLocalServerListViewController.h"
  15. #import "VLCAppDelegate.h"
  16. #import "UPnPManager.h"
  17. #import "VLCLocalNetworkListCell.h"
  18. #import "VLCLocalPlexFolderListViewController.h"
  19. #import "VLCPlexConnectServerViewController.h"
  20. #import "VLCFTPServerListViewController.h"
  21. #import "VLCUPnPServerListViewController.h"
  22. #import "VLCSharedLibraryListViewController.h"
  23. #import "VLCSharedLibraryParser.h"
  24. #import "VLCNetworkLoginViewController.h"
  25. #import "VLCHTTPUploaderController.h"
  26. #import "Reachability.h"
  27. #define kPlexServiceType @"_plexmediasvr._tcp."
  28. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate, VLCNetworkLoginViewController, NSNetServiceDelegate, VLCMediaListDelegate, UPnPDBObserver>
  29. {
  30. UIBarButtonItem *_backToMenuButton;
  31. NSArray *_sectionHeaderTexts;
  32. NSNetServiceBrowser *_ftpNetServiceBrowser;
  33. NSNetServiceBrowser *_PlexNetServiceBrowser;
  34. NSNetServiceBrowser *_httpNetServiceBrowser;
  35. NSMutableArray *_PlexServices;
  36. NSMutableArray *_PlexServicesInfo;
  37. NSMutableArray *_httpServices;
  38. NSMutableArray *_httpServicesInfo;
  39. NSMutableArray *_rawNetServices;
  40. NSMutableArray *_ftpServices;
  41. NSArray *_filteredUPNPDevices;
  42. NSArray *_UPNPdevices;
  43. VLCMediaDiscoverer *_sapDiscoverer;
  44. VLCMediaDiscoverer *_dsmDiscoverer;
  45. VLCSharedLibraryParser *_httpParser;
  46. UIRefreshControl *_refreshControl;
  47. UIActivityIndicatorView *_activityIndicator;
  48. Reachability *_reachability;
  49. NSString *_myHostName;
  50. BOOL _udnpDiscoveryRunning;
  51. NSTimer *_searchTimer;
  52. BOOL _setup;
  53. }
  54. @end
  55. @implementation VLCLocalServerListViewController
  56. - (void)dealloc
  57. {
  58. [[NSNotificationCenter defaultCenter] removeObserver:self];
  59. [_reachability stopNotifier];
  60. [_ftpNetServiceBrowser stop];
  61. [_PlexNetServiceBrowser stop];
  62. [_httpNetServiceBrowser stop];
  63. }
  64. - (void)loadView
  65. {
  66. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  67. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  68. _tableView.delegate = self;
  69. _tableView.dataSource = self;
  70. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  71. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  72. self.view = _tableView;
  73. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  74. _activityIndicator.center = _tableView.center;
  75. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  76. _activityIndicator.hidesWhenStopped = YES;
  77. [self.view addSubview:_activityIndicator];
  78. }
  79. - (void)applicationWillResignActive:(NSNotification *)notification
  80. {
  81. [self _stopUPNPDiscovery];
  82. [self _stopSAPDiscovery];
  83. [self _stopDSMDiscovery];
  84. }
  85. - (void)applicationDidBecomeActive:(NSNotification *)notification
  86. {
  87. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  88. [self _startUPNPDiscovery];
  89. [self _startSAPDiscovery];
  90. [self _startDSMDiscovery];
  91. }
  92. }
  93. - (void)viewDidLoad
  94. {
  95. [super viewDidLoad];
  96. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  97. [defaultCenter addObserver:self
  98. selector:@selector(applicationWillResignActive:)
  99. name:UIApplicationWillResignActiveNotification
  100. object:[UIApplication sharedApplication]];
  101. [defaultCenter addObserver:self
  102. selector:@selector(applicationDidBecomeActive:)
  103. name:UIApplicationDidBecomeActiveNotification
  104. object:[UIApplication sharedApplication]];
  105. [defaultCenter addObserver:self
  106. selector:@selector(sharedLibraryFound:)
  107. name:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
  108. object:nil];
  109. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPnP)", @"Plex Media Server (via Bonjour)", @"File Transfer Protocol (FTP)", NSLocalizedString(@"SHARED_VLC_IOS_LIBRARY", nil), NSLocalizedString(@"SMB_CIFS_FILE_SERVERS", nil)];
  110. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  111. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  112. self.tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  113. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  114. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  115. self.title = NSLocalizedString(@"LOCAL_NETWORK", nil);
  116. _ftpServices = [[NSMutableArray alloc] init];
  117. [_ftpServices addObject:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  118. _rawNetServices = [[NSMutableArray alloc] init];
  119. _ftpNetServiceBrowser = [[NSNetServiceBrowser alloc] init];
  120. _ftpNetServiceBrowser.delegate = self;
  121. _PlexServices = [[NSMutableArray alloc] init];
  122. _PlexServicesInfo = [[NSMutableArray alloc] init];
  123. _PlexNetServiceBrowser = [[NSNetServiceBrowser alloc] init];
  124. _PlexNetServiceBrowser.delegate = self;
  125. _httpServices = [[NSMutableArray alloc] init];
  126. _httpServicesInfo = [[NSMutableArray alloc] init];
  127. _httpNetServiceBrowser = [[NSNetServiceBrowser alloc] init];
  128. _httpNetServiceBrowser.delegate = self;
  129. _refreshControl = [[UIRefreshControl alloc] init];
  130. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  131. _refreshControl.tintColor = [UIColor whiteColor];
  132. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  133. [self.tableView addSubview:_refreshControl];
  134. _reachability = [Reachability reachabilityForLocalWiFi];
  135. [_reachability startNotifier];
  136. [self netReachabilityChanged:nil];
  137. _myHostName = [[VLCHTTPUploaderController sharedInstance] hostname];
  138. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  139. }
  140. - (void)viewWillDisappear:(BOOL)animated
  141. {
  142. [super viewWillDisappear:animated];
  143. [_activityIndicator stopAnimating];
  144. [_ftpNetServiceBrowser stop];
  145. [_PlexNetServiceBrowser stop];
  146. [_httpNetServiceBrowser stop];
  147. }
  148. - (void)viewWillAppear:(BOOL)animated
  149. {
  150. [_ftpNetServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  151. [_PlexNetServiceBrowser searchForServicesOfType:kPlexServiceType inDomain:@""];
  152. [_httpNetServiceBrowser searchForServicesOfType:@"_http._tcp." inDomain:@""];
  153. [_activityIndicator stopAnimating];
  154. [super viewWillAppear:animated];
  155. [self netReachabilityChanged:nil];
  156. }
  157. - (void)netReachabilityChanged:(NSNotification *)notification
  158. {
  159. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  160. [self _startUPNPDiscovery];
  161. [self _startSAPDiscovery];
  162. [self _startDSMDiscovery];
  163. } else {
  164. [self _stopUPNPDiscovery];
  165. [self _stopSAPDiscovery];
  166. [self _stopDSMDiscovery];
  167. }
  168. }
  169. - (void)_startUPNPDiscovery
  170. {
  171. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  172. return;
  173. UPnPManager *managerInstance = [UPnPManager GetInstance];
  174. _UPNPdevices = [[managerInstance DB] rootDevices];
  175. if (_UPNPdevices.count > 0)
  176. [self UPnPDBUpdated:nil];
  177. [[managerInstance DB] addObserver:self];
  178. //Optional; set User Agent
  179. if (!_setup) {
  180. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLCforiOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] andOS:[NSString stringWithFormat:@"iOS/%@", [[UIDevice currentDevice] systemVersion]]];
  181. _setup = YES;
  182. }
  183. //Search for UPnP Devices
  184. [[managerInstance SSDP] startSSDP];
  185. [[managerInstance SSDP] notifySSDPAlive];
  186. _searchTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0] interval:10.0 target:self selector:@selector(_performSSDPSearch) userInfo:nil repeats:YES];
  187. [[NSRunLoop mainRunLoop] addTimer:_searchTimer forMode:NSRunLoopCommonModes];
  188. _udnpDiscoveryRunning = YES;
  189. }
  190. - (void)_performSSDPSearch
  191. {
  192. UPnPManager *managerInstance = [UPnPManager GetInstance];
  193. [[managerInstance SSDP] searchSSDP];
  194. [[managerInstance SSDP] searchForMediaServer];
  195. [[managerInstance SSDP] SSDPDBUpdate];
  196. }
  197. - (void)_stopUPNPDiscovery
  198. {
  199. if (_udnpDiscoveryRunning) {
  200. UPnPManager *managerInstance = [UPnPManager GetInstance];
  201. [[managerInstance SSDP] notifySSDPByeBye];
  202. [_searchTimer invalidate];
  203. _searchTimer = nil;
  204. [[managerInstance DB] removeObserver:self];
  205. [[managerInstance SSDP] stopSSDP];
  206. _udnpDiscoveryRunning = NO;
  207. }
  208. }
  209. - (IBAction)goBack:(id)sender
  210. {
  211. [self _stopUPNPDiscovery];
  212. [self _stopSAPDiscovery];
  213. [self _stopDSMDiscovery];
  214. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  215. }
  216. - (BOOL)shouldAutorotate
  217. {
  218. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  219. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  220. return NO;
  221. return YES;
  222. }
  223. #pragma mark - table view handling
  224. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  225. {
  226. return _sectionHeaderTexts.count;
  227. }
  228. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  229. {
  230. if (section == 0)
  231. return _filteredUPNPDevices.count;
  232. else if (section == 1)
  233. return (_PlexServices.count + 1);
  234. else if (section == 2)
  235. return _ftpServices.count;
  236. else if (section == 3)
  237. return _httpServices.count;
  238. else if (section == 4)
  239. return _dsmDiscoverer.discoveredMedia.count;
  240. else if (section == 5)
  241. return _sapDiscoverer.discoveredMedia.count;
  242. return 0;
  243. }
  244. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  245. {
  246. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  247. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  248. }
  249. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  250. {
  251. static NSString *CellIdentifier = @"LocalNetworkCell";
  252. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  253. if (cell == nil)
  254. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  255. NSUInteger row = indexPath.row;
  256. NSUInteger section = indexPath.section;
  257. [cell setIsDirectory:YES];
  258. [cell setIcon:nil];
  259. if (section == 0) {
  260. UIImage *icon;
  261. if (_filteredUPNPDevices.count > row) {
  262. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  263. [cell setTitle:[device friendlyName]];
  264. icon = [device smallIcon];
  265. }
  266. [cell setIcon:icon != nil ? icon : [UIImage imageNamed:@"serverIcon"]];
  267. } else if (section == 1) {
  268. NSInteger totalRow = [tableView numberOfRowsInSection:section];
  269. if (row == (totalRow - 1))
  270. [cell setTitle:NSLocalizedString(@"PLEX_CONNECT_TO_SERVER", nil)];
  271. else {
  272. [cell setTitle:[_PlexServices[row] name]];
  273. [cell setIcon:[UIImage imageNamed:@"PlexServerIcon"]];
  274. }
  275. } else if (section == 2) {
  276. if (row == 0)
  277. [cell setTitle:_ftpServices[row]];
  278. else {
  279. [cell setTitle:[_ftpServices[row] name]];
  280. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  281. }
  282. } else if (section == 3) {
  283. [cell setTitle:[_httpServices[row] name]];
  284. [cell setIcon:[UIImage imageNamed:@"menuCone"]];
  285. } else if (section == 4) {
  286. [cell setTitle:[[_dsmDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  287. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  288. } else if (section == 5)
  289. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  290. return cell;
  291. }
  292. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  293. {
  294. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  295. NSUInteger row = indexPath.row;
  296. NSUInteger section = indexPath.section;
  297. if (section == 0) {
  298. if (_filteredUPNPDevices.count < row || _filteredUPNPDevices.count == 0)
  299. return;
  300. [_activityIndicator startAnimating];
  301. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  302. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  303. MediaServer1Device *server = (MediaServer1Device*)device;
  304. VLCUPnPServerListViewController *targetViewController = [[VLCUPnPServerListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  305. [self.navigationController pushViewController:targetViewController animated:YES];
  306. }
  307. } else if (section == 1) {
  308. NSInteger totalRow = [tableView numberOfRowsInSection:section];
  309. if (row == (totalRow - 1)) {
  310. VLCPlexConnectServerViewController *_connectPlexServer;
  311. _connectPlexServer = [[VLCPlexConnectServerViewController alloc] initWithNibName:@"VLCPlexConnectServerViewController" bundle:nil];
  312. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:_connectPlexServer];
  313. navCon.navigationBarHidden = NO;
  314. [self.navigationController pushViewController:_connectPlexServer animated:YES];
  315. } else {
  316. NSString *name = [_PlexServicesInfo[row] objectForKey:@"name"];
  317. NSString *hostName = [_PlexServicesInfo[row] objectForKey:@"hostName"];
  318. NSString *portNum = [_PlexServicesInfo[row] objectForKey:@"port"];
  319. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:name serverAddress:hostName portNumber:portNum atPath:@"" authentification:@""];
  320. [[self navigationController] pushViewController:targetViewController animated:YES];
  321. }
  322. } else if (section == 2) {
  323. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  324. loginViewController.delegate = self;
  325. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  326. navCon.navigationBarHidden = NO;
  327. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  328. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  329. [self presentViewController:navCon animated:YES completion:nil];
  330. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  331. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(dismissWithAnimation:)];
  332. } else
  333. [self.navigationController pushViewController:loginViewController animated:YES];
  334. if (row != 0 && [_ftpServices[row] hostName].length > 0) // FTP Connect To Server Special Item and hostname is long enough
  335. loginViewController.hostname = [_ftpServices[row] hostName];
  336. else
  337. loginViewController.hostname = @"";
  338. } else if (section == 3) {
  339. NSString *name = [_httpServicesInfo[row] objectForKey:@"name"];
  340. NSString *hostName = [_httpServicesInfo[row] objectForKey:@"hostName"];
  341. NSString *portNum = [_httpServicesInfo[row] objectForKey:@"port"];
  342. VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc]
  343. initWithHttpServer:name
  344. serverAddress:hostName
  345. portNumber:portNum];
  346. [[self navigationController] pushViewController:targetViewController animated:YES];
  347. } else if (section == 4) {
  348. NSLog(@"DSM entry (type %lu) selected", (unsigned long)[[_dsmDiscoverer.discoveredMedia mediaAtIndex:row] mediaType]);
  349. } else if (section == 5) {
  350. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  351. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  352. }
  353. }
  354. #pragma mark - Refresh
  355. -(void)handleRefresh
  356. {
  357. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  358. [_refreshControl endRefreshing];
  359. return;
  360. }
  361. UPnPManager *managerInstance = [UPnPManager GetInstance];
  362. [[managerInstance DB] removeObserver:self];
  363. [[managerInstance SSDP] stopSSDP];
  364. [self _stopDSMDiscovery];
  365. //set the title while refreshing
  366. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  367. //set the date and time of refreshing
  368. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  369. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  370. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  371. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  372. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  373. //end the refreshing
  374. [_refreshControl endRefreshing];
  375. [self.tableView reloadData];
  376. [self _startUPNPDiscovery];
  377. [self _startSAPDiscovery];
  378. [self _startDSMDiscovery];
  379. }
  380. #pragma mark - login panel protocol
  381. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  382. {
  383. if ([url.scheme isEqualToString:@"ftp"]) {
  384. if (url.host.length > 0) {
  385. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  386. [self.navigationController pushViewController:targetViewController animated:YES];
  387. }
  388. } else
  389. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  390. }
  391. #pragma mark - custom table view appearance
  392. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  393. {
  394. return 21.f;
  395. }
  396. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  397. {
  398. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  399. UIView *headerView = nil;
  400. if (headerText != [NSNull null]) {
  401. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  402. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  403. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  404. textLabel.text = (NSString *) headerText;
  405. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  406. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  407. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  408. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  409. textLabel.backgroundColor = [UIColor clearColor];
  410. [headerView addSubview:textLabel];
  411. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  412. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  413. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  414. [headerView addSubview:topLine];
  415. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  416. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  417. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  418. [headerView addSubview:bottomLine];
  419. }
  420. return headerView;
  421. }
  422. #pragma mark - bonjour discovery
  423. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  424. {
  425. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  426. [_rawNetServices addObject:aNetService];
  427. aNetService.delegate = self;
  428. [aNetService resolveWithTimeout:5.];
  429. }
  430. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  431. {
  432. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  433. if ([_rawNetServices containsObject:aNetService])
  434. [_rawNetServices removeObject:aNetService];
  435. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  436. [_ftpServices removeObject:aNetService];
  437. if ([aNetService.type isEqualToString:kPlexServiceType]) {
  438. [_PlexServices removeObject:aNetService];
  439. [_PlexServicesInfo removeAllObjects];
  440. }
  441. if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  442. [_httpServices removeObject:aNetService];
  443. [_httpServicesInfo removeAllObjects];
  444. }
  445. if (!moreComing)
  446. [self.tableView reloadData];
  447. }
  448. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  449. {
  450. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  451. if (![_ftpServices containsObject:aNetService])
  452. [_ftpServices addObject:aNetService];
  453. } else if ([aNetService.type isEqualToString:kPlexServiceType]) {
  454. if (![_PlexServices containsObject:aNetService]) {
  455. [_PlexServices addObject:aNetService];
  456. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  457. [_dictService setObject:[aNetService name] forKey:@"name"];
  458. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  459. NSString *portStr = [[NSString alloc] initWithFormat:@":%ld", (long)[aNetService port]];
  460. [_dictService setObject:portStr forKey:@"port"];
  461. [_PlexServicesInfo addObject:_dictService];
  462. }
  463. } else if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  464. if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) {
  465. if (!_httpParser)
  466. _httpParser = [[VLCSharedLibraryParser alloc] init];
  467. [_httpParser checkNetserviceForVLCService:aNetService];
  468. }
  469. }
  470. [_rawNetServices removeObject:aNetService];
  471. [self.tableView reloadData];
  472. }
  473. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  474. {
  475. APLog(@"failed to resolve: %@", aNetService.name);
  476. [_rawNetServices removeObject:aNetService];
  477. }
  478. #pragma mark - shared library stuff
  479. - (void)sharedLibraryFound:(NSNotification *)aNotification
  480. {
  481. NSNetService *aNetService = [aNotification.userInfo objectForKey:@"aNetService"];
  482. if (![_httpServices containsObject:aNetService]) {
  483. [_httpServices addObject:aNetService];
  484. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  485. [_dictService setObject:[aNetService name] forKey:@"name"];
  486. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  487. NSString *portStr = [[NSString alloc] initWithFormat:@"%ld", (long)[aNetService port]];
  488. [_dictService setObject:portStr forKey:@"port"];
  489. [_httpServicesInfo addObject:_dictService];
  490. }
  491. }
  492. #pragma mark - UPNP details
  493. //protocol UPnPDBObserver
  494. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  495. {
  496. }
  497. - (void)UPnPDBUpdated:(UPnPDB*)sender
  498. {
  499. NSUInteger count = _UPNPdevices.count;
  500. BasicUPnPDevice *device;
  501. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  502. for (NSUInteger x = 0; x < count; x++) {
  503. device = _UPNPdevices[x];
  504. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  505. [mutArray addObject:device];
  506. else
  507. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  508. }
  509. _filteredUPNPDevices = nil;
  510. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  511. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  512. }
  513. #pragma mark SAP discovery
  514. - (void)_startSAPDiscovery
  515. {
  516. return;
  517. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  518. return;
  519. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  520. [_sapDiscoverer startDiscoverer];
  521. _sapDiscoverer.discoveredMedia.delegate = self;
  522. }
  523. - (void)_stopSAPDiscovery
  524. {
  525. return;
  526. [_sapDiscoverer stopDiscoverer];
  527. _sapDiscoverer = nil;
  528. }
  529. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  530. {
  531. NSLog(@"found media %@ of type %lu", media.url.lastPathComponent, media.mediaType);
  532. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  533. }
  534. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  535. {
  536. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  537. }
  538. #pragma DSM discovery
  539. - (void)_startDSMDiscovery
  540. {
  541. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  542. return;
  543. if (_dsmDiscoverer)
  544. return;
  545. _dsmDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"dsm"];
  546. [_dsmDiscoverer startDiscoverer];
  547. _dsmDiscoverer.discoveredMedia.delegate = self;
  548. }
  549. - (void)_stopDSMDiscovery
  550. {
  551. [_dsmDiscoverer stopDiscoverer];
  552. _dsmDiscoverer = nil;
  553. }
  554. @end