VLCNetworkServerBrowserViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 "VLCStatusLabel.h"
  18. #import "VLCPlaybackController.h"
  19. #import "VLCDownloadViewController.h"
  20. #import "UIDevice+VLC.h"
  21. #import "NSString+SupportedMedia.h"
  22. #import "UIDevice+VLC.h"
  23. #import "VLCNetworkServerBrowser-Protocol.h"
  24. @interface VLCNetworkServerBrowserViewController () <VLCNetworkServerBrowserDelegate,VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
  25. {
  26. UIRefreshControl *_refreshControl;
  27. }
  28. @property (nonatomic) id<VLCNetworkServerBrowser> serverBrowser;
  29. @property (nonatomic) NSByteCountFormatter *byteCounterFormatter;
  30. @property (nonatomic) NSArray<id<VLCNetworkServerBrowserItem>> *searchArray;
  31. @end
  32. @implementation VLCNetworkServerBrowserViewController
  33. - (instancetype)initWithServerBrowser:(id<VLCNetworkServerBrowser>)browser
  34. {
  35. self = [super init];
  36. if (self) {
  37. _serverBrowser = browser;
  38. browser.delegate = self;
  39. }
  40. return self;
  41. }
  42. - (void)viewDidLoad
  43. {
  44. [super viewDidLoad];
  45. _refreshControl = [[UIRefreshControl alloc] init];
  46. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  47. _refreshControl.tintColor = [UIColor whiteColor];
  48. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  49. [self.tableView addSubview:_refreshControl];
  50. self.title = self.serverBrowser.title;
  51. [self update];
  52. }
  53. - (void)networkServerBrowserDidUpdate:(id<VLCNetworkServerBrowser>)networkBrowser {
  54. [self.tableView reloadData];
  55. [[VLCActivityManager defaultManager] networkActivityStopped];
  56. [_refreshControl endRefreshing];
  57. }
  58. - (void)networkServerBrowser:(id<VLCNetworkServerBrowser>)networkBrowser requestDidFailWithError:(NSError *)error {
  59. [self vlc_showAlertWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  60. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  61. buttonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  62. }
  63. - (void)update
  64. {
  65. [self.serverBrowser update];
  66. [[VLCActivityManager defaultManager] networkActivityStarted];
  67. }
  68. -(void)handleRefresh
  69. {
  70. //set the title while refreshing
  71. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH",nil)];
  72. //set the date and time of refreshing
  73. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  74. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  75. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE",nil),[formattedDate stringFromDate:[NSDate date]]];
  76. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  77. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  78. //end the refreshing
  79. [self update];
  80. }
  81. #pragma mark -
  82. - (NSByteCountFormatter *)byteCounterFormatter {
  83. if (!_byteCounterFormatter) {
  84. _byteCounterFormatter = [[NSByteCountFormatter alloc] init];
  85. }
  86. return _byteCounterFormatter;
  87. }
  88. #pragma mark - server browser item specifics
  89. - (void)_downloadItem:(id<VLCNetworkServerBrowserItem>)item
  90. {
  91. NSString *filename = item.name;
  92. if (filename.pathExtension.length == 0) {
  93. /* there are few crappy UPnP servers who don't reveal the correct file extension, so we use a generic fake (#11123) */
  94. NSString *urlExtension = item.URL.pathExtension;
  95. NSString *extension = urlExtension.length != 0 ? urlExtension : @"vlc";
  96. filename = [filename stringByAppendingPathExtension:extension];
  97. }
  98. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:item.URL
  99. fileNameOfMedia:filename];
  100. }
  101. - (BOOL)triggerDownloadForItem:(id<VLCNetworkServerBrowserItem>)item
  102. {
  103. if (item.fileSizeBytes.longLongValue < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  104. [self _downloadItem:item];
  105. return YES;
  106. } else {
  107. NSString *title = NSLocalizedString(@"DISK_FULL", nil);
  108. NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), item.name, [[UIDevice currentDevice] model]];
  109. NSString *button = NSLocalizedString(@"BUTTON_OK", nil);
  110. [self vlc_showAlertWithTitle:title message:messsage buttonTitle:button];
  111. return NO;
  112. }
  113. }
  114. - (void)didSelectItem:(id<VLCNetworkServerBrowserItem>)item index:(NSUInteger)index singlePlayback:(BOOL)singlePlayback
  115. {
  116. if (item.isContainer) {
  117. VLCNetworkServerBrowserViewController *targetViewController = [[VLCNetworkServerBrowserViewController alloc] initWithServerBrowser:item.containerBrowser];
  118. [self.navigationController pushViewController:targetViewController animated:YES];
  119. } else {
  120. if (singlePlayback) {
  121. [self _streamFileForItem:item];
  122. } else {
  123. VLCMediaList *mediaList = self.serverBrowser.mediaList;
  124. [self configureSubtitlesInMediaList:mediaList];
  125. [self _streamMediaList:mediaList startingAtIndex:index];
  126. }
  127. }
  128. }
  129. - (void)configureSubtitlesInMediaList:(VLCMediaList *)mediaList {
  130. NSArray *items = self.serverBrowser.items;
  131. id<VLCNetworkServerBrowserItem> loopItem;
  132. [mediaList lock];
  133. NSUInteger count = mediaList.count;
  134. for (NSUInteger i = 0; i < count; i++) {
  135. loopItem = items[i];
  136. NSString *URLofSubtitle = nil;
  137. NSURL *remoteSubtitleURL = nil;
  138. if ([loopItem respondsToSelector:@selector(subtitleURL)]) {
  139. [loopItem subtitleURL];
  140. }
  141. if (remoteSubtitleURL == nil) {
  142. NSArray *subtitlesList = [self _searchSubtitle:loopItem.URL.lastPathComponent];
  143. remoteSubtitleURL = subtitlesList.firstObject;
  144. }
  145. if(remoteSubtitleURL != nil) {
  146. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  147. if (URLofSubtitle != nil)
  148. [[mediaList mediaAtIndex:i] addOptions:@{ kVLCSettingSubtitlesFilePath : URLofSubtitle }];
  149. }
  150. }
  151. [mediaList unlock];
  152. }
  153. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  154. {
  155. NSString *FileSubtitlePath = nil;
  156. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  157. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  158. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  159. NSString *directoryPath = searchPaths[0];
  160. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  161. NSFileManager *fileManager = [NSFileManager defaultManager];
  162. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  163. //create local subtitle file
  164. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  165. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  166. APLog(@"file creation failed, no data was saved");
  167. return nil;
  168. }
  169. }
  170. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  171. } else {
  172. [self vlc_showAlertWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  173. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  174. buttonTitle:NSLocalizedString(@"BUTTON_OK", nil)];
  175. }
  176. return FileSubtitlePath;
  177. }
  178. - (void)_streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex
  179. {
  180. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  181. [vpc playMediaList:mediaList firstIndex:startIndex];
  182. }
  183. - (void)_streamFileForItem:(id<VLCNetworkServerBrowserItem>)item
  184. {
  185. NSString *URLofSubtitle = nil;
  186. NSURL *remoteSubtitleURL = nil;
  187. if ([item respondsToSelector:@selector(subtitleURL)]) {
  188. remoteSubtitleURL = [item subtitleURL];
  189. }
  190. if (!remoteSubtitleURL) {
  191. NSArray *SubtitlesList = [self _searchSubtitle:item.URL.lastPathComponent];
  192. remoteSubtitleURL = SubtitlesList.firstObject;
  193. }
  194. if(remoteSubtitleURL)
  195. URLofSubtitle = [self _getFileSubtitleFromServer:remoteSubtitleURL];
  196. NSURL *URLToPlay = item.URL;
  197. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  198. [vpc playURL:URLToPlay subtitlesFilePath:URLofSubtitle];
  199. }
  200. - (NSArray<NSURL*> *)_searchSubtitle:(NSString *)url
  201. {
  202. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  203. NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithArray:[self.serverBrowser.items valueForKey:@"URL"]];
  204. NSPredicate *namePredicate = [NSPredicate predicateWithFormat:@"SELF.path contains[c] %@", urlTemp];
  205. [urls filterUsingPredicate:namePredicate];
  206. NSPredicate *formatPrediate = [NSPredicate predicateWithBlock:^BOOL(NSURL *_Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
  207. return [evaluatedObject.path isSupportedSubtitleFormat];
  208. }];
  209. [urls filterUsingPredicate:formatPrediate];
  210. return [NSArray arrayWithArray:urls];
  211. }
  212. #pragma mark - table view data source, for more see super
  213. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  214. {
  215. if (tableView == self.searchDisplayController.searchResultsTableView)
  216. return _searchArray.count;
  217. return self.serverBrowser.items.count;
  218. }
  219. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  220. {
  221. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  222. if (cell == nil)
  223. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  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. cell.title = item.name;
  231. if (item.isContainer) {
  232. cell.isDirectory = YES;
  233. cell.icon = [UIImage imageNamed:@"folder"];
  234. } else {
  235. cell.isDirectory = NO;
  236. cell.icon = [UIImage imageNamed:@"blank"];
  237. NSString *sizeString = item.fileSizeBytes ? [self.byteCounterFormatter stringFromByteCount:item.fileSizeBytes.longLongValue] : nil;
  238. NSString *duration = nil;
  239. if ([item respondsToSelector:@selector(duration)]) {
  240. duration = item.duration;
  241. }
  242. NSString *subtitle = nil;
  243. if (sizeString && duration) {
  244. subtitle = [NSString stringWithFormat:@"%@ (%@)",sizeString, duration];
  245. } else if (sizeString) {
  246. subtitle = sizeString;
  247. } else if (duration) {
  248. subtitle = duration;
  249. }
  250. cell.subtitle = sizeString;
  251. cell.isDownloadable = YES;
  252. cell.delegate = self;
  253. NSURL *thumbnailURL = nil;
  254. if ([item respondsToSelector:@selector(thumbnailURL)]) {
  255. thumbnailURL = item.thumbnailURL;
  256. }
  257. if (thumbnailURL) {
  258. [cell setIconURL:thumbnailURL];
  259. }
  260. }
  261. return cell;
  262. }
  263. #pragma mark - table view delegate, for more see super
  264. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  265. {
  266. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  267. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  268. [[VLCActivityManager defaultManager] networkActivityStopped];
  269. }
  270. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  271. {
  272. id<VLCNetworkServerBrowserItem> item;
  273. NSInteger row = indexPath.row;
  274. BOOL singlePlayback = NO;
  275. if (tableView == self.searchDisplayController.searchResultsTableView) {
  276. item = _searchArray[row];
  277. singlePlayback = YES;
  278. } else {
  279. item = self.serverBrowser.items[row];
  280. }
  281. [self didSelectItem:item index:row singlePlayback:singlePlayback];
  282. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  283. }
  284. #pragma mark - VLCNetworkListCell delegation
  285. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  286. {
  287. id<VLCNetworkServerBrowserItem> item;
  288. if ([self.searchDisplayController isActive])
  289. item = _searchArray[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  290. else
  291. item = self.serverBrowser.items[[self.tableView indexPathForCell:cell].row];
  292. if ([self triggerDownloadForItem:item]) {
  293. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  294. }
  295. }
  296. #pragma mark - search
  297. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  298. {
  299. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@",searchString];
  300. self.searchArray = [self.serverBrowser.items filteredArrayUsingPredicate:predicate];
  301. return YES;
  302. }
  303. @end