VLCSharedLibraryListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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>
  23. {
  24. NSMutableArray *_mutableObjectList;
  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. _mutableObjectList = [[NSMutableArray alloc] init];
  57. _imageCache = [[NSCache alloc] init];
  58. [_imageCache setCountLimit:50];
  59. _httpParser = [[VLCSharedLibraryParser alloc] init];
  60. }
  61. return self;
  62. }
  63. - (void)viewDidLoad
  64. {
  65. [super viewDidLoad];
  66. [_mutableObjectList removeAllObjects];
  67. _mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
  68. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  69. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  70. NSString *titleValue;
  71. if (_mutableObjectList.count == 0)
  72. titleValue = _httpServerAddress;
  73. else
  74. titleValue = [_mutableObjectList.firstObject objectForKey:@"libTitle"];
  75. self.title = titleValue;
  76. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  77. UINavigationBar *navBar = self.navigationController.navigationBar;
  78. if (SYSTEM_RUNS_IOS7_OR_LATER)
  79. _searchBar.barTintColor = navBar.barTintColor;
  80. _searchBar.tintColor = navBar.tintColor;
  81. _searchBar.translucent = navBar.translucent;
  82. _searchBar.opaque = navBar.opaque;
  83. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  84. _searchDisplayController.delegate = self;
  85. _searchDisplayController.searchResultsDataSource = self;
  86. _searchDisplayController.searchResultsDelegate = self;
  87. if (SYSTEM_RUNS_IOS7_OR_LATER)
  88. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  89. _searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  90. _searchDisplayController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  91. _searchBar.delegate = self;
  92. _searchBar.hidden = YES;
  93. UITapGestureRecognizer *tapTwiceGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwiceGestureAction:)];
  94. [tapTwiceGesture setNumberOfTapsRequired:2];
  95. [self.navigationController.navigationBar addGestureRecognizer:tapTwiceGesture];
  96. // Active le Pull down to refresh
  97. _refreshControl = [[UIRefreshControl alloc] init];
  98. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  99. _refreshControl.tintColor = [UIColor whiteColor];
  100. // Call the refresh function
  101. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  102. [self.tableView addSubview:_refreshControl];
  103. _searchData = [[NSMutableArray alloc] init];
  104. [_searchData removeAllObjects];
  105. }
  106. - (BOOL)shouldAutorotate
  107. {
  108. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  109. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  110. return NO;
  111. return YES;
  112. }
  113. #pragma mark - Table view data source
  114. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  115. {
  116. return 1;
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  119. {
  120. if (tableView == self.searchDisplayController.searchResultsTableView)
  121. return _searchData.count;
  122. else
  123. return _mutableObjectList.count;
  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. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  132. [ObjList removeAllObjects];
  133. if (tableView == self.searchDisplayController.searchResultsTableView)
  134. [ObjList addObjectsFromArray:_searchData];
  135. else
  136. [ObjList addObjectsFromArray:_mutableObjectList];
  137. [cell setTitle:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"title"]];
  138. [cell setIcon:[UIImage imageNamed:@"blank"]];
  139. NSString *thumbPath = [[ObjList objectAtIndex:indexPath.row] 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 = [[[ObjList objectAtIndex:indexPath.row] objectForKey:@"size"] integerValue];
  153. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  154. NSString *duration = [[ObjList objectAtIndex:indexPath.row] 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. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  184. [ObjList removeAllObjects];
  185. if (tableView == self.searchDisplayController.searchResultsTableView)
  186. [ObjList addObjectsFromArray:_searchData];
  187. else
  188. [ObjList addObjectsFromArray:_mutableObjectList];
  189. NSString *URLofSubtitle = nil;
  190. if (![[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  191. NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  192. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:YES];
  193. }
  194. NSURL *itemURL = [NSURL URLWithString:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathfile"]];
  195. if (itemURL) {
  196. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  197. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  198. }
  199. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  200. }
  201. #pragma mark - Specifics
  202. - (void)_downloadFileFromMediaItem:(NSURL *)itemURL
  203. {
  204. if (![[itemURL absoluteString] isSupportedFormat]) {
  205. 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];
  206. [alert show];
  207. } else if (itemURL) {
  208. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:nil];
  209. }
  210. }
  211. - (void)reloadSharedLibrary
  212. {
  213. [_mutableObjectList removeAllObjects];
  214. _mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
  215. [self.tableView reloadData];
  216. }
  217. - (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream
  218. {
  219. NSString *FileSubtitlePath = nil;
  220. NSString *fileName = [[url path] lastPathComponent];
  221. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  222. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  223. NSArray *searchPaths = nil;
  224. if (modeStream)
  225. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  226. else
  227. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  228. NSString *directoryPath = [searchPaths objectAtIndex:0];
  229. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
  230. NSFileManager *fileManager = [NSFileManager defaultManager];
  231. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  232. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  233. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  234. APLog(@"file creation failed, no data was saved");
  235. }
  236. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  237. } else {
  238. 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];
  239. [alert show];
  240. }
  241. return FileSubtitlePath;
  242. }
  243. #pragma mark - VLCLocalNetworkListCell delegation
  244. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  245. {
  246. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  247. [ObjList removeAllObjects];
  248. if ([self.searchDisplayController isActive])
  249. [ObjList addObject:_searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row]];
  250. else
  251. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:cell].row]];
  252. NSURL *itemURL = [NSURL URLWithString:[ObjList[0] objectForKey:@"pathfile"]];
  253. NSInteger size = [[[ObjList objectAtIndex:0] objectForKey:@"size"] integerValue];
  254. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  255. NSString *URLofSubtitle = nil;
  256. if (![[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  257. NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  258. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:NO];
  259. }
  260. [self _downloadFileFromMediaItem:itemURL];
  261. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  262. } else {
  263. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [[ObjList objectAtIndex:0] objectForKey:@"title"], [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  264. [alert show];
  265. }
  266. }
  267. #pragma mark - Search Display Controller Delegate
  268. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  269. {
  270. [_searchData removeAllObjects];
  271. NSUInteger count = _mutableObjectList.count;
  272. for (NSUInteger i = 0; i < count; i++) {
  273. NSRange nameRange;
  274. nameRange = [[[_mutableObjectList objectAtIndex:i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  275. if (nameRange.location != NSNotFound)
  276. [_searchData addObject:_mutableObjectList[i]];
  277. }
  278. return YES;
  279. }
  280. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  281. {
  282. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  283. tableView.rowHeight = 80.0f;
  284. else
  285. tableView.rowHeight = 68.0f;
  286. tableView.backgroundColor = [UIColor blackColor];
  287. }
  288. #pragma mark - Refresh
  289. -(void)handleRefresh
  290. {
  291. //set the title while refreshing
  292. _refreshControl.attributedTitle = [[NSAttributedString alloc]initWithString:NSLocalizedString(@"LOCAL_SERVER_REFRESH", nil)];
  293. //set the date and time of refreshing
  294. NSDateFormatter *formattedDate = [[NSDateFormatter alloc]init];
  295. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  296. NSString *lastupdated = [NSString stringWithFormat:NSLocalizedString(@"LOCAL_SERVER_LAST_UPDATE", nil),[formattedDate stringFromDate:[NSDate date]]];
  297. NSDictionary *attrsDictionary = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
  298. _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
  299. //end the refreshing
  300. [_refreshControl endRefreshing];
  301. [self performSelector:@selector(reloadSharedLibrary) withObject:nil];
  302. }
  303. #pragma mark - Gesture Action
  304. - (void)tapTwiceGestureAction:(UIGestureRecognizer *)recognizer
  305. {
  306. _searchBar.hidden = !_searchBar.hidden;
  307. if (_searchBar.hidden)
  308. self.tableView.tableHeaderView = nil;
  309. else
  310. self.tableView.tableHeaderView = _searchBar;
  311. [self.tableView setContentOffset:CGPointMake(0.0f, -self.tableView.contentInset.top) animated:NO];
  312. }
  313. @end