VLCLocalServerListViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate, VLCNetworkLoginViewController, NSNetServiceDelegate>
  21. {
  22. UIBarButtonItem *_backToMenuButton;
  23. NSArray *_sectionHeaderTexts;
  24. NSNetServiceBrowser *_netServiceBrowser;
  25. NSMutableArray *_rawServices;
  26. NSMutableArray *_ftpServices;
  27. NSArray *_filteredUPNPDevices;
  28. NSArray *_UPNPdevices;
  29. VLCNetworkLoginViewController *_loginViewController;
  30. }
  31. @end
  32. @implementation VLCLocalServerListViewController
  33. - (void)dealloc
  34. {
  35. [_netServiceBrowser stop];
  36. }
  37. - (void)loadView
  38. {
  39. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  40. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  41. _tableView.delegate = self;
  42. _tableView.dataSource = self;
  43. self.view = _tableView;
  44. }
  45. - (void)viewDidLoad
  46. {
  47. [super viewDidLoad];
  48. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)"];
  49. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  50. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  51. self.tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  52. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  53. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  54. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  55. _ftpServices = [[NSMutableArray alloc] init];
  56. [_ftpServices addObject:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  57. _rawServices = [[NSMutableArray alloc] init];
  58. _netServiceBrowser = [[NSNetServiceBrowser alloc] init];
  59. _netServiceBrowser.delegate = self;
  60. }
  61. - (void)viewWillDisappear:(BOOL)animated
  62. {
  63. [super viewWillDisappear:animated];
  64. [_netServiceBrowser stop];
  65. }
  66. - (void)viewWillAppear:(BOOL)animated
  67. {
  68. [super viewWillAppear:animated];
  69. [self _triggerNetServiceBrowser];
  70. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  71. }
  72. - (void)_triggerNetServiceBrowser
  73. {
  74. [_netServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  75. }
  76. - (void)_startUPNPDiscovery
  77. {
  78. NSLog(@"_startUPNPDiscovery");
  79. UPnPManager *managerInstance = [UPnPManager GetInstance];
  80. _UPNPdevices = [[managerInstance DB] rootDevices];
  81. [[managerInstance DB] addObserver:(UPnPDBObserver*)self];
  82. //Optional; set User Agent
  83. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  84. //Search for UPnP Devices
  85. [[managerInstance SSDP] startSSDP];
  86. [[managerInstance SSDP] searchSSDP];
  87. [[managerInstance SSDP] SSDPDBUpdate];
  88. }
  89. - (IBAction)goBack:(id)sender
  90. {
  91. UPnPManager *managerInstance = [UPnPManager GetInstance];
  92. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  93. [[managerInstance SSDP] stopSSDP];
  94. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  95. }
  96. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  97. {
  98. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  99. return NO;
  100. return YES;
  101. }
  102. #pragma mark - table view handling
  103. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  104. {
  105. return _sectionHeaderTexts.count;
  106. }
  107. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  108. {
  109. if (section == 0)
  110. return _filteredUPNPDevices.count;
  111. else if (section == 1)
  112. return _ftpServices.count;
  113. return 0;
  114. }
  115. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  116. {
  117. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  118. }
  119. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  120. {
  121. static NSString *CellIdentifier = @"LocalNetworkCell";
  122. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  123. if (cell == nil)
  124. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  125. NSUInteger row = indexPath.row;
  126. NSUInteger section = indexPath.section;
  127. [cell setIsDirectory:YES];
  128. [cell setIcon:nil];
  129. if (section == 0) {
  130. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  131. [cell setTitle:[device friendlyName]];
  132. [cell setIcon:[device smallIcon]];
  133. } else if (section == 1) {
  134. if (row == 0)
  135. [cell setTitle:_ftpServices[row]];
  136. else
  137. [cell setTitle:[_ftpServices[row] name]];
  138. }
  139. return cell;
  140. }
  141. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  142. {
  143. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  144. if (indexPath.section == 0) {
  145. BasicUPnPDevice *device = _filteredUPNPDevices[indexPath.row];
  146. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  147. MediaServer1Device *server = (MediaServer1Device*)device;
  148. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  149. [self.navigationController pushViewController:targetViewController animated:YES];
  150. }
  151. } else if (indexPath.section == 1) {
  152. if (_loginViewController == nil) {
  153. _loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:nil bundle:nil];
  154. _loginViewController.delegate = self;
  155. }
  156. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_loginViewController];
  157. [navCon loadTheme];
  158. navCon.navigationBarHidden = NO;
  159. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  160. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  161. [self presentModalViewController:navCon animated:YES];
  162. if (_loginViewController.navigationItem.leftBarButtonItem == nil) {
  163. UIBarButtonItem *doneButton = [UIBarButtonItem themedDoneButtonWithTarget:_loginViewController andSelector:@selector(dismissWithAnimation:)];
  164. _loginViewController.navigationItem.leftBarButtonItem = doneButton;
  165. }
  166. } else
  167. [self.navigationController pushViewController:_loginViewController animated:YES];
  168. if (indexPath.row != 0) { // FTP Connect To Server Special Item
  169. if ([_ftpServices[indexPath.row] hostName].length > 0)
  170. _loginViewController.serverAddressField.text = [NSString stringWithFormat:@"ftp://%@", [_ftpServices[indexPath.row] hostName]];
  171. } else
  172. _loginViewController.serverAddressField.text = @"";
  173. }
  174. }
  175. #pragma mark - login panel protocol
  176. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  177. {
  178. _loginViewController = nil;
  179. if ([url.scheme isEqualToString:@"ftp"]) {
  180. if (url.host.length > 0) {
  181. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  182. [self.navigationController pushViewController:targetViewController animated:YES];
  183. }
  184. } else
  185. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  186. }
  187. #pragma mark - custom table view appearance
  188. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  189. return 21.f;
  190. }
  191. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  192. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  193. UIView *headerView = nil;
  194. if (headerText != [NSNull null]) {
  195. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  196. CAGradientLayer *gradient = [CAGradientLayer layer];
  197. gradient.frame = headerView.bounds;
  198. gradient.colors = @[
  199. (id)[UIColor colorWithRed:(66.0f/255.0f) green:(66.0f/255.0f) blue:(66.0f/255.0f) alpha:1.0f].CGColor,
  200. (id)[UIColor colorWithRed:(56.0f/255.0f) green:(56.0f/255.0f) blue:(56.0f/255.0f) alpha:1.0f].CGColor,
  201. ];
  202. [headerView.layer insertSublayer:gradient atIndex:0];
  203. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  204. textLabel.text = (NSString *) headerText;
  205. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  206. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  207. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  208. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  209. textLabel.backgroundColor = [UIColor clearColor];
  210. [headerView addSubview:textLabel];
  211. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  212. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  213. [headerView addSubview:topLine];
  214. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  215. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  216. [headerView addSubview:bottomLine];
  217. }
  218. return headerView;
  219. }
  220. #pragma mark - bonjour discovery
  221. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  222. {
  223. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  224. [_rawServices addObject:aNetService];
  225. aNetService.delegate = self;
  226. [aNetService resolveWithTimeout:5.];
  227. }
  228. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  229. {
  230. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  231. if ([_rawServices containsObject:aNetService])
  232. [_rawServices removeObject:aNetService];
  233. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  234. [_ftpServices removeObject:aNetService];
  235. if (!moreComing)
  236. [self.tableView reloadData];
  237. }
  238. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  239. {
  240. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  241. if (![_ftpServices containsObject:aNetService])
  242. [_ftpServices addObject:aNetService];
  243. }
  244. [_rawServices removeObject:aNetService];
  245. [self.tableView reloadData];
  246. }
  247. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  248. {
  249. APLog(@"failed to resolve: %@", aNetService.name);
  250. [_rawServices removeObject:aNetService];
  251. }
  252. #pragma mark - UPNP details
  253. //protocol UPnPDBObserver
  254. - (void)UPnPDBWillUpdate:(UPnPDB*)sender{
  255. APLog(@"UPnPDBWillUpdate %d", _UPNPdevices.count);
  256. }
  257. - (void)UPnPDBUpdated:(UPnPDB*)sender{
  258. APLog(@"UPnPDBUpdated %d", _UPNPdevices.count);
  259. NSUInteger count = _UPNPdevices.count;
  260. BasicUPnPDevice *device;
  261. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  262. for (NSUInteger x = 0; x < count; x++) {
  263. device = _UPNPdevices[x];
  264. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  265. [mutArray addObject:device];
  266. }
  267. _filteredUPNPDevices = nil;
  268. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  269. [self.tableView performSelectorOnMainThread : @ selector(reloadData) withObject:nil waitUntilDone:YES];
  270. }
  271. @end