VLCPlexMediaInformationViewController.m 9.6 KB

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