VLCLocalServerFolderListViewController.m 35 KB

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