VLCLocalServerListViewController.m 28 KB

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