VLCOneDriveController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*****************************************************************************
  2. * VLCOneDriveController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-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 "VLCOneDriveController.h"
  13. #import "VLCOneDriveConstants.h"
  14. #import "VLCOneDriveObject.h"
  15. /* the Live SDK doesn't have an umbrella header so we need to import what we need */
  16. #import <LiveSDK/LiveConnectClient.h>
  17. /* include private API headers */
  18. #import <LiveSDK/LiveApiHelper.h>
  19. @interface VLCOneDriveController () <LiveAuthDelegate, VLCOneDriveObjectDelegate, VLCOneDriveObjectDownloadDelegate>
  20. {
  21. LiveConnectClient *_liveClient;
  22. //VLCOneDriveObject *_folderiD;
  23. NSString *_folderId;
  24. NSArray *_liveScopes;
  25. BOOL _activeSession;
  26. BOOL _userAuthenticated;
  27. NSMutableArray *_pendingDownloads;
  28. BOOL _downloadInProgress;
  29. CGFloat _averageSpeed;
  30. CGFloat _fileSize;
  31. NSTimeInterval _startDL;
  32. NSTimeInterval _lastStatsUpdate;
  33. }
  34. @end
  35. @implementation VLCOneDriveController
  36. + (VLCCloudStorageController *)sharedInstance
  37. {
  38. static VLCOneDriveController *sharedInstance = nil;
  39. static dispatch_once_t pred;
  40. dispatch_once(&pred, ^{
  41. sharedInstance = [[self alloc] init];
  42. });
  43. return sharedInstance;
  44. }
  45. - (instancetype)init
  46. {
  47. self = [super init];
  48. if (!self)
  49. return self;
  50. _liveScopes = @[@"wl.signin",@"wl.offline_access",@"wl.skydrive"];
  51. _liveClient = [[LiveConnectClient alloc] initWithClientId:kVLCOneDriveClientID
  52. scopes:_liveScopes
  53. delegate:self
  54. userState:@"init"];
  55. return self;
  56. }
  57. #pragma mark - authentication
  58. - (BOOL)activeSession
  59. {
  60. return _activeSession;
  61. }
  62. - (void)loginWithViewController:(UIViewController *)presentingViewController
  63. {
  64. [_liveClient login:presentingViewController
  65. scopes:_liveScopes
  66. delegate:self
  67. userState:@"login"];
  68. }
  69. - (void)logout
  70. {
  71. [_liveClient logoutWithDelegate:self userState:@"logout"];
  72. _activeSession = NO;
  73. _userAuthenticated = NO;
  74. _currentFolder = nil;
  75. if ([self.delegate respondsToSelector:@selector(mediaListUpdated)])
  76. [self.delegate mediaListUpdated];
  77. }
  78. - (NSArray *)currentListFiles
  79. {
  80. return _currentFolder.items;
  81. }
  82. - (BOOL)isAuthorized
  83. {
  84. return _liveClient.session != NULL;
  85. }
  86. - (void)authCompleted:(LiveConnectSessionStatus)status session:(LiveConnectSession *)session userState:(id)userState
  87. {
  88. APLog(@"OneDrive: authCompleted, status %i, state %@", status, userState);
  89. if (session != NULL && [userState isEqualToString:@"init"] && status == 1)
  90. _activeSession = YES;
  91. if (session != NULL && [userState isEqualToString:@"login"] && status == 1)
  92. _userAuthenticated = YES;
  93. if (status == 0) {
  94. _activeSession = NO;
  95. _userAuthenticated = NO;
  96. }
  97. if (self.delegate) {
  98. if ([self.delegate respondsToSelector:@selector(sessionWasUpdated)])
  99. [self.delegate performSelector:@selector(sessionWasUpdated)];
  100. }
  101. [[NSNotificationCenter defaultCenter] postNotificationName:VLCOneDriveControllerSessionUpdated object:self];
  102. }
  103. - (void)authFailed:(NSError *)error userState:(id)userState
  104. {
  105. APLog(@"OneDrive auth failed: %@, %@", error, userState);
  106. _activeSession = NO;
  107. if (self.delegate) {
  108. if ([self.delegate respondsToSelector:@selector(sessionWasUpdated)])
  109. [self.delegate performSelector:@selector(sessionWasUpdated)];
  110. }
  111. [[NSNotificationCenter defaultCenter] postNotificationName:VLCOneDriveControllerSessionUpdated object:self];
  112. }
  113. - (void)liveOperationSucceeded:(LiveDownloadOperation *)operation
  114. {
  115. APLog(@"ODC: liveOperationSucceeded (%@)", operation.userState);
  116. }
  117. - (void)liveOperationFailed:(NSError *)error operation:(LiveDownloadOperation *)operation
  118. {
  119. APLog(@"ODC: liveOperationFailed %@ (%@)", error, operation.userState);
  120. }
  121. #pragma mark - listing
  122. - (void)requestDirectoryListingAtPath:(NSString *)path
  123. {
  124. [self loadCurrentFolder];
  125. }
  126. - (void)loadTopLevelFolder
  127. {
  128. _rootFolder = [[VLCOneDriveObject alloc] init];
  129. _rootFolder.objectId = @"me/skydrive";
  130. _rootFolder.name = @"OneDrive";
  131. _rootFolder.type = @"folder";
  132. _rootFolder.liveClient = _liveClient;
  133. _rootFolder.delegate = self;
  134. _currentFolder = _rootFolder;
  135. [_rootFolder loadFolderContent];
  136. }
  137. - (void)loadCurrentFolder
  138. {
  139. if (_currentFolder == nil)
  140. [self loadTopLevelFolder];
  141. else {
  142. _currentFolder.delegate = self;
  143. [_currentFolder loadFolderContent];
  144. }
  145. }
  146. #pragma mark - file handling
  147. - (void)downloadObject:(VLCOneDriveObject *)object
  148. {
  149. if (object.isFolder)
  150. return;
  151. object.downloadDelegate = self;
  152. if (!_pendingDownloads)
  153. _pendingDownloads = [[NSMutableArray alloc] init];
  154. [_pendingDownloads addObject:object];
  155. [self _triggerNextDownload];
  156. }
  157. - (void)_triggerNextDownload
  158. {
  159. if (_pendingDownloads.count > 0 && !_downloadInProgress) {
  160. _downloadInProgress = YES;
  161. [_pendingDownloads[0] saveObjectToDocuments];
  162. [_pendingDownloads removeObjectAtIndex:0];
  163. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  164. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  165. }
  166. }
  167. - (void)downloadStarted:(VLCOneDriveObject *)object
  168. {
  169. _startDL = [NSDate timeIntervalSinceReferenceDate];
  170. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  171. [self.delegate operationWithProgressInformationStarted];
  172. }
  173. - (void)downloadEnded:(VLCOneDriveObject *)object
  174. {
  175. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  176. [self.delegate operationWithProgressInformationStopped];
  177. _downloadInProgress = NO;
  178. [self _triggerNextDownload];
  179. }
  180. - (void)progressUpdated:(CGFloat)progress
  181. {
  182. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  183. [self.delegate currentProgressInformation:progress];
  184. }
  185. - (void)calculateRemainingTime:(CGFloat)receivedDataSize expectedDownloadSize:(CGFloat)expectedDownloadSize
  186. {
  187. CGFloat lastSpeed = receivedDataSize / ([NSDate timeIntervalSinceReferenceDate] - _startDL);
  188. CGFloat smoothingFactor = 0.005;
  189. _averageSpeed = isnan(_averageSpeed) ? lastSpeed : smoothingFactor * lastSpeed + (1 - smoothingFactor) * _averageSpeed;
  190. CGFloat RemainingInSeconds = (expectedDownloadSize - receivedDataSize)/_averageSpeed;
  191. NSDate *date = [NSDate dateWithTimeIntervalSince1970:RemainingInSeconds];
  192. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  193. [formatter setDateFormat:@"HH:mm:ss"];
  194. [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  195. NSString *remaingTime = [formatter stringFromDate:date];
  196. if ([self.delegate respondsToSelector:@selector(updateRemainingTime:)])
  197. [self.delegate updateRemainingTime:remaingTime];
  198. }
  199. #pragma mark - onedrive object delegation
  200. - (void)folderContentLoaded:(VLCOneDriveObject *)sender
  201. {
  202. if (self.delegate)
  203. [self.delegate performSelector:@selector(mediaListUpdated)];
  204. }
  205. - (void)folderContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
  206. {
  207. APLog(@"folder content loading failed %@", error);
  208. }
  209. - (void)fullFolderTreeLoaded:(VLCOneDriveObject *)sender
  210. {
  211. if (self.delegate)
  212. [self.delegate performSelector:@selector(mediaListUpdated)];
  213. }
  214. @end