VLCLocalServerListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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. #import "VLCPlaylistViewController.h"
  21. @interface VLCLocalServerListViewController () <UITableViewDataSource, UITableViewDelegate, NSNetServiceBrowserDelegate, VLCNetworkLoginViewController, NSNetServiceDelegate, VLCMediaListDelegate>
  22. {
  23. UIBarButtonItem *_backToMenuButton;
  24. NSArray *_sectionHeaderTexts;
  25. NSNetServiceBrowser *_netServiceBrowser;
  26. NSMutableArray *_rawServices;
  27. NSMutableArray *_ftpServices;
  28. NSArray *_filteredUPNPDevices;
  29. NSArray *_UPNPdevices;
  30. VLCMediaDiscoverer * _sapDiscoverer;
  31. VLCNetworkLoginViewController *_loginViewController;
  32. UIRefreshControl *refreshControl;
  33. }
  34. @end
  35. @implementation VLCLocalServerListViewController
  36. - (void)dealloc
  37. {
  38. [_netServiceBrowser stop];
  39. }
  40. - (void)loadView
  41. {
  42. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  43. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  44. _tableView.delegate = self;
  45. _tableView.dataSource = self;
  46. self.view = _tableView;
  47. }
  48. - (void)viewDidLoad
  49. {
  50. [super viewDidLoad];
  51. _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"File Transfer Protocol (FTP)", @"Network Streams (SAP)"];
  52. _backToMenuButton = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  53. self.navigationItem.leftBarButtonItem = _backToMenuButton;
  54. self.tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  55. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  56. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  57. self.title = NSLocalizedString(@"LOCAL_NETWORK", @"");
  58. _ftpServices = [[NSMutableArray alloc] init];
  59. [_ftpServices addObject:NSLocalizedString(@"CONNECT_TO_SERVER", nil)];
  60. _rawServices = [[NSMutableArray alloc] init];
  61. _netServiceBrowser = [[NSNetServiceBrowser alloc] init];
  62. _netServiceBrowser.delegate = self;
  63. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  64. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  65. refreshControl = [[UIRefreshControl alloc] init];
  66. [refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  67. [self.tableView addSubview:refreshControl];
  68. }
  69. - (void)viewWillDisappear:(BOOL)animated
  70. {
  71. [super viewWillDisappear:animated];
  72. [_netServiceBrowser stop];
  73. }
  74. - (void)viewWillAppear:(BOOL)animated
  75. {
  76. [super viewWillAppear:animated];
  77. [self _triggerNetServiceBrowser];
  78. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  79. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  80. }
  81. - (void)_triggerNetServiceBrowser
  82. {
  83. [_netServiceBrowser searchForServicesOfType:@"_ftp._tcp." inDomain:@""];
  84. }
  85. - (void)_startUPNPDiscovery
  86. {
  87. UPnPManager *managerInstance = [UPnPManager GetInstance];
  88. _UPNPdevices = [[managerInstance DB] rootDevices];
  89. if (_UPNPdevices.count > 0)
  90. [self UPnPDBUpdated:nil];
  91. [[managerInstance DB] addObserver:(UPnPDBObserver*)self];
  92. //Optional; set User Agent
  93. [[managerInstance SSDP] setUserAgentProduct:[NSString stringWithFormat:@"VLC for iOS/%@", [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]] andOS:@"iOS"];
  94. //Search for UPnP Devices
  95. [[managerInstance SSDP] startSSDP];
  96. [[managerInstance SSDP] searchSSDP];
  97. [[managerInstance SSDP] SSDPDBUpdate];
  98. }
  99. - (IBAction)goBack:(id)sender
  100. {
  101. UPnPManager *managerInstance = [UPnPManager GetInstance];
  102. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  103. [[managerInstance SSDP] stopSSDP];
  104. [self _stopSAPDiscovery];
  105. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  106. }
  107. - (BOOL)shouldAutorotate
  108. {
  109. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  110. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  111. return NO;
  112. return YES;
  113. }
  114. #pragma mark - table view handling
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  116. {
  117. return _sectionHeaderTexts.count;
  118. }
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  120. {
  121. if (section == 0)
  122. return _filteredUPNPDevices.count;
  123. else if (section == 1)
  124. return _ftpServices.count;
  125. else if (section == 2)
  126. return _sapDiscoverer.discoveredMedia.count;
  127. return 0;
  128. }
  129. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  132. }
  133. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. static NSString *CellIdentifier = @"LocalNetworkCell";
  136. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  137. if (cell == nil)
  138. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  139. NSUInteger row = indexPath.row;
  140. NSUInteger section = indexPath.section;
  141. [cell setIsDirectory:YES];
  142. [cell setIcon:nil];
  143. if (section == 0) {
  144. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  145. [cell setTitle:[device friendlyName]];
  146. [cell setIcon:[device smallIcon]];
  147. } else if (section == 1) {
  148. if (row == 0)
  149. [cell setTitle:_ftpServices[row]];
  150. else
  151. [cell setTitle:[_ftpServices[row] name]];
  152. } else if (section == 2)
  153. [cell setTitle:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] metadataForKey: VLCMetaInformationTitle]];
  154. return cell;
  155. }
  156. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  157. {
  158. [tableView deselectRowAtIndexPath:indexPath animated:YES];
  159. NSUInteger row = indexPath.row;
  160. NSUInteger section = indexPath.section;
  161. if (section == 0) {
  162. BasicUPnPDevice *device = _filteredUPNPDevices[row];
  163. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"]) {
  164. MediaServer1Device *server = (MediaServer1Device*)device;
  165. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:server header:[device friendlyName] andRootID:@"0"];
  166. [self.navigationController pushViewController:targetViewController animated:YES];
  167. }
  168. } else if (section == 1) {
  169. if (_loginViewController == nil) {
  170. _loginViewController = [[VLCNetworkLoginViewController alloc] initWithNibName:nil bundle:nil];
  171. _loginViewController.delegate = self;
  172. }
  173. UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:_loginViewController];
  174. [navCon loadTheme];
  175. navCon.navigationBarHidden = NO;
  176. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  177. navCon.modalPresentationStyle = UIModalPresentationFormSheet;
  178. [self presentViewController:navCon animated:YES completion:nil];
  179. if (_loginViewController.navigationItem.leftBarButtonItem == nil) {
  180. UIBarButtonItem *doneButton = [UIBarButtonItem themedDoneButtonWithTarget:_loginViewController andSelector:@selector(dismissWithAnimation:)];
  181. _loginViewController.navigationItem.leftBarButtonItem = doneButton;
  182. }
  183. } else
  184. [self.navigationController pushViewController:_loginViewController animated:YES];
  185. if (row != 0) { // FTP Connect To Server Special Item
  186. if ([_ftpServices[row] hostName].length > 0)
  187. _loginViewController.serverAddressField.text = [NSString stringWithFormat:@"ftp://%@", [_ftpServices[row] hostName]];
  188. } else
  189. _loginViewController.serverAddressField.text = @"";
  190. } else if (section == 2) {
  191. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  192. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:appDelegate.playlistViewController];
  193. [navController loadTheme];
  194. appDelegate.revealController.contentViewController = navController;
  195. [appDelegate.revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
  196. [appDelegate.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:[[_sapDiscoverer.discoveredMedia mediaAtIndex:row] url] afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  197. }
  198. }
  199. #pragma mark - Refresh
  200. -(void)handleRefresh
  201. {
  202. UPnPManager *managerInstance = [UPnPManager GetInstance];
  203. [[managerInstance DB] removeObserver:(UPnPDBObserver*)self];
  204. [[managerInstance SSDP] stopSSDP];
  205. //set the title while refreshing
  206. refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:@"Refresh"];
  207. //set the date and time of refreshing
  208. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  209. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  210. NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@",[formattedDate stringFromDate:[NSDate date]]];
  211. refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:lastupdated];
  212. //end the refreshing
  213. [refreshControl endRefreshing];
  214. [self.tableView reloadData];
  215. [self performSelectorInBackground:@selector(_startUPNPDiscovery) withObject:nil];
  216. [self performSelectorInBackground:@selector(_startSAPDiscovery) withObject:nil];
  217. }
  218. #pragma mark - login panel protocol
  219. - (void)loginToURL:(NSURL *)url confirmedWithUsername:(NSString *)username andPassword:(NSString *)password
  220. {
  221. _loginViewController = nil;
  222. if ([url.scheme isEqualToString:@"ftp"]) {
  223. if (url.host.length > 0) {
  224. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:url.host userName:username andPassword:password atPath:@"/"];
  225. [self.navigationController pushViewController:targetViewController animated:YES];
  226. }
  227. } else
  228. APLog(@"Unsupported URL Scheme requested %@", url.scheme);
  229. }
  230. #pragma mark - custom table view appearance
  231. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  232. return 21.f;
  233. }
  234. - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
  235. NSObject *headerText = NSLocalizedString(_sectionHeaderTexts[section], @"");
  236. UIView *headerView = nil;
  237. if (headerText != [NSNull null]) {
  238. headerView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 21.0f)];
  239. CAGradientLayer *gradient = [CAGradientLayer layer];
  240. gradient.frame = headerView.bounds;
  241. gradient.colors = @[
  242. (id)[UIColor colorWithRed:(66.0f/255.0f) green:(66.0f/255.0f) blue:(66.0f/255.0f) alpha:1.0f].CGColor,
  243. (id)[UIColor colorWithRed:(56.0f/255.0f) green:(56.0f/255.0f) blue:(56.0f/255.0f) alpha:1.0f].CGColor,
  244. ];
  245. [headerView.layer insertSublayer:gradient atIndex:0];
  246. UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectInset(headerView.bounds, 12.0f, 5.0f)];
  247. textLabel.text = (NSString *) headerText;
  248. textLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:([UIFont systemFontSize] * 0.8f)];
  249. textLabel.shadowOffset = CGSizeMake(0.0f, 1.0f);
  250. textLabel.shadowColor = [UIColor colorWithWhite:0.0f alpha:0.25f];
  251. textLabel.textColor = [UIColor colorWithRed:(118.0f/255.0f) green:(118.0f/255.0f) blue:(118.0f/255.0f) alpha:1.0f];
  252. textLabel.backgroundColor = [UIColor clearColor];
  253. [headerView addSubview:textLabel];
  254. UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  255. topLine.backgroundColor = [UIColor colorWithRed:(95.0f/255.0f) green:(95.0f/255.0f) blue:(95.0f/255.0f) alpha:1.0f];
  256. [headerView addSubview:topLine];
  257. UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 21.0f, [UIScreen mainScreen].bounds.size.height, 1.0f)];
  258. bottomLine.backgroundColor = [UIColor colorWithRed:(16.0f/255.0f) green:(16.0f/255.0f) blue:(16.0f/255.0f) alpha:1.0f];
  259. [headerView addSubview:bottomLine];
  260. }
  261. return headerView;
  262. }
  263. #pragma mark - bonjour discovery
  264. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didFindService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  265. {
  266. APLog(@"found bonjour service: %@ (%@)", aNetService.name, aNetService.type);
  267. [_rawServices addObject:aNetService];
  268. aNetService.delegate = self;
  269. [aNetService resolveWithTimeout:5.];
  270. }
  271. - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didRemoveService:(NSNetService *)aNetService moreComing:(BOOL)moreComing
  272. {
  273. APLog(@"bonjour service disappeared: %@ (%i)", aNetService.name, moreComing);
  274. if ([_rawServices containsObject:aNetService])
  275. [_rawServices removeObject:aNetService];
  276. if ([aNetService.type isEqualToString:@"_ftp._tcp."])
  277. [_ftpServices removeObject:aNetService];
  278. if (!moreComing)
  279. [self.tableView reloadData];
  280. }
  281. - (void)netServiceDidResolveAddress:(NSNetService *)aNetService
  282. {
  283. if ([aNetService.type isEqualToString:@"_ftp._tcp."]) {
  284. if (![_ftpServices containsObject:aNetService])
  285. [_ftpServices addObject:aNetService];
  286. }
  287. [_rawServices removeObject:aNetService];
  288. [self.tableView reloadData];
  289. }
  290. - (void)netService:(NSNetService *)aNetService didNotResolve:(NSDictionary *)errorDict
  291. {
  292. APLog(@"failed to resolve: %@", aNetService.name);
  293. [_rawServices removeObject:aNetService];
  294. }
  295. #pragma mark - UPNP details
  296. //protocol UPnPDBObserver
  297. - (void)UPnPDBWillUpdate:(UPnPDB*)sender{
  298. }
  299. - (void)UPnPDBUpdated:(UPnPDB*)sender{
  300. NSUInteger count = _UPNPdevices.count;
  301. BasicUPnPDevice *device;
  302. NSMutableArray *mutArray = [[NSMutableArray alloc] init];
  303. for (NSUInteger x = 0; x < count; x++) {
  304. device = _UPNPdevices[x];
  305. if ([[device urn] isEqualToString:@"urn:schemas-upnp-org:device:MediaServer:1"])
  306. [mutArray addObject:device];
  307. }
  308. _filteredUPNPDevices = nil;
  309. _filteredUPNPDevices = [NSArray arrayWithArray:mutArray];
  310. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
  311. }
  312. #pragma mark SAP discovery
  313. - (void)_startSAPDiscovery
  314. {
  315. _sapDiscoverer = [[VLCMediaDiscoverer alloc] initWithName:@"sap"];
  316. _sapDiscoverer.discoveredMedia.delegate = self;
  317. }
  318. - (void)_stopSAPDiscovery
  319. {
  320. _sapDiscoverer = nil;
  321. }
  322. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  323. {
  324. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  325. }
  326. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index
  327. {
  328. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  329. }
  330. @end