VLCLocalPlexFolderListViewController.m 16 KB

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