VLCPlexMediaInformationViewController.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*****************************************************************************
  2. * VLCPlexMediaInformationViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. *
  7. * Authors: Pierre Sagaspe <pierre.sagaspe # me.com>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCPlexMediaInformationViewController.h"
  12. #import "VLCPlexParser.h"
  13. #import "VLCAppDelegate.h"
  14. #import "NSString+SupportedMedia.h"
  15. #import "UIDevice+VLC.h"
  16. @interface VLCPlexMediaInformationViewController ()
  17. {
  18. NSMutableArray *_mutableMediaInformation;
  19. NSString *_PlexServerAddress;
  20. NSString *_PlexServerPort;
  21. NSString *_PlexServerPath;
  22. VLCPlexParser *_PlexParser;
  23. }
  24. @end
  25. @implementation VLCPlexMediaInformationViewController
  26. - (id)initPlexMediaInformation:(NSMutableArray *)mediaInformation serverAddress:(NSString *)serverAddress portNumber:(NSString *)portNumber atPath:(NSString *)path
  27. {
  28. self = [super init];
  29. if (self) {
  30. _mutableMediaInformation = [[NSMutableArray alloc] init];
  31. [_mutableMediaInformation addObjectsFromArray:mediaInformation];
  32. _PlexServerAddress = serverAddress;
  33. _PlexServerPort = portNumber;
  34. _PlexServerPath = path;
  35. _PlexParser = [[VLCPlexParser alloc] init];
  36. }
  37. return self;
  38. }
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  42. [self.view setBackgroundColor:[UIColor VLCDarkBackgroundColor]];
  43. [self.summary setBackgroundColor:[UIColor VLCDarkBackgroundColor]];
  44. self.automaticallyAdjustsScrollViewInsets = NO;
  45. self.navigationController.navigationBar.translucent = NO;
  46. NSString *title = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"title"];
  47. NSString *thumbPath = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"thumb"];
  48. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:thumbPath]]];
  49. NSInteger size = [[[_mutableMediaInformation objectAtIndex:0] objectForKey:@"size"] integerValue];
  50. NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
  51. NSString *durationInSeconds = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"duration"];
  52. NSString *displaySize = [NSString stringWithFormat:@"%@ (%@)", mediaSize, durationInSeconds];
  53. NSString *tag = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"state"];
  54. NSString *displaySummary = [NSString stringWithFormat:@"%@", [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"summary"]];
  55. NSString *audioCodec = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"audioCodec"];
  56. if (!audioCodec)
  57. audioCodec = @"no track";
  58. NSString *videoCodec = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"videoCodec"];
  59. if (!videoCodec)
  60. videoCodec = @"no track";
  61. NSString *displayCodec = [NSString stringWithFormat:@"audio(%@) video(%@)", audioCodec, videoCodec];
  62. NSString *grandparentTitle = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"grandparentTitle"];
  63. if (grandparentTitle)
  64. self.title = grandparentTitle;
  65. [self.thumb setContentMode:UIViewContentModeScaleAspectFit];
  66. [self.thumb setImage:image];
  67. [self.mediaTitle setText:title];
  68. [self.codec setText:displayCodec];
  69. [self.size setText:displaySize];
  70. [self.summary setText:displaySummary];
  71. if ([tag isEqualToString:@"watched"]) {
  72. [self.badgeUnread setHidden:YES];
  73. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  74. } else if ([tag isEqualToString:@"unwatched"]) {
  75. [self.badgeUnread setHidden:NO];
  76. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  77. } else {
  78. [self.badgeUnread setHidden:NO];
  79. [self.markMediaButton setEnabled:NO];
  80. }
  81. [self.badgeUnread setNeedsDisplay];
  82. }
  83. - (void)viewWillAppear:(BOOL)animated
  84. {
  85. [super viewWillAppear:animated];
  86. }
  87. #pragma mark - Specifics
  88. - (void)_playMediaItem:(NSMutableArray *)mutableMediaObject
  89. {
  90. NSString *newPath = nil;
  91. NSString *keyValue = [[mutableMediaObject objectAtIndex:0] objectForKey:@"key"];
  92. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  93. newPath = [_PlexServerPath stringByAppendingPathComponent:keyValue];
  94. else
  95. newPath = keyValue;
  96. if ([[[mutableMediaObject objectAtIndex:0] objectForKey:@"container"] isEqualToString:@"item"]) {
  97. [mutableMediaObject removeAllObjects];
  98. mutableMediaObject = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:newPath];
  99. NSString *URLofSubtitle = nil;
  100. if ([[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"])
  101. URLofSubtitle = [_PlexParser getFileSubtitleFromPlexServer:mutableMediaObject modeStream:YES];
  102. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  103. if (itemURL) {
  104. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  105. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  106. }
  107. }
  108. }
  109. - (void)_download:(NSMutableArray *)mutableMediaObject
  110. {
  111. NSString *path = [[mutableMediaObject objectAtIndex:0] objectForKey:@"key"];
  112. [mutableMediaObject removeAllObjects];
  113. mutableMediaObject = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:path];
  114. NSInteger size = [[[mutableMediaObject objectAtIndex:0] objectForKey:@"size"] integerValue];
  115. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  116. if ([[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"])
  117. [_PlexParser getFileSubtitleFromPlexServer:mutableMediaObject modeStream:NO];
  118. [self _downloadFileFromMediaItem:mutableMediaObject];
  119. } else {
  120. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [[mutableMediaObject objectAtIndex:0] objectForKey:@"title"], [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
  121. [alert show];
  122. }
  123. }
  124. - (void)_downloadFileFromMediaItem:(NSMutableArray *)mutableMediaObject
  125. {
  126. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  127. if (![[itemURL absoluteString] isSupportedFormat]) {
  128. 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];
  129. [alert show];
  130. } else if (itemURL) {
  131. NSString *fileName = [[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"];
  132. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  133. }
  134. }
  135. #pragma mark - Action
  136. - (IBAction)play:(id)sender
  137. {
  138. [self _playMediaItem:_mutableMediaInformation];
  139. [[self navigationController] popViewControllerAnimated:YES];
  140. }
  141. - (IBAction)download:(id)sender
  142. {
  143. [self _download:_mutableMediaInformation];
  144. [[self navigationController] popViewControllerAnimated:YES];
  145. }
  146. - (IBAction)markMedia:(id)sender
  147. {
  148. NSString *ratingKey = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"ratingKey"];
  149. NSString *tag = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"state"];
  150. NSInteger status = [_PlexParser MarkWatchedUnwatchedMedia:_PlexServerAddress port:_PlexServerPort videoRatingKey:ratingKey state:tag];
  151. if (status == 200) {
  152. if ([tag isEqualToString:@"watched"]) {
  153. tag = @"unwatched";
  154. [self.badgeUnread setHidden:NO];
  155. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  156. } else if ([tag isEqualToString:@"unwatched"]) {
  157. tag = @"watched";
  158. [self.badgeUnread setHidden:YES];
  159. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  160. }
  161. } else
  162. [self.badgeUnread setHidden:YES];
  163. [self.badgeUnread setNeedsDisplay];
  164. [[_mutableMediaInformation objectAtIndex:0] setObject:tag forKey:@"state"];
  165. }
  166. #pragma mark - UI interaction
  167. - (BOOL)shouldAutorotate
  168. {
  169. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  170. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  171. return NO;
  172. return YES;
  173. }
  174. @end