VLCLocalServerListViewController.m 28 KB

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