VLCLocalServerListViewController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate>
  17. {
  18. UIBarButtonItem *_backToMenuButton;
  19. NSArray *_devices;
  20. }
  21. @end
  22. @implementation VLCLocalServerListViewController
  23. - (void)loadView
  24. {
  25. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  26. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  27. _tableView.delegate = self;
  28. _tableView.dataSource = self;
  29. self.view = _tableView;
  30. }
  31. - (void)viewDidLoad
  32. {
  33. [super viewDidLoad];
  34. UPnPDB* db = [[UPnPManager GetInstance] DB];
  35. _devices = [db rootDevices]; //BasicUPnPDevice
  36. [db addObserver:(UPnPDBObserver*)self];
  37. //Optional; set User Agent
  38. [[[UPnPManager GetInstance] SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  39. //Search for UPnP Devices
  40. [[[UPnPManager GetInstance] SSDP] searchSSDP];
  41. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  42. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  43. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  44. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  45. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  46. }
  47. - (IBAction)goBack:(id)sender
  48. {
  49. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  50. }
  51. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  52. {
  53. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  54. return NO;
  55. return YES;
  56. }
  57. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  58. {
  59. return 1;
  60. }
  61. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  62. {
  63. return _devices.count;
  64. }
  65. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  66. {
  67. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  68. }
  69. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  70. {
  71. static NSString *CellIdentifier = @"LocalNetworkCell";
  72. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  73. if (cell == nil)
  74. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  75. BasicUPnPDevice *device = _devices[indexPath.row];
  76. [cell setTitle:[device friendlyName]];
  77. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  78. [cell setIsDirectory:YES];
  79. return cell;
  80. }
  81. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. BasicUPnPDevice *device = _devices[indexPath.row];
  84. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  85. MediaServer1Device *server = (MediaServer1Device*)device;
  86. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithDevice:server header:[device friendlyName] andRootID:@"0"];
  87. [[self navigationController] pushViewController:targetViewController animated:YES];
  88. } else if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaRenderer:1"]) {
  89. //FIXME: euh, we don't do rendering atm, at least not here.
  90. }
  91. }
  92. //protocol UPnPDBObserver
  93. -(void)UPnPDBWillUpdate:(UPnPDB*)sender{
  94. APLog(@"UPnPDBWillUpdate %d", _devices.count);
  95. }
  96. -(void)UPnPDBUpdated:(UPnPDB*)sender{
  97. APLog(@"UPnPDBUpdated %d", _devices.count);
  98. [self.tableView performSelectorOnMainThread : @ selector(reloadData) withObject:nil waitUntilDone:YES];
  99. }
  100. @end