VLCSharedLibraryParser.m 5.15 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*****************************************************************************
 * VLCSharedLibraryParser.m
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2015 VideoLAN. All rights reserved.
 *
 * Authors: Pierre Sagaspe <pierre.sagaspe # me.com>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

#import "VLCSharedLibraryParser.h"

#define kLibraryXmlFile @"libMediaVLC.xml"

16 17
NSString *const VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance = @"VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance";

18 19 20 21 22 23 24 25 26
@interface VLCSharedLibraryParser () <NSXMLParserDelegate>
{
    NSMutableArray *_containerInfo;
    NSMutableDictionary *_dicoInfo;
}
@end

@implementation VLCSharedLibraryParser

27 28 29 30 31 32 33
- (void)checkNetserviceForVLCService:(NSNetService *)aNetService
{
    [self performSelectorInBackground:@selector(parseNetServiceOnBackgroundThread:) withObject:aNetService];
}

- (void)parseNetServiceOnBackgroundThread:(NSNetService *)aNetService
{
34
    NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", [aNetService hostName], (long)[aNetService port]];
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
    NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnamePort];

    if (parsedContents.count > 0) {
        if ([[parsedContents.firstObject objectForKey:@"identifier"] isEqualToString:@"org.videolan.vlc-ios"]) {
            [[NSNotificationCenter defaultCenter] postNotificationName:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
                                                                object:self
                                                              userInfo:@{@"aNetService" : aNetService}];
        }
    }
}

- (void)fetchDataFromServer:(NSString *)hostname port:(long)port
{
    NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", hostname, port];
    [self performSelectorInBackground:@selector(processDataOnBackgroundThreadFromHostnameAndPort:) withObject:hostnamePort];
}

- (void)processDataOnBackgroundThreadFromHostnameAndPort:(NSString *)hostnameAndPort
{
    NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnameAndPort];

56 57 58 59 60 61 62 63
    __weak typeof(self) weakSelf = self;
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        id delegate = weakSelf.delegate;
        if ([delegate respondsToSelector:@selector(sharedLibraryDataProcessings:)]) {
            [delegate sharedLibraryDataProcessings:parsedContents];
        }
    }];

64 65 66
}

- (NSArray *)downloadAndProcessDataFromServer:(NSString *)hostnamePort
67 68 69 70
{
    _containerInfo = [[NSMutableArray alloc] init];
    [_containerInfo removeAllObjects];
    _dicoInfo = [[NSMutableDictionary alloc] init];
71
    NSString *serverURL = [NSString stringWithFormat:@"http://%@/%@", hostnamePort, kLibraryXmlFile];
72

73
    NSURL *url = [[NSURL alloc] initWithString:serverURL];
74 75 76
    NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
    [xmlparser setDelegate:self];

77
    if (![xmlparser parse]) {
78
        APLog(@"VLC Library Parser url Errors : %@", url);
79
        return [NSArray array];
80
    }
81 82

    return [NSArray arrayWithArray:_containerInfo];
83 84 85 86
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{
87
    if ([elementName isEqualToString:@"MediaContainer"]) {
88 89 90 91 92 93
        if ([attributeDict objectForKey:@"size"])
            [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
        if ([attributeDict objectForKey:@"identifier"])
            [_dicoInfo setObject:[attributeDict objectForKey:@"identifier"] forKey:@"identifier"];
        if ([attributeDict objectForKey:@"libraryTitle"])
            [_dicoInfo setObject:[attributeDict objectForKey:@"libraryTitle"] forKey:@"libTitle"];
94 95
    } else if ([elementName isEqualToString:@"Media"]) {
        if ([attributeDict objectForKey:@"title"])
96
            [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
97
        if ([attributeDict objectForKey:@"thumb"])
98
            [_dicoInfo setObject:[attributeDict objectForKey:@"thumb"] forKey:@"thumb"];
99
        if ([attributeDict objectForKey:@"duration"])
100
            [_dicoInfo setObject:[attributeDict objectForKey:@"duration"] forKey:@"duration"];
101
        if ([attributeDict objectForKey:@"size"])
102
            [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
103
        if ([attributeDict objectForKey:@"pathfile"])
104
            [_dicoInfo setObject:[attributeDict objectForKey:@"pathfile"] forKey:@"pathfile"];
105
        if ([attributeDict objectForKey:@"pathSubtitle"])
106
            [_dicoInfo setObject:[attributeDict objectForKey:@"pathSubtitle"] forKey:@"pathSubtitle"];
107 108 109 110 111
    }
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
112
    if (([elementName isEqualToString:@"Media"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
113 114 115 116 117 118
        [_containerInfo addObject:_dicoInfo];
        _dicoInfo = [[NSMutableDictionary alloc] init];
    }
}

@end