VLCLocalServerListViewController.m 25 KB

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