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
/*****************************************************************************
 * 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"
15
#import "VLCMetadata.h"
Felix Paul Kühne's avatar
Felix Paul Kühne committed
16
17
18
19
20
21
22
23

#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
24
    UIActivityIndicatorView *_activityIndicatorView;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
}
@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
44
        /* we may receive 'en-GB' so strip that to 'en' */
Felix Paul Kühne's avatar
Felix Paul Kühne committed
45
46
47
        if ([preferredLanguage containsString:@"-"]) {
            preferredLanguage = [[preferredLanguage componentsSeparatedByString:@"-"] firstObject];
        }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
48
        selectedLocale = [preferredLanguage VLCthreeLetterLanguageKeyForTwoLetterCode];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
49
50
51
52
53
54
55
56
        /* 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

    _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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
}

#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
95
    [self startActivity];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
96
97
98
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    _osoFetcher.subtitleLanguageId = [defaults stringForKey:kVLCSettingLastUsedSubtitlesSearchLanguage];
99
    [_osoFetcher searchForSubtitlesWithQuery:vpc.metadata.title];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
100
101
102
103
}

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

109
110
- (void)MDFOSOFetcher:(MDFOSOFetcher *)aFetcher didFailToDownloadForItem:(MDFSubtitleItem *)subtitleItem
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
111
    [self stopActivity];
112
113
114
115
116
117
    // 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
118
    [self stopActivity];
119
    VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
120
    [vpc openVideoSubTitlesFromFile:pathToFile];
121
    [self dismissViewControllerAnimated:YES completion:nil];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
122
    [[NSNotificationCenter defaultCenter] postNotificationName:VLCPlaybackControllerPlaybackMetadataDidChange object:nil];
123
124
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
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
153
#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;
154
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@", item.rating, [item.format uppercaseStringWithLocale:[NSLocale currentLocale]]];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
155
156
157
158
        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
159
        NSString *detail = [[selectedLocale VLCtwoLetterLanguageKeyForThreeLetterCode] VLClocalizedLanguageNameForTwoLetterCode];
160
        cell.detailTextLabel.text = detail ? detail : selectedLocale;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
161
162
163
164
165
166
167
168
169
170
171
        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
172
    return NSLocalizedString(@"FOUND_SUBS", nil);
Felix Paul Kühne's avatar
Felix Paul Kühne committed
173
174
175
176
177
178
179
180
181
182
}

#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];

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

187
        for (MDFSubtitleLanguage *item in languages) {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
188
189
190
191
192
193
194
195
            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];
196
                                                               [self.tableView reloadData];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
197
198
199
200
201
202
203
204
205
206
207
                                                           }];
            [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];
208
    } else {
Felix Paul Kühne's avatar
Felix Paul Kühne committed
209
        [self startActivity];
210
211
212
213
214
215
        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
216
217
218
    }
}

Felix Paul Kühne's avatar
Felix Paul Kühne committed
219
220
221
222
223
224
225
226
227
228
229
230
- (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
231
@end