VLCSharedLibraryListViewController.m 15 KB

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