VLCServerListViewController.m 31 KB

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