VLCLocalServerListViewController.m 27 KB

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