VLCServerListViewController.m 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  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. break;
  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. NSString *name = [_PlexServicesInfo[row] objectForKey:@"name"];
  354. NSString *hostName = [_PlexServicesInfo[row] objectForKey:@"hostName"];
  355. NSString *portNum = [_PlexServicesInfo[row] objectForKey:@"port"];
  356. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:name serverAddress:hostName portNumber:portNum atPath:@"" authentification:@""];
  357. [[self navigationController] pushViewController:targetViewController animated:YES];
  358. break;
  359. }
  360. case 3:
  361. {
  362. VLCNetworkLoginViewController *loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:@"VLCNetworkLoginViewController" bundle:nil];
  363. loginViewController.delegate = self;
  364. loginViewController.serverProtocol = VLCServerProtocolFTP;
  365. UINavigationController *navCon = [[VLCNavigationController alloc] initWithRootViewController:loginViewController];
  366. navCon.navigationBarHidden = NO;
  367. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  368. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  369. [self presentViewController:navCon animated:YES completion:nil];
  370. if (loginViewController.navigationItem.leftBarButtonItem == nil)
  371. loginViewController.navigationItem.leftBarButtonItem = [UIBarButtonItem themedDarkToolbarButtonWithTitle:NSLocalizedString(@"BUTTON_DONE", nil) target:loginViewController andSelector:@selector(dismissWithAnimation:)];
  372. } else
  373. [self.navigationController pushViewController:loginViewController animated:YES];
  374. loginViewController.hostname = [_ftpServices[row] hostName];
  375. break;
  376. }
  377. case 4:
  378. {
  379. NSString *name = [_httpServicesInfo[row] objectForKey:@"name"];
  380. NSString *hostName = [_httpServicesInfo[row] objectForKey:@"hostName"];
  381. NSString *portNum = [_httpServicesInfo[row] objectForKey:@"port"];
  382. VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc]
  383. initWithHttpServer:name
  384. serverAddress:hostName
  385. portNumber:portNum];
  386. [[self navigationController] pushViewController:targetViewController animated:YES];
  387. break;
  388. }
  389. case 5:
  390. {
  391. VLCMedia *cellMedia = [_dsmDiscoverer.discoveredMedia mediaAtIndex:row];
  392. if (cellMedia.mediaType != VLCMediaTypeDirectory)
  393. return;
  394. VLCDiscoveryListViewController *targetViewController = [[VLCDiscoveryListViewController alloc]
  395. initWithMedia:cellMedia];
  396. [[self navigationController] pushViewController:targetViewController animated:YES];
  397. break;
  398. }
  399. case 6:
  400. {
  401. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  402. VLCMedia *cellMedia = [_sapDiscoverer.discoveredMedia mediaAtIndex:row];
  403. VLCMediaType mediaType = cellMedia.mediaType;
  404. if (mediaType != VLCMediaTypeDirectory && mediaType != VLCMediaTypeDisc)
  405. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  406. break;
  407. }
  408. default:
  409. break;
  410. }
  411. }
  412. #pragma mark - Refresh
  413. -(void)handleRefresh
  414. {
  415. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  416. [_refreshControl endRefreshing];
  417. return;
  418. }
  419. UPnPManager *managerInstance = [UPnPManager GetInstance];
  420. [[managerInstance DB] removeObserver:self];
  421. [[managerInstance SSDP] stopSSDP];
  422. [self _stopDSMDiscovery];
  423. //set the title while refreshing
  424. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  425. //set the date and time of refreshing
  426. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  427. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  428. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  429. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  430. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  431. //end the refreshing
  432. [_refreshControl endRefreshing];
  433. [self.tableView reloadData];
  434. [self _startUPNPDiscovery];
  435. [self _startSAPDiscovery];
  436. [self _startDSMDiscovery];
  437. }
  438. #pragma mark - login panel protocol
  439. - (void)loginToServer:(NSString *)server
  440. port:(NSString *)port
  441. protocol:(VLCServerProtocol)protocol
  442. confirmedWithUsername:(NSString *)username
  443. andPassword:(NSString *)password
  444. {
  445. switch (protocol) {
  446. case VLCServerProtocolFTP:
  447. {
  448. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc]
  449. initWithFTPServer:[NSString stringWithFormat:@"ftp://%@", server]
  450. userName:username
  451. andPassword:password atPath:@"/"];
  452. [self.navigationController pushViewController:targetViewController animated:YES];
  453. break;
  454. }
  455. case VLCServerProtocolPLEX:
  456. {
  457. if (port == nil || port.length == 0)
  458. port = @"32400";
  459. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc]
  460. initWithPlexServer:server
  461. serverAddress:server
  462. portNumber:[NSString stringWithFormat:@":%@", port] atPath:@""
  463. authentification:username];
  464. [[self navigationController] pushViewController:targetViewController animated:YES];
  465. break;
  466. }
  467. default:
  468. APLog(@"Unsupported URL Scheme requested %lu", protocol);
  469. break;
  470. }
  471. }
  472. #pragma mark - custom table view appearance
  473. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  474. {
  475. switch (section) {
  476. case 0:
  477. {
  478. // always hide the header of the first section
  479. return 0.;
  480. }
  481. case 1:
  482. {
  483. if (_filteredUPNPDevices.count == 0)
  484. return .0;
  485. break;
  486. }
  487. case 2:
  488. {
  489. if (_plexServices.count == 0)
  490. return .0;
  491. break;
  492. }
  493. case 3:
  494. {
  495. if (_ftpServices.count == 0)
  496. return .0;
  497. break;
  498. }
  499. case 4:
  500. {
  501. if (_httpServices.count == 0)
  502. return .0;
  503. break;
  504. }
  505. case 5:
  506. {
  507. if (_dsmDiscoverer.discoveredMedia.count == 0)
  508. return .0;
  509. break;
  510. }
  511. case 6:
  512. {
  513. if (_sapDiscoverer.discoveredMedia.count == 0)
  514. return .0;
  515. break;
  516. }
  517. default:
  518. break;
  519. }
  520. return 21.f;
  521. }
  522. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  523. {
  524. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], nil);
  525. UIView *headerView = nil;
  526. if (headerText != [NSNull null]) {
  527. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  528. headerView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  529. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 0.f)];
  530. textLabel.text = (NSString *) headerText;
  531. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  532. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  533. textLabel.shadowColor = [UIColor VLCDarkTextShadowColor];
  534. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  535. textLabel.backgroundColor = [UIColor clearColor];
  536. [headerView addSubview:textLabel];
  537. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  538. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  539. topLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  540. [headerView addSubview:topLine];
  541. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  542. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  543. bottomLine.autoresizingMask = UIViewAutoresizingFlexibleWidth;
  544. [headerView addSubview:bottomLine];
  545. }
  546. return headerView;
  547. }
  548. #pragma mark - bonjour discovery
  549. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  550. {
  551. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  552. [_rawNetServices addObject:aNetService];
  553. aNetService.delegate = self;
  554. [aNetService resolveWithTimeout:5.];
  555. }
  556. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  557. {
  558. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  559. if ([_rawNetServices containsObject:aNetService])
  560. [_rawNetServices removeObject:aNetService];
  561. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  562. [_ftpServices removeObject:aNetService];
  563. if ([aNetService.type isEqualToString:kPlexServiceType]) {
  564. [_plexServices removeObject:aNetService];
  565. [_PlexServicesInfo removeAllObjects];
  566. }
  567. if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  568. [_httpServices removeObject:aNetService];
  569. [_httpServicesInfo removeAllObjects];
  570. }
  571. if (!moreComing)
  572. [self.tableView reloadData];
  573. }
  574. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  575. {
  576. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  577. if (![_ftpServices containsObject:aNetService])
  578. [_ftpServices addObject:aNetService];
  579. } else if ([aNetService.type isEqualToString:kPlexServiceType]) {
  580. if (![_plexServices containsObject:aNetService]) {
  581. [_plexServices addObject:aNetService];
  582. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  583. [_dictService setObject:[aNetService name] forKey:@"name"];
  584. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  585. NSString *portStr = [[NSString alloc] initWithFormat:@":%ld", (long)[aNetService port]];
  586. [_dictService setObject:portStr forKey:@"port"];
  587. [_PlexServicesInfo addObject:_dictService];
  588. }
  589. } else if ([aNetService.type isEqualToString:@"_http._tcp."]) {
  590. if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) {
  591. if (!_httpParser)
  592. _httpParser = [[VLCSharedLibraryParser alloc] init];
  593. [_httpParser checkNetserviceForVLCService:aNetService];
  594. }
  595. }
  596. [_rawNetServices removeObject:aNetService];
  597. [self.tableView reloadData];
  598. }
  599. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  600. {
  601. APLog(@"failed to resolve: %@", aNetService.name);
  602. [_rawNetServices removeObject:aNetService];
  603. }
  604. #pragma mark - shared library stuff
  605. - (void)sharedLibraryFound:(NSNotification *)aNotification
  606. {
  607. NSNetService *aNetService = [aNotification.userInfo objectForKey:@"aNetService"];
  608. if (![_httpServices containsObject:aNetService]) {
  609. [_httpServices addObject:aNetService];
  610. NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
  611. [_dictService setObject:[aNetService name] forKey:@"name"];
  612. [_dictService setObject:[aNetService hostName] forKey:@"hostName"];
  613. NSString *portStr = [[NSString alloc] initWithFormat:@"%ld", (long)[aNetService port]];
  614. [_dictService setObject:portStr forKey:@"port"];
  615. [_httpServicesInfo addObject:_dictService];
  616. }
  617. }
  618. #pragma mark - UPNP details
  619. //protocol UPnPDBObserver
  620. - (void)UPnPDBWillUpdate:(UPnPDB*)sender
  621. {
  622. }
  623. - (void)UPnPDBUpdated:(UPnPDB*)sender
  624. {
  625. NSUInteger count = _UPNPdevices.count;
  626. BasicUPnPDevice *device;
  627. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  628. for (NSUInteger x = 0; x < count; x++) {
  629. device = _UPNPdevices[x];
  630. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  631. [mutArray addObject:device];
  632. else
  633. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  634. }
  635. _filteredUPNPDevices = nil;
  636. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  637. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  638. }
  639. #pragma mark SAP discovery
  640. - (void)_startSAPDiscovery
  641. {
  642. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  643. return;
  644. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  645. [_sapDiscoverer startDiscoverer];
  646. _sapDiscoverer.discoveredMedia.delegate = self;
  647. }
  648. - (void)_stopSAPDiscovery
  649. {
  650. [_sapDiscoverer stopDiscoverer];
  651. _sapDiscoverer = nil;
  652. }
  653. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  654. {
  655. NSLog(@"found media %@ of type %lu", media.url.lastPathComponent, media.mediaType);
  656. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  657. }
  658. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  659. {
  660. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  661. }
  662. #pragma DSM discovery
  663. - (void)_startDSMDiscovery
  664. {
  665. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  666. return;
  667. if (_dsmDiscoverer)
  668. return;
  669. _dsmDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"dsm"];
  670. [_dsmDiscoverer startDiscoverer];
  671. _dsmDiscoverer.discoveredMedia.delegate = self;
  672. }
  673. - (void)_stopDSMDiscovery
  674. {
  675. [_dsmDiscoverer stopDiscoverer];
  676. _dsmDiscoverer = nil;
  677. }
  678. @end