VLCLocalServerListViewController.m 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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), NSLocalizedString(@"SMB_CIFS_FILE_SERVERS", 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. _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. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  297. } else if (section == 5)
  298. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  299. return cell;
  300. }
  301. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  302. {
  303. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  304. NSUInteger row = indexPath.row;
  305. NSUInteger section = indexPath.section;
  306. if (section == 0) {
  307. if (_filteredUPNPDevices.count < row || _filteredUPNPDevices.count == 0)
  308. return;
  309. [_activityIndicator startAnimating];
  310. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  311. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  312. MediaServer1Device *server = (MediaServer1Device*)device;
  313. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  314. [self.navigationController pushViewController:targetViewController animated:YES];
  315. }
  316. } else if (section == 1) {
  317. NSInteger totalRow = [tableView numberOfRowsInSection:section];
  318. if (row == (totalRow - 1)) {
  319. VLCPlexConnectServerViewController *_connectPlexServer;
  320. _connectPlexServer = [[VLCPlexConnectServerViewController alloc] initWithNibName:@"VLCPlexConnectServerViewController" bundle:nil];
  321. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:_connectPlexServer];
  322. navCon.navigationBarHidden = NO;
  323. [self.navigationController pushViewController:_connectPlexServer animated:YES];
  324. } else {
  325. NSString *name = [_PlexServicesInfo[row] objectForKey:@"name"];
  326. NSString *hostName = [_PlexServicesInfo[row] objectForKey:@"hostName"];
  327. NSString *portNum = [_PlexServicesInfo[row] objectForKey:@"port"];
  328. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:name serverAddress:hostName portNumber:portNum atPath:@"" authentification:@""];
  329. [[self navigationController] pushViewController:targetViewController animated:YES];
  330. }
  331. } else if (section == 2) {
  332. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:_loginViewController];
  333. navCon.navigationBarHidden = NO;
  334. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  335. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  336. [self presentViewController:navCon animated:YES completion:nil];
  337. if (_loginViewController.navigationItem.leftBarButtonItem == nil)
  338. _loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:_loginViewController andSelector:@selector(dismissWithAnimation:)];
  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]
  350. initWithHttpServer:name
  351. serverAddress:hostName
  352. portNumber:portNum];
  353. [[self navigationController] pushViewController:targetViewController animated:YES];
  354. } else if (section == 4) {
  355. NSLog(@"DSM entry selected");
  356. } else if (section == 5) {
  357. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  358. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  359. }
  360. }
  361. #pragma mark - Refresh
  362. -(void)handleRefresh
  363. {
  364. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  365. [_refreshControl endRefreshing];
  366. return;
  367. }
  368. UPnPManager *managerInstance = [UPnPManager GetInstance];
  369. [[managerInstance DB] removeObserver:self];
  370. [[managerInstance SSDP] stopSSDP];
  371. [self _stopDSMDiscovery];
  372. //set the title while refreshing
  373. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  374. //set the date and time of refreshing
  375. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  376. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  377. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  378. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  379. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  380. //end the refreshing
  381. [_refreshControl endRefreshing];
  382. [self.tableView reloadData];
  383. [self _startUPNPDiscovery];
  384. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  385. [self _startDSMDiscovery];
  386. }
  387. #pragma mark - login panel protocol
  388. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  389. {
  390. if ([url.scheme isEqualToString:@"ftp"]) {
  391. if (url.host.length > 0) {
  392. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  393. [self.navigationController pushViewController:targetViewController animated:YES];
  394. }
  395. } else
  396. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  397. }
  398. #pragma mark - custom table view appearance
  399. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  400. {
  401. return 21.f;
  402. }
  403. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  404. {
  405. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  406. UIView *headerView = nil;
  407. if (headerText != [NSNull null]) {
  408. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  409. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  410. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  411. textLabel.text = (NSString *) headerText;
  412. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  413. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  414. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  415. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  416. textLabel.backgroundColor = [UIColor clearColor];
  417. [headerView addSubview:textLabel];
  418. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  419. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  420. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  421. [headerView addSubview:topLine];
  422. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  423. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  424. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  425. [headerView addSubview:bottomLine];
  426. }
  427. return headerView;
  428. }
  429. #pragma mark - bonjour discovery
  430. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  431. {
  432. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  433. [_rawNetServices addObject:aNetService];
  434. aNetService.delegate = self;
  435. [aNetService resolveWithTimeout:5.];
  436. }
  437. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  438. {
  439. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  440. if ([_rawNetServices containsObject:aNetService])
  441. [_rawNetServices removeObject:aNetService];
  442. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  443. [_ftpServices removeObject:aNetService];
  444. if ([aNetService.type isEqualToString:kPlexServiceType]) {
  445. [_PlexServices removeObject:aNetService];
  446. [_PlexServicesInfo removeAllObjects];
  447. }
  448. if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  449. [_httpServices removeObject:aNetService];
  450. [_httpServicesInfo removeAllObjects];
  451. }
  452. if (!moreComing)
  453. [self.tableView reloadData];
  454. }
  455. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  456. {
  457. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  458. if (![_ftpServices containsObject:aNetService])
  459. [_ftpServices addObject:aNetService];
  460. } else if ([aNetService.type isEqualToString:kPlexServiceType]) {
  461. if (![_PlexServices containsObject:aNetService]) {
  462. [_PlexServices addObject:aNetService];
  463. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  464. [_dictService setObject:[aNetService name] forKey:@"name"];
  465. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  466. NSString *portStr = [[NSString alloc] initWithFormat:@":%ld", (long)[aNetService port]];
  467. [_dictService setObject:portStr forKey:@"port"];
  468. [_PlexServicesInfo addObject:_dictService];
  469. }
  470. } else if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  471. if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) {
  472. [_httpParser checkNetserviceForVLCService:aNetService];
  473. }
  474. }
  475. [_rawNetServices removeObject:aNetService];
  476. [self.tableView reloadData];
  477. }
  478. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  479. {
  480. APLog(@"failed to resolve: %@", aNetService.name);
  481. [_rawNetServices removeObject:aNetService];
  482. }
  483. #pragma mark - shared library stuff
  484. - (void)sharedLibraryFound:(NSNotification *)aNotification
  485. {
  486. NSNetService *aNetService = [aNotification.userInfo objectForKey:@"aNetService"];
  487. if (![_httpServices containsObject:aNetService]) {
  488. [_httpServices addObject:aNetService];
  489. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  490. [_dictService setObject:[aNetService name] forKey:@"name"];
  491. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  492. NSString *portStr = [[NSString alloc] initWithFormat:@"%ld", (long)[aNetService port]];
  493. [_dictService setObject:portStr forKey:@"port"];
  494. [_httpServicesInfo addObject:_dictService];
  495. }
  496. }
  497. #pragma mark - UPNP details
  498. //protocol UPnPDBObserver
  499. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  500. {
  501. }
  502. - (void)UPnPDBUpdated:(UPnPDB*)sender
  503. {
  504. NSUInteger count = _UPNPdevices.count;
  505. BasicUPnPDevice *device;
  506. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  507. for (NSUInteger x = 0; x < count; x++) {
  508. device = _UPNPdevices[x];
  509. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  510. [mutArray addObject:device];
  511. else
  512. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  513. }
  514. _filteredUPNPDevices = nil;
  515. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  516. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  517. }
  518. #pragma mark SAP discovery
  519. - (void)_startSAPDiscovery
  520. {
  521. return;
  522. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  523. return;
  524. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  525. [_sapDiscoverer startDiscoverer];
  526. _sapDiscoverer.discoveredMedia.delegate = self;
  527. }
  528. - (void)_stopSAPDiscovery
  529. {
  530. return;
  531. [_sapDiscoverer stopDiscoverer];
  532. _sapDiscoverer = nil;
  533. }
  534. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  535. {
  536. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  537. }
  538. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  539. {
  540. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  541. }
  542. #pragma DSM discovery
  543. - (void)_startDSMDiscovery
  544. {
  545. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  546. return;
  547. if (_dsmDiscoverer)
  548. return;
  549. _dsmDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"dsm"];
  550. [_dsmDiscoverer startDiscoverer];
  551. _dsmDiscoverer.discoveredMedia.delegate = self;
  552. }
  553. - (void)_stopDSMDiscovery
  554. {
  555. [_dsmDiscoverer stopDiscoverer];
  556. _dsmDiscoverer = nil;
  557. }
  558. @end