VLCNetworkServerBrowserVLCMedia.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. @interface VLCNetworkServerBrowserVLCMedia () <VLCMediaListDelegate>
  14. @property (nonatomic) VLCMedia *rootMedia;
  15. @property (nonatomic) VLCMediaList *mediaList;
  16. @property (nonatomic) NSMutableArray<id<VLCNetworkServerBrowserItem>> *mutableItems;
  17. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  18. @end
  19. @implementation VLCNetworkServerBrowserVLCMedia
  20. @synthesize delegate = _delegate;
  21. - (instancetype)initWithMedia:(VLCMedia *)media options:(nonnull NSDictionary *)mediaOptions
  22. {
  23. self = [super init];
  24. if (self) {
  25. _mutableItems = [[NSMutableArray alloc] init];
  26. _rootMedia = media;
  27. [media parseWithOptions:VLCMediaParseNetwork];
  28. _mediaList = [_rootMedia subitems];
  29. _mediaList.delegate = self;
  30. _mediaOptions = [mediaOptions copy];
  31. }
  32. return self;
  33. }
  34. - (void)update {
  35. [self.rootMedia parseWithOptions:VLCMediaParseNetwork];
  36. }
  37. - (NSString *)title {
  38. return [self.rootMedia metadataForKey:VLCMetaInformationTitle];
  39. }
  40. - (NSArray<id<VLCNetworkServerBrowserItem>> *)items {
  41. return self.mutableItems.copy;
  42. }
  43. #pragma mark - media list delegate
  44. - (void)mediaList:(VLCMediaList *)aMediaList mediaAdded:(VLCMedia *)media atIndex:(NSInteger)index
  45. {
  46. [media parseWithOptions:VLCMediaParseNetwork];
  47. [media addOptions:self.mediaOptions];
  48. [self.mutableItems addObject:[[VLCNetworkServerBrowserItemVLCMedia alloc] initWithMedia:media options:self.mediaOptions]];
  49. [self.delegate networkServerBrowserDidUpdate:self];
  50. }
  51. - (void)mediaList:(VLCMediaList *)aMediaList mediaRemovedAtIndex:(NSInteger)index {
  52. [self.mutableItems removeObjectAtIndex:index];
  53. [self.delegate networkServerBrowserDidUpdate:self];
  54. }
  55. @end
  56. @interface VLCNetworkServerBrowserItemVLCMedia ()
  57. @property (nonatomic, readonly) VLCMedia *media;
  58. @property (nonatomic, readonly) NSDictionary *mediaOptions;
  59. @end
  60. @implementation VLCNetworkServerBrowserItemVLCMedia
  61. @synthesize name = _name, container = _container, fileSizeBytes = _fileSizeBytes, URL = _URL;
  62. - (instancetype)initWithMedia:(VLCMedia *)media options:(NSDictionary *)mediaOptions;
  63. {
  64. self = [super init];
  65. if (self) {
  66. _media = media;
  67. _container = media.mediaType == VLCMediaTypeDirectory;
  68. NSString *title = [media metadataForKey:VLCMetaInformationTitle];
  69. if (!title) {
  70. title = media.url.lastPathComponent;
  71. }
  72. if (!title) {
  73. title = media.url.absoluteString;
  74. }
  75. _name = title;
  76. _URL = media.url;
  77. _mediaOptions = [mediaOptions copy];
  78. // _downloadable = NO; //TODO: add property for downloadable?
  79. }
  80. return self;
  81. }
  82. - (id<VLCNetworkServerBrowser>)containerBrowser {
  83. return [[VLCNetworkServerBrowserVLCMedia alloc] initWithMedia:self.media options:self.mediaOptions];
  84. }
  85. @end
  86. @implementation VLCNetworkServerBrowserVLCMedia (SMB)
  87. + (instancetype)SMBNetworkServerBrowserWithURL:(NSURL *)url username:(NSString *)username password:(NSString *)password workgroup:(NSString *)workgroup {
  88. VLCMedia *media = [VLCMedia mediaWithURL:url];
  89. NSDictionary *mediaOptions = @{@"smb-user" : username ?: @"",
  90. @"smb-pwd" : password ?: @"",
  91. @"smb-domain" : workgroup?: @"WORKGROUP"};
  92. [media addOptions:mediaOptions];
  93. return [[self alloc] initWithMedia:media options:mediaOptions];
  94. }
  95. @end