VLCOneDriveTableViewController.m 11 KB

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