VLCNetworkServerBrowserVLCMedia.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserVLCMedia.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 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 "VLCNetworkServerBrowserVLCMedia.h"
  13. #import "NSString+SupportedMedia.h"
  14. #import "VLC-Swift.h"
  15. @interface VLCNetworkServerBrowserVLCMedia () <VLCMediaListDelegate, VLCMediaDelegate>
  16. {
  17. VLCDialogProvider *_dialogProvider;
  18. VLCCustomDialogRendererHandler *_customDialogHandler;
  19. }
  20. @property (nonatomic) VLCMedia *rootMedia;
  21. @property (nonatomic) VLCMediaList *mediaList;
  22. @property (nonatomic) VLCMediaList *mediaListUnfiltered;
  23. @property (nonatomic) NSMutableArray<id<VLCNetworkServerBrowserItem>> *mutableItems;
  24. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  25. @end
  26. @implementation VLCNetworkServerBrowserVLCMedia
  27. @synthesize delegate = _delegate;
  28. - (instancetype)initWithMedia:(VLCMedia *)media options:(nonnull NSDictionary *)mediaOptions
  29. {
  30. self = [super init];
  31. if (self) {
  32. _mutableItems = [[NSMutableArray alloc] init];
  33. _mediaList = [[VLCMediaList alloc] init];
  34. _rootMedia = media;
  35. _rootMedia.delegate = self;
  36. // Set timeout to 0 in order to avoid getting interrupted in dialogs for timeout reasons
  37. [media parseWithOptions:VLCMediaParseNetwork|VLCMediaDoInteract timeout:0];
  38. _mediaListUnfiltered = [_rootMedia subitems];
  39. _mediaListUnfiltered.delegate = self;
  40. NSMutableDictionary *mediaOptionsNoFilter = [mediaOptions mutableCopy];
  41. [mediaOptionsNoFilter setObject:@" " forKey:@":ignore-filetypes"];
  42. _mediaOptions = [mediaOptionsNoFilter copy];
  43. [self _addMediaListRootItemsToList];
  44. _dialogProvider = [[VLCDialogProvider alloc] initWithLibrary:[VLCLibrary sharedLibrary] customUI:YES];
  45. _customDialogHandler = [[VLCCustomDialogRendererHandler alloc]
  46. initWithDialogProvider:_dialogProvider];
  47. _dialogProvider.customRenderer = _customDialogHandler;
  48. }
  49. return self;
  50. }
  51. - (BOOL)shouldFilterMedia:(VLCMedia *)media
  52. {
  53. NSString *absoluteString = media.url.absoluteString;
  54. return ![absoluteString isSupportedAudioMediaFormat] && ![absoluteString isSupportedMediaFormat] && ![absoluteString isSupportedPlaylistFormat] && media.mediaType != VLCMediaTypeDirectory;
  55. }
  56. - (void)_addMediaListRootItemsToList
  57. {
  58. VLCMediaList *rootItems = _rootMedia.subitems;
  59. [rootItems lock];
  60. NSUInteger count = rootItems.count;
  61. for (NSUInteger i = 0; i < count; i++) {
  62. VLCMedia *media = [rootItems mediaAtIndex:i];
  63. if (![self shouldFilterMedia:media]) {
  64. NSInteger mediaIndex = self.mutableItems.count;
  65. [self.mediaList insertMedia:media atIndex:mediaIndex];
  66. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  67. }
  68. }
  69. [rootItems unlock];
  70. }
  71. - (void)update {
  72. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  73. if (ret == -1) {
  74. [self.delegate networkServerBrowserDidUpdate:self];
  75. }
  76. }
  77. - (NSString *)title {
  78. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  79. }
  80. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  81. return self.mutableItems.copy;
  82. }
  83. #pragma mark - media list delegate
  84. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSUInteger)index
  85. {
  86. [media addOptions:self.mediaOptions];
  87. if (![self shouldFilterMedia:media]) {
  88. NSInteger mediaIndex = self.mutableItems.count;
  89. [self.mediaList insertMedia:media atIndex:mediaIndex];
  90. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  91. }
  92. [self.delegate networkServerBrowserDidUpdate:self];
  93. }
  94. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSUInteger)index
  95. {
  96. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  97. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  98. [self.mediaList removeMediaAtIndex:mediaIndex];
  99. [self.mutableItems removeObjectAtIndex:mediaIndex];
  100. [self.delegate networkServerBrowserDidUpdate:self];
  101. }
  102. #pragma mark - media delegate
  103. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  104. {
  105. if ([aMedia parsedStatus] != VLCMediaParsedStatusDone) {
  106. [self.delegate networkServerBrowserShouldPopView:self];
  107. } else if (self.mediaList.count != 0) {
  108. [self.delegate networkServerBrowserDidUpdate:self];
  109. } else {
  110. [self.delegate networkServerBrowserEndParsing:self];
  111. }
  112. }
  113. @end
  114. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  115. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  116. @end
  117. @implementation VLCNetworkServerBrowserItemVLCMedia
  118. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  119. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  120. {
  121. self = [super init];
  122. if (self) {
  123. _media = media;
  124. _container = media.mediaType == VLCMediaTypeDirectory;
  125. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  126. if (!title) {
  127. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  128. }
  129. if (!title) {
  130. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  131. }
  132. _name = title;
  133. _URL = media.url;
  134. _mediaOptions = [mediaOptions copy];
  135. _downloadable = NO;
  136. }
  137. return self;
  138. }
  139. - (id<VLCNetworkServerBrowser>)containerBrowser {
  140. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  141. }
  142. @end