VLCLocalServerListViewController.m 27 KB

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