VLCNetworkServerBrowserVLCMedia.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. [media parseWithOptions:VLCMediaParseNetwork|VLCMediaDoInteract];
  37. _mediaListUnfiltered = [_rootMedia subitems];
  38. _mediaListUnfiltered.delegate = self;
  39. NSMutableDictionary *mediaOptionsNoFilter = [mediaOptions mutableCopy];
  40. [mediaOptionsNoFilter setObject:@" " forKey:@":ignore-filetypes"];
  41. _mediaOptions = [mediaOptionsNoFilter copy];
  42. [self _addMediaListRootItemsToList];
  43. _dialogProvider = [[VLCDialogProvider alloc] initWithLibrary:[VLCLibrary sharedLibrary] customUI:YES];
  44. _customDialogHandler = [[VLCCustomDialogRendererHandler alloc]
  45. initWithDialogProvider:_dialogProvider];
  46. __weak typeof(self) weakSelf = self;
  47. _customDialogHandler.completionHandler = ^(VLCCustomDialogRendererHandlerCompletionType status)
  48. {
  49. [weakSelf customDialogCompletionHandlerWithStatus:status];
  50. };
  51. _dialogProvider.customRenderer = _customDialogHandler;
  52. }
  53. return self;
  54. }
  55. - (void)customDialogCompletionHandlerWithStatus:(VLCCustomDialogRendererHandlerCompletionType)status
  56. {
  57. switch (status) {
  58. case VLCCustomDialogRendererHandlerCompletionTypeError:
  59. [self.delegate networkServerBrowserShouldPopView:self];
  60. break;
  61. default:
  62. break;
  63. }
  64. }
  65. - (BOOL)shouldFilterMedia:(VLCMedia *)media
  66. {
  67. NSString *absoluteString = media.url.absoluteString;
  68. return ![absoluteString isSupportedAudioMediaFormat] && ![absoluteString isSupportedMediaFormat] && ![absoluteString isSupportedPlaylistFormat] && media.mediaType != VLCMediaTypeDirectory;
  69. }
  70. - (void)_addMediaListRootItemsToList
  71. {
  72. VLCMediaList *rootItems = _rootMedia.subitems;
  73. [rootItems lock];
  74. NSUInteger count = rootItems.count;
  75. for (NSUInteger i = 0; i < count; i++) {
  76. VLCMedia *media = [rootItems mediaAtIndex:i];
  77. if (![self shouldFilterMedia:media]) {
  78. NSInteger mediaIndex = self.mutableItems.count;
  79. [self.mediaList insertMedia:media atIndex:mediaIndex];
  80. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  81. }
  82. }
  83. [rootItems unlock];
  84. }
  85. - (void)update {
  86. int ret = [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  87. if (ret == -1) {
  88. [self.delegate networkServerBrowserDidUpdate:self];
  89. }
  90. }
  91. - (NSString *)title {
  92. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  93. }
  94. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  95. return self.mutableItems.copy;
  96. }
  97. #pragma mark - media list delegate
  98. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSUInteger)index
  99. {
  100. [media addOptions:self.mediaOptions];
  101. if (![self shouldFilterMedia:media]) {
  102. NSInteger mediaIndex = self.mutableItems.count;
  103. [self.mediaList insertMedia:media atIndex:mediaIndex];
  104. [self.mutableItems insertObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions] atIndex:mediaIndex];
  105. }
  106. [self.delegate networkServerBrowserDidUpdate:self];
  107. }
  108. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSUInteger)index
  109. {
  110. VLCMedia *media = [self.mediaListUnfiltered mediaAtIndex:index];
  111. NSInteger mediaIndex = [self.mediaList indexOfMedia:media];
  112. [self.mediaList removeMediaAtIndex:mediaIndex];
  113. [self.mutableItems removeObjectAtIndex:mediaIndex];
  114. [self.delegate networkServerBrowserDidUpdate:self];
  115. }
  116. #pragma mark - media delegate
  117. - (void)mediaDidFinishParsing:(VLCMedia *)aMedia
  118. {
  119. [self.delegate networkServerBrowserDidUpdate:self];
  120. }
  121. @end
  122. @interface VLCNetworkServerBrowserItemVLCMedia () <VLCMediaDelegate>
  123. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  124. @end
  125. @implementation VLCNetworkServerBrowserItemVLCMedia
  126. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL, media = _media, downloadable = _downloadable;
  127. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  128. {
  129. self = [super init];
  130. if (self) {
  131. _media = media;
  132. _container = media.mediaType == VLCMediaTypeDirectory;
  133. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  134. if (!title) {
  135. title = [media.url.lastPathComponent stringByRemovingPercentEncoding];
  136. }
  137. if (!title) {
  138. title = [media.url.absoluteString stringByRemovingPercentEncoding];
  139. }
  140. _name = title;
  141. _URL = media.url;
  142. _mediaOptions = [mediaOptions copy];
  143. _downloadable = NO;
  144. }
  145. return self;
  146. }
  147. - (id<VLCNetworkServerBrowser>)containerBrowser {
  148. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  149. }
  150. @end