VLCOneDriveTableViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. VLCMediaList *medialist = [[VLCMediaList alloc] init];
  96. [medialist addMedia: [VLCMedia mediaWithURL:url]];
  97. [[VLCPlaybackController sharedInstance] playMediaList:medialist firstIndex:0 subtitlesFilePath:subtitlePath];
  98. } else {
  99. NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
  100. NSInteger posIndex = 0;
  101. for (VLCOneDriveObject *item in folderItems) {
  102. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  103. continue;
  104. NSURL *url = [NSURL URLWithString:item.downloadPath];
  105. if (url) {
  106. [mediaItems addObject:[VLCMedia mediaWithURL:url]];
  107. if (item == selectedObject) {
  108. posIndex = mediaItems.count -1;
  109. }
  110. }
  111. }
  112. if (mediaItems.count > 0) {
  113. [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:posIndex subtitlesFilePath:nil];
  114. }
  115. }
  116. }
  117. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  118. }
  119. - (NSString *)_searchSubtitle:(NSString *)url
  120. {
  121. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  122. NSArray *folderItems = _oneDriveController.currentFolder.items;
  123. NSString *itemPath = nil;
  124. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
  125. NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
  126. for (int cnt = 0; cnt < results.count; cnt++) {
  127. VLCOneDriveObject *iter = results[cnt];
  128. NSString *itemName = iter.name;
  129. if ([itemName isSupportedSubtitleFormat])
  130. itemPath = iter.downloadPath;
  131. }
  132. return itemPath;
  133. }
  134. - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
  135. {
  136. NSString *FileSubtitlePath = nil;
  137. NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
  138. if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  139. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  140. NSString *directoryPath = searchPaths[0];
  141. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
  142. NSFileManager *fileManager = [NSFileManager defaultManager];
  143. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  144. //create local subtitle file
  145. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  146. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  147. APLog(@"file creation failed, no data was saved");
  148. return nil;
  149. }
  150. }
  151. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  152. } else {
  153. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  154. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
  155. delegate:self
  156. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  157. otherButtonTitles:nil];
  158. [alert show];
  159. }
  160. return FileSubtitlePath;
  161. }
  162. - (void)playAllAction:(id)sender
  163. {
  164. VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
  165. NSArray *folderItems = _oneDriveController.currentFolder.items;
  166. NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
  167. for (VLCOneDriveObject *item in folderItems) {
  168. if ((item.isFolder) || [item.name isSupportedSubtitleFormat])
  169. continue;
  170. NSURL *url = [NSURL URLWithString:item.downloadPath];
  171. if (url) {
  172. [mediaItems addObject:[VLCMedia mediaWithURL:url]];
  173. }
  174. }
  175. if (mediaItems.count > 0) {
  176. [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:0 subtitlesFilePath:nil];
  177. }
  178. }
  179. #pragma mark - login dialog
  180. - (void)loginAction:(id)sender
  181. {
  182. if (![_oneDriveController isAuthorized]) {
  183. self.authorizationInProgress = YES;
  184. [_oneDriveController loginWithViewController:self];
  185. } else
  186. [_oneDriveController logout];
  187. }
  188. #pragma mark - onedrive controller delegation
  189. - (void)sessionWasUpdated
  190. {
  191. [self updateViewAfterSessionChange];
  192. }
  193. #pragma mark - cell delegation
  194. #if TARGET_OS_IOS
  195. - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
  196. {
  197. NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
  198. _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
  199. if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
  200. /* selected item is a proper file, ask the user if s/he wants to download it */
  201. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
  202. message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  203. delegate:self
  204. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  205. otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
  206. [alert show];
  207. } else {
  208. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  209. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
  210. delegate:self
  211. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  212. otherButtonTitles:nil];
  213. [alert show];
  214. }
  215. }
  216. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  217. {
  218. if (buttonIndex == 1)
  219. [_oneDriveController downloadObject:_selectedFile];
  220. _selectedFile = nil;
  221. }
  222. #endif
  223. @end