VLCPlexMediaInformationViewController.m 8.9 KB

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