VLCOneDriveController.m 8.0 KB

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