VLCOneDriveController.m 6.9 KB

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