123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /*****************************************************************************
- * VLCOneDriveTableViewController.m
- * VLC for iOS
- *****************************************************************************
- * Copyright (c) 2014-2015 VideoLAN. All rights reserved.
- * $Id$
- *
- * Authors: Felix Paul Kühne <fkuehne # videolan.org>
- *
- * Refer to the COPYING file of the official project for license.
- *****************************************************************************/
- #import "VLCOneDriveTableViewController.h"
- #import "VLCOneDriveController.h"
- #import "VLCCloudStorageTableViewCell.h"
- #import "VLCPlaybackController.h"
- #import "VLCProgressView.h"
- #import "UIDevice+VLC.h"
- #import "NSString+SupportedMedia.h"
- @interface VLCOneDriveTableViewController () <VLCCloudStorageDelegate>
- {
- VLCOneDriveController *_oneDriveController;
- VLCOneDriveObject *_selectedFile;
- }
- @end
- @implementation VLCOneDriveTableViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- _oneDriveController = (VLCOneDriveController *)[VLCOneDriveController sharedInstance];
- self.controller = _oneDriveController;
- self.controller.delegate = self;
- self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"OneDriveWhite"]];
- #if TARGET_OS_IOS
- [self.cloudStorageLogo setImage:[UIImage imageNamed:@"OneDriveWhite"]];
- [self.cloudStorageLogo sizeToFit];
- self.cloudStorageLogo.center = self.view.center;
- #endif
- }
- - (void)viewWillAppear:(BOOL)animated
- {
- [super viewWillAppear:animated];
- [self updateViewAfterSessionChange];
- self.authorizationInProgress = NO;
- }
- #pragma mark - generic interface interaction
- - (void)goBack
- {
- if ((_oneDriveController.rootFolder != _oneDriveController.currentFolder) && [_oneDriveController isAuthorized]) {
- if ([_oneDriveController.rootFolder.name isEqualToString:_oneDriveController.currentFolder.parent.name]) {
- _oneDriveController.currentFolder = nil;
- self.title = _oneDriveController.rootFolder.name;
- } else {
- _oneDriveController.currentFolder = _oneDriveController.currentFolder.parent;
- self.title = _oneDriveController.currentFolder.name;
- }
- [self.activityIndicator startAnimating];
- [_oneDriveController loadCurrentFolder];
- } else
- [self.navigationController popViewControllerAnimated:YES];
- }
- #pragma mark - table view data source
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
- {
- static NSString *CellIdentifier = @"OneDriveCell";
- VLCCloudStorageTableViewCell *cell = (VLCCloudStorageTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
- if (cell == nil)
- cell = [VLCCloudStorageTableViewCell cellWithReuseIdentifier:CellIdentifier];
- NSArray *items = _oneDriveController.currentFolder.items;
- if (indexPath.row < items.count) {
- cell.oneDriveFile = _oneDriveController.currentFolder.items[indexPath.row];
- cell.delegate = self;
- }
- return cell;
- }
- #pragma mark - table view delegate
- - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
- {
- NSArray *folderItems = _oneDriveController.currentFolder.items;
- NSInteger row = indexPath.row;
- if (row >= folderItems.count)
- return;
- VLCOneDriveObject *selectedObject = folderItems[row];
- if (selectedObject.isFolder) {
- /* dive into sub folder */
- [self.activityIndicator startAnimating];
- _oneDriveController.currentFolder = selectedObject;
- [_oneDriveController loadCurrentFolder];
- self.title = selectedObject.name;
- } else {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- if (![[NSUserDefaults standardUserDefaults] boolForKey:kVLCAutomaticallyPlayNextItem]) {
- /* stream file */
- NSString *subtitlePath = [self _searchSubtitle:selectedObject.name];
- subtitlePath = [self _getFileSubtitleFromServer:[NSURL URLWithString:subtitlePath]];
- NSURL *url = [NSURL URLWithString:selectedObject.downloadPath];
- [vpc playURL:url subtitlesFilePath:subtitlePath];
- } else {
- NSUInteger count = folderItems.count;
- NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
- NSInteger firstIndex = 0;
- NSInteger posIndex = 0;
- for (NSInteger x = count - 1; x > -1; x--) {
- VLCOneDriveObject *iter = folderItems[x];
- if ((iter.isFolder) || [iter.name isSupportedSubtitleFormat])
- continue;
- NSURL *url = [NSURL URLWithString:iter.downloadPath];
- if (url) {
- [mediaItems addObject:[VLCMedia mediaWithURL:url]];
- if (iter == selectedObject) {
- posIndex = mediaItems.count;
- }
- }
- }
- if (mediaItems.count > 0) {
- firstIndex = mediaItems.count - posIndex;
- [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:firstIndex];
- }
- }
- }
- [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
- }
- - (NSString *)_searchSubtitle:(NSString *)url
- {
- NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
- NSArray *folderItems = _oneDriveController.currentFolder.items;
- NSString *itemPath = nil;
- NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name contains[c] %@", urlTemp];
- NSArray *results = [folderItems filteredArrayUsingPredicate:predicate];
- for (int cnt = 0; cnt < results.count; cnt++) {
- VLCOneDriveObject *iter = results[cnt];
- NSString *itemName = iter.name;
- if ([itemName isSupportedSubtitleFormat])
- itemPath = iter.downloadPath;
- }
- return itemPath;
- }
- - (NSString *)_getFileSubtitleFromServer:(NSURL *)subtitleURL
- {
- NSString *FileSubtitlePath = nil;
- NSData *receivedSub = [NSData dataWithContentsOfURL:subtitleURL]; // TODO: fix synchronous load
- if (receivedSub.length < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
- NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *directoryPath = searchPaths[0];
- FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[subtitleURL lastPathComponent]];
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
- //create local subtitle file
- [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
- if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
- APLog(@"file creation failed, no data was saved");
- return nil;
- }
- }
- [receivedSub writeToFile:FileSubtitlePath atomically:YES];
- } else {
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
- message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [subtitleURL lastPathComponent], [[UIDevice currentDevice] model]]
- delegate:self
- cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
- otherButtonTitles:nil];
- [alert show];
- }
- return FileSubtitlePath;
- }
- - (void)playAllAction:(id)sender
- {
- VLCPlaybackController *vpc = [VLCPlaybackController sharedInstance];
- NSArray *folderItems = _oneDriveController.currentFolder.items;
- NSUInteger count = folderItems.count;
- NSMutableArray *mediaItems = [[NSMutableArray alloc] init];
- for (NSInteger x = count - 1; x > -1; x--) {
- VLCOneDriveObject *iter = folderItems[x];
- if ((iter.isFolder) || [iter.name isSupportedSubtitleFormat])
- continue;
- NSURL *url = [NSURL URLWithString:iter.downloadPath];
- if (url) {
- [mediaItems addObject:[VLCMedia mediaWithURL:url]];
- }
- }
- if (mediaItems.count > 0) {
- [vpc playMediaList:[[VLCMediaList alloc] initWithArray:mediaItems] firstIndex:0];
- }
- }
- #pragma mark - login dialog
- - (void)loginAction:(id)sender
- {
- if (![_oneDriveController isAuthorized]) {
- self.authorizationInProgress = YES;
- [_oneDriveController loginWithViewController:self];
- } else
- [_oneDriveController logout];
- }
- #pragma mark - onedrive controller delegation
- - (void)sessionWasUpdated
- {
- [self updateViewAfterSessionChange];
- }
- #pragma mark - cell delegation
- #if TARGET_OS_IOS
- - (void)triggerDownloadForCell:(VLCCloudStorageTableViewCell *)cell
- {
- NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
- _selectedFile = _oneDriveController.currentFolder.items[indexPath.row];
- if (_selectedFile.size.longLongValue < [[UIDevice currentDevice] VLCFreeDiskSpace].longLongValue) {
- /* selected item is a proper file, ask the user if s/he wants to download it */
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DROPBOX_DOWNLOAD", nil)
- message:[NSString stringWithFormat:NSLocalizedString(@"DROPBOX_DL_LONG", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
- delegate:self
- cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
- otherButtonTitles:NSLocalizedString(@"BUTTON_DOWNLOAD", nil), nil];
- [alert show];
- } else {
- VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
- message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), _selectedFile.name, [[UIDevice currentDevice] model]]
- delegate:self
- cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
- otherButtonTitles:nil];
- [alert show];
- }
- }
- - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
- {
- if (buttonIndex == 1)
- [_oneDriveController downloadObject:_selectedFile];
- _selectedFile = nil;
- }
- #endif
- @end
|