VLCNetworkServerBrowserSharedLibrary.m 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*****************************************************************************
  2. * VLCNetworkServerBrowserSharedLibrary.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 "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;
  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. _items = [items copy];
  46. [[NSOperationQueue mainQueue] addOperationWithBlock:^{
  47. [self.delegate networkServerBrowserDidUpdate:self];
  48. }];
  49. }
  50. @end
  51. @implementation VLCNetworkServerBrowserItemSharedLibrary
  52. @synthesize name = _name, URL = _URL, fileSizeBytes = _fileSizeBytes, container = _container;
  53. - (instancetype)initWithDictionary:(NSDictionary *)dictionary
  54. {
  55. self = [super init];
  56. if (self) {
  57. _name = dictionary[@"title"];
  58. _fileSizeBytes = dictionary[@"size"];
  59. _duration = dictionary[@"duration"];
  60. NSString *subtitleURLString = dictionary[@"pathSubtitle"];
  61. if ([subtitleURLString isEqualToString:@"(null)"]) subtitleURLString = nil;
  62. subtitleURLString = [subtitleURLString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  63. _subtitleURL = subtitleURLString.length ? [NSURL URLWithString:subtitleURLString] : nil;
  64. _URL = [NSURL URLWithString:dictionary[@"pathfile"]];
  65. _container = NO;
  66. NSString *thumbURLString = dictionary[@"thumb"];
  67. _thumbnailURL = thumbURLString.length ? [NSURL URLWithString:thumbURLString] : nil;
  68. }
  69. return self;
  70. }
  71. - (id<VLCNetworkServerBrowser>)containerBrowser {
  72. return nil;
  73. }
  74. @end