VLCLocalServerFolderListViewController.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  1. /*****************************************************************************
  2. * VLCLocalServerFolderListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 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 "VLCLocalServerFolderListViewController.h"
  14. #import "MediaServerBasicObjectParser.h"
  15. #import "MediaServer1ItemObject.h"
  16. #import "MediaServer1ContainerObject.h"
  17. #import "MediaServer1Device.h"
  18. #import "VLCLocalNetworkListCell.h"
  19. #import "VLCAppDelegate.h"
  20. #import "VLCPlaylistViewController.h"
  21. #import "UINavigationController+Theme.h"
  22. #import "VLCDownloadViewController.h"
  23. #import "WhiteRaccoon.h"
  24. #import "NSString+SupportedMedia.h"
  25. #import "VLCStatusLabel.h"
  26. #define kVLCServerTypeUPNP 0
  27. #define kVLCServerTypeFTP 1
  28. @interface VLCLocalServerFolderListViewController () <UITableViewDataSource, UITableViewDelegate, WRRequestDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate>
  29. {
  30. /* UI */
  31. UIBarButtonItem *_backButton;
  32. /* generic data storage */
  33. NSString *_listTitle;
  34. NSArray *_objectList;
  35. NSMutableArray *_mutableObjectList;
  36. NSUInteger _serverType;
  37. /* UPNP specifics */
  38. MediaServer1Device *_UPNPdevice;
  39. NSString *_UPNProotID;
  40. /* FTP specifics */
  41. NSString *_ftpServerAddress;
  42. NSString *_ftpServerUserName;
  43. NSString *_ftpServerPassword;
  44. NSString *_ftpServerPath;
  45. WRRequestListDirectory *_FTPListDirRequest;
  46. NSMutableArray *_searchData;
  47. UISearchBar *_searchBar;
  48. UISearchDisplayController *_searchDisplayController;
  49. }
  50. @end
  51. @implementation VLCLocalServerFolderListViewController
  52. - (void)loadView
  53. {
  54. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  55. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  56. _tableView.delegate = self;
  57. _tableView.dataSource = self;
  58. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  59. self.view = _tableView;
  60. }
  61. - (id)initWithUPNPDevice:(MediaServer1Device*)device header:(NSString*)header andRootID:(NSString*)rootID
  62. {
  63. self = [super init];
  64. if (self) {
  65. _UPNPdevice = device;
  66. _listTitle = header;
  67. _UPNProotID = rootID;
  68. _serverType = kVLCServerTypeUPNP;
  69. _mutableObjectList = [[NSMutableArray alloc] init];
  70. }
  71. return self;
  72. }
  73. - (id)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  74. {
  75. self = [super init];
  76. if (self) {
  77. _ftpServerAddress = serverAddress;
  78. _ftpServerUserName = username;
  79. _ftpServerPassword = password;
  80. _ftpServerPath = path;
  81. _serverType = kVLCServerTypeFTP;
  82. }
  83. return self;
  84. }
  85. - (void)viewDidLoad
  86. {
  87. [super viewDidLoad];
  88. if (_serverType == kVLCServerTypeUPNP) {
  89. NSMutableString *outResult = [[NSMutableString alloc] init];
  90. NSMutableString *outNumberReturned = [[NSMutableString alloc] init];
  91. NSMutableString *outTotalMatches = [[NSMutableString alloc] init];
  92. NSMutableString *outUpdateID = [[NSMutableString alloc] init];
  93. [[_UPNPdevice contentDirectory] BrowseWithObjectID:_UPNProotID BrowseFlag:@"BrowseDirectChildren" Filter:@"*" StartingIndex:@"0" RequestedCount:@"0" SortCriteria:@"+dc:title" OutResult:outResult OutNumberReturned:outNumberReturned OutTotalMatches:outTotalMatches OutUpdateID:outUpdateID];
  94. [_mutableObjectList removeAllObjects];
  95. NSData *didl = [outResult dataUsingEncoding:NSUTF8StringEncoding];
  96. MediaServerBasicObjectParser *parser = [[MediaServerBasicObjectParser alloc] initWithMediaObjectArray:_mutableObjectList itemsOnly:NO];
  97. [parser parseFromData:didl];
  98. } else if (_serverType == kVLCServerTypeFTP) {
  99. if ([_ftpServerPath isEqualToString:@"/"])
  100. _listTitle = _ftpServerAddress;
  101. else
  102. _listTitle = [_ftpServerPath lastPathComponent];
  103. [self _listFTPDirectory];
  104. }
  105. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  106. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  107. self.title = _listTitle;
  108. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  109. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  110. _searchDisplayController.delegate = self;
  111. _searchDisplayController.searchResultsDataSource = self;
  112. _searchDisplayController.searchResultsDelegate = self;
  113. if (SYSTEM_RUNS_IOS7_OR_LATER)
  114. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  115. _searchBar.delegate = self;
  116. self.tableView.tableHeaderView = _searchBar; //this line add the searchBar on the top of tableView.
  117. _searchData = [[NSMutableArray alloc] init];
  118. [_searchData removeAllObjects];
  119. }
  120. - (BOOL)shouldAutorotate
  121. {
  122. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  123. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  124. return NO;
  125. return YES;
  126. }
  127. #pragma mark - Table view data source
  128. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  129. {
  130. return 1;
  131. }
  132. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  133. {
  134. if (tableView == self.searchDisplayController.searchResultsTableView)
  135. return _searchData.count;
  136. else {
  137. if (_serverType == kVLCServerTypeUPNP)
  138. return _mutableObjectList.count;
  139. return _objectList.count;
  140. }
  141. }
  142. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  143. {
  144. static NSString *CellIdentifier = @"LocalNetworkCellDetail";
  145. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  146. if (cell == nil)
  147. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  148. if (_serverType == kVLCServerTypeUPNP) {
  149. MediaServer1BasicObject *item;
  150. if (tableView == self.searchDisplayController.searchResultsTableView)
  151. item = _searchData[indexPath.row];
  152. else
  153. item = _mutableObjectList[indexPath.row];
  154. if (![item isContainer]) {
  155. MediaServer1ItemObject *mediaItem;
  156. long long mediaSize = 0;
  157. unsigned int durationInSeconds = 0;
  158. unsigned int bitrate = 0;
  159. if (tableView == self.searchDisplayController.searchResultsTableView)
  160. mediaItem = _searchData[indexPath.row];
  161. else
  162. mediaItem = _mutableObjectList[indexPath.row];
  163. MediaServer1ItemRes *resource = nil;
  164. NSEnumerator *e = [[mediaItem resources] objectEnumerator];
  165. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  166. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  167. mediaSize = resource.size;
  168. durationInSeconds = resource.durationInSeconds;
  169. bitrate = resource.bitrate;
  170. }
  171. }
  172. if (mediaSize < 1)
  173. mediaSize = [mediaItem.size longLongValue];
  174. if (mediaSize < 1)
  175. mediaSize = (bitrate * durationInSeconds);
  176. [cell setSubtitle: [NSString stringWithFormat:@"%@ (%@)", [NSByteCountFormatter stringFromByteCount:mediaSize countStyle:NSByteCountFormatterCountStyleFile], [VLCTime timeWithInt:durationInSeconds * 1000].stringValue]];
  177. [cell setIsDirectory:NO];
  178. cell.isDownloadable = YES;
  179. if (mediaItem.albumArt != nil)
  180. [cell setIconURL:[NSURL URLWithString:mediaItem.albumArt]];
  181. [cell setIcon:[UIImage imageNamed:@"blank"]];
  182. cell.delegate = self;
  183. } else {
  184. [cell setIsDirectory:YES];
  185. if (item.albumArt != nil)
  186. [cell setIconURL:[NSURL URLWithString:item.albumArt]];
  187. [cell setIcon:[UIImage imageNamed:@"folder"]];
  188. }
  189. [cell setTitle:[item title]];
  190. } else if (_serverType == kVLCServerTypeFTP) {
  191. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  192. [ObjList removeAllObjects];
  193. if (tableView == self.searchDisplayController.searchResultsTableView)
  194. [ObjList addObjectsFromArray:_searchData];
  195. else
  196. [ObjList addObjectsFromArray:_objectList];
  197. NSString *rawFileName = [ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  198. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  199. cell.title = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  200. if ([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  201. cell.isDirectory = YES;
  202. cell.icon = [UIImage imageNamed:@"folder"];
  203. } else {
  204. cell.isDirectory = NO;
  205. cell.icon = [UIImage imageNamed:@"blank"];
  206. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceSize] intValue] / 1e6)];
  207. cell.isDownloadable = YES;
  208. cell.delegate = self;
  209. }
  210. }
  211. return cell;
  212. }
  213. #pragma mark - Table view delegate
  214. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  215. {
  216. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  217. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  218. if (_serverType == kVLCServerTypeFTP)
  219. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  220. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
  221. }
  222. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  223. {
  224. if (_serverType == kVLCServerTypeUPNP) {
  225. MediaServer1BasicObject *item;
  226. if (tableView == self.searchDisplayController.searchResultsTableView)
  227. item = _searchData[indexPath.row];
  228. else
  229. item = _mutableObjectList[indexPath.row];
  230. if ([item isContainer]) {
  231. MediaServer1ContainerObject *container;
  232. if (tableView == self.searchDisplayController.searchResultsTableView)
  233. container = _searchData[indexPath.row];
  234. else
  235. container = _mutableObjectList[indexPath.row];
  236. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:_UPNPdevice header:[container title] andRootID:[container objectID]];
  237. [[self navigationController] pushViewController:targetViewController animated:YES];
  238. } else {
  239. MediaServer1ItemObject *mediaItem;
  240. if (tableView == self.searchDisplayController.searchResultsTableView)
  241. mediaItem = _searchData[indexPath.row];
  242. else
  243. mediaItem = _mutableObjectList[indexPath.row];
  244. NSURL *itemURL;
  245. NSArray *uriCollectionKeys = [[mediaItem uriCollection] allKeys];
  246. NSUInteger count = uriCollectionKeys.count;
  247. NSRange position;
  248. NSUInteger correctIndex = 0;
  249. for (NSUInteger i = 0; i < count; i++) {
  250. position = [uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"];
  251. if (position.location != NSNotFound)
  252. correctIndex = i;
  253. }
  254. NSArray *uriCollectionObjects = [[mediaItem uriCollection] allValues];
  255. if (uriCollectionObjects.count > 0)
  256. itemURL = [NSURL URLWithString:uriCollectionObjects[correctIndex]];
  257. if (itemURL) {
  258. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  259. [appDelegate openMovieFromURL:itemURL];
  260. }
  261. }
  262. } else if (_serverType == kVLCServerTypeFTP) {
  263. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  264. [ObjList removeAllObjects];
  265. if (tableView == self.searchDisplayController.searchResultsTableView)
  266. [ObjList addObjectsFromArray:_searchData];
  267. else
  268. [ObjList addObjectsFromArray:_objectList];
  269. if ([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  270. NSString *newPath = [NSString stringWithFormat:@"%@/%@", _ftpServerPath, [ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceName]];
  271. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:_ftpServerAddress userName:_ftpServerUserName andPassword:_ftpServerPassword atPath:newPath];
  272. [self.navigationController pushViewController:targetViewController animated:YES];
  273. } else {
  274. NSString *rawObjectName = [ObjList[indexPath.row] 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. if (![properObjectName isSupportedFormat]) {
  278. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), properObjectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  279. [alert show];
  280. } else
  281. [self _streamFTPFile:properObjectName];
  282. }
  283. }
  284. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  285. }
  286. #pragma mark - FTP specifics
  287. - (void)_listFTPDirectory
  288. {
  289. if (_FTPListDirRequest)
  290. return;
  291. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  292. _FTPListDirRequest.delegate = self;
  293. _FTPListDirRequest.hostname = _ftpServerAddress;
  294. _FTPListDirRequest.username = _ftpServerUserName;
  295. _FTPListDirRequest.password = _ftpServerPassword;
  296. _FTPListDirRequest.path = _ftpServerPath;
  297. _FTPListDirRequest.passive = YES;
  298. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStarted];
  299. [_FTPListDirRequest start];
  300. }
  301. - (NSString *)_credentials
  302. {
  303. NSString * cred;
  304. if (_ftpServerUserName.length > 0) {
  305. if (_ftpServerPassword.length > 0)
  306. cred = [NSString stringWithFormat:@"%@:%@@", _ftpServerUserName, _ftpServerPassword];
  307. else
  308. cred = [NSString stringWithFormat:@"%@@", _ftpServerPassword];
  309. } else
  310. cred = @"";
  311. return [cred stringByStandardizingPath];
  312. }
  313. - (void)_downloadFTPFile:(NSString *)fileName
  314. {
  315. NSURL *URLToQueue = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  316. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:URLToQueue fileNameOfMedia:nil];
  317. }
  318. - (void)_downloadUPNPFileFromMediaItem:(MediaServer1ItemObject *)mediaItem
  319. {
  320. NSURL *itemURL;
  321. NSArray *uriCollectionKeys = [[mediaItem uriCollection] allKeys];
  322. NSUInteger count = uriCollectionKeys.count;
  323. NSRange position;
  324. NSUInteger correctIndex = 0;
  325. for (NSUInteger i = 0; i < count; i++) {
  326. position = [uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"];
  327. if (position.location != NSNotFound)
  328. correctIndex = i;
  329. }
  330. NSArray *uriCollectionObjects = [[mediaItem uriCollection] allValues];
  331. if (uriCollectionObjects.count > 0)
  332. itemURL = [NSURL URLWithString:uriCollectionObjects[correctIndex]];
  333. if (![itemURL.absoluteString isSupportedFormat]) {
  334. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), [mediaItem uri]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  335. [alert show];
  336. } else if (itemURL) {
  337. NSString *fileName = [[mediaItem.title stringByAppendingString:@"."] stringByAppendingString:[[itemURL absoluteString] pathExtension]];
  338. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  339. }
  340. }
  341. - (void)requestCompleted:(WRRequest *)request
  342. {
  343. if (request == _FTPListDirRequest) {
  344. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  345. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  346. NSUInteger count = rawList.count;
  347. for (NSUInteger x = 0; x < count; x++) {
  348. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  349. [filteredList addObject:rawList[x]];
  350. }
  351. _objectList = [NSArray arrayWithArray:filteredList];
  352. [self.tableView reloadData];
  353. } else
  354. APLog(@"unknown request %@ completed", request);
  355. }
  356. - (void)requestFailed:(WRRequest *)request
  357. {
  358. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_TITLE", nil) message:NSLocalizedString(@"LOCAL_SERVER_CONNECTION_FAILED_MESSAGE", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  359. [alert show];
  360. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  361. }
  362. #pragma mark - VLCLocalNetworkListCell delegation
  363. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  364. {
  365. if (_serverType == kVLCServerTypeUPNP) {
  366. MediaServer1ItemObject *item;
  367. if ([self.searchDisplayController isActive])
  368. item = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  369. else
  370. item = _mutableObjectList[[self.tableView indexPathForCell:cell].row];
  371. [self _downloadUPNPFileFromMediaItem:item];
  372. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  373. }else if (_serverType == kVLCServerTypeFTP) {
  374. NSString *rawObjectName;
  375. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  376. [ObjList removeAllObjects];
  377. if ([self.searchDisplayController isActive]) {
  378. [ObjList addObjectsFromArray:_searchData];
  379. rawObjectName = [ObjList[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  380. } else {
  381. [ObjList addObjectsFromArray:_objectList];
  382. rawObjectName = [ObjList[[self.tableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  383. }
  384. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  385. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  386. if (![properObjectName isSupportedFormat]) {
  387. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), properObjectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  388. [alert show];
  389. } else {
  390. [self _downloadFTPFile:properObjectName];
  391. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  392. }
  393. }
  394. }
  395. #pragma mark - communication with playback engine
  396. - (void)_streamFTPFile:(NSString *)fileName
  397. {
  398. NSString *URLofSubtitle = nil;
  399. NSMutableArray *SubtitlesList = [[NSMutableArray alloc] init];
  400. [SubtitlesList removeAllObjects];
  401. SubtitlesList = [self _searchSubtitle:fileName];
  402. if(SubtitlesList.count > 0)
  403. URLofSubtitle = [self _getFileSubtitleFromFtpServer:SubtitlesList[0]];
  404. NSURL *URLToPlay = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  405. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  406. [appDelegate openMovieWithExternalSubtitleFromURL:URLToPlay externalSubURL:URLofSubtitle];
  407. }
  408. - (NSMutableArray *)_searchSubtitle:(NSString *)url
  409. {
  410. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  411. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  412. [ObjList removeAllObjects];
  413. for (int loop = 0; loop < _objectList.count; loop++)
  414. [ObjList addObject:[_objectList[loop] objectForKey:(id)kCFFTPResourceName]];
  415. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", urlTemp];
  416. NSArray *results = [ObjList filteredArrayUsingPredicate:predicate];
  417. [ObjList removeAllObjects];
  418. for (int cnt = 0; cnt < results.count; cnt++) {
  419. if ([results[cnt] isSupportedSubtitleFormat])
  420. [ObjList addObject:results[cnt]];
  421. }
  422. return ObjList;
  423. }
  424. - (NSString *)_getFileSubtitleFromFtpServer:(NSString *)fileName
  425. {
  426. NSURL *url = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  427. NSString *receivedSub = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
  428. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  429. NSString *directoryPath = searchPaths[0];
  430. NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[fileName lastPathComponent]];
  431. NSFileManager *fileManager = [NSFileManager defaultManager];
  432. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  433. //create local subtitle file
  434. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  435. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  436. APLog(@"file creation failed, no data was saved");
  437. }
  438. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  439. return FileSubtitlePath;
  440. }
  441. #pragma mark - Search Display Controller Delegate
  442. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  443. {
  444. MediaServer1BasicObject *item;
  445. NSInteger listCount = 0;
  446. [_searchData removeAllObjects];
  447. if (_serverType == kVLCServerTypeUPNP)
  448. listCount = _mutableObjectList.count;
  449. else if (_serverType == kVLCServerTypeFTP)
  450. listCount = _objectList.count;
  451. for (int i = 0; i < listCount; i++) {
  452. NSRange nameRange;
  453. if (_serverType == kVLCServerTypeUPNP) {
  454. item = _mutableObjectList[i];
  455. nameRange = [[item title] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  456. } else if (_serverType == kVLCServerTypeFTP) {
  457. NSString *rawObjectName = [_objectList[i] objectForKey:(id)kCFFTPResourceName];
  458. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  459. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  460. nameRange = [properObjectName rangeOfString:searchString options:NSCaseInsensitiveSearch];
  461. }
  462. if (nameRange.location != NSNotFound) {
  463. if (_serverType == kVLCServerTypeUPNP)
  464. [_searchData addObject:_mutableObjectList[i]];
  465. else
  466. [_searchData addObject:_objectList[i]];
  467. }
  468. }
  469. return YES;
  470. }
  471. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  472. {
  473. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  474. tableView.rowHeight = 80.0f;
  475. else
  476. tableView.rowHeight = 68.0f;
  477. tableView.backgroundColor = [UIColor blackColor];
  478. }
  479. @end