VLCOneDriveTableViewController.m 11 KB

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