VLCPlaybackInfoSubtitlesFetcherViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*****************************************************************************
  2. * VLC for iOS
  3. *****************************************************************************
  4. * Copyright (c) 2015 VideoLAN. All rights reserved.
  5. * $Id$
  6. *
  7. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCPlaybackInfoSubtitlesFetcherViewController.h"
  12. #import "MetadataFetcherKit.h"
  13. #import "NSString+Locale.h"
  14. #import "VLCMetadata.h"
  15. #define SPUDownloadReUseIdentifier @"SPUDownloadReUseIdentifier"
  16. #define SPUDownloadHeaderReUseIdentifier @"SPUDownloadHeaderReUseIdentifier"
  17. @interface VLCPlaybackInfoSubtitlesFetcherViewController () <UITableViewDataSource, UITableViewDelegate, MDFOSOFetcherDataRecipient>
  18. {
  19. MDFOSOFetcher *_osoFetcher;
  20. NSArray <MDFSubtitleItem *>* _searchResults;
  21. UIActivityIndicatorView *_activityIndicatorView;
  22. }
  23. @end
  24. @implementation VLCPlaybackInfoSubtitlesFetcherViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.titleLabel.text = self.title;
  28. _osoFetcher = [[MDFOSOFetcher alloc] init];
  29. _osoFetcher.userAgentKey = @"VLSub 0.9";
  30. _osoFetcher.dataRecipient = self;
  31. [_osoFetcher prepareForFetching];
  32. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  33. NSString *selectedLocale = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  34. if (!selectedLocale) {
  35. NSString *preferredLanguage = [[NSLocale preferredLanguages] firstObject];
  36. /* we may receive 'en-GB' so strip that to 'en' */
  37. if ([preferredLanguage containsString:@"-"]) {
  38. preferredLanguage = [[preferredLanguage componentsSeparatedByString:@"-"] firstObject];
  39. }
  40. selectedLocale = [preferredLanguage VLCthreeLetterLanguageKeyForTwoLetterCode];
  41. /* last resort */
  42. if (selectedLocale == nil) {
  43. selectedLocale = @"eng";
  44. }
  45. [defaults setObject:selectedLocale forKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  46. [defaults synchronize];
  47. }
  48. _osoFetcher.subtitleLanguageId = selectedLocale;
  49. _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
  50. [_activityIndicatorView sizeToFit];
  51. [_activityIndicatorView setTranslatesAutoresizingMaskIntoConstraints:NO];
  52. _activityIndicatorView.hidesWhenStopped = YES;
  53. [self.view addSubview:_activityIndicatorView];
  54. NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:_activityIndicatorView
  55. attribute:NSLayoutAttributeCenterY
  56. relatedBy:NSLayoutRelationEqual
  57. toItem:self.view
  58. attribute:NSLayoutAttributeCenterY
  59. multiplier:1.0
  60. constant:0.0];
  61. [self.view addConstraint:yConstraint];
  62. NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:_activityIndicatorView
  63. attribute:NSLayoutAttributeCenterX
  64. relatedBy:NSLayoutRelationEqual
  65. toItem:self.view
  66. attribute:NSLayoutAttributeCenterX
  67. multiplier:1.0
  68. constant:0.0];
  69. [self.view addConstraint:xConstraint];
  70. }
  71. #pragma mark - OSO Fetcher delegation
  72. - (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher readyToSearch:(BOOL)bValue
  73. {
  74. if (!bValue)
  75. return;
  76. [self searchForMedia];
  77. }
  78. - (void)searchForMedia
  79. {
  80. [self startActivity];
  81. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  82. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  83. _osoFetcher.subtitleLanguageId = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  84. [_osoFetcher searchForSubtitlesWithQuery:vpc.metadata.title];
  85. }
  86. - (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher didFindSubtitles:(NSArray<MDFSubtitleItem *> *)subtitles forSearchRequest:(NSString *)searchRequest
  87. {
  88. [self stopActivity];
  89. _searchResults = subtitles;
  90. [self.tableView reloadData];
  91. }
  92. - (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher didFailToDownloadForItem:(MDFSubtitleItem *)subtitleItem
  93. {
  94. [self stopActivity];
  95. // FIXME: missing error handling
  96. [self dismissViewControllerAnimated:YES completion:nil];
  97. }
  98. - (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher subtitleDownloadSucceededForItem:(MDFSubtitleItem *)subtitleItem atPath:(NSString *)pathToFile
  99. {
  100. [self stopActivity];
  101. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  102. [vpc.mediaPlayer openVideoSubTitlesFromFile:pathToFile];
  103. [self dismissViewControllerAnimated:YES completion:nil];
  104. [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
  105. }
  106. #pragma mark - table view datasource
  107. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  108. {
  109. return 2;
  110. }
  111. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  112. {
  113. if (section == 0)
  114. return 1;
  115. if (_searchResults) {
  116. return _searchResults.count;
  117. }
  118. return 0;
  119. }
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SPUDownloadReUseIdentifier];
  123. if (!cell)
  124. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SPUDownloadReUseIdentifier];
  125. if (indexPath.section != 0) {
  126. MDFSubtitleItem *item = _searchResults[indexPath.row];
  127. cell.textLabel.text = item.name;
  128. cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", item.rating, [item.format uppercaseStringWithLocale:[NSLocale currentLocale]]];
  129. cell.accessoryType = UITableViewCellAccessoryNone;
  130. } else {
  131. NSString *selectedLocale = [[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  132. cell.textLabel.text = NSLocalizedString(@"LANGUAGE", nil);
  133. NSString *detail = [[selectedLocale VLCtwoLetterLanguageKeyForThreeLetterCode] VLClocalizedLanguageNameForTwoLetterCode];
  134. cell.detailTextLabel.text = detail ? detail : selectedLocale;
  135. cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  136. }
  137. return cell;
  138. }
  139. - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
  140. {
  141. if (section == 0)
  142. return @"";
  143. return NSLocalizedString(@"FOUND_SUBS", nil);
  144. }
  145. #pragma mark - table view delegate
  146. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  147. {
  148. if (indexPath.section == 0) {
  149. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"LANGUAGE", nil)
  150. message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  151. NSArray<MDFSubtitleLanguage *> *languages = _osoFetcher.availableLanguages;
  152. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  153. NSString *currentCode = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  154. for (MDFSubtitleLanguage *item in languages) {
  155. NSString *itemID = item.ID;
  156. UIAlertAction *action = [UIAlertAction actionWithTitle:item.localizedName
  157. style:UIAlertActionStyleDefault
  158. handler:^(UIAlertAction * _Nonnull action) {
  159. _osoFetcher.subtitleLanguageId = itemID;
  160. [defaults setObject:itemID forKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
  161. [defaults synchronize];
  162. [self searchForMedia];
  163. [self.tableView reloadData];
  164. }];
  165. [alertController addAction:action];
  166. if ([itemID isEqualToString:currentCode])
  167. [alertController setPreferredAction:action];
  168. }
  169. [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  170. style:UIAlertActionStyleCancel
  171. handler:nil]];
  172. [self presentViewController:alertController animated:YES completion:nil];
  173. } else {
  174. [self startActivity];
  175. MDFSubtitleItem *item = _searchResults[indexPath.row];
  176. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  177. NSString *folderPath = [searchPaths[0] stringByAppendingPathComponent:@"tempsubs"];
  178. [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
  179. NSString *subStorageLocation = [folderPath stringByAppendingPathComponent:item.name];
  180. [_osoFetcher downloadSubtitleItem:item toPath:subStorageLocation];
  181. }
  182. }
  183. - (void)startActivity
  184. {
  185. [_activityIndicatorView startAnimating];
  186. self.tableView.userInteractionEnabled = NO;
  187. }
  188. - (void)stopActivity
  189. {
  190. [_activityIndicatorView stopAnimating];
  191. self.tableView.userInteractionEnabled = YES;
  192. }
  193. @end