VLCLocalServerListViewController.m 12 KB

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