VLCSharedLibraryParser.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 isKindOfClass:[NSDictionary class]]) {
  31. if ([[parsedContents.firstObject objectForKey:@"identifier"] isEqualToString:@"org.videolan.vlc-ios"]) {
  32. [[NSNotificationCenter defaultCenter] postNotificationName:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
  33. object:self
  34. userInfo:@{@"aNetService" : aNetService}];
  35. }
  36. }
  37. }
  38. }
  39. - (void)fetchDataFromServer:(NSString *)hostname port:(long)port
  40. {
  41. NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", hostname, port];
  42. [self performSelectorInBackground:@selector(processDataOnBackgroundThreadFromHostnameAndPort:) withObject:hostnamePort];
  43. }
  44. - (void)processDataOnBackgroundThreadFromHostnameAndPort:(NSString *)hostnameAndPort
  45. {
  46. NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnameAndPort];
  47. __weak typeof(self) weakSelf = self;
  48. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  49. id delegate = weakSelf.delegate;
  50. if ([delegate respondsToSelector:@selector(sharedLibraryDataProcessings:)]) {
  51. [delegate sharedLibraryDataProcessings:parsedContents];
  52. }
  53. }];
  54. }
  55. - (NSArray *)downloadAndProcessDataFromServer:(NSString *)hostnamePort
  56. {
  57. _containerInfo = [[NSMutableArray alloc] init];
  58. _dicoInfo = [[NSMutableDictionary alloc] init];
  59. NSString *serverURL = [NSString stringWithFormat:@"http://%@/%@", hostnamePort, kLibraryXmlFile];
  60. NSURL *url = [[NSURL alloc] initWithString:serverURL];
  61. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  62. [xmlparser setDelegate:self];
  63. if (![xmlparser parse]) {
  64. APLog(@"VLC Library Parser url Errors : %@", url);
  65. return [NSArray array];
  66. }
  67. return [_containerInfo copy];
  68. }
  69. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  70. {
  71. if ([elementName isEqualToString:@"MediaContainer"]) {
  72. if ([attributeDict objectForKey:@"size"])
  73. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  74. if ([attributeDict objectForKey:@"identifier"])
  75. [_dicoInfo setObject:[attributeDict objectForKey:@"identifier"] forKey:@"identifier"];
  76. if ([attributeDict objectForKey:@"libraryTitle"])
  77. [_dicoInfo setObject:[attributeDict objectForKey:@"libraryTitle"] forKey:@"libTitle"];
  78. } else if ([elementName isEqualToString:@"Media"]) {
  79. if ([attributeDict objectForKey:@"title"])
  80. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  81. if ([attributeDict objectForKey:@"thumb"])
  82. [_dicoInfo setObject:[attributeDict objectForKey:@"thumb"] forKey:@"thumb"];
  83. if ([attributeDict objectForKey:@"duration"])
  84. [_dicoInfo setObject:[attributeDict objectForKey:@"duration"] forKey:@"duration"];
  85. if ([attributeDict objectForKey:@"size"])
  86. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  87. if ([attributeDict objectForKey:@"pathfile"])
  88. [_dicoInfo setObject:[attributeDict objectForKey:@"pathfile"] forKey:@"pathfile"];
  89. if ([attributeDict objectForKey:@"pathSubtitle"])
  90. [_dicoInfo setObject:[attributeDict objectForKey:@"pathSubtitle"] forKey:@"pathSubtitle"];
  91. }
  92. }
  93. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  94. {
  95. if (([elementName isEqualToString:@"Media"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  96. [_containerInfo addObject:_dicoInfo];
  97. _dicoInfo = [[NSMutableDictionary alloc] init];
  98. }
  99. }
  100. @end