VLCNetworkServerBrowserViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Tobias Conradi <videolan # tobias-conradi.de>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCNetworkServerBrowserViewController.h"
  15. #import "VLCNetworkListCell.h"
  16. #import "VLCActivityManager.h"
  17. #import "NSString+SupportedMedia.h"
  18. #import "UIDevice+VLC.h"
  19. #import "VLCStatusLabel.h"
  20. #import "VLCPlaybackController.h"
  21. #import "VLCDownloadViewController.h"
  22. #import "WhiteRaccoon.h"
  23. #import "VLCNetworkServerBrowser-Protocol.h"
  24. @interface VLCNetworkServerBrowserViewController () <VLCNetworkServerBrowserDelegate,VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
  25. @property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
  26. @property (nonatomic) NSByteCountFormatter *byteCounterFormatter;
  27. @property (nonatomic) NSArray<id<VLCNetworkServerBrowserItem>> *searchArray;
  28. @property (nonatomic, readonly) NSCache *imageCache;
  29. @end
  30. @implementation VLCNetworkServerBrowserViewController
  31. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)browser
  32. {
  33. self = [super init];
  34. if (self) {
  35. _serverBrowser = browser;
  36. browser.delegate = self;
  37. _imageCache = [[NSCache alloc] init];
  38. [_imageCache setCountLimit:50];
  39. }
  40. return self;
  41. }
  42. - (void)viewDidLoad
  43. {
  44. [super viewDidLoad];
  45. self.title = self.serverBrowser.title;
  46. [self update];
  47. }
  48. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
  49. [self.tableView reloadData];
  50. [[VLCActivityManager defaultManager] networkActivityStopped];
  51. }
  52. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  53. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  54. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  55. delegate:self
  56. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  57. otherButtonTitles:nil];
  58. [alert show];
  59. }
  60. - (void)update
  61. {
  62. [self.serverBrowser update];
  63. [[VLCActivityManager defaultManager] networkActivityStarted];
  64. }
  65. #pragma mark -
  66. - (NSByteCountFormatter *)byteCounterFormatter {
  67. if (!_byteCounterFormatter) {
  68. _byteCounterFormatter = [[NSByteCountFormatter alloc] init];
  69. }
  70. return _byteCounterFormatter;
  71. }
  72. - (BOOL)isSupportedItem:(id<VLCNetworkServerBrowserItem>)item {
  73. NSString *properObjectName = item.name;
  74. NSString *itemURLName = item.URL.lastPathComponent;
  75. return [properObjectName isSupportedFormat] || [itemURLName isSupportedFormat];
  76. }
  77. - (void)showUnsupportedFileAlertForItem:(id<VLCNetworkServerBrowserItem>)item {
  78. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  79. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), item.name]
  80. delegate:self
  81. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  82. otherButtonTitles:nil];
  83. [alert show];
  84. }
  85. #pragma mark - server browser item specifics
  86. - (void)_downloadItem:(id<VLCNetworkServerBrowserItem>)item
  87. {
  88. NSString *filename = item.name;
  89. if (filename.pathExtension.length == 0) {
  90. /* there are few crappy UPnP servers who don't reveal the correct file extension, so we use a generic fake (#11123) */
  91. NSString *urlExtension = item.URL.pathExtension;
  92. NSString *extension = urlExtension.length!=0 ? urlExtension : @"vlc";
  93. filename = [filename stringByAppendingPathExtension:extension];
  94. }
  95. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL
  96. fileNameOfMedia:filename];
  97. }
  98. - (void)_streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  99. {
  100. NSString *URLofSubtitle = nil;
  101. NSURL *remoteSubtitleURL = nil;
  102. if ([item respondsToSelector:@selector(subtitleURL)]) {
  103. remoteSubtitleURL = [item subtitleURL];
  104. }
  105. if (!remoteSubtitleURL) {
  106. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  107. remoteSubtitleURL = SubtitlesList.firstObject;
  108. }
  109. if(remoteSubtitleURL)
  110. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  111. NSURL *URLToPlay = item.URL;
  112. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  113. [vpc playURL:URLToPlay subtitlesFilePath:URLofSubtitle];
  114. }
  115. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  116. {
  117. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  118. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  119. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  120. [urls filterUsingPredicate:namePredicate];
  121. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  122. return [evaluatedObject.path isSupportedSubtitleFormat];
  123. }];
  124. [urls filterUsingPredicate:formatPrediate];
  125. return [NSArray arrayWithArray:urls];
  126. }
  127. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  128. {
  129. NSString *FileSubtitlePath = nil;
  130. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  131. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  132. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  133. NSString *directoryPath = searchPaths[0];
  134. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  135. NSFileManager *fileManager = [NSFileManager defaultManager];
  136. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  137. //create local subtitle file
  138. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  139. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  140. APLog(@"file creation failed, no data was saved");
  141. }
  142. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  143. } else {
  144. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  145. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  146. delegate:self
  147. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  148. otherButtonTitles:nil];
  149. [alert show];
  150. }
  151. return FileSubtitlePath;
  152. }
  153. - (UIImage *)getCachedImage:(NSURL *)url
  154. {
  155. UIImage *image = [self.imageCache objectForKey:url];
  156. if ((image != nil) && [image isKindOfClass:[UIImage class]]) {
  157. return image;
  158. } else {
  159. NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
  160. if (imageData) {
  161. image = [[UIImage alloc] initWithData:imageData];
  162. [self.imageCache setObject:image forKey:url];
  163. }
  164. return image;
  165. }
  166. }
  167. #pragma mark - table view data source, for more see super
  168. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  169. {
  170. if (tableView == self.searchDisplayController.searchResultsTableView)
  171. return _searchArray.count;
  172. return self.serverBrowser.items.count;
  173. }
  174. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  175. {
  176. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  177. if (cell == nil)
  178. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  179. id<VLCNetworkServerBrowserItem> item;
  180. if (tableView == self.searchDisplayController.searchResultsTableView) {
  181. item = _searchArray[indexPath.row];
  182. } else {
  183. item = self.serverBrowser.items[indexPath.row];
  184. }
  185. cell.title = item.name;
  186. if (item.isContainer) {
  187. cell.isDirectory = YES;
  188. cell.icon = [UIImage imageNamed:@"folder"];
  189. } else {
  190. cell.isDirectory = NO;
  191. cell.icon = [UIImage imageNamed:@"blank"];
  192. NSString *sizeString = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  193. NSString *duration = nil;
  194. if ([item respondsToSelector:@selector(duration)]) {
  195. duration = item.duration;
  196. }
  197. NSString *subtitle = nil;
  198. if (sizeString && duration) {
  199. subtitle = [NSString stringWithFormat:@"%@ (%@)",sizeString, duration];
  200. } else if (sizeString) {
  201. subtitle = sizeString;
  202. } else if (duration) {
  203. subtitle = duration;
  204. }
  205. cell.subtitle = sizeString;
  206. cell.isDownloadable = YES;
  207. cell.delegate = self;
  208. NSURL *thumbnailURL = nil;
  209. if ([item respondsToSelector:@selector(thumbnailURL)]) {
  210. thumbnailURL = item.thumbnailURL;
  211. }
  212. if (thumbnailURL) {
  213. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  214. dispatch_async(queue, ^{
  215. UIImage *img = [self getCachedImage:thumbnailURL];
  216. dispatch_async(dispatch_get_main_queue(), ^{
  217. if (img) {
  218. [cell setIcon:img];
  219. }
  220. });
  221. });
  222. }
  223. }
  224. return cell;
  225. }
  226. #pragma mark - table view delegate, for more see super
  227. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  228. {
  229. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  230. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  231. [[VLCActivityManager defaultManager] networkActivityStopped];
  232. }
  233. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  234. {
  235. id<VLCNetworkServerBrowserItem> item;
  236. if (tableView == self.searchDisplayController.searchResultsTableView) {
  237. item = _searchArray[indexPath.row];
  238. } else {
  239. item = self.serverBrowser.items[indexPath.row];
  240. }
  241. if (item.isContainer) {
  242. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  243. [self.navigationController pushViewController:targetViewController animated:YES];
  244. } else {
  245. if (![self isSupportedItem:item]) {
  246. [self showUnsupportedFileAlertForItem:item];
  247. } else
  248. [self _streamFileForItem:item];
  249. }
  250. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  251. }
  252. #pragma mark - VLCNetworkListCell delegation
  253. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  254. {
  255. id<VLCNetworkServerBrowserItem> item;
  256. if ([self.searchDisplayController isActive])
  257. item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  258. else
  259. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  260. if (![self isSupportedItem:item]) {
  261. [self showUnsupportedFileAlertForItem:item];
  262. } else {
  263. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  264. [self _downloadItem:item];
  265. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  266. } else {
  267. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  268. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]]
  269. delegate:self
  270. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  271. otherButtonTitles:nil];
  272. [alert show];
  273. }
  274. }
  275. }
  276. #pragma mark - search
  277. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  278. {
  279. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  280. self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  281. return YES;
  282. }
  283. @end