VLCOneDriveTableViewController.m 11 KB

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