VLCOneDriveObject.m 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*****************************************************************************
  2. * VLCOneDriveObject.h
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. *
  10. * Refer to the COPYING file of the official project for license.
  11. *****************************************************************************/
  12. #import "VLCOneDriveObject.h"
  13. @implementation VLCOneDriveObject
  14. #pragma mark properties
  15. - (BOOL)isFolder
  16. {
  17. return [self.type isEqual:@"folder"] || [self.type isEqual:@"album"];
  18. }
  19. - (NSString *)filesPath
  20. {
  21. return [self.objectId stringByAppendingString:@"/files"];
  22. }
  23. - (NSString *)downloadPath
  24. {
  25. return self.objectId;
  26. }
  27. - (BOOL)hasFullFolderTree
  28. {
  29. BOOL hasFullTree = YES;
  30. if (self.folders != nil) {
  31. NSUInteger count = self.folders.count;
  32. for (NSUInteger x = 0; x < count; x++) {
  33. VLCOneDriveObject *folder = self.folders[x];
  34. if (!folder.hasFullFolderTree) {
  35. hasFullTree = NO;
  36. break;
  37. }
  38. }
  39. } else
  40. hasFullTree = NO;
  41. return hasFullTree;
  42. }
  43. #pragma mark - actions
  44. - (void)loadFolderContent
  45. {
  46. NSLog(@"loadFolderContent");
  47. if (!self.isFolder) {
  48. APLog(@"%@ is no folder, can't load content", self.objectId);
  49. return;
  50. }
  51. if (self.folders == nil) {
  52. [self.liveClient getWithPath:self.filesPath
  53. delegate:self
  54. userState:@"load-folder-content"];
  55. } else {
  56. NSUInteger count = self.folders.count;
  57. for (NSUInteger x = 0; x < count; x++) {
  58. VLCOneDriveObject *folder = self.folders[x];
  59. if (!folder.hasFullFolderTree) {
  60. [folder loadFolderContent];
  61. return;
  62. }
  63. }
  64. [self.delegate fullFolderTreeLoaded:self];
  65. }
  66. }
  67. - (void)loadFileContent
  68. {
  69. }
  70. #pragma mark - live operations
  71. - (void)liveOperationSucceeded:(LiveDownloadOperation *)operation
  72. {
  73. NSString *userState = operation.userState;
  74. NSLog(@"liveOperationSucceeded: %@", userState);
  75. if ([userState isEqualToString:@"load-file-content"]) {
  76. // LiveDownloadOperation *downloadOperation = (LiveDownloadOperation *)operation;
  77. //FIXME: handle the incoming data!
  78. [self.delegate fileContentLoaded:self];
  79. } else if ([userState isEqualToString:@"load-folder-content"]) {
  80. NSMutableArray *subFolders = [[NSMutableArray alloc] init];
  81. NSMutableArray *folderFiles = [[NSMutableArray alloc] init];
  82. NSMutableArray *items = [[NSMutableArray alloc] init];
  83. NSArray *rawFolderObjects = operation.result[@"data"];
  84. BOOL hasSubFolders = NO;
  85. NSUInteger count = rawFolderObjects.count;
  86. for (NSUInteger x = 0; x < count; x++) {
  87. NSDictionary *rawObject = rawFolderObjects[x];
  88. VLCOneDriveObject *oneDriveObject = [[VLCOneDriveObject alloc] init];
  89. oneDriveObject.parent = self;
  90. oneDriveObject.objectId = rawObject[@"id"];
  91. oneDriveObject.name = rawObject[@"name"];
  92. oneDriveObject.type = rawObject[@"type"];
  93. oneDriveObject.liveClient = self.liveClient;
  94. if (oneDriveObject.isFolder) {
  95. hasSubFolders = YES;
  96. [subFolders addObject:oneDriveObject];
  97. } else {
  98. oneDriveObject.size = rawObject[@"size"];
  99. oneDriveObject.thumbnailURL = rawObject[@"picture"];
  100. [folderFiles addObject:oneDriveObject];
  101. }
  102. [items addObject:oneDriveObject];
  103. }
  104. NSLog(@"we found %i items", items.count);
  105. for (NSUInteger x = 0; x < items.count; x++)
  106. NSLog(@"%@", [items[x] name]);
  107. self.folders = subFolders;
  108. self.files = folderFiles;
  109. self.items = items;
  110. [self.delegate folderContentLoaded:self];
  111. }
  112. }
  113. - (void)liveOperationFailed:(NSError *)error operation:(LiveDownloadOperation *)operation
  114. {
  115. NSString *userState = operation.userState;
  116. NSLog(@"liveOperationFailed %@ (%@)", userState, error);
  117. if ([userState isEqualToString:@"load-folder-content"])
  118. [self.delegate folderContentLoadingFailed:error sender:self];
  119. else if ([userState isEqualToString:@"load-file-content"])
  120. [self.delegate fileContentLoadingFailed:error sender:self];
  121. else
  122. APLog(@"failing live operation with state %@ failed with error %@", userState, error);
  123. }
  124. #pragma mark - delegation
  125. - (void)folderContentLoaded:(VLCOneDriveObject *)sender
  126. {
  127. }
  128. - (void)folderContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
  129. {
  130. if (self.delegate)
  131. [self.delegate folderContentLoadingFailed:error sender:self];
  132. }
  133. - (void)fullFolderTreeLoaded:(VLCOneDriveObject *)sender
  134. {
  135. [self loadFolderContent];
  136. }
  137. - (void)fileContentLoaded:(VLCOneDriveObject *)sender
  138. {
  139. }
  140. - (void)fileContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
  141. {
  142. }
  143. @end