VLCSharedLibraryParser.m 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*****************************************************************************
  2. * VLCSharedLibraryParser.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. *
  7. * Authors: Pierre Sagaspe <pierre.sagaspe # me.com>
  8. *
  9. * Refer to the COPYING file of the official project for license.
  10. *****************************************************************************/
  11. #import "VLCSharedLibraryParser.h"
  12. #define kLibraryXmlFile @"libMediaVLC.xml"
  13. NSString *const VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance = @"VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance";
  14. @interface VLCSharedLibraryParser () <NSXMLParserDelegate>
  15. {
  16. NSMutableArray *_containerInfo;
  17. NSMutableDictionary *_dicoInfo;
  18. }
  19. @end
  20. @implementation VLCSharedLibraryParser
  21. - (void)checkNetserviceForVLCService:(NSNetService *)aNetService
  22. {
  23. [self performSelectorInBackground:@selector(parseNetServiceOnBackgroundThread:) withObject:aNetService];
  24. }
  25. - (void)parseNetServiceOnBackgroundThread:(NSNetService *)aNetService
  26. {
  27. NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", [aNetService hostName], (long)[aNetService port]];
  28. NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnamePort];
  29. if (parsedContents.count > 0) {
  30. if ([[parsedContents.firstObject objectForKey:@"identifier"] isEqualToString:@"org.videolan.vlc-ios"]) {
  31. [[NSNotificationCenter defaultCenter] postNotificationName:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
  32. object:self
  33. userInfo:@{@"aNetService" : aNetService}];
  34. }
  35. }
  36. }
  37. - (void)fetchDataFromServer:(NSString *)hostname port:(long)port
  38. {
  39. NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", hostname, port];
  40. [self performSelectorInBackground:@selector(processDataOnBackgroundThreadFromHostnameAndPort:) withObject:hostnamePort];
  41. }
  42. - (void)processDataOnBackgroundThreadFromHostnameAndPort:(NSString *)hostnameAndPort
  43. {
  44. NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnameAndPort];
  45. __weak typeof(self) weakSelf = self;
  46. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  47. id delegate = weakSelf.delegate;
  48. if ([delegate respondsToSelector:@selector(sharedLibraryDataProcessings:)]) {
  49. [delegate sharedLibraryDataProcessings:parsedContents];
  50. }
  51. }];
  52. }
  53. - (NSArray *)downloadAndProcessDataFromServer:(NSString *)hostnamePort
  54. {
  55. _containerInfo = [[NSMutableArray alloc] init];
  56. [_containerInfo removeAllObjects];
  57. _dicoInfo = [[NSMutableDictionary alloc] init];
  58. NSString *serverURL = [NSString stringWithFormat:@"http://%@/%@", hostnamePort, kLibraryXmlFile];
  59. NSURL *url = [[NSURL alloc] initWithString:serverURL];
  60. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  61. [xmlparser setDelegate:self];
  62. if (![xmlparser parse]) {
  63. APLog(@"VLC Library Parser url Errors : %@", url);
  64. return [NSArray array];
  65. }
  66. return [NSArray arrayWithArray:_containerInfo];
  67. }
  68. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  69. {
  70. if ([elementName isEqualToString:@"MediaContainer"]) {
  71. if ([attributeDict objectForKey:@"size"])
  72. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  73. if ([attributeDict objectForKey:@"identifier"])
  74. [_dicoInfo setObject:[attributeDict objectForKey:@"identifier"] forKey:@"identifier"];
  75. if ([attributeDict objectForKey:@"libraryTitle"])
  76. [_dicoInfo setObject:[attributeDict objectForKey:@"libraryTitle"] forKey:@"libTitle"];
  77. } else if ([elementName isEqualToString:@"Media"]) {
  78. if ([attributeDict objectForKey:@"title"])
  79. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  80. if ([attributeDict objectForKey:@"thumb"])
  81. [_dicoInfo setObject:[attributeDict objectForKey:@"thumb"] forKey:@"thumb"];
  82. if ([attributeDict objectForKey:@"duration"])
  83. [_dicoInfo setObject:[attributeDict objectForKey:@"duration"] forKey:@"duration"];
  84. if ([attributeDict objectForKey:@"size"])
  85. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  86. if ([attributeDict objectForKey:@"pathfile"])
  87. [_dicoInfo setObject:[attributeDict objectForKey:@"pathfile"] forKey:@"pathfile"];
  88. if ([attributeDict objectForKey:@"pathSubtitle"])
  89. [_dicoInfo setObject:[attributeDict objectForKey:@"pathSubtitle"] forKey:@"pathSubtitle"];
  90. }
  91. }
  92. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  93. {
  94. if (([elementName isEqualToString:@"Media"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  95. [_containerInfo addObject:_dicoInfo];
  96. _dicoInfo = [[NSMutableDictionary alloc] init];
  97. }
  98. }
  99. @end