VLCLocalServerFolderListViewController.m 37 KB

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