VLCPlexMediaInformationViewController.m 8.8 KB

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