VLCOneDriveController.m 7.2 KB

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