VLCServerListViewController.m 27 KB

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