VLCLocalPlexFolderListViewController.m 15 KB

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