VLCSharedLibraryListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*****************************************************************************
  2. * VLCSharedLibraryListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. * Pierre SAGASPE <pierre.sagaspe # me.com>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCSharedLibraryListViewController.h"
  13. #import "VLCSharedLibraryParser.h"
  14. #import "VLCLocalNetworkListCell.h"
  15. #import "VLCAppDelegate.h"
  16. #import "VLCPlaylistViewController.h"
  17. #import "VLCDownloadViewController.h"
  18. #import "NSString+SupportedMedia.h"
  19. #import "VLCStatusLabel.h"
  20. #import "UIDevice+VLC.h"
  21. @interface VLCSharedLibraryListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate, VLCSharedLibraryParserDelegate>
  22. {
  23. NSArray *_serverDataArray;
  24. NSCache *_imageCache;
  25. NSString *_httpServerName;
  26. NSString *_httpServerAddress;
  27. NSString *_httpServerPort;
  28. VLCSharedLibraryParser *_httpParser;
  29. NSMutableArray *_searchData;
  30. UISearchBar *_searchBar;
  31. UISearchDisplayController *_searchDisplayController;
  32. UIRefreshControl *_refreshControl;
  33. UIBarButtonItem *_menuButton;
  34. }
  35. @end
  36. @implementation VLCSharedLibraryListViewController
  37. - (void)loadView
  38. {
  39. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  40. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  41. _tableView.delegate = self;
  42. _tableView.dataSource = self;
  43. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  44. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  45. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  46. self.view = _tableView;
  47. }
  48. - (id)initWithHttpServer:(NSString *)serverName serverAddress:(NSString *)serverAddress portNumber:(NSString *)portNumber
  49. {
  50. self = [super init];
  51. if (self) {
  52. _httpServerName = serverName;
  53. _httpServerAddress = serverAddress;
  54. _httpServerPort = portNumber;
  55. _imageCache = [[NSCache alloc] init];
  56. [_imageCache setCountLimit:50];
  57. _httpParser = [[VLCSharedLibraryParser alloc] init];
  58. _httpParser.delegate = self;
  59. }
  60. return self;
  61. }
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [super viewWillAppear:animated];
  65. [_httpParser fetchDataFromServer:_httpServerAddress port:_httpServerPort.longLongValue];
  66. }
  67. - (void)viewDidLoad
  68. {
  69. [super viewDidLoad];
  70. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  71. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  72. self.title = _httpServerAddress;
  73. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  74. UINavigationBar *navBar = self.navigationController.navigationBar;
  75. if (SYSTEM_RUNS_IOS7_OR_LATER)
  76. _searchBar.barTintColor = navBar.barTintColor;
  77. _searchBar.tintColor = navBar.tintColor;
  78. _searchBar.translucent = navBar.translucent;
  79. _searchBar.opaque = navBar.opaque;
  80. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  81. _searchDisplayController.delegate = self;
  82. _searchDisplayController.searchResultsDataSource = self;
  83. _searchDisplayController.searchResultsDelegate = self;
  84. if (SYSTEM_RUNS_IOS7_OR_LATER)
  85. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  86. _searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  87. _searchDisplayController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  88. _searchBar.delegate = self;
  89. _searchBar.hidden = YES;
  90. UITapGestureRecognizer *tapTwiceGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwiceGestureAction:)];
  91. [tapTwiceGesture setNumberOfTapsRequired:2];
  92. [self.navigationController.navigationBar addGestureRecognizer:tapTwiceGesture];
  93. // Active le Pull down to refresh
  94. _refreshControl = [[UIRefreshControl alloc] init];
  95. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  96. _refreshControl.tintColor = [UIColor whiteColor];
  97. // Call the refresh function
  98. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  99. [self.tableView addSubview:_refreshControl];
  100. _searchData = [[NSMutableArray alloc] init];
  101. [_searchData removeAllObjects];
  102. }
  103. - (BOOL)shouldAutorotate
  104. {
  105. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  106. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  107. return NO;
  108. return YES;
  109. }
  110. #pragma mark - Table view data source
  111. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  112. {
  113. return 1;
  114. }
  115. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  116. {
  117. @synchronized(self) {
  118. if (tableView == self.searchDisplayController.searchResultsTableView)
  119. return _searchData.count;
  120. else
  121. return _serverDataArray.count;
  122. }
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. static NSString *CellIdentifier = @"libraryVLCCellDetail";
  127. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  128. if (cell == nil)
  129. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  130. NSDictionary *cellObject;
  131. @synchronized(self) {
  132. if (tableView == self.searchDisplayController.searchResultsTableView)
  133. cellObject = _searchData[indexPath.row];
  134. else
  135. cellObject = _serverDataArray[indexPath.row];
  136. }
  137. [cell setTitle:[cellObject objectForKey:@"title"]];
  138. [cell setIcon:[UIImage imageNamed:@"blank"]];
  139. NSString *thumbPath = [cellObject objectForKey:@"thumb"];
  140. if (thumbPath) {
  141. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  142. dispatch_async(queue, ^{
  143. UIImage *img = [self getCachedImage:thumbPath];
  144. dispatch_async(dispatch_get_main_queue(), ^{
  145. if (!img)
  146. [cell setIcon:[UIImage imageNamed:@"blank"]];
  147. else
  148. [cell setIcon:img];
  149. });
  150. });
  151. }
  152. NSInteger size = [[cellObject objectForKey:@"size"] integerValue];
  153. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  154. NSString *duration = [cellObject objectForKey:@"duration"];
  155. [cell setIsDirectory:NO];
  156. [cell setSubtitle:[NSString stringWithFormat:@"%@ (%@)", mediaSize, duration]];
  157. [cell setIsDownloadable:YES];
  158. [cell setDelegate:self];
  159. return cell;
  160. }
  161. - (UIImage *)getCachedImage:(NSString *)url
  162. {
  163. UIImage *image = [_imageCache objectForKey:url];
  164. if ((image != nil) && [image isKindOfClass:[UIImage class]]) {
  165. return image;
  166. } else {
  167. NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
  168. if (imageData) {
  169. image = [[UIImage alloc] initWithData:imageData];
  170. [_imageCache setObject:image forKey:url];
  171. }
  172. return image;
  173. }
  174. }
  175. #pragma mark - Table view delegate
  176. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  177. {
  178. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  179. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  180. }
  181. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  182. {
  183. NSDictionary *selectedObject;
  184. @synchronized(self) {
  185. if (tableView == self.searchDisplayController.searchResultsTableView)
  186. selectedObject = _searchData[indexPath.row];
  187. else
  188. selectedObject = _serverDataArray[indexPath.row];
  189. }
  190. NSString *URLofSubtitle = nil;
  191. if (![[selectedObject objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  192. NSURL *url = [NSURL URLWithString:[[selectedObject objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  193. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:YES];
  194. }
  195. NSURL *itemURL = [NSURL URLWithString:[selectedObject objectForKey:@"pathfile"]];
  196. if (itemURL) {
  197. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  198. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  199. }
  200. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  201. }
  202. #pragma mark - Specifics
  203. - (void)sharedLibraryDataProcessings:(NSArray *)result
  204. {
  205. @synchronized(self) {
  206. _serverDataArray = result;
  207. self.title = [_serverDataArray.firstObject objectForKey:@"libTitle"];
  208. }
  209. [self.tableView reloadData];
  210. }
  211. - (void)_downloadFileFromMediaItem:(NSURL *)itemURL
  212. {
  213. APLog(@"trying to download %@", [itemURL absoluteString]);
  214. if (![[itemURL absoluteString] isSupportedFormat]) {
  215. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  216. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [itemURL absoluteString]]
  217. delegate:self
  218. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  219. otherButtonTitles:nil];
  220. [alert show];
  221. } else if (itemURL) {
  222. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:nil];
  223. }
  224. }
  225. - (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream
  226. {
  227. NSString *FileSubtitlePath = nil;
  228. NSString *fileName = [[url path] lastPathComponent];
  229. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  230. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  231. NSArray *searchPaths = nil;
  232. if (modeStream)
  233. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  234. else
  235. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  236. NSString *directoryPath = [searchPaths objectAtIndex:0];
  237. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
  238. NSFileManager *fileManager = [NSFileManager defaultManager];
  239. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  240. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  241. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  242. APLog(@"file creation failed, no data was saved");
  243. }
  244. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  245. } else {
  246. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  247. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), fileName, [[UIDevice currentDevice] model]]
  248. delegate:self
  249. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  250. otherButtonTitles:nil];
  251. [alert show];
  252. }
  253. return FileSubtitlePath;
  254. }
  255. #pragma mark - VLCLocalNetworkListCell delegation
  256. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  257. {
  258. NSDictionary *dataItem;
  259. @synchronized(self) {
  260. if ([self.searchDisplayController isActive])
  261. dataItem = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  262. else
  263. dataItem = _serverDataArray[[self.tableView indexPathForCell:cell].row];
  264. }
  265. NSURL *itemURL = [NSURL URLWithString:[dataItem objectForKey:@"pathfile"]];
  266. NSInteger size = [[dataItem objectForKey:@"size"] integerValue];
  267. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  268. NSString *URLofSubtitle = nil;
  269. if (![[dataItem objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  270. NSURL *url = [NSURL URLWithString:[[dataItem objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  271. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:NO];
  272. }
  273. [self _downloadFileFromMediaItem:itemURL];
  274. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  275. } else {
  276. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  277. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [dataItem objectForKey:@"title"], [[UIDevice currentDevice] model]]
  278. delegate:self
  279. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  280. otherButtonTitles:nil];
  281. [alert show];
  282. }
  283. }
  284. #pragma mark - Search Display Controller Delegate
  285. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  286. {
  287. @synchronized (self) {
  288. [_searchData removeAllObjects];
  289. NSUInteger count = _serverDataArray.count;
  290. for (NSUInteger i = 0; i < count; i++) {
  291. NSRange nameRange;
  292. nameRange = [[_serverDataArray[i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  293. if (nameRange.location != NSNotFound)
  294. [_searchData addObject:_serverDataArray[i]];
  295. }
  296. }
  297. return YES;
  298. }
  299. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  300. {
  301. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  302. tableView.rowHeight = 80.0f;
  303. else
  304. tableView.rowHeight = 68.0f;
  305. tableView.backgroundColor = [UIColor blackColor];
  306. }
  307. #pragma mark - Refresh
  308. -(void)handleRefresh
  309. {
  310. //set the title while refreshing
  311. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH", nil)];
  312. //set the date and time of refreshing
  313. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  314. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  315. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE", nil),[formattedDate stringFromDate:[NSDate date]]];
  316. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  317. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  318. //end the refreshing
  319. [_refreshControl endRefreshing];
  320. @synchronized(self) {
  321. _serverDataArray = nil;
  322. }
  323. [_httpParser fetchDataFromServer:_httpServerAddress port:_httpServerPort.longLongValue];
  324. }
  325. #pragma mark - Gesture Action
  326. - (void)tapTwiceGestureAction:(UIGestureRecognizer *)recognizer
  327. {
  328. _searchBar.hidden = !_searchBar.hidden;
  329. if (_searchBar.hidden)
  330. self.tableView.tableHeaderView = nil;
  331. else
  332. self.tableView.tableHeaderView = _searchBar;
  333. [self.tableView setContentOffset:CGPointMake(0.0f, -self.tableView.contentInset.top) animated:NO];
  334. }
  335. @end