VLCPlexMediaInformationViewController.m 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. [self.badgeUnread setBackgroundColor:[UIColor whiteColor]];
  83. self.badgeUnread.layer.cornerRadius = 10;
  84. self.badgeUnread.layer.masksToBounds = YES;
  85. if ([tag isEqualToString:@"watched"]) {
  86. [self.badgeUnread setHidden:YES];
  87. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  88. } else if ([tag isEqualToString:@"unwatched"]) {
  89. [self.badgeUnread setHidden:NO];
  90. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  91. } else {
  92. [self.badgeUnread setHidden:NO];
  93. [self.markMediaButton setEnabled:NO];
  94. }
  95. [self.badgeUnread setNeedsDisplay];
  96. }
  97. #pragma mark - Specifics
  98. - (void)_playMediaItem
  99. {
  100. if (_mediaObject == nil)
  101. return;
  102. NSString *newPath = nil;
  103. NSString *keyValue = _mediaObject[@"key"];
  104. if ([keyValue rangeOfString:@"library"].location == NSNotFound)
  105. newPath = [_PlexServerPath stringByAppendingPathComponent:keyValue];
  106. else
  107. newPath = keyValue;
  108. if ([_mediaObject[@"container"] isEqualToString:@"item"]) {
  109. NSArray *mediaList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:newPath authentification:@""];
  110. NSString *URLofSubtitle = nil;
  111. NSDictionary *firstObject = [mediaList firstObject];
  112. if (!firstObject)
  113. return;
  114. if (firstObject[@"keySubtitle"])
  115. URLofSubtitle = [_PlexWebAPI getFileSubtitleFromPlexServer:firstObject modeStream:YES];
  116. NSURL *itemURL = [NSURL URLWithString:[_PlexWebAPI urlAuth:firstObject[@"keyMedia"] autentification:_PlexAuthentification]];
  117. if (itemURL) {
  118. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  119. [vpc playURL:itemURL subtitlesFilePath:URLofSubtitle];
  120. }
  121. }
  122. }
  123. - (void)_download
  124. {
  125. if (_mediaObject == nil)
  126. return;
  127. NSString *path = _mediaObject[@"key"];
  128. NSArray *mediaList = [_PlexParser PlexMediaServerParser:_PlexServerAddress port:_PlexServerPort navigationPath:path authentification:@""];
  129. NSDictionary *firstObject = [mediaList firstObject];
  130. if (!firstObject)
  131. return;
  132. NSInteger size = [firstObject[@"size"] integerValue];
  133. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  134. if (firstObject[@"keySubtitle"])
  135. [_PlexWebAPI getFileSubtitleFromPlexServer:firstObject modeStream:NO];
  136. NSURL *itemURL = [NSURL URLWithString:firstObject[@"keyMedia"]];
  137. if (![[itemURL absoluteString] isSupportedFormat]) {
  138. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  139. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [itemURL absoluteString]]
  140. delegate:self
  141. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  142. otherButtonTitles:nil];
  143. [alert show];
  144. } else if (itemURL) {
  145. NSString *fileName = [firstObject objectForKey:@"namefile"];
  146. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  147. }
  148. } else {
  149. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  150. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), firstObject[@"title"], [[UIDevice currentDevice] model]]
  151. delegate:self
  152. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  153. otherButtonTitles:nil];
  154. [alert show];
  155. }
  156. }
  157. #pragma mark - Action
  158. - (IBAction)play:(id)sender
  159. {
  160. [self _playMediaItem];
  161. [[self navigationController] popViewControllerAnimated:YES];
  162. }
  163. - (IBAction)download:(id)sender
  164. {
  165. [self _download];
  166. [[self navigationController] popViewControllerAnimated:YES];
  167. }
  168. - (IBAction)markMedia:(id)sender
  169. {
  170. NSString *ratingKey = _mediaObject[@"ratingKey"];
  171. NSString *tag = _mediaObject[@"state"];
  172. NSInteger status = [_PlexWebAPI MarkWatchedUnwatchedMedia:_PlexServerAddress port:_PlexServerPort videoRatingKey:ratingKey state:tag authentification:_PlexAuthentification];
  173. if (status == 200) {
  174. if ([tag isEqualToString:@"watched"]) {
  175. tag = @"unwatched";
  176. [self.badgeUnread setHidden:NO];
  177. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_WATCHED", nil)];
  178. } else if ([tag isEqualToString:@"unwatched"]) {
  179. tag = @"watched";
  180. [self.badgeUnread setHidden:YES];
  181. [self.markMediaButton setTitle:NSLocalizedString(@"PLEX_UNWATCHED", nil)];
  182. }
  183. } else
  184. [self.badgeUnread setHidden:YES];
  185. [self.badgeUnread setNeedsDisplay];
  186. NSMutableDictionary *mutableMediaObject = [NSMutableDictionary dictionaryWithDictionary:_mediaObject];
  187. [mutableMediaObject setObject:tag forKey:@"state"];
  188. _mediaObject = [NSDictionary dictionaryWithDictionary:mutableMediaObject];
  189. }
  190. #pragma mark - UI interaction
  191. - (BOOL)shouldAutorotate
  192. {
  193. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  194. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  195. return NO;
  196. return YES;
  197. }
  198. @end