VLCLocalServerListViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate>
  19. {
  20. UIBarButtonItem *_backToMenuButton;
  21. NSArray *_sectionHeaderTexts;
  22. NSNetServiceBrowser *_netServiceBrowser;
  23. NSMutableArray *_ftpServices;
  24. NSArray *_filteredUPNPDevices;
  25. NSArray *_UPNPdevices;
  26. }
  27. @end
  28. @implementation VLCLocalServerListViewController
  29. - (void)dealloc
  30. {
  31. [_netServiceBrowser stop];
  32. }
  33. - (void)loadView
  34. {
  35. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  36. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  37. _tableView.delegate = self;
  38. _tableView.dataSource = self;
  39. self.view = _tableView;
  40. }
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  44. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)"];
  45. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  46. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  47. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  48. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  49. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  50. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  51. _ftpServices = [[NSMutableArray alloc] init];
  52. [_ftpServices addObject:@"Connect to Server"];
  53. _netServiceBrowser = [[NSNetServiceBrowser alloc] init];
  54. _netServiceBrowser.delegate = self;
  55. [self _triggerNetServiceBrowser];
  56. }
  57. - (void)viewWillDisappear:(BOOL)animated
  58. {
  59. [super viewWillDisappear:animated];
  60. [_netServiceBrowser stop];
  61. }
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [super viewWillAppear:animated];
  65. [self _triggerNetServiceBrowser];
  66. }
  67. - (void)_triggerNetServiceBrowser
  68. {
  69. [_netServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  70. }
  71. - (void)_startUPNPDiscovery
  72. {
  73. UPnPDB* db = [[UPnPManager GetInstance] DB];
  74. _UPNPdevices = [db rootDevices];
  75. [db addObserver:(UPnPDBObserver*)self];
  76. //Optional; set User Agent
  77. [[[UPnPManager GetInstance] SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  78. //Search for UPnP Devices
  79. [[[UPnPManager GetInstance] SSDP] searchSSDP];
  80. }
  81. - (IBAction)goBack:(id)sender
  82. {
  83. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  84. }
  85. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  86. {
  87. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  88. return NO;
  89. return YES;
  90. }
  91. #pragma mark - table view handling
  92. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  93. {
  94. return _sectionHeaderTexts.count;
  95. }
  96. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  97. {
  98. if (section == 0)
  99. return _filteredUPNPDevices.count;
  100. else if (section == 1)
  101. return _ftpServices.count;
  102. return 0;
  103. }
  104. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  105. {
  106. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  107. }
  108. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  109. {
  110. static NSString *CellIdentifier = @"LocalNetworkCell";
  111. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  112. if (cell == nil)
  113. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  114. NSUInteger row = indexPath.row;
  115. NSUInteger section = indexPath.section;
  116. [cell setIsDirectory:YES];
  117. if (section == 0) {
  118. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  119. [cell setTitle:[device friendlyName]];
  120. [cell setIcon:[device smallIcon]];
  121. } else if (section == 1) {
  122. if (row == 0)
  123. [cell setTitle:_ftpServices[row]];
  124. else
  125. [cell setTitle:[_ftpServices[row] name]];
  126. }
  127. return cell;
  128. }
  129. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. if (indexPath.section == 0) {
  132. BasicUPnPDevice *device = _filteredUPNPDevices[indexPath.row];
  133. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  134. MediaServer1Device *server = (MediaServer1Device*)device;
  135. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithDevice:server header:[device friendlyName] andRootID:@"0"];
  136. [[self navigationController] pushViewController:targetViewController animated:YES];
  137. }
  138. } else if (indexPath.section == 1) {
  139. if (indexPath.row == 0) {
  140. // FIXME LOTS OF CUSTOM CONNECTION CODE
  141. NSLog(@"should show login form");
  142. } else
  143. // CONNECT TO "KNOWN" HOST
  144. NSLog(@"connect to host");
  145. }
  146. }
  147. #pragma mark - custom table view appearance
  148. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  149. return 21.f;
  150. }
  151. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  152. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  153. UIView *headerView = nil;
  154. if (headerText != [NSNull null]) {
  155. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  156. CAGradientLayer *gradient = [CAGradientLayer layer];
  157. gradient.frame = headerView.bounds;
  158. gradient.colors = @[
  159. (id)[UIColor colorWithRed:(67.0f/255.0f) green:(74.0f/255.0f) blue:(94.0f/255.0f) alpha:1.0f].CGColor,
  160. (id)[UIColor colorWithRed:(57.0f/255.0f) green:(64.0f/255.0f) blue:(82.0f/255.0f) alpha:1.0f].CGColor,
  161. ];
  162. [headerView.layer insertSublayer:gradient atIndex:0];
  163. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  164. textLabel.text = (NSString *) headerText;
  165. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  166. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  167. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  168. textLabel.textColor = [UIColor colorWithRed:(125.0f/255.0f) green:(129.0f/255.0f) blue:(146.0f/255.0f) alpha:1.0f];
  169. textLabel.backgroundColor = [UIColor clearColor];
  170. [headerView addSubview:textLabel];
  171. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  172. topLine.backgroundColor = [UIColor colorWithRed:(78.0f/255.0f) green:(86.0f/255.0f) blue:(103.0f/255.0f) alpha:1.0f];
  173. [headerView addSubview:topLine];
  174. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  175. bottomLine.backgroundColor = [UIColor colorWithRed:(36.0f/255.0f) green:(42.0f/255.0f) blue:(5.0f/255.0f) alpha:1.0f];
  176. [headerView addSubview:bottomLine];
  177. }
  178. return headerView;
  179. }
  180. #pragma mark - bonjour discovery
  181. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  182. {
  183. [_ftpServices addObject:aNetService];
  184. if (!moreComing)
  185. [self.tableView reloadData];
  186. }
  187. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  188. {
  189. [_ftpServices removeObject:aNetService];
  190. if (!moreComing)
  191. [self.tableView reloadData];
  192. }
  193. #pragma mark - UPNP details
  194. //protocol UPnPDBObserver
  195. - (void)UPnPDBWillUpdate:(UPnPDB*)sender{
  196. APLog(@"UPnPDBWillUpdate %d", _UPNPdevices.count);
  197. }
  198. - (void)UPnPDBUpdated:(UPnPDB*)sender{
  199. APLog(@"UPnPDBUpdated %d", _UPNPdevices.count);
  200. NSUInteger count = _UPNPdevices.count;
  201. BasicUPnPDevice *device;
  202. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  203. for (NSUInteger x = 0; x < count; x++) {
  204. device = _UPNPdevices[x];
  205. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  206. [mutArray addObject:device];
  207. }
  208. _filteredUPNPDevices = nil;
  209. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  210. [self.tableView performSelectorOnMainThread : @ selector(reloadData) withObject:nil waitUntilDone:YES];
  211. }
  212. @end