VLCFTPServerListViewController.m 11 KB

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