VLCLocalServerFolderListViewController.m 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. #import "BasicUPnPDevice+VLC.h"
  27. #define kVLCServerTypeUPNP 0
  28. #define kVLCServerTypeFTP 1
  29. @interface VLCLocalServerFolderListViewController () <UITableViewDataSource, UITableViewDelegate, WRRequestDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate, UIActionSheetDelegate>
  30. {
  31. /* UI */
  32. UIBarButtonItem *_backButton;
  33. /* generic data storage */
  34. NSString *_listTitle;
  35. NSArray *_objectList;
  36. NSMutableArray *_mutableObjectList;
  37. NSUInteger _serverType;
  38. /* UPNP specifics */
  39. MediaServer1Device *_UPNPdevice;
  40. NSString *_UPNProotID;
  41. /* FTP specifics */
  42. NSString *_ftpServerAddress;
  43. NSString *_ftpServerUserName;
  44. NSString *_ftpServerPassword;
  45. NSString *_ftpServerPath;
  46. WRRequestListDirectory *_FTPListDirRequest;
  47. NSMutableArray *_searchData;
  48. UISearchBar *_searchBar;
  49. UISearchDisplayController *_searchDisplayController;
  50. /* UPnP items with multiple resources specifics */
  51. MediaServer1ItemObject *_lastSelectedMediaItem;
  52. UIView *_resourceSelectionActionSheetAnchorView;
  53. }
  54. @end
  55. @implementation VLCLocalServerFolderListViewController
  56. - (void)loadView
  57. {
  58. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  59. _tableView.backgroundColor = [UIColor VLCDarkBackgroundColor];
  60. _tableView.delegate = self;
  61. _tableView.dataSource = self;
  62. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  63. _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  64. self.view = _tableView;
  65. }
  66. - (id)initWithUPNPDevice:(MediaServer1Device*)device header:(NSString*)header andRootID:(NSString*)rootID
  67. {
  68. self = [super init];
  69. if (self) {
  70. _UPNPdevice = device;
  71. _listTitle = header;
  72. _UPNProotID = rootID;
  73. _serverType = kVLCServerTypeUPNP;
  74. _mutableObjectList = [[NSMutableArray alloc] init];
  75. }
  76. return self;
  77. }
  78. - (id)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  79. {
  80. self = [super init];
  81. if (self) {
  82. _ftpServerAddress = serverAddress;
  83. _ftpServerUserName = username;
  84. _ftpServerPassword = password;
  85. _ftpServerPath = path;
  86. _serverType = kVLCServerTypeFTP;
  87. }
  88. return self;
  89. }
  90. - (void)viewDidLoad
  91. {
  92. [super viewDidLoad];
  93. if (_serverType == kVLCServerTypeUPNP) {
  94. NSString *sortCriteria = @"";
  95. NSMutableString *outSortCaps = [[NSMutableString alloc] init];
  96. [[_UPNPdevice contentDirectory] GetSortCapabilitiesWithOutSortCaps:outSortCaps];
  97. if ([outSortCaps rangeOfString:@"dc:title"].location != NSNotFound)
  98. {
  99. sortCriteria = @"+dc:title";
  100. }
  101. NSMutableString *outResult = [[NSMutableString alloc] init];
  102. NSMutableString *outNumberReturned = [[NSMutableString alloc] init];
  103. NSMutableString *outTotalMatches = [[NSMutableString alloc] init];
  104. NSMutableString *outUpdateID = [[NSMutableString alloc] init];
  105. [[_UPNPdevice contentDirectory] BrowseWithObjectID:_UPNProotID BrowseFlag:@"BrowseDirectChildren" Filter:@"*" StartingIndex:@"0" RequestedCount:@"0" SortCriteria:sortCriteria OutResult:outResult OutNumberReturned:outNumberReturned OutTotalMatches:outTotalMatches OutUpdateID:outUpdateID];
  106. [_mutableObjectList removeAllObjects];
  107. NSData *didl = [outResult dataUsingEncoding:NSUTF8StringEncoding];
  108. MediaServerBasicObjectParser *parser = [[MediaServerBasicObjectParser alloc] initWithMediaObjectArray:_mutableObjectList itemsOnly:NO];
  109. [parser parseFromData:didl];
  110. } else if (_serverType == kVLCServerTypeFTP) {
  111. if ([_ftpServerPath isEqualToString:@"/"])
  112. _listTitle = _ftpServerAddress;
  113. else
  114. _listTitle = [_ftpServerPath lastPathComponent];
  115. [self _listFTPDirectory];
  116. }
  117. self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
  118. self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
  119. self.title = _listTitle;
  120. _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
  121. _searchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:_searchBar contentsController:self];
  122. _searchDisplayController.delegate = self;
  123. _searchDisplayController.searchResultsDataSource = self;
  124. _searchDisplayController.searchResultsDelegate = self;
  125. if (SYSTEM_RUNS_IOS7_OR_LATER)
  126. _searchDisplayController.searchBar.searchBarStyle = UIBarStyleBlack;
  127. _searchBar.delegate = self;
  128. self.tableView.tableHeaderView = _searchBar; //this line add the searchBar on the top of tableView.
  129. _searchData = [[NSMutableArray alloc] init];
  130. [_searchData removeAllObjects];
  131. }
  132. - (BOOL)shouldAutorotate
  133. {
  134. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  135. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  136. return NO;
  137. return YES;
  138. }
  139. #pragma mark - Table view data source
  140. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  141. {
  142. return 1;
  143. }
  144. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  145. {
  146. if (tableView == self.searchDisplayController.searchResultsTableView)
  147. return _searchData.count;
  148. else {
  149. if (_serverType == kVLCServerTypeUPNP)
  150. return _mutableObjectList.count;
  151. return _objectList.count;
  152. }
  153. }
  154. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  155. {
  156. static NSString *CellIdentifier = @"LocalNetworkCellDetail";
  157. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  158. if (cell == nil)
  159. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  160. if (_serverType == kVLCServerTypeUPNP) {
  161. MediaServer1BasicObject *item;
  162. if (tableView == self.searchDisplayController.searchResultsTableView)
  163. item = _searchData[indexPath.row];
  164. else
  165. item = _mutableObjectList[indexPath.row];
  166. if (![item isContainer]) {
  167. MediaServer1ItemObject *mediaItem;
  168. long long mediaSize = 0;
  169. unsigned int durationInSeconds = 0;
  170. unsigned int bitrate = 0;
  171. if (tableView == self.searchDisplayController.searchResultsTableView)
  172. mediaItem = _searchData[indexPath.row];
  173. else
  174. mediaItem = _mutableObjectList[indexPath.row];
  175. MediaServer1ItemRes *resource = nil;
  176. NSEnumerator *e = [[mediaItem resources] objectEnumerator];
  177. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  178. if (resource.bitrate > 0 && resource.durationInSeconds > 0) {
  179. mediaSize = resource.size;
  180. durationInSeconds = resource.durationInSeconds;
  181. bitrate = resource.bitrate;
  182. }
  183. }
  184. if (mediaSize < 1)
  185. mediaSize = [mediaItem.size longLongValue];
  186. if (mediaSize < 1)
  187. mediaSize = (bitrate * durationInSeconds);
  188. // object.item.videoItem.videoBroadcast items (like the HDHomeRun) may not have this information. Center the title (this makes channel names look better for the HDHomeRun)
  189. if (mediaSize > 0 && durationInSeconds > 0) {
  190. [cell setSubtitle: [NSString stringWithFormat:@"%@ (%@)", [NSByteCountFormatter stringFromByteCount:mediaSize countStyle:NSByteCountFormatterCountStyleFile], [VLCTime timeWithInt:durationInSeconds * 1000].stringValue]];
  191. } else {
  192. cell.titleLabelCentered = YES;
  193. }
  194. // Custom TV icon for video broadcasts
  195. if ([[mediaItem objectClass] isEqualToString:@"object.item.videoItem.videoBroadcast"]) {
  196. UIImage *broadcastImage;
  197. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  198. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon"];
  199. } else {
  200. broadcastImage = [UIImage imageNamed:@"TVBroadcastIcon~ipad"];
  201. }
  202. [cell setIcon:broadcastImage];
  203. } else {
  204. [cell setIcon:[UIImage imageNamed:@"blank"]];
  205. }
  206. [cell setIsDirectory:NO];
  207. if (mediaItem.albumArt != nil)
  208. [cell setIconURL:[NSURL URLWithString:mediaItem.albumArt]];
  209. // Disable downloading for the HDHomeRun for now to avoid infinite downloads (URI needs a duration parameter, otherwise you are just downloading a live stream). VLC also needs an extension in the file name for this to work.
  210. if (![_UPNPdevice VLC_isHDHomeRunMediaServer]) {
  211. cell.isDownloadable = YES;
  212. }
  213. cell.delegate = self;
  214. } else {
  215. [cell setIsDirectory:YES];
  216. if (item.albumArt != nil)
  217. [cell setIconURL:[NSURL URLWithString:item.albumArt]];
  218. [cell setIcon:[UIImage imageNamed:@"folder"]];
  219. }
  220. [cell setTitle:[item title]];
  221. } else if (_serverType == kVLCServerTypeFTP) {
  222. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  223. [ObjList removeAllObjects];
  224. if (tableView == self.searchDisplayController.searchResultsTableView)
  225. [ObjList addObjectsFromArray:_searchData];
  226. else
  227. [ObjList addObjectsFromArray:_objectList];
  228. NSString *rawFileName = [ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  229. NSData *flippedData = [rawFileName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  230. cell.title = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  231. if ([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  232. cell.isDirectory = YES;
  233. cell.icon = [UIImage imageNamed:@"folder"];
  234. } else {
  235. cell.isDirectory = NO;
  236. cell.icon = [UIImage imageNamed:@"blank"];
  237. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceSize] intValue] / 1e6)];
  238. cell.isDownloadable = YES;
  239. cell.delegate = self;
  240. }
  241. }
  242. return cell;
  243. }
  244. #pragma mark - Table view delegate
  245. - (void)tableView:(UITableView *)tableView willDisplayCell:(VLCLocalNetworkListCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  246. {
  247. UIColor *color = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  248. cell.contentView.backgroundColor = cell.titleLabel.backgroundColor = cell.folderTitleLabel.backgroundColor = cell.subtitleLabel.backgroundColor = color;
  249. if (_serverType == kVLCServerTypeFTP)
  250. if([indexPath row] == ((NSIndexPath*)[[tableView indexPathsForVisibleRows] lastObject]).row)
  251. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStopped];
  252. }
  253. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  254. {
  255. if (_serverType == kVLCServerTypeUPNP) {
  256. MediaServer1BasicObject *item;
  257. if (tableView == self.searchDisplayController.searchResultsTableView)
  258. item = _searchData[indexPath.row];
  259. else
  260. item = _mutableObjectList[indexPath.row];
  261. if ([item isContainer]) {
  262. MediaServer1ContainerObject *container;
  263. if (tableView == self.searchDisplayController.searchResultsTableView)
  264. container = _searchData[indexPath.row];
  265. else
  266. container = _mutableObjectList[indexPath.row];
  267. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:_UPNPdevice header:[container title] andRootID:[container objectID]];
  268. [[self navigationController] pushViewController:targetViewController animated:YES];
  269. } else {
  270. MediaServer1ItemObject *mediaItem;
  271. if (tableView == self.searchDisplayController.searchResultsTableView)
  272. mediaItem = _searchData[indexPath.row];
  273. else
  274. mediaItem = _mutableObjectList[indexPath.row];
  275. NSURL *itemURL;
  276. NSArray *uriCollectionKeys = [[mediaItem uriCollection] allKeys];
  277. NSUInteger count = uriCollectionKeys.count;
  278. NSRange position;
  279. NSUInteger correctIndex = 0;
  280. NSUInteger numberOfDownloadableResources = 0;
  281. for (NSUInteger i = 0; i < count; i++) {
  282. position = [uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"];
  283. if (position.location != NSNotFound) {
  284. correctIndex = i;
  285. numberOfDownloadableResources++;
  286. }
  287. }
  288. NSArray *uriCollectionObjects = [[mediaItem uriCollection] allValues];
  289. // Present an action sheet for the user to choose which URI to download. Do not deselect the cell to provide visual feedback to the user
  290. if (numberOfDownloadableResources > 1) {
  291. _resourceSelectionActionSheetAnchorView = [tableView cellForRowAtIndexPath:indexPath];
  292. [self presentResourceSelectionActionSheetForUPnPMediaItem:mediaItem forDownloading:NO];
  293. } else {
  294. if (uriCollectionObjects.count > 0) {
  295. itemURL = [NSURL URLWithString:uriCollectionObjects[correctIndex]];
  296. }
  297. if (itemURL) {
  298. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  299. [appDelegate openMovieFromURL:itemURL];
  300. }
  301. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  302. }
  303. }
  304. } else if (_serverType == kVLCServerTypeFTP) {
  305. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  306. [ObjList removeAllObjects];
  307. if (tableView == self.searchDisplayController.searchResultsTableView)
  308. [ObjList addObjectsFromArray:_searchData];
  309. else
  310. [ObjList addObjectsFromArray:_objectList];
  311. if ([[ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  312. NSString *newPath = [NSString stringWithFormat:@"%@/%@", _ftpServerPath, [ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceName]];
  313. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:_ftpServerAddress userName:_ftpServerUserName andPassword:_ftpServerPassword atPath:newPath];
  314. [self.navigationController pushViewController:targetViewController animated:YES];
  315. } else {
  316. NSString *rawObjectName = [ObjList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  317. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  318. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  319. if (![properObjectName isSupportedFormat]) {
  320. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
  321. [alert show];
  322. } else
  323. [self _streamFTPFile:properObjectName];
  324. }
  325. [tableView deselectRowAtIndexPath:indexPath animated:NO];
  326. }
  327. }
  328. #pragma mark - UPnP Multiple Resources
  329. /// Presents an UIActionSheet for the user to choose which <res> resource to play or download. Contains some display code specific to the HDHomeRun devices. Also parses "DLNA.ORG_PN" protocolInfo.
  330. - (void)presentResourceSelectionActionSheetForUPnPMediaItem:(MediaServer1ItemObject *)mediaItem forDownloading:(BOOL)forDownloading {
  331. NSParameterAssert(mediaItem);
  332. if (!mediaItem) {
  333. return;
  334. }
  335. // Store it so we can act on the action sheet callback.
  336. _lastSelectedMediaItem = mediaItem;
  337. NSArray *uriCollectionKeys = [[_lastSelectedMediaItem uriCollection] allKeys];
  338. NSArray *uriCollectionObjects = [[_lastSelectedMediaItem uriCollection] allValues];
  339. NSUInteger count = uriCollectionKeys.count;
  340. NSRange position;
  341. NSString *titleString;
  342. if (!forDownloading) {
  343. titleString = NSLocalizedString(@"SELECT_RESOURCE_TO_PLAY", nil);
  344. } else {
  345. titleString = NSLocalizedString(@"SELECT_RESOURCE_TO_DOWNLOAD", nil);
  346. }
  347. UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:titleString
  348. delegate:self
  349. cancelButtonTitle:nil
  350. destructiveButtonTitle:nil
  351. otherButtonTitles:nil];
  352. // Provide users with a descriptive action sheet for them to choose based on the multiple resources advertised by DLNA devices (HDHomeRun for example)
  353. for (NSUInteger i = 0; i < count; i++) {
  354. position = [uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"];
  355. if (position.location != NSNotFound) {
  356. NSString *orgPNValue;
  357. NSString *transcodeValue;
  358. // Attempt to parse DLNA.ORG_PN first
  359. NSString *protocolInfo = uriCollectionKeys[i];
  360. NSArray *components = [protocolInfo componentsSeparatedByString:@";"];
  361. NSArray *nonFlagsComponents = [components[0] componentsSeparatedByString:@":"];
  362. NSString *orgPN = [nonFlagsComponents lastObject];
  363. // Check to see if we are where we should be
  364. NSRange orgPNRange = [orgPN rangeOfString:@"DLNA.ORG_PN="];
  365. if (orgPNRange.location == 0) {
  366. orgPNValue = [orgPN substringFromIndex:orgPNRange.length];
  367. }
  368. // HDHomeRun: Get the transcode profile from the HTTP API if possible
  369. if ([_UPNPdevice VLC_isHDHomeRunMediaServer]) {
  370. NSRange transcodeRange = [uriCollectionObjects[i] rangeOfString:@"transcode="];
  371. if (transcodeRange.location != NSNotFound) {
  372. transcodeValue = [uriCollectionObjects[i] substringFromIndex:transcodeRange.location + transcodeRange.length];
  373. // Check that there are no more parameters
  374. NSRange ampersandRange = [transcodeValue rangeOfString:@"&"];
  375. if (ampersandRange.location != NSNotFound) {
  376. transcodeValue = [transcodeValue substringToIndex:transcodeRange.location];
  377. }
  378. transcodeValue = [transcodeValue capitalizedString];
  379. }
  380. }
  381. // Fallbacks to get the most descriptive resource title
  382. NSString *profileTitle;
  383. if ([transcodeValue length] && [orgPNValue length]) {
  384. profileTitle = [NSString stringWithFormat:@"%@ (%@)", transcodeValue, orgPNValue];
  385. // The extra whitespace is to get UIActionSheet to render the text better (this bug has been fixed in iOS 8)
  386. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  387. if (!SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
  388. profileTitle = [NSString stringWithFormat:@" %@ ", profileTitle];
  389. }
  390. }
  391. } else if ([transcodeValue length]) {
  392. profileTitle = transcodeValue;
  393. } else if ([orgPNValue length]) {
  394. profileTitle = orgPNValue;
  395. } else if ([uriCollectionKeys[i] length]) {
  396. profileTitle = uriCollectionKeys[i];
  397. } else if ([uriCollectionObjects[i] length]) {
  398. profileTitle = uriCollectionObjects[i];
  399. } else {
  400. profileTitle = NSLocalizedString(@"UNKNOWN", nil);
  401. }
  402. [actionSheet addButtonWithTitle:profileTitle];
  403. }
  404. }
  405. // If no resources are found, an empty action sheet will be presented, but the fact that we got here implies that we have playable resources, so no special handling for this case is included
  406. actionSheet.cancelButtonIndex = [actionSheet addButtonWithTitle:NSLocalizedString(@"BUTTON_CANCEL", nil)];
  407. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  408. // Attach it to a specific view (a cell, a download button, etc)
  409. if (_resourceSelectionActionSheetAnchorView) {
  410. CGRect presentationRect = [self.view convertRect:_resourceSelectionActionSheetAnchorView.frame fromView:_resourceSelectionActionSheetAnchorView.superview];
  411. [actionSheet showFromRect:presentationRect inView:self.view animated:YES];
  412. } else {
  413. [actionSheet showInView:self.view];
  414. }
  415. } else {
  416. [actionSheet showInView:self.view];
  417. }
  418. }
  419. #pragma mark - UIActionSheetDelegate
  420. - (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
  421. {
  422. // Act on the selected resource that the user selected
  423. if (_lastSelectedMediaItem) {
  424. if (buttonIndex != actionSheet.cancelButtonIndex) {
  425. // Check again through our raw list which items are playable, since this same code was used to build the action sheet. Make sure we choose the right object based on the action sheet button index.
  426. NSArray *uriCollectionKeys = [[_lastSelectedMediaItem uriCollection] allKeys];
  427. NSArray *uriCollectionObjects = [[_lastSelectedMediaItem uriCollection] allValues];
  428. if (uriCollectionObjects.count > 0) {
  429. NSUInteger count = uriCollectionKeys.count;
  430. NSMutableArray *possibleCollectionObjects = [[NSMutableArray alloc] initWithCapacity:[uriCollectionObjects count]];
  431. for (NSUInteger i = 0; i < count; i++) {
  432. if ([uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"].location != NSNotFound) {
  433. [possibleCollectionObjects addObject:uriCollectionObjects[i]];
  434. }
  435. }
  436. NSString *itemURLString = uriCollectionObjects[buttonIndex];
  437. if ([itemURLString length]) {
  438. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  439. [appDelegate openMovieFromURL:[NSURL URLWithString:itemURLString]];
  440. }
  441. }
  442. }
  443. _lastSelectedMediaItem = nil;
  444. _resourceSelectionActionSheetAnchorView = nil;
  445. UITableView *activeTableView;
  446. if ([self.searchDisplayController isActive]) {
  447. activeTableView = self.searchDisplayController.searchResultsTableView;
  448. } else {
  449. activeTableView = self.tableView;
  450. }
  451. [activeTableView deselectRowAtIndexPath:[activeTableView indexPathForSelectedRow] animated:NO];
  452. }
  453. }
  454. #pragma mark - FTP specifics
  455. - (void)_listFTPDirectory
  456. {
  457. if (_FTPListDirRequest)
  458. return;
  459. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  460. _FTPListDirRequest.delegate = self;
  461. _FTPListDirRequest.hostname = _ftpServerAddress;
  462. _FTPListDirRequest.username = _ftpServerUserName;
  463. _FTPListDirRequest.password = _ftpServerPassword;
  464. _FTPListDirRequest.path = _ftpServerPath;
  465. _FTPListDirRequest.passive = YES;
  466. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate networkActivityStarted];
  467. [_FTPListDirRequest start];
  468. }
  469. - (NSString *)_credentials
  470. {
  471. NSString * cred;
  472. if (_ftpServerUserName.length > 0) {
  473. if (_ftpServerPassword.length > 0)
  474. cred = [NSString stringWithFormat:@"%@:%@@", _ftpServerUserName, _ftpServerPassword];
  475. else
  476. cred = [NSString stringWithFormat:@"%@@", _ftpServerPassword];
  477. } else
  478. cred = @"";
  479. return [cred stringByStandardizingPath];
  480. }
  481. - (void)_downloadFTPFile:(NSString *)fileName
  482. {
  483. NSURL *URLToQueue = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  484. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:URLToQueue fileNameOfMedia:nil];
  485. }
  486. - (void)_downloadUPNPFileFromMediaItem:(MediaServer1ItemObject *)mediaItem
  487. {
  488. NSURL *itemURL;
  489. NSArray *uriCollectionKeys = [[mediaItem uriCollection] allKeys];
  490. NSUInteger count = uriCollectionKeys.count;
  491. NSRange position;
  492. NSUInteger correctIndex = 0;
  493. for (NSUInteger i = 0; i < count; i++) {
  494. position = [uriCollectionKeys[i] rangeOfString:@"http-get:*:video/"];
  495. if (position.location != NSNotFound)
  496. correctIndex = i;
  497. }
  498. NSArray *uriCollectionObjects = [[mediaItem uriCollection] allValues];
  499. if (uriCollectionObjects.count > 0)
  500. itemURL = [NSURL URLWithString:uriCollectionObjects[correctIndex]];
  501. if (![itemURL.absoluteString isSupportedFormat]) {
  502. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [mediaItem uri]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
  503. [alert show];
  504. } else if (itemURL) {
  505. NSString *fileName = [[mediaItem.title stringByAppendingString:@"."] stringByAppendingString:[[itemURL absoluteString] pathExtension]];
  506. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:itemURL fileNameOfMedia:fileName];
  507. }
  508. }
  509. - (void)requestCompleted:(WRRequest *)request
  510. {
  511. if (request == _FTPListDirRequest) {
  512. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  513. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  514. NSUInteger count = rawList.count;
  515. for (NSUInteger x = 0; x < count; x++) {
  516. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  517. [filteredList addObject:rawList[x]];
  518. }
  519. _objectList = [NSArray arrayWithArray:filteredList];
  520. [self.tableView reloadData];
  521. } else
  522. APLog(@"unknown request %@ completed", request);
  523. }
  524. - (void)requestFailed:(WRRequest *)request
  525. {
  526. 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", nil) otherButtonTitles:nil];
  527. [alert show];
  528. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  529. }
  530. #pragma mark - VLCLocalNetworkListCell delegation
  531. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  532. {
  533. if (_serverType == kVLCServerTypeUPNP) {
  534. MediaServer1ItemObject *item;
  535. if ([self.searchDisplayController isActive])
  536. item = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
  537. else
  538. item = _mutableObjectList[[self.tableView indexPathForCell:cell].row];
  539. [self _downloadUPNPFileFromMediaItem:item];
  540. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  541. }else if (_serverType == kVLCServerTypeFTP) {
  542. NSString *rawObjectName;
  543. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  544. [ObjList removeAllObjects];
  545. if ([self.searchDisplayController isActive]) {
  546. [ObjList addObjectsFromArray:_searchData];
  547. rawObjectName = [ObjList[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  548. } else {
  549. [ObjList addObjectsFromArray:_objectList];
  550. rawObjectName = [ObjList[[self.tableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  551. }
  552. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  553. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  554. if (![properObjectName isSupportedFormat]) {
  555. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), properObjectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
  556. [alert show];
  557. } else {
  558. [self _downloadFTPFile:properObjectName];
  559. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
  560. }
  561. }
  562. }
  563. #pragma mark - communication with playback engine
  564. - (void)_streamFTPFile:(NSString *)fileName
  565. {
  566. NSString *URLofSubtitle = nil;
  567. NSMutableArray *SubtitlesList = [[NSMutableArray alloc] init];
  568. [SubtitlesList removeAllObjects];
  569. SubtitlesList = [self _searchSubtitle:fileName];
  570. if(SubtitlesList.count > 0)
  571. URLofSubtitle = [self _getFileSubtitleFromFtpServer:SubtitlesList[0]];
  572. NSURL *URLToPlay = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  573. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  574. [appDelegate openMovieWithExternalSubtitleFromURL:URLToPlay externalSubURL:URLofSubtitle];
  575. }
  576. - (NSMutableArray *)_searchSubtitle:(NSString *)url
  577. {
  578. NSString *urlTemp = [[url lastPathComponent] stringByDeletingPathExtension];
  579. NSMutableArray *ObjList = [[NSMutableArray alloc] init];
  580. [ObjList removeAllObjects];
  581. for (int loop = 0; loop < _objectList.count; loop++)
  582. [ObjList addObject:[_objectList[loop] objectForKey:(id)kCFFTPResourceName]];
  583. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains[c] %@", urlTemp];
  584. NSArray *results = [ObjList filteredArrayUsingPredicate:predicate];
  585. [ObjList removeAllObjects];
  586. for (int cnt = 0; cnt < results.count; cnt++) {
  587. if ([results[cnt] isSupportedSubtitleFormat])
  588. [ObjList addObject:results[cnt]];
  589. }
  590. return ObjList;
  591. }
  592. - (NSString *)_getFileSubtitleFromFtpServer:(NSString *)fileName
  593. {
  594. NSURL *url = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  595. NSString *receivedSub = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:nil];
  596. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  597. NSString *directoryPath = searchPaths[0];
  598. NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[fileName lastPathComponent]];
  599. NSFileManager *fileManager = [NSFileManager defaultManager];
  600. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  601. //create local subtitle file
  602. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  603. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  604. APLog(@"file creation failed, no data was saved");
  605. }
  606. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  607. return FileSubtitlePath;
  608. }
  609. #pragma mark - Search Display Controller Delegate
  610. - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
  611. {
  612. MediaServer1BasicObject *item;
  613. NSInteger listCount = 0;
  614. [_searchData removeAllObjects];
  615. if (_serverType == kVLCServerTypeUPNP)
  616. listCount = _mutableObjectList.count;
  617. else if (_serverType == kVLCServerTypeFTP)
  618. listCount = _objectList.count;
  619. for (int i = 0; i < listCount; i++) {
  620. NSRange nameRange;
  621. if (_serverType == kVLCServerTypeUPNP) {
  622. item = _mutableObjectList[i];
  623. nameRange = [[item title] rangeOfString:searchString options:NSCaseInsensitiveSearch];
  624. } else if (_serverType == kVLCServerTypeFTP) {
  625. NSString *rawObjectName = [_objectList[i] objectForKey:(id)kCFFTPResourceName];
  626. NSData *flippedData = [rawObjectName dataUsingEncoding:[[[NSUserDefaults standardUserDefaults] objectForKey:kVLCSettingFTPTextEncoding] intValue] allowLossyConversion:YES];
  627. NSString *properObjectName = [[NSString alloc] initWithData:flippedData encoding:NSUTF8StringEncoding];
  628. nameRange = [properObjectName rangeOfString:searchString options:NSCaseInsensitiveSearch];
  629. }
  630. if (nameRange.location != NSNotFound) {
  631. if (_serverType == kVLCServerTypeUPNP)
  632. [_searchData addObject:_mutableObjectList[i]];
  633. else
  634. [_searchData addObject:_objectList[i]];
  635. }
  636. }
  637. return YES;
  638. }
  639. - (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
  640. {
  641. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad)
  642. tableView.rowHeight = 80.0f;
  643. else
  644. tableView.rowHeight = 68.0f;
  645. tableView.backgroundColor = [UIColor blackColor];
  646. }
  647. @end