VLCOneDriveController.m 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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)login
  63. {
  64. [_liveClient login:self.delegate
  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. }
  75. - (NSArray *)currentListFiles
  76. {
  77. return _currentFolder.items;
  78. }
  79. - (BOOL)isAuthorized
  80. {
  81. return _liveClient.session != NULL;
  82. }
  83. - (void)authCompleted:(LiveConnectSessionStatus)status session:(LiveConnectSession *)session userState:(id)userState
  84. {
  85. APLog(@"OneDrive: authCompleted, status %i, state %@", status, userState);
  86. if (session != NULL && [userState isEqualToString:@"init"] && status == 1)
  87. _activeSession = YES;
  88. if (session != NULL && [userState isEqualToString:@"login"] && status == 1)
  89. _userAuthenticated = YES;
  90. if (status == 0) {
  91. _activeSession = NO;
  92. _userAuthenticated = NO;
  93. }
  94. if (self.delegate) {
  95. if ([self.delegate respondsToSelector:@selector(sessionWasUpdated)])
  96. [self.delegate performSelector:@selector(sessionWasUpdated)];
  97. }
  98. [[NSNotificationCenter defaultCenter] postNotificationName:VLCOneDriveControllerSessionUpdated object:self];
  99. }
  100. - (void)authFailed:(NSError *)error userState:(id)userState
  101. {
  102. APLog(@"OneDrive auth failed: %@, %@", error, userState);
  103. _activeSession = NO;
  104. if (self.delegate) {
  105. if ([self.delegate respondsToSelector:@selector(sessionWasUpdated)])
  106. [self.delegate performSelector:@selector(sessionWasUpdated)];
  107. }
  108. [[NSNotificationCenter defaultCenter] postNotificationName:VLCOneDriveControllerSessionUpdated object:self];
  109. }
  110. - (void)liveOperationSucceeded:(LiveDownloadOperation *)operation
  111. {
  112. APLog(@"ODC: liveOperationSucceeded (%@)", operation.userState);
  113. }
  114. - (void)liveOperationFailed:(NSError *)error operation:(LiveDownloadOperation *)operation
  115. {
  116. APLog(@"ODC: liveOperationFailed %@ (%@)", error, operation.userState);
  117. }
  118. #pragma mark - listing
  119. - (void)requestDirectoryListingAtPath:(NSString *)path
  120. {
  121. [self loadCurrentFolder];
  122. }
  123. - (void)loadTopLevelFolder
  124. {
  125. _rootFolder = [[VLCOneDriveObject alloc] init];
  126. _rootFolder.objectId = @"me/skydrive";
  127. _rootFolder.name = @"OneDrive";
  128. _rootFolder.type = @"folder";
  129. _rootFolder.liveClient = _liveClient;
  130. _rootFolder.delegate = self;
  131. _currentFolder = _rootFolder;
  132. [_rootFolder loadFolderContent];
  133. }
  134. - (void)loadCurrentFolder
  135. {
  136. if (_currentFolder == nil)
  137. [self loadTopLevelFolder];
  138. else {
  139. _currentFolder.delegate = self;
  140. [_currentFolder loadFolderContent];
  141. }
  142. }
  143. #pragma mark - file handling
  144. - (void)downloadObject:(VLCOneDriveObject *)object
  145. {
  146. if (object.isFolder)
  147. return;
  148. object.downloadDelegate = self;
  149. if (!_pendingDownloads)
  150. _pendingDownloads = [[NSMutableArray alloc] init];
  151. [_pendingDownloads addObject:object];
  152. [self _triggerNextDownload];
  153. }
  154. - (void)_triggerNextDownload
  155. {
  156. if (_pendingDownloads.count > 0 && !_downloadInProgress) {
  157. _downloadInProgress = YES;
  158. [_pendingDownloads[0] saveObjectToDocuments];
  159. [_pendingDownloads removeObjectAtIndex:0];
  160. if ([self.delegate respondsToSelector:@selector(numberOfFilesWaitingToBeDownloadedChanged)])
  161. [self.delegate numberOfFilesWaitingToBeDownloadedChanged];
  162. }
  163. }
  164. - (void)downloadStarted:(VLCOneDriveObject *)object
  165. {
  166. _startDL = [NSDate timeIntervalSinceReferenceDate];
  167. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStarted)])
  168. [self.delegate operationWithProgressInformationStarted];
  169. }
  170. - (void)downloadEnded:(VLCOneDriveObject *)object
  171. {
  172. if ([self.delegate respondsToSelector:@selector(operationWithProgressInformationStopped)])
  173. [self.delegate operationWithProgressInformationStopped];
  174. _downloadInProgress = NO;
  175. [self _triggerNextDownload];
  176. }
  177. - (void)progressUpdated:(CGFloat)progress
  178. {
  179. if ([self.delegate respondsToSelector:@selector(currentProgressInformation:)])
  180. [self.delegate currentProgressInformation:progress];
  181. }
  182. - (void)calculateRemainingTime:(CGFloat)receivedDataSize expectedDownloadSize:(CGFloat)expectedDownloadSize
  183. {
  184. CGFloat lastSpeed = receivedDataSize / ([NSDate timeIntervalSinceReferenceDate] - _startDL);
  185. CGFloat smoothingFactor = 0.005;
  186. _averageSpeed = isnan(_averageSpeed) ? lastSpeed : smoothingFactor * lastSpeed + (1 - smoothingFactor) * _averageSpeed;
  187. CGFloat RemainingInSeconds = (expectedDownloadSize - receivedDataSize)/_averageSpeed;
  188. NSDate *date = [NSDate dateWithTimeIntervalSince1970:RemainingInSeconds];
  189. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  190. [formatter setDateFormat:@"HH:mm:ss"];
  191. [formatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
  192. NSString *remaingTime = [formatter stringFromDate:date];
  193. if ([self.delegate respondsToSelector:@selector(updateRemainingTime:)])
  194. [self.delegate updateRemainingTime:remaingTime];
  195. }
  196. #pragma mark - onedrive object delegation
  197. - (void)folderContentLoaded:(VLCOneDriveObject *)sender
  198. {
  199. if (self.delegate)
  200. [self.delegate performSelector:@selector(mediaListUpdated)];
  201. }
  202. - (void)folderContentLoadingFailed:(NSError *)error sender:(VLCOneDriveObject *)sender
  203. {
  204. APLog(@"folder content loading failed %@", error);
  205. }
  206. - (void)fullFolderTreeLoaded:(VLCOneDriveObject *)sender
  207. {
  208. if (self.delegate)
  209. [self.delegate performSelector:@selector(mediaListUpdated)];
  210. }
  211. @end