VLCNetworkServerBrowserSharedLibrary.m 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserSharedLibrary.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015-2018 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Tobias Conradi <videolan # tobias-conradi.de>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCNetworkServerBrowserSharedLibrary.h"
  13. #import "VLCSharedLibraryParser.h"
  14. @interface VLCNetworkServerBrowserSharedLibrary () <VLCSharedLibraryParserDelegate>
  15. @property (nonatomic, readonly) NSString *addressOrName;
  16. @property (nonatomic, readonly) NSUInteger port;
  17. @property (nonatomic, readonly) VLCSharedLibraryParser *httpParser;
  18. @end
  19. @implementation VLCNetworkServerBrowserSharedLibrary
  20. @synthesize title = _title, delegate = _delegate, items = _items, mediaList = _mediaList;
  21. - (instancetype)initWithName:(NSString *)name host:(NSString *)addressOrName portNumber:(NSUInteger)portNumber
  22. {
  23. self = [super init];
  24. if (self) {
  25. _title = name;
  26. _addressOrName = addressOrName;
  27. _port = portNumber;
  28. _items = [NSArray array];
  29. _httpParser = [[VLCSharedLibraryParser alloc] init];
  30. _httpParser.delegate = self;
  31. }
  32. return self;
  33. }
  34. - (void)update {
  35. [self.httpParser fetchDataFromServer:self.addressOrName port:self.port];
  36. }
  37. #pragma mark - Specifics
  38. - (void)sharedLibraryDataProcessings:(NSArray *)result
  39. {
  40. _title = [result.firstObject objectForKey:@"libTitle"];
  41. NSMutableArray *items = [NSMutableArray array];
  42. for (NSDictionary *dict in result) {
  43. [items addObject:[[VLCNetworkServerBrowserItemSharedLibrary alloc] initWithDictionary:dict]];
  44. }
  45. @synchronized(_items) {
  46. _items = [items copy];
  47. }
  48. _mediaList = [self buildMediaList];
  49. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  50. [self.delegate networkServerBrowserDidUpdate:self];
  51. }];
  52. }
  53. - (VLCMediaList *)buildMediaList
  54. {
  55. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  56. @synchronized(_items) {
  57. for (id<VLCNetworkServerBrowserItem> browseritem in _items) {
  58. VLCMedia *media = [browseritem media];
  59. if (media)
  60. [mediaList addMedia:media];
  61. }
  62. }
  63. return mediaList;
  64. }
  65. @end
  66. @implementation VLCNetworkServerBrowserItemSharedLibrary
  67. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  68. - (instancetype)initWithDictionary:(NSDictionary *)dictionary
  69. {
  70. self = [super init];
  71. if (self) {
  72. _name = dictionary[@"title"];
  73. _fileSizeBytes = dictionary[@"size"];
  74. _duration = dictionary[@"duration"];
  75. NSString *subtitleURLString = dictionary[@"pathSubtitle"];
  76. if ([subtitleURLString isEqualToString:@"(null)"])
  77. subtitleURLString = nil;
  78. subtitleURLString = [subtitleURLString stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLFragmentAllowedCharacterSet];
  79. _subtitleURL = subtitleURLString.length ? [NSURL URLWithString:subtitleURLString] : nil;
  80. _URL = [NSURL URLWithString:dictionary[@"pathfile"]];
  81. _container = NO;
  82. NSString *thumbURLString = dictionary[@"thumb"];
  83. _thumbnailURL = thumbURLString.length ? [NSURL URLWithString:thumbURLString] : nil;
  84. }
  85. return self;
  86. }
  87. - (id<VLCNetworkServerBrowser>)containerBrowser
  88. {
  89. return nil;
  90. }
  91. - (BOOL)isDownloadable
  92. {
  93. //VLC also needs an extension in the filename for this to work.
  94. return YES;
  95. }
  96. - (VLCMedia *)media
  97. {
  98. if (!_URL)
  99. return [VLCMedia mediaAsNodeWithName:self.name];
  100. return [VLCMedia mediaWithURL:_URL];
  101. }
  102. @end