VLCOneDriveTableViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*****************************************************************************
  2. * VLCOneDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2019 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre Sagaspe <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCOneDriveTableViewController.h"
  14. #import "VLCOneDriveController.h"
  15. #import "VLCCloudStorageTableViewCell.h"
  16. #import "VLCPlaybackController.h"
  17. #import "VLCProgressView.h"
  18. #import "UIDevice+VLC.h"
  19. #import "NSString+SupportedMedia.h"
  20. #import "VLC-Swift.h"
  21. #import <OneDriveSDK/ODItem.h>
  22. #import <OneDriveSDK/ODItemReference.h>
  23. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  24. {
  25. VLCOneDriveController *_oneDriveController;
  26. }
  27. @end
  28. @implementation VLCOneDriveTableViewController
  29. - (void)viewDidLoad {
  30. [super viewDidLoad];
  31. [self prepareOneDriveControllerIfNeeded];
  32. self.controller = _oneDriveController;
  33. self.controller.delegate = self;
  34. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  35. #if TARGET_OS_IOS
  36. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  37. [self.cloudStorageLogo sizeToFit];
  38. self.cloudStorageLogo.center = self.view.center;
  39. #endif
  40. }
  41. - (void)viewWillAppear:(BOOL)animated
  42. {
  43. [super viewWillAppear:animated];
  44. if (@available(iOS 11.0, *)) {
  45. self.navigationController.navigationBar.prefersLargeTitles = NO;
  46. }
  47. [self updateViewAfterSessionChange];
  48. self.authorizationInProgress = NO;
  49. [self prepareOneDriveControllerIfNeeded];
  50. }
  51. - (void)prepareOneDriveControllerIfNeeded
  52. {
  53. if (!_oneDriveController) {
  54. _oneDriveController = [VLCOneDriveController sharedInstance];
  55. _oneDriveController.presentingViewController = self;
  56. }
  57. }
  58. #pragma mark - generic interface interaction
  59. - (void)goBack
  60. {
  61. NSString *currentItemID = _oneDriveController.currentItem.id;
  62. if (currentItemID && ![currentItemID isEqualToString:_oneDriveController.rootItemID]) {
  63. if (!_oneDriveController.parentItem
  64. || [_oneDriveController.rootItemID isEqualToString:_oneDriveController.parentItem.id]) {
  65. _oneDriveController.currentItem = nil;
  66. } else {
  67. _oneDriveController.currentItem = [[ODItem alloc] initWithDictionary:_oneDriveController.parentItem.dictionaryFromItem];
  68. _oneDriveController.parentItem.id = _oneDriveController.parentItem.parentReference.id;
  69. }
  70. self.title = _oneDriveController.currentItem.name;
  71. [self.activityIndicator startAnimating];
  72. [_oneDriveController loadODItems];
  73. [_oneDriveController loadODParentItem];
  74. } else {
  75. // We're at root, we need to pop the view
  76. [self.navigationController popViewControllerAnimated:YES];
  77. }
  78. return;
  79. }
  80. #pragma mark - table view data source
  81. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  82. {
  83. static NSString *CellIdentifier = @"OneDriveCell";
  84. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  85. if (cell == nil)
  86. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  87. NSArray *items = _oneDriveController.currentListFiles;
  88. if (indexPath.row < items.count) {
  89. cell.oneDriveFile = items[indexPath.row];
  90. cell.delegate = self;
  91. }
  92. return cell;
  93. }
  94. #pragma mark - table view delegate
  95. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  96. {
  97. NSArray *items = _oneDriveController.currentListFiles;
  98. NSInteger row = indexPath.row;
  99. if (row >= items.count)
  100. return;
  101. ODItem *selectedItem = items[row];
  102. if (selectedItem.folder) {
  103. [self.activityIndicator startAnimating];
  104. _oneDriveController.parentItem = _oneDriveController.currentItem;
  105. _oneDriveController.currentItem = selectedItem;
  106. [_oneDriveController loadODItems];
  107. self.title = selectedItem.name;
  108. } else {
  109. NSString *streamingURLString = selectedItem.dictionaryFromItem[@"@content.downloadUrl"];
  110. if (streamingURLString) {
  111. VLCMediaList *mediaList;
  112. NSURL *url = [NSURL URLWithString:streamingURLString];
  113. NSString *subtitlePath = nil;
  114. NSInteger positionIndex = 0;
  115. if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
  116. mediaList = [[VLCMediaList alloc] initWithArray:@[[VLCMedia mediaWithURL:url]]];
  117. subtitlePath = [_oneDriveController configureSubtitleWithFileName:selectedItem.name
  118. folderItems:items];
  119. } else {
  120. mediaList = [self createMediaListWithODItem:selectedItem positionIndex:&positionIndex];
  121. }
  122. [self streamMediaList:mediaList startingAtIndex:positionIndex subtitlesFilePath:subtitlePath];
  123. } else {
  124. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"ERROR", nil)
  125. message:NSLocalizedString(@"ONEDRIVE_MEDIA_WITHOUT_URL", nil)
  126. preferredStyle:UIAlertControllerStyleAlert];
  127. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_OK", nil)
  128. style:UIAlertActionStyleCancel
  129. handler:nil];
  130. [alertController addAction:okAction];
  131. [self presentViewController:alertController animated:YES completion:nil];
  132. }
  133. }
  134. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  135. }
  136. - (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex subtitlesFilePath:(NSString *)subtitlesFilePath
  137. {
  138. if (mediaList.count <= 0) {
  139. NSLog(@"VLCOneDriveTableViewController: Empty or wrong mediaList");
  140. return;
  141. }
  142. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  143. vpc.fullscreenSessionRequested = NO;
  144. [vpc playMediaList:mediaList firstIndex:startIndex subtitlesFilePath:subtitlesFilePath];
  145. }
  146. - (VLCMediaList *)createMediaListWithODItem:(ODItem *)item positionIndex:(NSInteger *)index
  147. {
  148. NSUInteger counter = 0;
  149. NSArray *folderItems = _oneDriveController.currentListFiles;
  150. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  151. for (ODItem *tmpItem in folderItems) {
  152. if (tmpItem.folder || [tmpItem.name isSupportedSubtitleFormat]) {
  153. continue;
  154. }
  155. NSURL *url = [NSURL URLWithString:tmpItem.dictionaryFromItem[@"@content.downloadUrl"]];
  156. if (url) {
  157. [mediaList addMedia:[VLCMedia mediaWithURL:url]];
  158. NSString *subtitlePath = [_oneDriveController configureSubtitleWithFileName:tmpItem.name
  159. folderItems:folderItems];
  160. if (subtitlePath) {
  161. [[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : subtitlePath }];
  162. }
  163. // Index needed to know where to begin in the medialist
  164. if (item == tmpItem) {
  165. *index = mediaList.count - 1;
  166. }
  167. counter++;
  168. }
  169. }
  170. return mediaList;
  171. }
  172. - (VLCMediaList *)createMediaList
  173. {
  174. return [self createMediaListWithODItem:nil positionIndex:0];
  175. }
  176. - (void)playAllAction:(id)sender
  177. {
  178. [self streamMediaList:[self createMediaList] startingAtIndex:0 subtitlesFilePath:nil];
  179. }
  180. #pragma mark - login dialog
  181. - (void)loginAction:(id)sender
  182. {
  183. if (![_oneDriveController isAuthorized]) {
  184. self.authorizationInProgress = YES;
  185. [_oneDriveController loginWithViewController:self];
  186. } else
  187. [_oneDriveController logout];
  188. }
  189. #pragma mark - onedrive controller delegation
  190. - (void)sessionWasUpdated
  191. {
  192. [self updateViewAfterSessionChange];
  193. }
  194. #pragma mark - cell delegation
  195. #if TARGET_OS_IOS
  196. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  197. {
  198. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  199. ODItem *selectedItem = _oneDriveController.currentListFiles[indexPath.row];
  200. if (selectedItem.size < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  201. /* selected item is a proper file, ask the user if s/he wants to download it */
  202. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  203. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil),
  204. selectedItem.name,
  205. [[UIDevice currentDevice] model]]
  206. preferredStyle:UIAlertControllerStyleAlert];
  207. UIAlertAction *downloadAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_DOWNLOAD", nil)
  208. style:UIAlertActionStyleDefault
  209. handler:^(UIAlertAction *alertAction){
  210. [self->_oneDriveController startDownloadingODItem:selectedItem];
  211. }];
  212. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  213. style:UIAlertActionStyleCancel
  214. handler:nil];
  215. [alertController addAction:downloadAction];
  216. [alertController addAction:cancelAction];
  217. [self presentViewController:alertController animated:YES completion:nil];
  218. } else {
  219. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  220. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil),
  221. selectedItem.name,
  222. [[UIDevice currentDevice] model]]
  223. preferredStyle:UIAlertControllerStyleAlert];
  224. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"BUTTON_OK", nil)
  225. style:UIAlertActionStyleCancel
  226. handler:nil];
  227. [alertController addAction:okAction];
  228. [self presentViewController:alertController animated:YES completion:nil];
  229. }
  230. }
  231. #endif
  232. @end