VLCBoxCollectionViewController.m 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*****************************************************************************
  2. * VLCBoxCollectionViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Carola Nitz <nitz.carola # googlemail.com>
  9. * Felix Paul Kühne <fkuehne # videolan.org>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCBoxCollectionViewController.h"
  14. #import "VLCBoxController.h"
  15. #import <SSKeychain/SSKeychain.h>
  16. #import "VLCPlaybackController.h"
  17. @interface VLCBoxCollectionViewController () <VLCCloudStorageDelegate, NSURLConnectionDataDelegate>
  18. {
  19. BoxFile *_selectedFile;
  20. VLCBoxController *_boxController;
  21. NSArray *_listOfFiles;
  22. }
  23. @end
  24. @implementation VLCBoxCollectionViewController
  25. - (instancetype)initWithPath:(NSString *)path
  26. {
  27. self = [super initWithNibName:@"VLCRemoteBrowsingCollectionViewController" bundle:nil];
  28. if (self) {
  29. self.currentPath = path;
  30. }
  31. return self;
  32. }
  33. - (void)dealloc
  34. {
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (void)viewDidLoad {
  38. [super viewDidLoad];
  39. _boxController = [VLCBoxController sharedInstance];
  40. self.controller = _boxController;
  41. self.controller.delegate = self;
  42. self.title = @"Box";
  43. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  44. [defaultCenter addObserver:self
  45. selector:@selector(boxApiTokenDidRefresh)
  46. name:BoxOAuth2SessionDidRefreshTokensNotification
  47. object:[BoxSDK sharedSDK].OAuth2Session];
  48. [defaultCenter addObserver:self
  49. selector:@selector(boxApiTokenDidRefresh)
  50. name:BoxOAuth2SessionDidBecomeAuthenticatedNotification
  51. object:[BoxSDK sharedSDK].OAuth2Session];
  52. }
  53. - (void)viewWillAppear:(BOOL)animated
  54. {
  55. [super viewWillAppear:animated];
  56. _boxController = [VLCBoxController sharedInstance];
  57. self.controller = _boxController;
  58. self.controller.delegate = self;
  59. if (!_listOfFiles || _listOfFiles.count == 0)
  60. [self requestInformationForCurrentPath];
  61. }
  62. - (void)viewWillDisappear:(BOOL)animated
  63. {
  64. [super viewWillDisappear:animated];
  65. if ([UIApplication sharedApplication].delegate.window.rootViewController.presentedViewController == nil) {
  66. [_boxController stopSession];
  67. [self.collectionView reloadData];
  68. }
  69. }
  70. - (void)mediaListUpdated
  71. {
  72. _listOfFiles = [[VLCBoxController sharedInstance].currentListFiles copy];
  73. [self.collectionView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  74. }
  75. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. VLCCloudStorageCollectionViewCell *cell = (VLCCloudStorageCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:VLCRemoteBrowsingTVCellIdentifier forIndexPath:indexPath];
  78. NSUInteger index = indexPath.row;
  79. if (_listOfFiles) {
  80. if (index < _listOfFiles.count) {
  81. cell.boxFile = _listOfFiles[index];
  82. }
  83. }
  84. return cell;
  85. }
  86. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
  87. {
  88. return _listOfFiles.count;
  89. }
  90. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
  91. {
  92. if (indexPath.row >= _listOfFiles.count)
  93. return;
  94. _selectedFile = _listOfFiles[indexPath.row];
  95. if (![_selectedFile.type isEqualToString:@"folder"])
  96. [self streamFile:(BoxFile *)_selectedFile];
  97. else {
  98. /* dive into subdirectory */
  99. NSString *path = self.currentPath;
  100. if (![path isEqualToString:@""])
  101. path = [path stringByAppendingString:@"/"];
  102. path = [path stringByAppendingString:_selectedFile.modelID];
  103. VLCBoxCollectionViewController *targetViewController = [[VLCBoxCollectionViewController alloc] initWithPath:path];
  104. [self.navigationController pushViewController:targetViewController animated:YES];
  105. }
  106. }
  107. - (void)streamFile:(BoxFile *)file
  108. {
  109. /* the Box API requires us to set an HTTP header to get the actual URL:
  110. * curl -L https://api.box.com/2.0/files/FILE_ID/content -H "Authorization: Bearer ACCESS_TOKEN"
  111. *
  112. * ... however, libvlc does not support setting custom HTTP headers, so we are resolving the redirect ourselves with a NSURLConnection
  113. * and pass the final location to libvlc, which does not require a custom HTTP header */
  114. NSURL *baseURL = [[[BoxSDK sharedSDK] filesManager] URLWithResource:@"files"
  115. ID:file.modelID
  116. subresource:@"content"
  117. subID:nil];
  118. NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:baseURL
  119. cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
  120. timeoutInterval:60];
  121. [urlRequest setValue:[NSString stringWithFormat:@"Bearer %@", [BoxSDK sharedSDK].OAuth2Session.accessToken] forHTTPHeaderField:@"Authorization"];
  122. NSURLConnection *theTestConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
  123. [theTestConnection start];
  124. }
  125. - (NSURLRequest *)connection:(NSURLConnection *)connection willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)response
  126. {
  127. if (response != nil) {
  128. /* we have 1 redirect from the original URL, so as soon as we'd do that,
  129. * we grab the URL and cancel the connection */
  130. NSURL *theActualURL = request.URL;
  131. [connection cancel];
  132. /* now ask VLC to stream the URL we were just passed */
  133. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  134. [vpc playURL:theActualURL successCallback:nil errorCallback:nil];
  135. VLCFullscreenMovieTVViewController *movieVC = [VLCFullscreenMovieTVViewController fullscreenMovieTVViewController];
  136. [self presentViewController:movieVC
  137. animated:YES
  138. completion:nil];
  139. }
  140. return request;
  141. }
  142. #pragma mark - BoxAuthorizationViewControllerDelegate
  143. - (void)boxApiTokenDidRefresh
  144. {
  145. NSString *token = [BoxSDK sharedSDK].OAuth2Session.refreshToken;
  146. [SSKeychain setPassword:token forService:kVLCBoxService account:kVLCBoxAccount];
  147. NSUbiquitousKeyValueStore *ubiquitousStore = [NSUbiquitousKeyValueStore defaultStore];
  148. [ubiquitousStore setString:token forKey:kVLCStoreBoxCredentials];
  149. [ubiquitousStore synchronize];
  150. self.authorizationInProgress = YES;
  151. [self updateViewAfterSessionChange];
  152. self.authorizationInProgress = NO;
  153. }
  154. - (void)scrollViewDidScroll:(UIScrollView *)scrollView
  155. {
  156. NSInteger currentOffset = scrollView.contentOffset.y;
  157. NSInteger maximumOffset = scrollView.contentSize.height - scrollView.frame.size.height;
  158. if (_boxController.hasMoreFiles && !self.activityIndicator.isAnimating) {
  159. [self requestInformationForCurrentPath];
  160. }
  161. }
  162. @end