VLCPlexMediaInformationViewController.m 8.8 KB

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