VLCLocalServerFolderListViewController.m 25 KB

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