VLCFTPServerListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. VLCNetworkListCell *cell = (VLCNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:VLCNetworkListCellIdentifier];
  148. if (cell == nil)
  149. cell = [VLCNetworkListCell cellWithReuseIdentifier:VLCNetworkListCellIdentifier];
  150. NSDictionary *cellObject;
  151. if (tableView == self.searchDisplayController.searchResultsTableView)
  152. cellObject = _searchData[indexPath.row];
  153. else
  154. cellObject = _objectList[indexPath.row];
  155. NSString *rawFileName = [cellObject objectForKey:(id)kCFFTPResourceName];
  156. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  157. cell.title = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  158. if ([cellObject[(id)kCFFTPResourceType] intValue] == 4) {
  159. cell.isDirectory = YES;
  160. cell.icon = [UIImage imageNamed:@"folder"];
  161. } else {
  162. cell.isDirectory = NO;
  163. cell.icon = [UIImage imageNamed:@"blank"];
  164. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([cellObject[(id)kCFFTPResourceSize] intValue] / 1e6)];
  165. cell.isDownloadable = YES;
  166. cell.delegate = self;
  167. }
  168. return cell;
  169. }
  170. #pragma mark - table view delegate, for more see super
  171. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  172. {
  173. [super tableView:tableView willDisplayCell:cell forRowAtIndexPath:indexPath];
  174. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  175. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
  176. }
  177. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  178. {
  179. NSDictionary *cellObject;
  180. if (tableView == self.searchDisplayController.searchResultsTableView)
  181. cellObject = _searchData[indexPath.row];
  182. else
  183. cellObject = _objectList[indexPath.row];
  184. if ([cellObject[(id)kCFFTPResourceType] intValue] == 4) {
  185. NSString *newPath = [NSString stringWithFormat:@"%@/%@", _ftpServerPath, cellObject[(id)kCFFTPResourceName]];
  186. VLCFTPServerListViewController *targetViewController = [[VLCFTPServerListViewController alloc] initWithFTPServer:_ftpServerAddress userName:_ftpServerUserName andPassword:_ftpServerPassword atPath:newPath];
  187. [self.navigationController pushViewController:targetViewController animated:YES];
  188. } else {
  189. NSString *rawObjectName = cellObject[(id)kCFFTPResourceName];
  190. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  191. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  192. if (![properObjectName isSupportedFormat]) {
  193. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  194. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName]
  195. delegate:self
  196. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  197. otherButtonTitles:nil];
  198. [alert show];
  199. } else
  200. [self _streamFTPFile:properObjectName];
  201. }
  202. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  203. }
  204. #pragma mark - VLCNetworkListCell delegation
  205. - (void)triggerDownloadForCell:(VLCNetworkListCell *)cell
  206. {
  207. NSString *rawObjectName;
  208. NSInteger size;
  209. NSDictionary *cellObject;
  210. if ([self.searchDisplayController isActive])
  211. cellObject = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  212. else
  213. cellObject = _objectList[[self.tableView indexPathForCell:cell].row];
  214. rawObjectName = cellObject[(id)kCFFTPResourceName];
  215. size = [cellObject[(id)kCFFTPResourceSize] intValue];
  216. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  217. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  218. if (![properObjectName isSupportedFormat]) {
  219. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil)
  220. message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName]
  221. delegate:self
  222. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  223. otherButtonTitles:nil];
  224. [alert show];
  225. } else {
  226. if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
  227. [self _downloadFTPFile:properObjectName];
  228. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  229. } else {
  230. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil)
  231. message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), properObjectName, [[UIDevice currentDevice] model]]
  232. delegate:self
  233. cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil)
  234. otherButtonTitles:nil];
  235. [alert show];
  236. }
  237. }
  238. }
  239. #pragma mark - white raccoon delegation
  240. - (void)requestCompleted:(WRRequest *)request
  241. {
  242. if (request == _FTPListDirRequest) {
  243. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  244. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  245. NSUInteger count = rawList.count;
  246. for (NSUInteger x = 0; x < count; x++) {
  247. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  248. [filteredList addObject:rawList[x]];
  249. }
  250. _objectList = [NSArray arrayWithArray:filteredList];
  251. [self.tableView reloadData];
  252. } else
  253. APLog(@"unknown request %@ completed", request);
  254. }
  255. - (void)requestFailed:(WRRequest *)request
  256. {
  257. VLCAlertView *alert = [[VLCAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil)
  258. message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil)
  259. delegate:self
  260. cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)
  261. otherButtonTitles:nil];
  262. [alert show];
  263. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  264. }
  265. #pragma mark - search
  266. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  267. {
  268. NSInteger listCount = 0;
  269. [_searchData removeAllObjects];
  270. listCount = _objectList.count;
  271. for (int i = 0; i < listCount; i++) {
  272. NSRange nameRange;
  273. NSString *rawObjectName = [_objectList[i] objectForKey:(id)kCFFTPResourceName];
  274. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  275. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  276. nameRange = [properObjectName rangeOfString:searchString options:NSCaseInsensitiveSearch];
  277. if (nameRange.location != NSNotFound)
  278. [_searchData addObject:_objectList[i]];
  279. }
  280. return YES;
  281. }
  282. @end