VLCLocalServerListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //
  2. // VLCLocalServerListViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 10.08.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCLocalServerListViewController.h"
  11. #import "UIBarButtonItem+Theme.h"
  12. #import "VLCAppDelegate.h"
  13. #import "UPnPManager.h"
  14. #import "VLCLocalNetworkListCell.h"
  15. #import "VLCLocalServerFolderListViewController.h"
  16. #import <QuartzCore/QuartzCore.h>
  17. #import "GHRevealViewController.h"
  18. #import "VLCNetworkLoginViewController.h"
  19. #import "UINavigationController+Theme.h"
  20. #import "VLCPlaylistViewController.h"
  21. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate, VLCNetworkLoginViewController, NSNetServiceDelegate, VLCMediaListDelegate>
  22. {
  23. UIBarButtonItem *_backToMenuButton;
  24. NSArray *_sectionHeaderTexts;
  25. NSNetServiceBrowser *_netServiceBrowser;
  26. NSMutableArray *_rawServices;
  27. NSMutableArray *_ftpServices;
  28. NSArray *_filteredUPNPDevices;
  29. NSArray *_UPNPdevices;
  30. VLCMediaDiscoverer * _sapDiscoverer;
  31. VLCNetworkLoginViewController *_loginViewController;
  32. UIRefreshControl *_refreshControl;
  33. UIActivityIndicatorView *_activityIndicator;
  34. }
  35. @end
  36. @implementation VLCLocalServerListViewController
  37. - (void)dealloc
  38. {
  39. [_netServiceBrowser stop];
  40. }
  41. - (void)loadView
  42. {
  43. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  44. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  45. _tableView.delegate = self;
  46. _tableView.dataSource = self;
  47. self.view = _tableView;
  48. _activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  49. _activityIndicator.center = _tableView.center;
  50. _activityIndicator.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
  51. _activityIndicator.hidesWhenStopped = YES;
  52. [self.view addSubview:_activityIndicator];
  53. }
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)", @"Network Streams (SAP)"];
  58. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  59. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  60. self.tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  61. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  62. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  63. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  64. _ftpServices = [[NSMutableArray alloc] init];
  65. [_ftpServices addObject:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  66. _rawServices = [[NSMutableArray alloc] init];
  67. _netServiceBrowser = [[NSNetServiceBrowser alloc] init];
  68. _netServiceBrowser.delegate = self;
  69. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  70. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  71. _refreshControl = [[UIRefreshControl alloc] init];
  72. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  73. [self.tableView addSubview:_refreshControl];
  74. _loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:nil bundle:nil];
  75. _loginViewController.delegate = self;
  76. }
  77. - (void)viewWillDisappear:(BOOL)animated
  78. {
  79. [super viewWillDisappear:animated];
  80. [_activityIndicator stopAnimating];
  81. [_netServiceBrowser stop];
  82. }
  83. - (void)viewWillAppear:(BOOL)animated
  84. {
  85. [_activityIndicator stopAnimating];
  86. [super viewWillAppear:animated];
  87. [self _triggerNetServiceBrowser];
  88. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  89. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  90. }
  91. - (void)_triggerNetServiceBrowser
  92. {
  93. [_netServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  94. }
  95. - (void)_startUPNPDiscovery
  96. {
  97. UPnPManager *managerInstance = [UPnPManager GetInstance];
  98. _UPNPdevices = [[managerInstance DB] rootDevices];
  99. if (_UPNPdevices.count > 0)
  100. [self UPnPDBUpdated:nil];
  101. [[managerInstance DB] addObserver:(UPnPDBObserver*)self];
  102. //Optional; set User Agent
  103. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  104. //Search for UPnP Devices
  105. [[managerInstance SSDP] startSSDP];
  106. [[managerInstance SSDP] searchSSDP];
  107. [[managerInstance SSDP] SSDPDBUpdate];
  108. }
  109. - (IBAction)goBack:(id)sender
  110. {
  111. UPnPManager *managerInstance = [UPnPManager GetInstance];
  112. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  113. [[managerInstance SSDP] stopSSDP];
  114. [self _stopSAPDiscovery];
  115. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  116. }
  117. - (BOOL)shouldAutorotate
  118. {
  119. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  120. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  121. return NO;
  122. return YES;
  123. }
  124. #pragma mark - table view handling
  125. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  126. {
  127. return _sectionHeaderTexts.count;
  128. }
  129. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  130. {
  131. if (section == 0)
  132. return _filteredUPNPDevices.count;
  133. else if (section == 1)
  134. return _ftpServices.count;
  135. else if (section == 2)
  136. return _sapDiscoverer.discoveredMedia.count;
  137. return 0;
  138. }
  139. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  142. }
  143. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. static NSString *CellIdentifier = @"LocalNetworkCell";
  146. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  147. if (cell == nil)
  148. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  149. NSUInteger row = indexPath.row;
  150. NSUInteger section = indexPath.section;
  151. [cell setIsDirectory:YES];
  152. [cell setIcon:nil];
  153. if (section == 0) {
  154. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  155. [cell setTitle:[device friendlyName]];
  156. [cell setIcon:[device smallIcon]];
  157. } else if (section == 1) {
  158. if (row == 0)
  159. [cell setTitle:_ftpServices[row]];
  160. else
  161. [cell setTitle:[_ftpServices[row] name]];
  162. } else if (section == 2)
  163. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  164. return cell;
  165. }
  166. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  169. NSUInteger row = indexPath.row;
  170. NSUInteger section = indexPath.section;
  171. if (section == 0) {
  172. [_activityIndicator startAnimating];
  173. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  174. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  175. MediaServer1Device *server = (MediaServer1Device*)device;
  176. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  177. [self.navigationController pushViewController:targetViewController animated:YES];
  178. }
  179. } else if (section == 1) {
  180. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_loginViewController];
  181. [navCon loadTheme];
  182. navCon.navigationBarHidden = NO;
  183. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  184. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  185. [self presentViewController:navCon animated:YES completion:nil];
  186. if (_loginViewController.navigationItem.leftBarButtonItem == nil) {
  187. UIBarButtonItem *doneButton = [UIBarButtonItem themedDoneButtonWithTarget:_loginViewController andSelector:@selector(dismissWithAnimation:)];
  188. _loginViewController.navigationItem.leftBarButtonItem = doneButton;
  189. }
  190. } else
  191. [self.navigationController pushViewController:_loginViewController animated:YES];
  192. if (row != 0 && [_ftpServices[row] hostName].length > 0) { // FTP Connect To Server Special Item and hostname is long enough
  193. _loginViewController.hostname = [_ftpServices[row] hostName];
  194. } else
  195. _loginViewController.hostname = @"";
  196. } else if (section == 2) {
  197. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  198. [appDelegate openMovieFromURL:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url]];
  199. }
  200. }
  201. #pragma mark - Refresh
  202. -(void)handleRefresh
  203. {
  204. UPnPManager *managerInstance = [UPnPManager GetInstance];
  205. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  206. [[managerInstance SSDP] stopSSDP];
  207. //set the title while refreshing
  208. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refresh"];
  209. //set the date and time of refreshing
  210. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  211. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  212. NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@",[formattedDate stringFromDate:[NSDate date]]];
  213. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
  214. //end the refreshing
  215. [_refreshControl endRefreshing];
  216. [self.tableView reloadData];
  217. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  218. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  219. }
  220. #pragma mark - login panel protocol
  221. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  222. {
  223. _loginViewController = nil;
  224. if ([url.scheme isEqualToString:@"ftp"]) {
  225. if (url.host.length > 0) {
  226. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  227. [self.navigationController pushViewController:targetViewController animated:YES];
  228. }
  229. } else
  230. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  231. }
  232. #pragma mark - custom table view appearance
  233. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  234. return 21.f;
  235. }
  236. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  237. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  238. UIView *headerView = nil;
  239. if (headerText != [NSNull null]) {
  240. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  241. CAGradientLayer *gradient = [CAGradientLayer layer];
  242. gradient.frame = headerView.bounds;
  243. gradient.colors = @[
  244. (id)[UIColor colorWithRed:(66.0f/255.0f) green:(66.0f/255.0f) blue:(66.0f/255.0f) alpha:1.0f].CGColor,
  245. (id)[UIColor colorWithRed:(56.0f/255.0f) green:(56.0f/255.0f) blue:(56.0f/255.0f) alpha:1.0f].CGColor,
  246. ];
  247. [headerView.layer insertSublayer:gradient atIndex:0];
  248. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  249. textLabel.text = (NSString *) headerText;
  250. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  251. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  252. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  253. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  254. textLabel.backgroundColor = [UIColor clearColor];
  255. [headerView addSubview:textLabel];
  256. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  257. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  258. [headerView addSubview:topLine];
  259. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  260. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  261. [headerView addSubview:bottomLine];
  262. }
  263. return headerView;
  264. }
  265. #pragma mark - bonjour discovery
  266. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  267. {
  268. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  269. [_rawServices addObject:aNetService];
  270. aNetService.delegate = self;
  271. [aNetService resolveWithTimeout:5.];
  272. }
  273. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  274. {
  275. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  276. if ([_rawServices containsObject:aNetService])
  277. [_rawServices removeObject:aNetService];
  278. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  279. [_ftpServices removeObject:aNetService];
  280. if (!moreComing)
  281. [self.tableView reloadData];
  282. }
  283. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  284. {
  285. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  286. if (![_ftpServices containsObject:aNetService])
  287. [_ftpServices addObject:aNetService];
  288. }
  289. [_rawServices removeObject:aNetService];
  290. [self.tableView reloadData];
  291. }
  292. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  293. {
  294. APLog(@"failed to resolve: %@", aNetService.name);
  295. [_rawServices removeObject:aNetService];
  296. }
  297. #pragma mark - UPNP details
  298. //protocol UPnPDBObserver
  299. - (void)UPnPDBWillUpdate:(UPnPDB*)sender{
  300. }
  301. - (void)UPnPDBUpdated:(UPnPDB*)sender{
  302. NSUInteger count = _UPNPdevices.count;
  303. BasicUPnPDevice *device;
  304. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  305. for (NSUInteger x = 0; x < count; x++) {
  306. device = _UPNPdevices[x];
  307. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  308. [mutArray addObject:device];
  309. else
  310. APLog(@"found device '%@' with unsupported urn '%@'", [device friendlyName], [device urn]);
  311. }
  312. _filteredUPNPDevices = nil;
  313. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  314. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  315. }
  316. #pragma mark SAP discovery
  317. - (void)_startSAPDiscovery
  318. {
  319. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  320. _sapDiscoverer.discoveredMedia.delegate = self;
  321. }
  322. - (void)_stopSAPDiscovery
  323. {
  324. _sapDiscoverer = nil;
  325. }
  326. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  327. {
  328. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  329. }
  330. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  331. {
  332. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  333. }
  334. @end