VLCLocalPlexFolderListViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*****************************************************************************
  2. * VLCLocalPlexFolderListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014 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 "VLCLocalPlexFolderListViewController.h"
  13. #import "VLCPlexParser.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. @interface VLCLocalPlexFolderListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate>
  22. {
  23. UIBarButtonItem *_backButton;
  24. NSMutableArray *_mutableObjectList;
  25. NSCache *_imageCache;
  26. NSString *_PlexServerName;
  27. NSString *_PlexServerAddress;
  28. NSString *_PlexServerPort;
  29. NSString *_PlexServerPath;
  30. VLCPlexParser *_PlexParser;
  31. NSMutableArray *_searchData;
  32. UISearchBar *_searchBar;
  33. UISearchDisplayController *_searchDisplayController;
  34. UIRefreshControl *refreshControl;
  35. }
  36. @end
  37. @implementation VLCLocalPlexFolderListViewController
  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)initWithPlexServer:(NSString *)serverName serverAddress:(NSString *)serverAddress portNumber:(NSString *)portNumber atPath:(NSString *)path
  50. {
  51. self = [super init];
  52. if (self) {
  53. _PlexServerName = serverName;
  54. _PlexServerAddress = serverAddress;
  55. _PlexServerPort = portNumber;
  56. _PlexServerPath = path;
  57. _mutableObjectList = [[NSMutableArray alloc] init];
  58. _imageCache = [[NSCache alloc] init];
  59. [_imageCache setCountLimit:50];
  60. _PlexParser = [[VLCPlexParser alloc] init];
  61. }
  62. return self;
  63. }
  64. - (void)viewDidLoad
  65. {
  66. [super viewDidLoad];
  67. [_mutableObjectList removeAllObjects];
  68. _mutableObjectList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:_PlexServerPath];
  69. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  70. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  71. NSString *titleValue;
  72. if ([_PlexServerPath isEqualToString:@""])
  73. titleValue = _PlexServerName;
  74. else
  75. titleValue = [_mutableObjectList[0] objectForKey:@"libTitle"];
  76. self.title = titleValue;
  77. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  78. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  79. _searchDisplayController.delegate = self;
  80. _searchDisplayController.searchResultsDataSource = self;
  81. _searchDisplayController.searchResultsDelegate = self;
  82. if (SYSTEM_RUNS_IOS7_OR_LATER)
  83. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  84. _searchBar.delegate = self;
  85. self.tableView.tableHeaderView = _searchBar;
  86. // Active le Pull down to refresh
  87. refreshControl = [[UIRefreshControl alloc] init];
  88. // Call the refresh function
  89. [refreshControl addTarget:self action:@selector(handleRefresh) forControlEvents:UIControlEventValueChanged];
  90. [self.tableView addSubview:refreshControl];
  91. _searchData = [[NSMutableArray alloc] init];
  92. [_searchData removeAllObjects];
  93. }
  94. - (BOOL)shouldAutorotate
  95. {
  96. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  97. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  98. return NO;
  99. return YES;
  100. }
  101. #pragma mark - Table view data source
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  103. {
  104. return 1;
  105. }
  106. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  107. {
  108. if (tableView == self.searchDisplayController.searchResultsTableView)
  109. return _searchData.count;
  110. else
  111. return _mutableObjectList.count;
  112. }
  113. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  114. {
  115. static NSString *CellIdentifier = @"PlexCellDetail";
  116. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  117. if (cell == nil)
  118. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  119. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  120. [ObjList removeAllObjects];
  121. if (tableView == self.searchDisplayController.searchResultsTableView)
  122. [ObjList addObjectsFromArray:_searchData];
  123. else
  124. [ObjList addObjectsFromArray:_mutableObjectList];
  125. [cell setTitle:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"title"]];
  126. [cell setIcon:[UIImage imageNamed:@"blank"]];
  127. NSString *thumbPath = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"thumb"];
  128. if (thumbPath) {
  129. dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
  130. dispatch_async(queue, ^{
  131. UIImage *img = [self getCachedImage:thumbPath];
  132. dispatch_async(dispatch_get_main_queue(), ^{
  133. if (!img)
  134. [cell setIcon:[UIImage imageNamed:@"blank"]];
  135. else
  136. [cell setIcon:img];
  137. });
  138. });
  139. }
  140. if ([[[ObjList objectAtIndex:indexPath.row] objectForKey:@"container"] isEqualToString:@"item"]) {
  141. UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightGestureAction:)];
  142. [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
  143. [cell addGestureRecognizer:swipeRight];
  144. NSInteger size = [[[ObjList objectAtIndex:indexPath.row] objectForKey:@"size"] integerValue];
  145. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  146. NSString *durationInSeconds = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"duration"];
  147. [cell setIsDirectory:NO];
  148. [cell setSubtitle:[NSString stringWithFormat:@"%@ (%@)", mediaSize, durationInSeconds]];
  149. [cell setIsDownloadable:YES];
  150. [cell setDelegate:self];
  151. } else {
  152. [cell setIsDirectory:YES];
  153. if (!thumbPath)
  154. [cell setIcon:[UIImage imageNamed:@"folder"]];
  155. }
  156. return cell;
  157. }
  158. - (UIImage *)getCachedImage:(NSString *)url
  159. {
  160. UIImage *image = [_imageCache objectForKey:url];
  161. if ((image != nil) && [image isKindOfClass:[UIImage class]]) {
  162. return image;
  163. } else {
  164. NSData *imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
  165. if (imageData) {
  166. image = [[UIImage alloc] initWithData:imageData];
  167. [_imageCache setObject:image forKey:url];
  168. }
  169. return image;
  170. }
  171. }
  172. #pragma mark - Table view delegate
  173. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  174. {
  175. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  176. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  181. [ObjList removeAllObjects];
  182. NSString *newPath = nil;
  183. if (tableView == self.searchDisplayController.searchResultsTableView)
  184. [ObjList addObjectsFromArray:_searchData];
  185. else
  186. [ObjList addObjectsFromArray:_mutableObjectList];
  187. NSString *keyValue = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"key"];
  188. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  189. newPath = [_PlexServerPath stringByAppendingPathComponent:keyValue];
  190. else
  191. newPath = keyValue;
  192. if ([[[ObjList objectAtIndex:indexPath.row] objectForKey:@"container"] isEqualToString:@"item"]) {
  193. [ObjList removeAllObjects];
  194. ObjList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:newPath];
  195. NSString *URLofSubtitle = nil;
  196. if ([[ObjList objectAtIndex:0] objectForKey:@"keySubtitle"])
  197. URLofSubtitle = [self _getFileSubtitleFromPlexServer:ObjList modeStream:YES];
  198. NSURL *itemURL = [NSURL URLWithString:[[ObjList objectAtIndex:0] objectForKey:@"keyMedia"]];
  199. if (itemURL) {
  200. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  201. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  202. }
  203. } else {
  204. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:_PlexServerName serverAddress:_PlexServerAddress portNumber:_PlexServerPort atPath:newPath];
  205. [[self navigationController] pushViewController:targetViewController animated:YES];
  206. }
  207. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  208. }
  209. #pragma mark - Specifics
  210. - (void)_downloadFileFromMediaItem:(NSMutableArray *)mutableMediaObject
  211. {
  212. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  213. if (![[itemURL absoluteString] isSupportedFormat]) {
  214. 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];
  215. [alert show];
  216. } else if (itemURL) {
  217. NSString *fileName = [[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"];
  218. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  219. }
  220. }
  221. - (NSString *)_getFileSubtitleFromPlexServer:(NSMutableArray *)mutableMediaObject modeStream:(BOOL)modeStream
  222. {
  223. NSURL *url = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"]];
  224. NSString *receivedSub = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
  225. NSArray *searchPaths = nil;
  226. if (modeStream)
  227. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  228. else
  229. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  230. NSString *directoryPath = [searchPaths objectAtIndex:0];
  231. NSString *FileSubtitlePath = [[directoryPath stringByAppendingPathComponent:[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension]] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
  232. NSFileManager *fileManager = [NSFileManager defaultManager];
  233. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  234. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  235. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  236. APLog(@"file creation failed, no data was saved");
  237. }
  238. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  239. return FileSubtitlePath;
  240. }
  241. - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
  242. {
  243. NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:[recognizer locationInView:self.tableView]];
  244. UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
  245. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[[self tableView] cellForRowAtIndexPath:swipedIndexPath];
  246. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  247. [ObjList removeAllObjects];
  248. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:swipedCell].row]];
  249. NSString *ratingKey = [[ObjList objectAtIndex:0] objectForKey:@"ratingKey"];
  250. NSString *tag = [[ObjList objectAtIndex:0] objectForKey:@"state"];
  251. NSString *cellStatusLbl = nil;
  252. NSInteger status = [_PlexParser MarkWatchedUnwatchedMedia:_PlexServerAddress port:_PlexServerPort videoRatingKey:ratingKey state:tag];
  253. if (status == 200) {
  254. if ([tag isEqualToString:@"watched"]) {
  255. tag = @"unwatched";
  256. cellStatusLbl = NSLocalizedString(@"PLEX_UNWATCHED", nil);
  257. } else if ([tag isEqualToString:@"unwatched"]) {
  258. tag = @"watched";
  259. cellStatusLbl = NSLocalizedString(@"PLEX_WATCHED", nil);
  260. }
  261. } else
  262. cellStatusLbl = NSLocalizedString(@"PLEX_ERROR_MARK", nil);
  263. [cell.statusLabel showStatusMessage:cellStatusLbl];
  264. [[_mutableObjectList objectAtIndex:[self.tableView indexPathForCell:swipedCell].row] setObject:tag forKey:@"state"];
  265. }
  266. - (void)reloadTableViewPlex
  267. {
  268. [_mutableObjectList removeAllObjects];
  269. _mutableObjectList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:_PlexServerPath];
  270. [self.tableView reloadData];
  271. }
  272. #pragma mark - VLCLocalNetworkListCell delegation
  273. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  274. {
  275. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  276. [ObjList removeAllObjects];
  277. if ([self.searchDisplayController isActive])
  278. [ObjList addObject:_searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row]];
  279. else
  280. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:cell].row]];
  281. NSString *path = [[ObjList objectAtIndex:0] objectForKey:@"key"];
  282. [ObjList removeAllObjects];
  283. ObjList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:path];
  284. if ([[ObjList objectAtIndex:0] objectForKey:@"keySubtitle"])
  285. [self _getFileSubtitleFromPlexServer:ObjList modeStream:NO];
  286. [self _downloadFileFromMediaItem:ObjList];
  287. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  288. }
  289. #pragma mark - Search Display Controller Delegate
  290. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  291. {
  292. [_searchData removeAllObjects];
  293. for (int i = 0; i < [_mutableObjectList count]; i++) {
  294. NSRange nameRange;
  295. nameRange = [[[_mutableObjectList objectAtIndex:i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  296. if (nameRange.location != NSNotFound)
  297. [_searchData addObject:_mutableObjectList[i]];
  298. }
  299. return YES;
  300. }
  301. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  302. {
  303. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  304. tableView.rowHeight = 80.0f;
  305. else
  306. tableView.rowHeight = 68.0f;
  307. tableView.backgroundColor = [UIColor blackColor];
  308. }
  309. #pragma mark - Refresh
  310. -(void)handleRefresh
  311. {
  312. //set the title while refreshing
  313. refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:@"Refresh"];
  314. //set the date and time of refreshing
  315. NSDateFormatter *formattedDate = [[NSDateFormatter alloc] init];
  316. [formattedDate setDateFormat:@"MMM d, h:mm a"];
  317. NSString *lastupdated = [NSString stringWithFormat:@"Last Updated on %@", [formattedDate stringFromDate:[NSDate date]]];
  318. refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated];
  319. //end the refreshing
  320. [refreshControl endRefreshing];
  321. [self performSelector:@selector(reloadTableViewPlex) withObject:nil];
  322. }
  323. @end