VLCPlexMediaInformationViewController.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. 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 setImage:image];
  66. [self.mediaTitle setText:title];
  67. [self.codec setText:displayCodec];
  68. [self.size setText:displaySize];
  69. [self.summary setText:displaySummary];
  70. if ([tag isEqualToString:@"watched"]) {
  71. [self.badgeUnread setHidden:YES];
  72. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  73. } else if ([tag isEqualToString:@"unwatched"]) {
  74. [self.badgeUnread setHidden:NO];
  75. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  76. } else {
  77. [self.badgeUnread setHidden:NO];
  78. [self.markMediaButton setEnabled:NO];
  79. }
  80. [self.badgeUnread setNeedsDisplay];
  81. }
  82. - (void)viewWillAppear:(BOOL)animated
  83. {
  84. [super viewWillAppear:animated];
  85. }
  86. #pragma mark - Specifics
  87. - (void)_playMediaItem:(NSMutableArray *)mutableMediaObject
  88. {
  89. NSString *newPath = nil;
  90. NSString *keyValue = [[mutableMediaObject objectAtIndex:0] objectForKey:@"key"];
  91. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  92. newPath = [_PlexServerPath stringByAppendingPathComponent:keyValue];
  93. else
  94. newPath = keyValue;
  95. if ([[[mutableMediaObject objectAtIndex:0] objectForKey:@"container"] isEqualToString:@"item"]) {
  96. [mutableMediaObject removeAllObjects];
  97. mutableMediaObject = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:newPath];
  98. NSString *URLofSubtitle = nil;
  99. if ([[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"])
  100. URLofSubtitle = [_PlexParser getFileSubtitleFromPlexServer:mutableMediaObject modeStream:YES];
  101. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  102. if (itemURL) {
  103. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  104. [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
  105. }
  106. }
  107. }
  108. - (void)_download:(NSMutableArray *)mutableMediaObject
  109. {
  110. NSString *path = [[mutableMediaObject objectAtIndex:0] objectForKey:@"key"];
  111. [mutableMediaObject removeAllObjects];
  112. mutableMediaObject = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:path];
  113. NSInteger size = [[[mutableMediaObject objectAtIndex:0] objectForKey:@"size"] integerValue];
  114. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  115. if ([[mutableMediaObject objectAtIndex:0] objectForKey:@"keySubtitle"])
  116. [_PlexParser getFileSubtitleFromPlexServer:mutableMediaObject modeStream:NO];
  117. [self _downloadFileFromMediaItem:mutableMediaObject];
  118. } else {
  119. 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];
  120. [alert show];
  121. }
  122. }
  123. - (void)_downloadFileFromMediaItem:(NSMutableArray *)mutableMediaObject
  124. {
  125. NSURL *itemURL = [NSURL URLWithString:[[mutableMediaObject objectAtIndex:0] objectForKey:@"keyMedia"]];
  126. if (![[itemURL absoluteString] isSupportedFormat]) {
  127. 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];
  128. [alert show];
  129. } else if (itemURL) {
  130. NSString *fileName = [[mutableMediaObject objectAtIndex:0] objectForKey:@"namefile"];
  131. [[(VLCAppDelegate *)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  132. }
  133. }
  134. #pragma mark - Action
  135. - (IBAction)play:(id)sender
  136. {
  137. [self _playMediaItem:_mutableMediaInformation];
  138. [[self navigationController] popViewControllerAnimated:YES];
  139. }
  140. - (IBAction)download:(id)sender
  141. {
  142. [self _download:_mutableMediaInformation];
  143. [[self navigationController] popViewControllerAnimated:YES];
  144. }
  145. - (IBAction)markMedia:(id)sender
  146. {
  147. NSString *ratingKey = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"ratingKey"];
  148. NSString *tag = [[_mutableMediaInformation objectAtIndex:0] objectForKey:@"state"];
  149. NSInteger status = [_PlexParser MarkWatchedUnwatchedMedia:_PlexServerAddress port:_PlexServerPort videoRatingKey:ratingKey state:tag];
  150. if (status == 200) {
  151. if ([tag isEqualToString:@"watched"]) {
  152. tag = @"unwatched";
  153. [self.badgeUnread setHidden:NO];
  154. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  155. } else if ([tag isEqualToString:@"unwatched"]) {
  156. tag = @"watched";
  157. [self.badgeUnread setHidden:YES];
  158. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  159. }
  160. } else
  161. [self.badgeUnread setHidden:YES];
  162. [self.badgeUnread setNeedsDisplay];
  163. [[_mutableMediaInformation objectAtIndex:0] setObject:tag forKey:@"state"];
  164. }
  165. #pragma mark - UI interaction
  166. - (BOOL)shouldAutorotate
  167. {
  168. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  169. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  170. return NO;
  171. return YES;
  172. }
  173. @end