VLCLocalPlexFolderListViewController.m 17 KB

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