VLCLocalServerListViewController.m 23 KB

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