VLCOneDriveTableViewController.m 8.3 KB

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