VLCLocalServerListViewController.m 27 KB

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