VLCNetworkServerBrowserSharedLibrary.m 3.58 KB
Newer Older
1
/*****************************************************************************
Tobias's avatar
Tobias committed
2
 * VLCNetworkServerBrowserSharedLibrary.m
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Tobias Conradi <videolan # tobias-conradi.de>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCNetworkServerBrowserSharedLibrary.h"
#import "VLCSharedLibraryParser.h"

@interface VLCNetworkServerBrowserSharedLibrary () <VLCSharedLibraryParserDelegate>
@property (nonatomic, readonly) NSString *addressOrName;
@property (nonatomic, readonly) NSUInteger port;
@property (nonatomic, readonly) VLCSharedLibraryParser *httpParser;

@end

@implementation VLCNetworkServerBrowserSharedLibrary
24
@synthesize title = _title, delegate = _delegate, items = _items, mediaList = _mediaList;
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54

- (instancetype)initWithName:(NSString *)name host:(NSString *)addressOrName portNumber:(NSUInteger)portNumber
{
    self = [super init];
    if (self) {
        _title = name;
        _addressOrName = addressOrName;
        _port = portNumber;
        _items = [NSArray array];
        _httpParser = [[VLCSharedLibraryParser alloc] init];
        _httpParser.delegate = self;
    }
    return self;
}

- (void)update {
    [self.httpParser fetchDataFromServer:self.addressOrName port:self.port];
}

#pragma mark - Specifics


- (void)sharedLibraryDataProcessings:(NSArray *)result
{
    _title = [result.firstObject objectForKey:@"libTitle"];

    NSMutableArray *items = [NSMutableArray array];
    for (NSDictionary *dict in result) {
        [items addObject:[[VLCNetworkServerBrowserItemSharedLibrary alloc] initWithDictionary:dict]];
    }
55 56 57
    @synchronized(_items) {
        _items = [items copy];
    }
58

59
    _mediaList = [self buildMediaList];
60 61 62 63 64 65
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.delegate networkServerBrowserDidUpdate:self];

    }];
}

66
- (VLCMediaList *)buildMediaList
67
{
68
    VLCMediaList *mediaList = [[VLCMediaList alloc] init];
69 70
    @synchronized(_items) {
        NSUInteger count = _items.count;
71
        for (NSInteger i = count - 1; i >= 0; i--) {
72 73
            VLCMedia *media = [_items[i] media];
            if (media)
74
                [mediaList addMedia:media];
75 76
        }
    }
77
    return mediaList;
78 79
}

80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
@end


@implementation VLCNetworkServerBrowserItemSharedLibrary
@synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
- (instancetype)initWithDictionary:(NSDictionary *)dictionary
{
    self = [super init];
    if (self) {
        _name = dictionary[@"title"];
        _fileSizeBytes = dictionary[@"size"];
        _duration = dictionary[@"duration"];
        NSString *subtitleURLString = dictionary[@"pathSubtitle"];
        if ([subtitleURLString isEqualToString:@"(null)"]) subtitleURLString = nil;
        subtitleURLString = [subtitleURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
95
        _subtitleURL = subtitleURLString.length ? [NSURL URLWithString:subtitleURLString] : nil;
96 97
        _URL = [NSURL URLWithString:dictionary[@"pathfile"]];
        _container = NO;
98 99 100 101

        NSString *thumbURLString = dictionary[@"thumb"];
        _thumbnailURL = thumbURLString.length ? [NSURL URLWithString:thumbURLString] : nil;

102 103 104 105 106 107 108 109
    }
    return self;
}

- (id<VLCNetworkServerBrowser>)containerBrowser {
    return nil;
}

110 111 112
- (VLCMedia *)media
{
    if (!_URL)
113
        return [VLCMedia mediaAsNodeWithName:self.name];
114 115 116
    return [VLCMedia mediaWithURL:_URL];
}

117
@end