VLCNetworkServerBrowserViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL fileNameOfMedia:nil];
  89. }
  90. - (void)_streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  91. {
  92. NSString *URLofSubtitle = nil;
  93. NSURL *remoteSubtitleURL = nil;
  94. if ([item respondsToSelector:@selector(subtitleURL)]) {
  95. remoteSubtitleURL = [item subtitleURL];
  96. }
  97. if (!remoteSubtitleURL) {
  98. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  99. remoteSubtitleURL = SubtitlesList.firstObject;
  100. }
  101. if(remoteSubtitleURL)
  102. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  103. NSURL *URLToPlay = item.URL;
  104. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  105. [vpc playURL:URLToPlay subtitlesFilePath:URLofSubtitle];
  106. }
  107. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  108. {
  109. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  110. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  111. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  112. [urls filterUsingPredicate:namePredicate];
  113. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  114. return [evaluatedObject.path isSupportedSubtitleFormat];
  115. }];
  116. [urls filterUsingPredicate:formatPrediate];
  117. return [NSArray arrayWithArray:urls];
  118. }
  119. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  120. {
  121. NSString *FileSubtitlePath = nil;
  122. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  123. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  124. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  125. NSString *directoryPath = searchPaths[0];
  126. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  127. NSFileManager *fileManager = [NSFileManager defaultManager];
  128. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  129. //create local subtitle file
  130. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  131. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  132. APLog(@"file creation failed, no data was saved");
  133. }
  134. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  135. } else {
  136. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  137. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  138. delegate:self
  139. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  140. otherButtonTitles:nil];
  141. [alert show];
  142. }
  143. return FileSubtitlePath;
  144. }
  145. - (UIImage *)getCachedImage:(NSURL *)url
  146. {
  147. UIImage *image = [self.imageCache objectForKey:url];
  148. if ((image != nil) && [image isKindOfClass:[UIImage class]]) {
  149. return image;
  150. } else {
  151. NSData *imageData = [[NSData alloc] initWithContentsOfURL:url];
  152. if (imageData) {
  153. image = [[UIImage alloc] initWithData:imageData];
  154. [self.imageCache setObject:image forKey:url];
  155. }
  156. return image;
  157. }
  158. }
  159. #pragma mark - table view data source, for more see super
  160. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  161. {
  162. if (tableView == self.searchDisplayController.searchResultsTableView)
  163. return _searchArray.count;
  164. return self.serverBrowser.items.count;
  165. }
  166. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  169. if (cell == nil)
  170. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  171. id<VLCNetworkServerBrowserItem> item;
  172. if (tableView == self.searchDisplayController.searchResultsTableView) {
  173. item = _searchArray[indexPath.row];
  174. } else {
  175. item = self.serverBrowser.items[indexPath.row];
  176. }
  177. cell.title = item.name;
  178. if (item.isContainer) {
  179. cell.isDirectory = YES;
  180. cell.icon = [UIImage imageNamed:@"folder"];
  181. } else {
  182. cell.isDirectory = NO;
  183. cell.icon = [UIImage imageNamed:@"blank"];
  184. NSString *sizeString = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  185. NSString *duration = nil;
  186. if ([item respondsToSelector:@selector(duration)]) {
  187. duration = item.duration;
  188. }
  189. NSString *subtitle = nil;
  190. if (sizeString && duration) {
  191. subtitle = [NSString stringWithFormat:@"%@ (%@)",sizeString, duration];
  192. } else if (sizeString) {
  193. subtitle = sizeString;
  194. } else if (duration) {
  195. subtitle = duration;
  196. }
  197. cell.subtitle = sizeString;
  198. cell.isDownloadable = YES;
  199. cell.delegate = self;
  200. NSURL *thumbnailURL = nil;
  201. if (thumbnailURL) {
  202. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  203. dispatch_async(queue, ^{
  204. UIImage *img = [self getCachedImage:thumbnailURL];
  205. dispatch_async(dispatch_get_main_queue(), ^{
  206. if (img) {
  207. [cell setIcon:img];
  208. }
  209. });
  210. });
  211. }
  212. }
  213. return cell;
  214. }
  215. #pragma mark - table view delegate, for more see super
  216. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  217. {
  218. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  219. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  220. [[VLCActivityManager defaultManager] networkActivityStopped];
  221. }
  222. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  223. {
  224. id<VLCNetworkServerBrowserItem> item;
  225. if (tableView == self.searchDisplayController.searchResultsTableView) {
  226. item = _searchArray[indexPath.row];
  227. } else {
  228. item = self.serverBrowser.items[indexPath.row];
  229. }
  230. if (item.isContainer) {
  231. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  232. [self.navigationController pushViewController:targetViewController animated:YES];
  233. } else {
  234. if (![self isSupportedItem:item]) {
  235. [self showUnsupportedFileAlertForItem:item];
  236. } else
  237. [self _streamFileForItem:item];
  238. }
  239. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  240. }
  241. #pragma mark - VLCNetworkListCell delegation
  242. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  243. {
  244. id<VLCNetworkServerBrowserItem> item;
  245. if ([self.searchDisplayController isActive])
  246. item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  247. else
  248. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  249. if (![self isSupportedItem:item]) {
  250. [self showUnsupportedFileAlertForItem:item];
  251. } else {
  252. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  253. [self _downloadItem:item];
  254. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  255. } else {
  256. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  257. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]]
  258. delegate:self
  259. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  260. otherButtonTitles:nil];
  261. [alert show];
  262. }
  263. }
  264. }
  265. #pragma mark - search
  266. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  267. {
  268. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  269. self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  270. return YES;
  271. }
  272. @end