VLCSharedLibraryListViewController.m 16 KB

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