VLCOneDriveController.m 8.0 KB

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