VLCOneDriveTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #import "NSString+SupportedMedia.h"
  19. @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
  20. {
  21. VLCOneDriveController *_oneDriveController;
  22. VLCOneDriveObject *_selectedFile;
  23. }
  24. @end
  25. @implementation VLCOneDriveTableViewController
  26. - (void)viewDidLoad {
  27. [super viewDidLoad];
  28. _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
  29. self.controller = _oneDriveController;
  30. self.controller.delegate = self;
  31. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
  32. #if TARGET_OS_IOS
  33. [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
  34. [self.cloudStorageLogo sizeToFit];
  35. self.cloudStorageLogo.center = self.view.center;
  36. #endif
  37. }
  38. - (void)viewWillAppear:(BOOL)animated
  39. {
  40. [super viewWillAppear:animated];
  41. [self updateViewAfterSessionChange];
  42. self.authorizationInProgress = NO;
  43. }
  44. #pragma mark - generic interface interaction
  45. - (void)goBack
  46. {
  47. if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
  48. if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
  49. _oneDriveController.currentFolder = nil;
  50. self.title = _oneDriveController.rootFolder.name;
  51. } else {
  52. _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
  53. self.title = _oneDriveController.currentFolder.name;
  54. }
  55. [self.activityIndicator startAnimating];
  56. [_oneDriveController loadCurrentFolder];
  57. } else
  58. [self.navigationController popViewControllerAnimated:YES];
  59. }
  60. #pragma mark - table view data source
  61. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  62. {
  63. static NSString *CellIdentifier = @"OneDriveCell";
  64. VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  65. if (cell == nil)
  66. cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
  67. NSArray *items = _oneDriveController.currentFolder.items;
  68. if (indexPath.row < items.count) {
  69. cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
  70. cell.delegate = self;
  71. }
  72. return cell;
  73. }
  74. #pragma mark - table view delegate
  75. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  76. {
  77. NSArray *folderItems = _oneDriveController.currentFolder.items;
  78. NSInteger row = indexPath.row;
  79. if (row >= folderItems.count)
  80. return;
  81. VLCOneDriveObject *selectedObject = folderItems[row];
  82. if (selectedObject.isFolder) {
  83. /* dive into sub folder */
  84. [self.activityIndicator startAnimating];
  85. _oneDriveController.currentFolder = selectedObject;
  86. [_oneDriveController loadCurrentFolder];
  87. self.title = selectedObject.name;
  88. } else {
  89. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  90. if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
  91. /* stream file */
  92. NSString *subtitlePath = [self _searchSubtitle:selectedObject.name];
  93. subtitlePath = [self _getFileSubtitleFromServer:[NSURL URLWithString:subtitlePath]];
  94. NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
  95. [vpc playURL:url subtitlesFilePath:subtitlePath];
  96. } else {
  97. NSUInteger count = folderItems.count;
  98. NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
  99. NSInteger firstIndex = 0;
  100. NSInteger posIndex = 0;
  101. for (NSInteger x = count - 1; x > -1; x--) {
  102. VLCOneDriveObject *iter = folderItems[x];
  103. if ((iter.isFolder) || [iter.name isSupportedSubtitleFormat])
  104. continue;
  105. NSURL *url = [NSURL URLWithString:iter.downloadPath];
  106. if (url) {
  107. [mediaItems addObject:[VLCMedia mediaWithURL:url]];
  108. if (iter == selectedObject) {
  109. posIndex = mediaItems.count;
  110. }
  111. }
  112. }
  113. if (mediaItems.count > 0) {
  114. firstIndex = mediaItems.count - posIndex;
  115. [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:firstIndex];
  116. }
  117. }
  118. }
  119. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  120. }
  121. - (NSString *)_searchSubtitle:(NSString *)url
  122. {
  123. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  124. NSArray *folderItems = _oneDriveController.currentFolder.items;
  125. NSString *itemPath = nil;
  126. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
  127. NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
  128. for (int cnt = 0; cnt < results.count; cnt++) {
  129. VLCOneDriveObject *iter = results[cnt];
  130. NSString *itemName = iter.name;
  131. if ([itemName isSupportedSubtitleFormat])
  132. itemPath = iter.downloadPath;
  133. }
  134. return itemPath;
  135. }
  136. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  137. {
  138. NSString *FileSubtitlePath = nil;
  139. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  140. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  141. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  142. NSString *directoryPath = searchPaths[0];
  143. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  144. NSFileManager *fileManager = [NSFileManager defaultManager];
  145. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  146. //create local subtitle file
  147. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  148. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  149. APLog(@"file creation failed, no data was saved");
  150. return nil;
  151. }
  152. }
  153. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  154. } else {
  155. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  156. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  157. delegate:self
  158. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  159. otherButtonTitles:nil];
  160. [alert show];
  161. }
  162. return FileSubtitlePath;
  163. }
  164. - (void)playAllAction:(id)sender
  165. {
  166. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  167. NSArray *folderItems = _oneDriveController.currentFolder.items;
  168. NSUInteger count = folderItems.count;
  169. NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
  170. for (NSInteger x = count - 1; x > -1; x--) {
  171. VLCOneDriveObject *iter = folderItems[x];
  172. if ((iter.isFolder) || [iter.name isSupportedSubtitleFormat])
  173. continue;
  174. NSURL *url = [NSURL URLWithString:iter.downloadPath];
  175. if (url) {
  176. [mediaItems addObject:[VLCMedia mediaWithURL:url]];
  177. }
  178. }
  179. if (mediaItems.count > 0) {
  180. [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:0];
  181. }
  182. }
  183. #pragma mark - login dialog
  184. - (void)loginAction:(id)sender
  185. {
  186. if (![_oneDriveController isAuthorized]) {
  187. self.authorizationInProgress = YES;
  188. [_oneDriveController loginWithViewController:self];
  189. } else
  190. [_oneDriveController logout];
  191. }
  192. #pragma mark - onedrive controller delegation
  193. - (void)sessionWasUpdated
  194. {
  195. [self updateViewAfterSessionChange];
  196. }
  197. #pragma mark - cell delegation
  198. #if TARGET_OS_IOS
  199. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  200. {
  201. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  202. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  203. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  204. /* selected item is a proper file, ask the user if s/he wants to download it */
  205. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  206. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  207. delegate:self
  208. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  209. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  210. [alert show];
  211. } else {
  212. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  213. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  214. delegate:self
  215. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  216. otherButtonTitles:nil];
  217. [alert show];
  218. }
  219. }
  220. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  221. {
  222. if (buttonIndex == 1)
  223. [_oneDriveController downloadObject:_selectedFile];
  224. _selectedFile = nil;
  225. }
  226. #endif
  227. @end