VLCServerListViewController.m 28 KB

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