VLCLocalServerListViewController.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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>
  19. {
  20. UIBarButtonItem *_backToMenuButton;
  21. NSArray *_sectionHeaderTexts;
  22. NSArray *_filteredUPNPDevices;
  23. NSArray *_UPNPdevices;
  24. }
  25. @end
  26. @implementation VLCLocalServerListViewController
  27. - (void)loadView
  28. {
  29. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  30. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  31. _tableView.delegate = self;
  32. _tableView.dataSource = self;
  33. self.view = _tableView;
  34. }
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)"];
  39. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  40. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  41. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  42. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  43. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  44. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  45. }
  46. - (void)_startUPNPDiscovery
  47. {
  48. UPnPDB* db = [[UPnPManager GetInstance] DB];
  49. _UPNPdevices = [db rootDevices]; //BasicUPnPDevice
  50. [db addObserver:(UPnPDBObserver*)self];
  51. //Optional; set User Agent
  52. [[[UPnPManager GetInstance] SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  53. //Search for UPnP Devices
  54. [[[UPnPManager GetInstance] SSDP] searchSSDP];
  55. }
  56. - (IBAction)goBack:(id)sender
  57. {
  58. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  59. }
  60. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  61. {
  62. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  63. return NO;
  64. return YES;
  65. }
  66. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  67. {
  68. return _sectionHeaderTexts.count;
  69. }
  70. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  71. {
  72. if (section == 0)
  73. return _filteredUPNPDevices.count;
  74. return 0;
  75. }
  76. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  77. {
  78. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  79. }
  80. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  81. {
  82. static NSString *CellIdentifier = @"LocalNetworkCell";
  83. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  84. if (cell == nil)
  85. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  86. if (indexPath.section == 0) {
  87. BasicUPnPDevice *device = _filteredUPNPDevices[indexPath.row];
  88. [cell setTitle:[device friendlyName]];
  89. [cell setIcon:[device smallIcon]];
  90. [cell setIsDirectory:YES];
  91. }
  92. return cell;
  93. }
  94. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  95. {
  96. if (indexPath.section == 0) {
  97. BasicUPnPDevice *device = _filteredUPNPDevices[indexPath.row];
  98. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  99. MediaServer1Device *server = (MediaServer1Device*)device;
  100. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithDevice:server header:[device friendlyName] andRootID:@"0"];
  101. [[self navigationController] pushViewController:targetViewController animated:YES];
  102. }
  103. }
  104. }
  105. #pragma mark - custom table view appearance
  106. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  107. return 21.f;
  108. }
  109. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  110. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  111. UIView *headerView = nil;
  112. if (headerText != [NSNull null]) {
  113. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  114. CAGradientLayer *gradient = [CAGradientLayer layer];
  115. gradient.frame = headerView.bounds;
  116. gradient.colors = @[
  117. (id)[UIColor colorWithRed:(67.0f/255.0f) green:(74.0f/255.0f) blue:(94.0f/255.0f) alpha:1.0f].CGColor,
  118. (id)[UIColor colorWithRed:(57.0f/255.0f) green:(64.0f/255.0f) blue:(82.0f/255.0f) alpha:1.0f].CGColor,
  119. ];
  120. [headerView.layer insertSublayer:gradient atIndex:0];
  121. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  122. textLabel.text = (NSString *) headerText;
  123. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  124. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  125. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  126. textLabel.textColor = [UIColor colorWithRed:(125.0f/255.0f) green:(129.0f/255.0f) blue:(146.0f/255.0f) alpha:1.0f];
  127. textLabel.backgroundColor = [UIColor clearColor];
  128. [headerView addSubview:textLabel];
  129. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  130. topLine.backgroundColor = [UIColor colorWithRed:(78.0f/255.0f) green:(86.0f/255.0f) blue:(103.0f/255.0f) alpha:1.0f];
  131. [headerView addSubview:topLine];
  132. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  133. bottomLine.backgroundColor = [UIColor colorWithRed:(36.0f/255.0f) green:(42.0f/255.0f) blue:(5.0f/255.0f) alpha:1.0f];
  134. [headerView addSubview:bottomLine];
  135. }
  136. return headerView;
  137. }
  138. #pragma mark - UPNP details
  139. //protocol UPnPDBObserver
  140. -(void)UPnPDBWillUpdate:(UPnPDB*)sender{
  141. APLog(@"UPnPDBWillUpdate %d", _UPNPdevices.count);
  142. }
  143. -(void)UPnPDBUpdated:(UPnPDB*)sender{
  144. APLog(@"UPnPDBUpdated %d", _UPNPdevices.count);
  145. NSUInteger count = _UPNPdevices.count;
  146. BasicUPnPDevice *device;
  147. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  148. for (NSUInteger x = 0; x < count; x++) {
  149. device = _UPNPdevices[x];
  150. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  151. [mutArray addObject:device];
  152. }
  153. _filteredUPNPDevices = nil;
  154. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  155. [self.tableView performSelectorOnMainThread : @ selector(reloadData) withObject:nil waitUntilDone:YES];
  156. }
  157. @end