VLCLocalServerListViewController.m 16 KB

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