VLCPlaybackInfoSubtitlesFetcherViewController.m 10 KB
Newer Older
Felix Paul Kühne's avatar
Felix Paul Kühne committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*****************************************************************************
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Felix Paul Kühne <fkuehne # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCPlaybackInfoSubtitlesFetcherViewController.h"
#import "MetadataFetcherKit.h"
#import "NSString+Locale.h"

#define SPUDownloadReUseIdentifier @"SPUDownloadReUseIdentifier"
#define SPUDownloadHeaderReUseIdentifier @"SPUDownloadHeaderReUseIdentifier"

@interface VLCPlaybackInfoSubtitlesFetcherViewController () <UITableViewDataSource, UITableViewDelegate, MDFOSOFetcherDataRecipient>
{
    MDFOSOFetcher *_osoFetcher;
    NSArray <MDFSubtitleItem *>* _searchResults;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
23
    UIActivityIndicatorView *_activityIndicatorView;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
}
@end

@implementation VLCPlaybackInfoSubtitlesFetcherViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.titleLabel.text = self.title;

    _osoFetcher = [[MDFOSOFetcher alloc] init];
    _osoFetcher.userAgentKey = @"VLSub 0.9";
    _osoFetcher.dataRecipient = self;
    [_osoFetcher prepareForFetching];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *selectedLocale = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
    if (!selectedLocale) {
        NSString *preferredLanguage = [[NSLocale preferredLanguages] firstObject];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
43
        /* we may receive 'en-GB' so strip that to 'en' */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
44
45
46
        if ([preferredLanguage containsString:@"-"]) {
            preferredLanguage = [[preferredLanguage componentsSeparatedByString:@"-"] firstObject];
        }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
47
        selectedLocale = [preferredLanguage VLCthreeLetterLanguageKeyForTwoLetterCode];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
48
49
50
51
52
53
54
55
        /* last resort */
        if (selectedLocale == nil) {
            selectedLocale = @"eng";
        }
        [defaults setObject:selectedLocale forKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
        [defaults synchronize];
    }
    _osoFetcher.subtitleLanguageId = selectedLocale;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79

    _activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [_activityIndicatorView sizeToFit];
    [_activityIndicatorView setTranslatesAutoresizingMaskIntoConstraints:NO];
    _activityIndicatorView.hidesWhenStopped = YES;
    [self.view addSubview:_activityIndicatorView];

    NSLayoutConstraint *yConstraint = [NSLayoutConstraint constraintWithItem:_activityIndicatorView
                                                                   attribute:NSLayoutAttributeCenterY
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterY
                                                                  multiplier:1.0
                                                                    constant:0.0];
    [self.view addConstraint:yConstraint];
    NSLayoutConstraint *xConstraint = [NSLayoutConstraint constraintWithItem:_activityIndicatorView
                                                                   attribute:NSLayoutAttributeCenterX
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:self.view
                                                                   attribute:NSLayoutAttributeCenterX
                                                                  multiplier:1.0
                                                                    constant:0.0];
    [self.view addConstraint:xConstraint];

Felix Paul Kühne's avatar
Felix Paul Kühne committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
}

#pragma mark - OSO Fetcher delegation

- (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher readyToSearch:(BOOL)bValue
{
    if (!bValue)
        return;

    [self searchForMedia];
}

- (void)searchForMedia
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
94
    [self startActivity];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
95
96
97
98
99
100
101
102
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _osoFetcher.subtitleLanguageId = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
    [_osoFetcher searchForSubtitlesWithQuery:vpc.mediaTitle];
}

- (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher didFindSubtitles:(NSArray<MDFSubtitleItem *> *)subtitles forSearchRequest:(NSString *)searchRequest
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
103
    [self stopActivity];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
104
105
106
107
    _searchResults = subtitles;
    [self.tableView reloadData];
}

