VLCLocalPlexFolderListViewController.m 16 KB

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