VLCNetworkServerBrowserViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 "VLCNetworkServerBrowserFTP.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. @end
  29. @implementation VLCNetworkServerBrowserViewController
  30. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)browser
  31. {
  32. self = [super init];
  33. if (self) {
  34. _serverBrowser = browser;
  35. browser.delegate = self;
  36. }
  37. return self;
  38. }
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  42. self.title = self.serverBrowser.title;
  43. [self update];
  44. }
  45. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
  46. [self.tableView reloadData];
  47. [[VLCActivityManager defaultManager] networkActivityStopped];
  48. }
  49. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  50. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  51. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  52. delegate:self
  53. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  54. otherButtonTitles:nil];
  55. [alert show];
  56. }
  57. - (void)update
  58. {
  59. [self.serverBrowser update];
  60. [[VLCActivityManager defaultManager] networkActivityStarted];
  61. }
  62. #pragma mark -
  63. - (NSByteCountFormatter *)byteCounterFormatter {
  64. if (!_byteCounterFormatter) {
  65. _byteCounterFormatter = [[NSByteCountFormatter alloc] init];
  66. }
  67. return _byteCounterFormatter;
  68. }
  69. #pragma mark - server browser item specifics
  70. - (void)_downloadItem:(id<VLCNetworkServerBrowserItem>)item
  71. {
  72. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL fileNameOfMedia:nil];
  73. }
  74. - (void)_streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  75. {
  76. NSString *URLofSubtitle = nil;
  77. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  78. if(SubtitlesList.count > 0)
  79. URLofSubtitle = [self _getFileSubtitleFromFtpServer:SubtitlesList[0]];
  80. NSURL *URLToPlay = item.URL;
  81. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  82. [vpc playURL:URLToPlay subtitlesFilePath:URLofSubtitle];
  83. }
  84. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  85. {
  86. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  87. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  88. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  89. [urls filterUsingPredicate:namePredicate];
  90. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  91. return [evaluatedObject.path isSupportedSubtitleFormat];
  92. }];
  93. [urls filterUsingPredicate:formatPrediate];
  94. return [NSArray arrayWithArray:urls];
  95. }
  96. - (NSString *)_getFileSubtitleFromFtpServer:(NSURL *)subtitleURL
  97. {
  98. NSString *FileSubtitlePath = nil;
  99. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  100. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  101. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  102. NSString *directoryPath = searchPaths[0];
  103. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  104. NSFileManager *fileManager = [NSFileManager defaultManager];
  105. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  106. //create local subtitle file
  107. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  108. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  109. APLog(@"file creation failed, no data was saved");
  110. }
  111. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  112. } else {
  113. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  114. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  115. delegate:self
  116. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  117. otherButtonTitles:nil];
  118. [alert show];
  119. }
  120. return FileSubtitlePath;
  121. }
  122. #pragma mark - table view data source, for more see super
  123. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  124. {
  125. if (tableView == self.searchDisplayController.searchResultsTableView)
  126. return _searchArray.count;
  127. return self.serverBrowser.items.count;
  128. }
  129. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  132. if (cell == nil)
  133. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  134. id<VLCNetworkServerBrowserItem> item;
  135. if (tableView == self.searchDisplayController.searchResultsTableView) {
  136. item = _searchArray[indexPath.row];
  137. } else {
  138. item = self.serverBrowser.items[indexPath.row];
  139. }
  140. cell.title = item.name;
  141. if (item.isContainer) {
  142. cell.isDirectory = YES;
  143. cell.icon = [UIImage imageNamed:@"folder"];
  144. } else {
  145. cell.isDirectory = NO;
  146. cell.icon = [UIImage imageNamed:@"blank"];
  147. cell.subtitle = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  148. cell.isDownloadable = YES;
  149. cell.delegate = self;
  150. }
  151. return cell;
  152. }
  153. #pragma mark - table view delegate, for more see super
  154. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  157. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  158. [[VLCActivityManager defaultManager] networkActivityStopped];
  159. }
  160. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  161. {
  162. id<VLCNetworkServerBrowserItem> item;
  163. if (tableView == self.searchDisplayController.searchResultsTableView) {
  164. item = _searchArray[indexPath.row];
  165. } else {
  166. item = self.serverBrowser.items[indexPath.row];
  167. }
  168. if (item.isContainer) {
  169. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  170. [self.navigationController pushViewController:targetViewController animated:YES];
  171. } else {
  172. NSString *properObjectName = item.name;
  173. if (![properObjectName isSupportedFormat]) {
  174. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  175. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName]
  176. delegate:self
  177. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  178. otherButtonTitles:nil];
  179. [alert show];
  180. } else
  181. [self _streamFileForItem:item];
  182. }
  183. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  184. }
  185. #pragma mark - VLCNetworkListCell delegation
  186. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  187. {
  188. id<VLCNetworkServerBrowserItem> item;
  189. if ([self.searchDisplayController isActive])
  190. item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  191. else
  192. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  193. if (![item.name isSupportedFormat]) {
  194. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  195. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), item.name]
  196. delegate:self
  197. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  198. otherButtonTitles:nil];
  199. [alert show];
  200. } else {
  201. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  202. [self _downloadItem:item];
  203. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  204. } else {
  205. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  206. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]]
  207. delegate:self
  208. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  209. otherButtonTitles:nil];
  210. [alert show];
  211. }
  212. }
  213. }
  214. #pragma mark - search
  215. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  216. {
  217. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  218. self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  219. return YES;
  220. }
  221. @end