VLCLocalServerListViewController.m 28 KB

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