VLCSharedLibraryListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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 "UINavigationController+Theme.h"
  18. #import "VLCDownloadViewController.h"
  19. #import "NSString+SupportedMedia.h"
  20. #import "VLCStatusLabel.h"
  21. #import "VLCAlertView.h"
  22. #import "UIDevice+VLC.h"
  23. @interface VLCSharedLibraryListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate>
  24. {
  25. NSMutableArray *_mutableObjectList;
  26. NSCache *_imageCache;
  27. NSString *_httpServerName;
  28. NSString *_httpServerAddress;
  29. NSString *_httpServerPort;
  30. VLCSharedLibraryParser *_httpParser;
  31. NSMutableArray *_searchData;
  32. UISearchBar *_searchBar;
  33. UISearchDisplayController *_searchDisplayController;
  34. UIRefreshControl *_refreshControl;
  35. UIBarButtonItem *_menuButton;
  36. }
  37. @end
  38. @implementation VLCSharedLibraryListViewController
  39. - (void)loadView
  40. {
  41. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  42. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  43. _tableView.delegate = self;
  44. _tableView.dataSource = self;
  45. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  46. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  47. _tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  48. self.view = _tableView;
  49. }
  50. - (id)initWithHttpServer:(NSString *)serverName serverAddress:(NSString *)serverAddress portNumber:(NSString *)portNumber
  51. {
  52. self = [super init];
  53. if (self) {
  54. _httpServerName = serverName;
  55. _httpServerAddress = serverAddress;
  56. _httpServerPort = portNumber;
  57. _mutableObjectList = [[NSMutableArray alloc] init];
  58. _imageCache = [[NSCache alloc] init];
  59. [_imageCache setCountLimit:50];
  60. _httpParser = [[VLCSharedLibraryParser alloc] init];
  61. }
  62. return self;
  63. }
  64. - (void)viewDidLoad
  65. {
  66. [super viewDidLoad];
  67. [_mutableObjectList removeAllObjects];
  68. _mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
  69. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  70. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  71. NSString *titleValue;
  72. if (_mutableObjectList.count == 0)
  73. titleValue = _httpServerAddress;
  74. else
  75. titleValue = [_mutableObjectList[0] objectForKey:@"libTitle"];
  76. self.title = titleValue;
  77. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  78. UINavigationBar *navBar = self.navigationController.navigationBar;
  79. if (SYSTEM_RUNS_IOS7_OR_LATER)
  80. _searchBar.barTintColor = navBar.barTintColor;
  81. _searchBar.tintColor = navBar.tintColor;
  82. _searchBar.translucent = navBar.translucent;
  83. _searchBar.opaque = navBar.opaque;
  84. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  85. _searchDisplayController.delegate = self;
  86. _searchDisplayController.searchResultsDataSource = self;
  87. _searchDisplayController.searchResultsDelegate = self;
  88. if (SYSTEM_RUNS_IOS7_OR_LATER)
  89. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  90. _searchDisplayController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  91. _searchDisplayController.searchResultsTableView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
  92. _searchBar.delegate = self;
  93. _searchBar.hidden = YES;
  94. UITapGestureRecognizer *tapTwiceGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTwiceGestureAction:)];
  95. [tapTwiceGesture setNumberOfTapsRequired:2];
  96. [self.navigationController.navigationBar addGestureRecognizer:tapTwiceGesture];
  97. // Active le Pull down to refresh
  98. _refreshControl = [[UIRefreshControl alloc] init];
  99. _refreshControl.backgroundColor = [UIColor VLCDarkBackgroundColor];
  100. _refreshControl.tintColor = [UIColor whiteColor];
  101. // Call the refresh function
  102. [_refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  103. [self.tableView addSubview:_refreshControl];
  104. _searchData = [[NSMutableArray alloc] init];
  105. [_searchData removeAllObjects];
  106. }
  107. - (BOOL)shouldAutorotate
  108. {
  109. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  110. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  111. return NO;
  112. return YES;
  113. }
  114. #pragma mark - Table view data source
  115. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  116. {
  117. return 1;
  118. }
  119. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  120. {
  121. if (tableView == self.searchDisplayController.searchResultsTableView)
  122. return _searchData.count;
  123. else
  124. return _mutableObjectList.count;
  125. }
  126. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. static NSString *CellIdentifier = @"libraryVLCCellDetail";
  129. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  130. if (cell == nil)
  131. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  132. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  133. [ObjList removeAllObjects];
  134. if (tableView == self.searchDisplayController.searchResultsTableView)
  135. [ObjList addObjectsFromArray:_searchData];
  136. else
  137. [ObjList addObjectsFromArray:_mutableObjectList];
  138. [cell setTitle:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"title"]];
  139. [cell setIcon:[UIImage imageNamed:@"blank"]];
  140. NSString *thumbPath = [[ObjList objectAtIndex:indexPath.row] 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 = [[[ObjList objectAtIndex:indexPath.row] objectForKey:@"size"] integerValue];
  154. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  155. NSString *duration = [[ObjList objectAtIndex:indexPath.row] 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. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  185. [ObjList removeAllObjects];
  186. if (tableView == self.searchDisplayController.searchResultsTableView)
  187. [ObjList addObjectsFromArray:_searchData];
  188. else
  189. [ObjList addObjectsFromArray:_mutableObjectList];
  190. NSString *URLofSubtitle = nil;
  191. if (![[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  192. NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  193. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:YES];
  194. }
  195. NSURL *itemURL = [NSURL URLWithString:[[ObjList objectAtIndex:indexPath.row] 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)_downloadFileFromMediaItem:(NSURL *)itemURL
  204. {
  205. if (![[itemURL absoluteString] isSupportedFormat]) {
  206. 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];
  207. [alert show];
  208. } else if (itemURL) {
  209. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:nil];
  210. }
  211. }
  212. - (void)reloadSharedLibrary
  213. {
  214. [_mutableObjectList removeAllObjects];
  215. _mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
  216. [self.tableView reloadData];
  217. }
  218. - (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream
  219. {
  220. NSString *FileSubtitlePath = nil;
  221. NSString *fileName = [[url path] lastPathComponent];
  222. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  223. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  224. NSArray *searchPaths = nil;
  225. if (modeStream)
  226. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  227. else
  228. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  229. NSString *directoryPath = [searchPaths objectAtIndex:0];
  230. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:fileName];
  231. NSFileManager *fileManager = [NSFileManager defaultManager];
  232. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  233. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  234. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  235. APLog(@"file creation failed, no data was saved");
  236. }
  237. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  238. } else {
  239. 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];
  240. [alert show];
  241. }
  242. return FileSubtitlePath;
  243. }
  244. #pragma mark - VLCLocalNetworkListCell delegation
  245. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  246. {
  247. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  248. [ObjList removeAllObjects];
  249. if ([self.searchDisplayController isActive])
  250. [ObjList addObject:_searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row]];
  251. else
  252. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:cell].row]];
  253. NSURL *itemURL = [NSURL URLWithString:[ObjList[0] objectForKey:@"pathfile"]];
  254. NSInteger size = [[[ObjList objectAtIndex:0] objectForKey:@"size"] integerValue];
  255. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  256. NSString *URLofSubtitle = nil;
  257. if (![[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
  258. NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  259. URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:NO];
  260. }
  261. [self _downloadFileFromMediaItem:itemURL];
  262. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  263. } else {
  264. 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];
  265. [alert show];
  266. }
  267. }
  268. #pragma mark - Search Display Controller Delegate
  269. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  270. {
  271. [_searchData removeAllObjects];
  272. for (int i = 0; i < [_mutableObjectList 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