VLCFTPServerListViewController.m 15 KB

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