VLCLocalServerListViewController.m 26 KB

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