VLCLocalServerListViewController.m 13 KB

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