VLCLocalPlexFolderListViewController.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. NSString *_PlexServerName;
  26. NSString *_PlexServerAddress;
  27. NSString *_PlexServerPort;
  28. NSString *_PlexServerPath;
  29. VLCPlexParser *_PlexParser;
  30. NSMutableArray *_searchData;
  31. UISearchBar *_searchBar;
  32. UISearchDisplayController *_searchDisplayController;
  33. }
  34. @end
  35. @implementation VLCLocalPlexFolderListViewController
  36. - (void)loadView
  37. {
  38. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  39. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  40. _tableView.delegate = self;
  41. _tableView.dataSource = self;
  42. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  43. self.view = _tableView;
  44. }
  45. - (id)initWithPlexServer:(NSString *)serverName serverAddress:(NSString *)serverAddress portNumber:(NSString *)portNumber atPath:(NSString *)path
  46. {
  47. self = [super init];
  48. if (self) {
  49. _PlexServerName = serverName;
  50. _PlexServerAddress = serverAddress;
  51. _PlexServerPort = portNumber;
  52. _PlexServerPath = path;
  53. _mutableObjectList = [[NSMutableArray alloc] init];
  54. _PlexParser = [[VLCPlexParser alloc] init];
  55. }
  56. return self;
  57. }
  58. - (void)viewDidLoad
  59. {
  60. [super viewDidLoad];
  61. [_mutableObjectList removeAllObjects];
  62. _mutableObjectList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:_PlexServerPath];
  63. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  64. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  65. NSString *titleValue;
  66. if ([_PlexServerPath isEqualToString:@""])
  67. titleValue = _PlexServerName;
  68. else
  69. titleValue = [_PlexServerPath lastPathComponent];
  70. self.title = titleValue;
  71. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  72. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  73. _searchDisplayController.delegate = self;
  74. _searchDisplayController.searchResultsDataSource = self;
  75. _searchDisplayController.searchResultsDelegate = self;
  76. if (SYSTEM_RUNS_IOS7_OR_LATER)
  77. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  78. _searchBar.delegate = self;
  79. self.tableView.tableHeaderView = _searchBar;
  80. _searchData = [[NSMutableArray alloc] init];
  81. [_searchData removeAllObjects];
  82. }
  83. - (BOOL)shouldAutorotate
  84. {
  85. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  86. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  87. return NO;
  88. return YES;
  89. }
  90. #pragma mark - Table view data source
  91. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  92. {
  93. return 1;
  94. }
  95. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  96. {
  97. if (tableView == self.searchDisplayController.searchResultsTableView)
  98. return _searchData.count;
  99. else
  100. return _mutableObjectList.count;
  101. }
  102. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  103. {
  104. static NSString *CellIdentifier = @"PlexCellDetail";
  105. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[[self tableView] dequeueReusableCellWithIdentifier:CellIdentifier];
  106. if (cell == nil)
  107. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  108. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  109. [ObjList removeAllObjects];
  110. if (tableView == self.searchDisplayController.searchResultsTableView)
  111. [ObjList addObjectsFromArray:_searchData];
  112. else
  113. [ObjList addObjectsFromArray:_mutableObjectList];
  114. [cell setTitle:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"title"]];
  115. NSString *thumbPath = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"thumb"];
  116. if (thumbPath)
  117. [cell setIconURL:[NSURL URLWithString:thumbPath]];
  118. if ([[[ObjList objectAtIndex:indexPath.row] objectForKey:@"container"] isEqualToString:@"item"]) {
  119. UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeRightGestureAction:)];
  120. [swipeRight setDirection:(UISwipeGestureRecognizerDirectionRight)];
  121. [cell addGestureRecognizer:swipeRight];
  122. NSInteger size = [[[ObjList objectAtIndex:indexPath.row] objectForKey:@"size"] integerValue];
  123. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  124. NSString *durationInSeconds = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"duration"];
  125. [cell setIsDirectory:NO];
  126. [cell setIcon:[UIImage imageNamed:@"blank"]];
  127. [cell setSubtitle:[NSString stringWithFormat:@"%@ (%@)", mediaSize, durationInSeconds]];
  128. [cell setIsDownloadable:YES];
  129. [cell setDelegate:self];
  130. } else {
  131. [cell setIsDirectory:YES];
  132. [cell setIcon:[UIImage imageNamed:@"folder"]];
  133. }
  134. return cell;
  135. }
  136. #pragma mark - Table view delegate
  137. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  140. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  141. }
  142. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  145. [ObjList removeAllObjects];
  146. NSString *newPath = nil;
  147. if (tableView == self.searchDisplayController.searchResultsTableView)
  148. [ObjList addObjectsFromArray:_searchData];
  149. else
  150. [ObjList addObjectsFromArray:_mutableObjectList];
  151. NSString *keyValue = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"key"];
  152. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  153. newPath = [_PlexServerPath stringByAppendingPathComponent:keyValue];
  154. else
  155. newPath = keyValue;
  156. if ([[[ObjList objectAtIndex:indexPath.row] objectForKey:@"container"] isEqualToString:@"item"]) {
  157. [ObjList removeAllObjects];
  158. ObjList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:newPath];
  159. NSString *URLofSubtitle = nil;
  160. if ([[ObjList objectAtIndex:0] objectForKey:@"keySubtitle"])
  161. URLofSubtitle = [self _getFileSubtitleFromPlexServer:ObjList modeStream:YES];
  162. NSURL *itemURL = [NSURL URLWithString:[[ObjList objectAtIndex:0] objectForKey:@"keyMedia"]];
  163. if (itemURL) {
  164. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  165. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  166. }
  167. } else {
  168. VLCLocalPlexFolderListViewController *targetViewController = [[VLCLocalPlexFolderListViewController alloc] initWithPlexServer:_PlexServerName serverAddress:_PlexServerAddress portNumber:_PlexServerPort atPath:newPath];
  169. [[self navigationController] pushViewController:targetViewController animated:YES];
  170. }
  171. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  172. }
  173. #pragma mark - Specifics
  174. - (void)_downloadFileFromMediaItem:(NSMutableArray *)mutableMediaObject
  175. {
  176. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  177. if (![[itemURL absoluteString] isSupportedFormat]) {
  178. 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];
  179. [alert show];
  180. } else if (itemURL) {
  181. NSString *fileName = [[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"];
  182. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  183. }
  184. }
  185. - (NSString *)_getFileSubtitleFromPlexServer:(NSMutableArray *)mutableMediaObject modeStream:(BOOL)modeStream
  186. {
  187. NSURL *url = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"]];
  188. NSString *receivedSub = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
  189. NSArray *searchPaths = nil;
  190. if (modeStream)
  191. searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  192. else
  193. searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  194. NSString *directoryPath = [searchPaths objectAtIndex:0];
  195. NSString *FileSubtitlePath = [[directoryPath stringByAppendingPathComponent:[[[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"] stringByDeletingPathExtension]] stringByAppendingPathExtension:[[mutableMediaObject objectAtIndex:0] objectForKey:@"codecSubtitle"]];
  196. NSFileManager *fileManager = [NSFileManager defaultManager];
  197. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  198. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  199. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  200. APLog(@"file creation failed, no data was saved");
  201. }
  202. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  203. return FileSubtitlePath;
  204. }
  205. - (void)swipeRightGestureAction:(UIGestureRecognizer *)recognizer
  206. {
  207. NSIndexPath *swipedIndexPath = [self.tableView indexPathForRowAtPoint:[recognizer locationInView:self.tableView]];
  208. UITableViewCell *swipedCell = [self.tableView cellForRowAtIndexPath:swipedIndexPath];
  209. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[[self tableView] cellForRowAtIndexPath:swipedIndexPath];
  210. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  211. [ObjList removeAllObjects];
  212. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:swipedCell].row]];
  213. NSString *ratingKey = [[ObjList objectAtIndex:0] objectForKey:@"ratingKey"];
  214. NSString *tag = [[ObjList objectAtIndex:0] objectForKey:@"state"];
  215. NSString *cellStatusLbl = nil;
  216. NSInteger status = [_PlexParser MarkWatchedUnwatchedMedia:_PlexServerAddress port:_PlexServerPort videoRatingKey:ratingKey state:tag];
  217. if (status == 200) {
  218. if ([tag isEqualToString:@"watched"]) {
  219. tag = @"unwatched";
  220. cellStatusLbl = NSLocalizedString(@"PLEX_UNWATCHED", @"");
  221. } else if ([tag isEqualToString:@"unwatched"]) {
  222. tag = @"watched";
  223. cellStatusLbl = NSLocalizedString(@"PLEX_WATCHED", @"");
  224. }
  225. } else
  226. cellStatusLbl = NSLocalizedString(@"PLEX_ERROR_MARK", @"");
  227. [cell.statusLabel showStatusMessage:cellStatusLbl];
  228. [[_mutableObjectList objectAtIndex:[self.tableView indexPathForCell:swipedCell].row] setObject:tag forKey:@"state"];
  229. }
  230. #pragma mark - VLCLocalNetworkListCell delegation
  231. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  232. {
  233. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  234. [ObjList removeAllObjects];
  235. if ([self.searchDisplayController isActive])
  236. [ObjList addObject:_searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row]];
  237. else
  238. [ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:cell].row]];
  239. NSString *path = [[ObjList objectAtIndex:0] objectForKey:@"key"];
  240. [ObjList removeAllObjects];
  241. ObjList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:path];
  242. if ([[ObjList objectAtIndex:0] objectForKey:@"keySubtitle"])
  243. [self _getFileSubtitleFromPlexServer:ObjList modeStream:NO];
  244. [self _downloadFileFromMediaItem:ObjList];
  245. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  246. }
  247. #pragma mark - Search Display Controller Delegate
  248. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  249. {
  250. [_searchData removeAllObjects];
  251. for (int i = 0; i < [_mutableObjectList count]; i++) {
  252. NSRange nameRange;
  253. nameRange = [[[_mutableObjectList objectAtIndex:i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  254. if (nameRange.location != NSNotFound)
  255. [_searchData addObject:_mutableObjectList[i]];
  256. }
  257. return YES;
  258. }
  259. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  260. {
  261. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  262. tableView.rowHeight = 80.0f;
  263. else
  264. tableView.rowHeight = 68.0f;
  265. tableView.backgroundColor = [UIColor blackColor];
  266. }
  267. @end