VLCSharedLibraryParser.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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], [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. if ([self.delegate respondsToSelector:@selector(sharedLibraryDataProcessings:)])
  46. [self.delegate sharedLibraryDataProcessings:parsedContents];
  47. }
  48. - (NSArray *)downloadAndProcessDataFromServer:(NSString *)hostnamePort
  49. {
  50. _containerInfo = [[NSMutableArray alloc] init];
  51. [_containerInfo removeAllObjects];
  52. _dicoInfo = [[NSMutableDictionary alloc] init];
  53. NSString *serverURL = [NSString stringWithFormat:@"http://%@/%@", hostnamePort, kLibraryXmlFile];
  54. NSURL *url = [[NSURL alloc] initWithString:serverURL];
  55. NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
  56. [xmlparser setDelegate:self];
  57. if (![xmlparser parse]) {
  58. APLog(@"VLC Library Parser url Errors : %@", url);
  59. return [NSArray array];
  60. }
  61. return [NSArray arrayWithArray:_containerInfo];
  62. }
  63. - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
  64. {
  65. if ([elementName isEqualToString:@"MediaContainer"]) {
  66. if ([attributeDict objectForKey:@"size"])
  67. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  68. if ([attributeDict objectForKey:@"identifier"])
  69. [_dicoInfo setObject:[attributeDict objectForKey:@"identifier"] forKey:@"identifier"];
  70. if ([attributeDict objectForKey:@"libraryTitle"])
  71. [_dicoInfo setObject:[attributeDict objectForKey:@"libraryTitle"] forKey:@"libTitle"];
  72. } else if ([elementName isEqualToString:@"Media"]) {
  73. if ([attributeDict objectForKey:@"title"])
  74. [_dicoInfo setObject:[attributeDict objectForKey:@"title"] forKey:@"title"];
  75. if ([attributeDict objectForKey:@"thumb"])
  76. [_dicoInfo setObject:[attributeDict objectForKey:@"thumb"] forKey:@"thumb"];
  77. if ([attributeDict objectForKey:@"duration"])
  78. [_dicoInfo setObject:[attributeDict objectForKey:@"duration"] forKey:@"duration"];
  79. if ([attributeDict objectForKey:@"size"])
  80. [_dicoInfo setObject:[attributeDict objectForKey:@"size"] forKey:@"size"];
  81. if ([attributeDict objectForKey:@"pathfile"])
  82. [_dicoInfo setObject:[attributeDict objectForKey:@"pathfile"] forKey:@"pathfile"];
  83. if ([attributeDict objectForKey:@"pathSubtitle"])
  84. [_dicoInfo setObject:[attributeDict objectForKey:@"pathSubtitle"] forKey:@"pathSubtitle"];
  85. }
  86. }
  87. - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
  88. {
  89. if (([elementName isEqualToString:@"Media"] || [elementName isEqualToString:@"MediaContainer"]) && [_dicoInfo count] > 0) {
  90. [_containerInfo addObject:_dicoInfo];
  91. _dicoInfo = [[NSMutableDictionary alloc] init];
  92. }
  93. }
  94. @end