VLCOneDriveTableViewController.m 11 KB

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