VLCFTPServerListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*****************************************************************************
  2. * VLCFTPServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCFTPServerListViewController.h"
  14. #import "VLCNetworkListCell.h"
  15. #import "VLCAppDelegate.h"
  16. #import "NSString+SupportedMedia.h"
  17. #import "UIDevice+VLC.h"
  18. #import "VLCStatusLabel.h"
  19. #import "WhiteRaccoon.h"
  20. @interface VLCFTPServerListViewController () <WRRequestDelegate, VLCNetworkListCellDelegate, UITableViewDataSource, UITableViewDelegate, UIActionSheetDelegate>
  21. {
  22. NSString *_ftpServerAddress;
  23. NSString *_ftpServerUserName;
  24. NSString *_ftpServerPassword;
  25. NSString *_ftpServerPath;
  26. WRRequestListDirectory *_FTPListDirRequest;
  27. NSArray *_objectList;
  28. NSMutableArray *_searchData;
  29. }
  30. @end
  31. @implementation VLCFTPServerListViewController
  32. - (id)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  33. {
  34. self = [super init];
  35. if (self) {
  36. _ftpServerAddress = serverAddress;
  37. _ftpServerUserName = username;
  38. _ftpServerPassword = password;
  39. _ftpServerPath = path;
  40. }
  41. return self;
  42. }
  43. - (void)viewDidLoad
  44. {
  45. [super viewDidLoad];
  46. if ([_ftpServerPath isEqualToString:@"/"])
  47. self.title = _ftpServerAddress;
  48. else
  49. self.title = [_ftpServerPath lastPathComponent];
  50. [self _listFTPDirectory];
  51. }
  52. #pragma mark - ftp specifics
  53. - (void)_listFTPDirectory
  54. {
  55. if (_FTPListDirRequest)
  56. return;
  57. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  58. _FTPListDirRequest.delegate = self;
  59. _FTPListDirRequest.hostname = _ftpServerAddress;
  60. _FTPListDirRequest.username = _ftpServerUserName;
  61. _FTPListDirRequest.password = _ftpServerPassword;
  62. _FTPListDirRequest.path = _ftpServerPath;
  63. _FTPListDirRequest.passive = YES;
  64. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStarted];
  65. [_FTPListDirRequest start];
  66. }
  67. - (NSString *)_credentials
  68. {
  69. NSString *cred;
  70. if (_ftpServerUserName.length > 0) {
  71. if (_ftpServerPassword.length > 0)
  72. cred = [NSString stringWithFormat:@"%@:%@@", _ftpServerUserName, _ftpServerPassword];
  73. else
  74. cred = [NSString stringWithFormat:@"%@@", _ftpServerPassword];
  75. } else
  76. cred = @"";
  77. return [cred stringByStandardizingPath];
  78. }
  79. - (void)_downloadFTPFile:(NSString *)fileName
  80. {
  81. NSURL *URLToQueue = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  82. [[VLCDownloadViewController sharedInstance] addURLToDownloadList:URLToQueue fileNameOfMedia:nil];
  83. }
  84. - (void)_streamFTPFile:(NSString *)fileName
  85. {
  86. NSString *URLofSubtitle = nil;
  87. NSArray *SubtitlesList = [self _searchSubtitle:fileName];
  88. if(SubtitlesList.count > 0)
  89. URLofSubtitle = [self _getFileSubtitleFromFtpServer:SubtitlesList[0]];
  90. NSURL *URLToPlay = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  91. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  92. [appDelegate openMovieWithExternalSubtitleFromURL:URLToPlay externalSubURL:URLofSubtitle];
  93. }
  94. - (NSArray *)_searchSubtitle:(NSString *)url
  95. {
  96. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  97. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  98. NSUInteger count = _objectList.count;
  99. for (NSUInteger i = 0; i < count; i++)
  100. [ObjList addObject:[_objectList[i] objectForKey:(id)kCFFTPResourceName]];
  101. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", urlTemp];
  102. NSArray *results = [ObjList filteredArrayUsingPredicate:predicate];
  103. [ObjList removeAllObjects];
  104. count = results.count;
  105. for (NSUInteger i = 0; i < count; i++) {
  106. if ([results[i] isSupportedSubtitleFormat])
  107. [ObjList addObject:results[i]];
  108. }
  109. return [NSArray arrayWithArray:ObjList];
  110. }
  111. - (NSString *)_getFileSubtitleFromFtpServer:(NSString *)fileName
  112. {
  113. NSURL *url = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  114. NSString *FileSubtitlePath = nil;
  115. NSData *receivedSub = [NSData dataWithContentsOfURL:url];
  116. if (receivedSub.length < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  117. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  118. NSString *directoryPath = searchPaths[0];
  119. FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[fileName lastPathComponent]];
  120. NSFileManager *fileManager = [NSFileManager defaultManager];
  121. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  122. //create local subtitle file
  123. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  124. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  125. APLog(@"file creation failed, no data was saved");
  126. }
  127. [receivedSub writeToFile:FileSubtitlePath atomically:YES];
  128. } else {
  129. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  130. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [fileName lastPathComponent], [[UIDevice currentDevice] model]]
  131. delegate:self
  132. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  133. otherButtonTitles:nil];
  134. [alert show];
  135. }
  136. return FileSubtitlePath;
  137. }
  138. #pragma mark - table view data source, for more see super
  139. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  140. {
  141. if (tableView == self.searchDisplayController.searchResultsTableView)
  142. return _searchData.count;
  143. return _objectList.count;
  144. }
  145. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  146. {
  147. static NSString *CellIdentifier = @"LocalNetworkCellDetail";
  148. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  149. if (cell == nil)
  150. cell = [VLCNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  151. NSDictionary *cellObject;
  152. if (tableView == self.searchDisplayController.searchResultsTableView)
  153. cellObject = _searchData[indexPath.row];
  154. else
  155. cellObject = _objectList[indexPath.row];
  156. NSString *rawFileName = [cellObject objectForKey:(id)kCFFTPResourceName];
  157. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  158. cell.title = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  159. if ([cellObject[(id)kCFFTPResourceType] intValue] == 4) {
  160. cell.isDirectory = YES;
  161. cell.icon = [UIImage imageNamed:@"folder"];
  162. } else {
  163. cell.isDirectory = NO;
  164. cell.icon = [UIImage imageNamed:@"blank"];
  165. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([cellObject[(id)kCFFTPResourceSize] intValue] / 1e6)];
  166. cell.isDownloadable = YES;
  167. cell.delegate = self;
  168. }
  169. return cell;
  170. }
  171. #pragma mark - table view delegate, for more see super
  172. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  173. {
  174. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  175. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  176. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
  177. }
  178. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  179. {
  180. NSDictionary *cellObject;
  181. if (tableView == self.searchDisplayController.searchResultsTableView)
  182. cellObject = _searchData[indexPath.row];
  183. else
  184. cellObject = _objectList[indexPath.row];
  185. if ([cellObject[(id)kCFFTPResourceType] intValue] == 4) {
  186. NSString *newPath = [NSString stringWithFormat:@"%@/%@", _ftpServerPath, cellObject[(id)kCFFTPResourceName]];
  187. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc] initWithFTPServer:_ftpServerAddress userName:_ftpServerUserName andPassword:_ftpServerPassword atPath:newPath];
  188. [self.navigationController pushViewController:targetViewController animated:YES];
  189. } else {
  190. NSString *rawObjectName = cellObject[(id)kCFFTPResourceName];
  191. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  192. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  193. if (![properObjectName isSupportedFormat]) {
  194. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  195. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName]
  196. delegate:self
  197. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  198. otherButtonTitles:nil];
  199. [alert show];
  200. } else
  201. [self _streamFTPFile:properObjectName];
  202. }
  203. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  204. }
  205. #pragma mark - VLCNetworkListCell delegation
  206. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  207. {
  208. NSString *rawObjectName;
  209. NSInteger size;
  210. NSDictionary *cellObject;
  211. if ([self.searchDisplayController isActive])
  212. cellObject = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  213. else
  214. cellObject = _objectList[[self.tableView indexPathForCell:cell].row];
  215. rawObjectName = cellObject[(id)kCFFTPResourceName];
  216. size = [cellObject[(id)kCFFTPResourceSize] intValue];
  217. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  218. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  219. if (![properObjectName isSupportedFormat]) {
  220. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  221. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName]
  222. delegate:self
  223. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  224. otherButtonTitles:nil];
  225. [alert show];
  226. } else {
  227. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  228. [self _downloadFTPFile:properObjectName];
  229. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  230. } else {
  231. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  232. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), properObjectName, [[UIDevice currentDevice] model]]
  233. delegate:self
  234. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  235. otherButtonTitles:nil];
  236. [alert show];
  237. }
  238. }
  239. }
  240. #pragma mark - white raccoon delegation
  241. - (void)requestCompleted:(WRRequest *)request
  242. {
  243. if (request == _FTPListDirRequest) {
  244. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  245. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  246. NSUInteger count = rawList.count;
  247. for (NSUInteger x = 0; x < count; x++) {
  248. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  249. [filteredList addObject:rawList[x]];
  250. }
  251. _objectList = [NSArray arrayWithArray:filteredList];
  252. [self.tableView reloadData];
  253. } else
  254. APLog(@"unknown request %@ completed", request);
  255. }
  256. - (void)requestFailed:(WRRequest *)request
  257. {
  258. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  259. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  260. delegate:self
  261. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  262. otherButtonTitles:nil];
  263. [alert show];
  264. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  265. }
  266. #pragma mark - search
  267. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  268. {
  269. NSInteger listCount = 0;
  270. [_searchData removeAllObjects];
  271. listCount = _objectList.count;
  272. for (int i = 0; i < listCount; i++) {
  273. NSRange nameRange;
  274. NSString *rawObjectName = [_objectList[i] objectForKey:(id)kCFFTPResourceName];
  275. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  276. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  277. nameRange = [properObjectName rangeOfString:searchString options:NSCaseInsensitiveSearch];
  278. if (nameRange.location != NSNotFound)
  279. [_searchData addObject:_objectList[i]];
  280. }
  281. return YES;
  282. }
  283. @end