VLCLocalServerListViewController.m 28 KB

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