VLCLocalPlexFolderListViewController.m 16 KB

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