108
109
- (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher didFailToDownloadForItem:(MDFSubtitleItem *)subtitleItem
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
110
    [self stopActivity];
111
112
113
114
115
116
    // FIXME: missing error handling
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher subtitleDownloadSucceededForItem:(MDFSubtitleItem *)subtitleItem atPath:(NSString *)pathToFile
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
117
    [self stopActivity];
118
119
120
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    [vpc.mediaPlayer openVideoSubTitlesFromFile:pathToFile];
    [self dismissViewControllerAnimated:YES completion:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
121
    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
122
123
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#pragma mark - table view datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0)
        return 1;

    if (_searchResults) {
        return _searchResults.count;
    }

    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:SPUDownloadReUseIdentifier];

    if (!cell)
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:SPUDownloadReUseIdentifier];

    if (indexPath.section != 0) {
        MDFSubtitleItem *item = _searchResults[indexPath.row];
        cell.textLabel.text = item.name;
153
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", item.rating, [item.format uppercaseStringWithLocale:[NSLocale currentLocale]]];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
154
155
156
157
        cell.accessoryType = UITableViewCellAccessoryNone;
    } else {
        NSString *selectedLocale = [[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
        cell.textLabel.text = NSLocalizedString(@"LANGUAGE", nil);
Felix Paul Kühne's avatar
Felix Paul Kühne committed
158
        NSString *detail = [[selectedLocale VLCtwoLetterLanguageKeyForThreeLetterCode] VLClocalizedLanguageNameForTwoLetterCode];
159
        cell.detailTextLabel.text = detail ? detail : selectedLocale;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
160
161
162
163
164
165
166
167
168
169
170
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    return cell;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0)
        return @"";

Felix Paul Kühne's avatar
Felix Paul Kühne committed
171
    return NSLocalizedString(@"FOUND_SUBS", nil);
Felix Paul Kühne's avatar
Felix Paul Kühne committed
172
173
174
175
176
177
178
179
180
181
}

#pragma mark - table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"LANGUAGE", nil)
                                                                                 message:nil preferredStyle:UIAlertControllerStyleActionSheet];

182
        NSArray<MDFSubtitleLanguage *> *languages = _osoFetcher.availableLanguages;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
183
184
185
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *currentCode = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];

186
        for (MDFSubtitleLanguage *item in languages) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
187
188
189
190
191
192
193
194
            NSString *itemID = item.ID;
            UIAlertAction *action = [UIAlertAction actionWithTitle:item.localizedName
                                                             style:UIAlertActionStyleDefault
                                                           handler:^(UIAlertAction * _Nonnull action) {
                                                               _osoFetcher.subtitleLanguageId = itemID;
                                                               [defaults setObject:itemID forKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
                                                               [defaults synchronize];
                                                               [self searchForMedia];
195
                                                               [self.tableView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
196
197
198
199
200
201
202
203
204
205
206
                                                           }];
            [alertController addAction:action];
            if ([itemID isEqualToString:currentCode])
                [alertController setPreferredAction:action];
        }

        [alertController addAction:[UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
                                                            style:UIAlertActionStyleCancel
                                                          handler:nil]];

        [self presentViewController:alertController animated:YES completion:nil];
207
    } else {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
208
        [self startActivity];
209
210
211
212
213
214
        MDFSubtitleItem *item = _searchResults[indexPath.row];
        NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *folderPath = [searchPaths[0] stringByAppendingPathComponent:@"tempsubs"];
        [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:nil];
        NSString *subStorageLocation = [folderPath stringByAppendingPathComponent:item.name];
        [_osoFetcher downloadSubtitleItem:item toPath:subStorageLocation];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
215
216
217
    }
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
218
219
220
221
222
223
224
225
226
227
228
229
- (void)startActivity
{
    [_activityIndicatorView startAnimating];
    self.tableView.userInteractionEnabled = NO;
}

- (void)stopActivity
{
    [_activityIndicatorView stopAnimating];
    self.tableView.userInteractionEnabled = YES;
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
230
@end