VLCBoxCollectionViewController.m 7.3 KB

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