VLCOneDriveTableViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*****************************************************************************
  2. * VLCOneDriveTableViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2014-2018 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. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  22. {
  23. VLCOneDriveController *_oneDriveController;
  24. VLCOneDriveObject *_selectedFile;
  25. }
  26. @end
  27. @implementation VLCOneDriveTableViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
  31. self.controller = _oneDriveController;
  32. self.controller.delegate = self;
  33. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  34. #if TARGET_OS_IOS
  35. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  36. [self.cloudStorageLogo sizeToFit];
  37. self.cloudStorageLogo.center = self.view.center;
  38. #endif
  39. }
  40. - (void)viewWillAppear:(BOOL)animated
  41. {
  42. [super viewWillAppear:animated];
  43. [self updateViewAfterSessionChange];
  44. self.authorizationInProgress = NO;
  45. }
  46. #pragma mark - generic interface interaction
  47. - (void)goBack
  48. {
  49. if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
  50. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  51. _oneDriveController.currentFolder = nil;
  52. self.title = _oneDriveController.rootFolder.name;
  53. } else {
  54. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  55. self.title = _oneDriveController.currentFolder.name;
  56. }
  57. [self.activityIndicator startAnimating];
  58. [_oneDriveController loadCurrentFolder];
  59. } else
  60. [self.navigationController popViewControllerAnimated:YES];
  61. }
  62. #pragma mark - table view data source
  63. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  64. {
  65. static NSString *CellIdentifier = @"OneDriveCell";
  66. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  67. if (cell == nil)
  68. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  69. NSArray *items = _oneDriveController.currentFolder.items;
  70. if (indexPath.row < items.count) {
  71. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  72. cell.delegate = self;
  73. }
  74. return cell;
  75. }
  76. #pragma mark - table view delegate
  77. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  78. {
  79. NSArray *folderItems = _oneDriveController.currentFolder.items;
  80. NSInteger row = indexPath.row;
  81. if (row >= folderItems.count)
  82. return;
  83. VLCOneDriveObject *selectedObject = folderItems[row];
  84. if (selectedObject.isFolder) {
  85. /* dive into sub folder */
  86. [self.activityIndicator startAnimating];
  87. _oneDriveController.currentFolder = selectedObject;
  88. [_oneDriveController loadCurrentFolder];
  89. self.title = selectedObject.name;
  90. } else {
  91. if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
  92. /* stream file */
  93. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  94. VLCMediaList *mediaList = [[VLCMediaList alloc] initWithArray:@[[VLCMedia mediaWithURL:url]]];
  95. [self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:selectedObject.subtitleURL];
  96. } else {
  97. NSInteger posIndex = 0;
  98. NSUInteger counter = 0;
  99. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  100. for (VLCOneDriveObject *item in folderItems) {
  101. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  102. continue;
  103. NSURL *url = [NSURL URLWithString:item.downloadPath];
  104. if (url) {
  105. [mediaList addMedia:[VLCMedia mediaWithURL:url]];
  106. if (item.subtitleURL)
  107. [[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
  108. counter ++;
  109. if (item == selectedObject)
  110. posIndex = mediaList.count - 1;
  111. }
  112. }
  113. if (mediaList.count > 0)
  114. [self streamMediaList:mediaList startingAtIndex:posIndex subtitlesFilePath:nil];
  115. }
  116. }
  117. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  118. }
  119. - (void)streamMediaList:(VLCMediaList *)mediaList startingAtIndex:(NSInteger)startIndex subtitlesFilePath:(NSString *)subtitlesFilePath
  120. {
  121. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  122. vpc.fullscreenSessionRequested = NO;
  123. [vpc playMediaList:mediaList firstIndex:startIndex subtitlesFilePath:subtitlesFilePath];
  124. }
  125. - (void)playAllAction:(id)sender
  126. {
  127. NSUInteger counter = 0;
  128. NSArray *folderItems = _oneDriveController.currentFolder.items;
  129. VLCMediaList *mediaList = [[VLCMediaList alloc] init];
  130. for (VLCOneDriveObject *item in folderItems) {
  131. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  132. continue;
  133. NSURL *url = [NSURL URLWithString:item.downloadPath];
  134. if (url) {
  135. [mediaList addMedia:[VLCMedia mediaWithURL:url]];
  136. if (item.subtitleURL)
  137. [[mediaList mediaAtIndex:counter] addOptions:@{ kVLCSettingSubtitlesFilePath : item.subtitleURL }];
  138. counter ++;
  139. }
  140. }
  141. if (mediaList.count > 0)
  142. [self streamMediaList:mediaList startingAtIndex:0 subtitlesFilePath:nil];
  143. }
  144. #pragma mark - login dialog
  145. - (void)loginAction:(id)sender
  146. {
  147. if (![_oneDriveController isAuthorized]) {
  148. self.authorizationInProgress = YES;
  149. [_oneDriveController loginWithViewController:self];
  150. } else
  151. [_oneDriveController logout];
  152. }
  153. #pragma mark - onedrive controller delegation
  154. - (void)sessionWasUpdated
  155. {
  156. [self updateViewAfterSessionChange];
  157. }
  158. #pragma mark - cell delegation
  159. #if TARGET_OS_IOS
  160. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  161. {
  162. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  163. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  164. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  165. /* selected item is a proper file, ask the user if s/he wants to download it */
  166. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  167. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  168. delegate:self
  169. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  170. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  171. [alert show];
  172. } else {
  173. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  174. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  175. delegate:self
  176. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  177. otherButtonTitles:nil];
  178. [alert show];
  179. }
  180. }
  181. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  182. {
  183. if (buttonIndex == 1)
  184. [_oneDriveController downloadObject:_selectedFile];
  185. _selectedFile = nil;
  186. }
  187. #endif
  188. @end