VLCServerListViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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 "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. - (void)_startUPNPDiscovery
  169. {
  170. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  171. return;
  172. UPnPManager *managerInstance = [UPnPManager GetInstance];
  173. _UPNPdevices = [[managerInstance DB] rootDevices];
  174. if (_UPNPdevices.count > 0)
  175. [self UPnPDBUpdated:nil];
  176. [[managerInstance DB] addObserver:self];
  177. //Optional; set User Agent
  178. if (!_setup) {
  179. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLCforiOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"]] andOS:[NSString stringWithFormat:@"iOS/%@", [[UIDevice currentDevice] systemVersion]]];
  180. _setup = YES;
  181. }
  182. //Search for UPnP Devices
  183. [[managerInstance SSDP] startSSDP];
  184. [[managerInstance SSDP] notifySSDPAlive];
  185. _searchTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:1.0] interval:10.0 target:self selector:@selector(_performSSDPSearch) userInfo:nil repeats:YES];
  186. [[NSRunLoop mainRunLoop] addTimer:_searchTimer forMode:NSRunLoopCommonModes];
  187. _udnpDiscoveryRunning = YES;
  188. }
  189. - (void)_performSSDPSearch
  190. {
  191. UPnPManager *managerInstance = [UPnPManager GetInstance];
  192. [[managerInstance SSDP] searchSSDP];
  193. [[managerInstance SSDP] searchForMediaServer];
  194. [[managerInstance SSDP] performSelectorInBackground:@selector(SSDPDBUpdate) withObject:nil];
  195. }
  196. - (void)_stopUPNPDiscovery
  197. {
  198. if (_udnpDiscoveryRunning) {
  199. UPnPManager *managerInstance = [UPnPManager GetInstance];
  200. [[managerInstance SSDP] notifySSDPByeBye];
  201. [_searchTimer invalidate];
  202. _searchTimer = nil;
  203. [[managerInstance DB] removeObserver:self];
  204. [[managerInstance SSDP] stopSSDP];
  205. _udnpDiscoveryRunning = NO;
  206. }
  207. }
  208. - (IBAction)goBack:(id)sender
  209. {
  210. [self _stopUPNPDiscovery];
  211. [self _stopSAPDiscovery];
  212. [self _stopDSMDiscovery];
  213. [[VLCSidebarController sharedInstance] toggleSidebar];
  214. }
  215. - (BOOL)shouldAutorotate
  216. {
  217. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  218. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  219. return NO;
  220. return YES;
  221. }
  222. #pragma mark - table view handling
  223. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  224. {
  225. return _sectionHeaderTexts.count;
  226. }
  227. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  228. {
  229. switch (section) {
  230. case 0:
  231. return 1;
  232. case 1:
  233. return _filteredUPNPDevices.count;
  234. case 2:
  235. return _plexServices.count;
  236. case 3:
  237. return _ftpServices.count;
  238. case 4:
  239. return _httpServices.count;
  240. case 5:
  241. return _dsmDiscoverer.discoveredMedia.count;
  242. case 6:
  243. return _sapDiscoverer.discoveredMedia.count;
  244. default:
  245. return 0;
  246. }
  247. }
  248. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  249. {
  250. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  251. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  252. }
  253. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  254. {
  255. static NSString *CellIdentifier = @"LocalNetworkCell";
  256. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  257. if (cell == nil)
  258. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  259. NSUInteger row = indexPath.row;
  260. NSUInteger section = indexPath.section;
  261. [cell setIsDirectory:YES];
  262. [cell setIcon:nil];
  263. switch (section) {
  264. case 0:
  265. {
  266. [cell setTitle:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  267. [cell setIcon:[UIImage imageNamed:@"menuCone"]];
  268. break;
  269. }
  270. case 1:
  271. {
  272. UIImage *icon;
  273. if (_filteredUPNPDevices.count > row) {
  274. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  275. [cell setTitle:[device friendlyName]];
  276. icon = [device smallIcon];
  277. }
  278. [cell setIcon:icon != nil ? icon : [UIImage imageNamed:@"serverIcon"]];
  279. break;
  280. }
  281. case 2:
  282. {
  283. [cell setTitle:[_plexServices[row] name]];
  284. [cell setIcon:[UIImage imageNamed:@"PlexServerIcon"]];
  285. break;
  286. }
  287. case 3:
  288. {
  289. [cell setTitle:[_ftpServices[row] name]];
  290. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  291. break;
  292. }
  293. case 4:
  294. {
  295. [cell setTitle:[_httpServices[row] name]];
  296. [cell setIcon:[UIImage imageNamed:@"menuCone"]];
  297. break;
  298. }
  299. case 5:
  300. {
  301. [cell setTitle:[[_dsmDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  302. [cell setIcon:[UIImage imageNamed:@"serverIcon"]];
  303. break;
  304. }
  305. case 6:
  306. {
  307. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  308. [cell setIcon:[UIImage imageNamed:@"TVBroadcastIcon"]];
  309. break;
  310. }
  311. default:
  312. break;
  313. }
  314. return cell;
  315. }
  316. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  317. {
  318. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  319. NSUInteger row = indexPath.row;
  320. NSUInteger section = indexPath.section;
  321. switch (section) {
  322. case 0:
  323. {
  324. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  325. loginViewController.delegate = self;
  326. loginViewController.serverProtocol = VLCServerProtocolUndefined;
  327. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  328. navCon.navigationBarHidden = NO;
  329. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  330. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  331. [self presentViewController:navCon animated:YES completion:nil];
  332. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  333. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(dismissWithAnimation:)];
  334. } else
  335. [self.navigationController pushViewController:loginViewController animated:YES];
  336. }
  337. case 1:
  338. {
  339. if (_filteredUPNPDevices.count < row || _filteredUPNPDevices.count == 0)
  340. return;
  341. [_activityIndicator startAnimating];
  342. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  343. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  344. MediaServer1Device *server = (MediaServer1Device*)device;
  345. VLCUPnPServerListViewController *targetViewController = [[VLCUPnPServerListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  346. [self.navigationController pushViewController:targetViewController animated:YES];
  347. }
  348. break;
  349. }
  350. case 2:
  351. {
  352. NSString *name = [_PlexServicesInfo[row] objectForKey:@"name"];
  353. NSString *hostName = [_PlexServicesInfo[row] objectForKey:@"hostName"];
  354. NSString *portNum = [_PlexServicesInfo[row] objectForKey:@"port"];
  355. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:name serverAddress:hostName portNumber:portNum atPath:@"" authentification:@""];
  356. [[self navigationController] pushViewController:targetViewController animated:YES];
  357. break;
  358. }
  359. case 3:
  360. {
  361. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  362. loginViewController.delegate = self;
  363. loginViewController.serverProtocol = VLCServerProtocolFTP;
  364. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  365. navCon.navigationBarHidden = NO;
  366. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  367. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  368. [self presentViewController:navCon animated:YES completion:nil];
  369. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  370. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(dismissWithAnimation:)];
  371. } else
  372. [self.navigationController pushViewController:loginViewController animated:YES];
  373. loginViewController.hostname = [_ftpServices[row] hostName];
  374. break;
  375. }
  376. case 4:
  377. {
  378. NSString *name = [_httpServicesInfo[row] objectForKey:@"name"];
  379. NSString *hostName = [_httpServicesInfo[row] objectForKey:@"hostName"];
  380. NSString *portNum = [_httpServicesInfo[row] objectForKey:@"port"];
  381. VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc]
  382. initWithHttpServer:name
  383. serverAddress:hostName
  384. portNumber:portNum];
  385. [[self navigationController] pushViewController:targetViewController animated:YES];
  386. break;
  387. }
  388. case 5:
  389. {
  390. VLCMedia *cellMedia = [_dsmDiscoverer.discoveredMedia mediaAtIndex:row];
  391. if (cellMedia.mediaType != VLCMediaTypeDirectory)
  392. return;
  393. VLCDiscoveryListViewController *targetViewController = [[VLCDiscoveryListViewController alloc]
  394. initWithMedia:cellMedia];
  395. [[self navigationController] pushViewController:targetViewController animated:YES];
  396. break;
  397. }
  398. case 6:
  399. {
  400. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  401. VLCMedia *cellMedia = [_sapDiscoverer.discoveredMedia mediaAtIndex:row];
  402. VLCMediaType mediaType = cellMedia.mediaType;
  403. if (mediaType != VLCMediaTypeDirectory && mediaType != VLCMediaTypeDisc)
  404. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  405. break;
  406. }
  407. default:
  408. break;
  409. }
  410. }
  411. #pragma mark - Refresh
  412. -(void)handleRefresh
  413. {
  414. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  415. [_refreshControl endRefreshing];
  416. return;
  417. }
  418. UPnPManager *managerInstance = [UPnPManager GetInstance];
  419. [[managerInstance DB] removeObserver:self];
  420. [[managerInstance SSDP] stopSSDP];
  421. [self _stopDSMDiscovery];
  422. //set the title while refreshing
  423. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  424. //set the date and time of refreshing
  425. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  426. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  427. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  428. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  429. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  430. //end the refreshing
  431. [_refreshControl endRefreshing];
  432. [self.tableView reloadData];
  433. [self _startUPNPDiscovery];
  434. [self _startSAPDiscovery];
  435. [self _startDSMDiscovery];
  436. }
  437. #pragma mark - login panel protocol
  438. - (void)loginToServer:(NSString *)server
  439. port:(NSString *)port
  440. protocol:(VLCServerProtocol)protocol
  441. confirmedWithUsername:(NSString *)username
  442. andPassword:(NSString *)password
  443. {
  444. switch (protocol) {
  445. case VLCServerProtocolFTP:
  446. {
  447. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc]
  448. initWithFTPServer:[NSString stringWithFormat:@"ftp://%@", server]
  449. userName:username
  450. andPassword:password atPath:@"/"];
  451. [self.navigationController pushViewController:targetViewController animated:YES];
  452. break;
  453. }
  454. case VLCServerProtocolPLEX:
  455. {
  456. if (port == nil || port.length == 0)
  457. port = @"32400";
  458. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc]
  459. initWithPlexServer:server
  460. serverAddress:server
  461. portNumber:[NSString stringWithFormat:@":%@", port] atPath:@""
  462. authentification:@""];
  463. [[self navigationController] pushViewController:targetViewController animated:YES];
  464. break;
  465. }
  466. default:
  467. APLog(@"Unsupported URL Scheme requested %lu", protocol);
  468. break;
  469. }
  470. }
  471. #pragma mark - custom table view appearance
  472. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  473. {
  474. switch (section) {
  475. case 0:
  476. {
  477. // always hide the header of the first section
  478. return 0.;
  479. }
  480. case 1:
  481. {
  482. if (_filteredUPNPDevices.count == 0)
  483. return .0;
  484. break;
  485. }
  486. case 2:
  487. {
  488. if (_plexServices.count == 0)
  489. return .0;
  490. break;
  491. }
  492. case 3:
  493. {
  494. if (_ftpServices.count == 0)
  495. return .0;
  496. break;
  497. }
  498. case 4:
  499. {
  500. if (_httpServices.count == 0)
  501. return .0;
  502. break;
  503. }
  504. case 5:
  505. {
  506. if (_dsmDiscoverer.discoveredMedia.count == 0)
  507. return .0;
  508. break;
  509. }
  510. case 6:
  511. {
  512. if (_sapDiscoverer.discoveredMedia.count == 0)
  513. return .0;
  514. break;
  515. }
  516. default:
  517. break;
  518. }
  519. return 21.f;
  520. }
  521. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  522. {
  523. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  524. UIView *headerView = nil;
  525. if (headerText != [NSNull null]) {
  526. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  527. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  528. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  529. textLabel.text = (NSString *) headerText;
  530. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  531. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  532. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  533. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  534. textLabel.backgroundColor = [UIColor clearColor];
  535. [headerView addSubview:textLabel];
  536. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  537. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  538. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  539. [headerView addSubview:topLine];
  540. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  541. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  542. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  543. [headerView addSubview:bottomLine];
  544. }
  545. return headerView;
  546. }
  547. #pragma mark - bonjour discovery
  548. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  549. {
  550. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  551. [_rawNetServices addObject:aNetService];
  552. aNetService.delegate = self;
  553. [aNetService resolveWithTimeout:5.];
  554. }
  555. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  556. {
  557. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  558. if ([_rawNetServices containsObject:aNetService])
  559. [_rawNetServices removeObject:aNetService];
  560. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  561. [_ftpServices removeObject:aNetService];
  562. if ([aNetService.type isEqualToString:kPlexServiceType]) {
  563. [_plexServices removeObject:aNetService];
  564. [_PlexServicesInfo removeAllObjects];
  565. }
  566. if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  567. [_httpServices removeObject:aNetService];
  568. [_httpServicesInfo removeAllObjects];
  569. }
  570. if (!moreComing)
  571. [self.tableView reloadData];
  572. }
  573. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  574. {
  575. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  576. if (![_ftpServices containsObject:aNetService])
  577. [_ftpServices addObject:aNetService];
  578. } else if ([aNetService.type isEqualToString:kPlexServiceType]) {
  579. if (![_plexServices containsObject:aNetService]) {
  580. [_plexServices addObject:aNetService];
  581. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  582. [_dictService setObject:[aNetService name] forKey:@"name"];
  583. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  584. NSString *portStr = [[NSString alloc] initWithFormat:@":%ld", (long)[aNetService port]];
  585. [_dictService setObject:portStr forKey:@"port"];
  586. [_PlexServicesInfo addObject:_dictService];
  587. }
  588. } else if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  589. if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) {
  590. if (!_httpParser)
  591. _httpParser = [[VLCSharedLibraryParser alloc] init];
  592. [_httpParser checkNetserviceForVLCService:aNetService];
  593. }
  594. }
  595. [_rawNetServices removeObject:aNetService];
  596. [self.tableView reloadData];
  597. }
  598. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  599. {
  600. APLog(@"failed to resolve: %@", aNetService.name);
  601. [_rawNetServices removeObject:aNetService];
  602. }
  603. #pragma mark - shared library stuff
  604. - (void)sharedLibraryFound:(NSNotification *)aNotification
  605. {
  606. NSNetService *aNetService = [aNotification.userInfo objectForKey:@"aNetService"];
  607. if (![_httpServices containsObject:aNetService]) {
  608. [_httpServices addObject:aNetService];
  609. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  610. [_dictService setObject:[aNetService name] forKey:@"name"];
  611. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  612. NSString *portStr = [[NSString alloc] initWithFormat:@"%ld", (long)[aNetService port]];
  613. [_dictService setObject:portStr forKey:@"port"];
  614. [_httpServicesInfo addObject:_dictService];
  615. }
  616. }
  617. #pragma mark - UPNP details
  618. //protocol UPnPDBObserver
  619. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  620. {
  621. }
  622. - (void)UPnPDBUpdated:(UPnPDB*)sender
  623. {
  624. NSUInteger count = _UPNPdevices.count;
  625. BasicUPnPDevice *device;
  626. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  627. for (NSUInteger x = 0; x < count; x++) {
  628. device = _UPNPdevices[x];
  629. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  630. [mutArray addObject:device];
  631. else
  632. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  633. }
  634. _filteredUPNPDevices = nil;
  635. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  636. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  637. }
  638. #pragma mark SAP discovery
  639. - (void)_startSAPDiscovery
  640. {
  641. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  642. return;
  643. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  644. [_sapDiscoverer startDiscoverer];
  645. _sapDiscoverer.discoveredMedia.delegate = self;
  646. }
  647. - (void)_stopSAPDiscovery
  648. {
  649. [_sapDiscoverer stopDiscoverer];
  650. _sapDiscoverer = nil;
  651. }
  652. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  653. {
  654. NSLog(@"found media %@ of type %lu", media.url.lastPathComponent, media.mediaType);
  655. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  656. }
  657. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  658. {
  659. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  660. }
  661. #pragma DSM discovery
  662. - (void)_startDSMDiscovery
  663. {
  664. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  665. return;
  666. if (_dsmDiscoverer)
  667. return;
  668. _dsmDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"dsm"];
  669. [_dsmDiscoverer startDiscoverer];
  670. _dsmDiscoverer.discoveredMedia.delegate = self;
  671. }
  672. - (void)_stopDSMDiscovery
  673. {
  674. [_dsmDiscoverer stopDiscoverer];
  675. _dsmDiscoverer = nil;
  676. }
  677. @end