VLCLocalServerFolderListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. /*****************************************************************************
  2. * VLCLocalServerFolderListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. *
  11. * Refer to the COPYING file of the official project for license.
  12. *****************************************************************************/
  13. #import "VLCLocalServerFolderListViewController.h"
  14. #import "MediaServerBasicObjectParser.h"
  15. #import "MediaServer1ItemObject.h"
  16. #import "MediaServer1ContainerObject.h"
  17. #import "MediaServer1Device.h"
  18. #import "VLCLocalNetworkListCell.h"
  19. #import "VLCAppDelegate.h"
  20. #import "VLCPlaylistViewController.h"
  21. #import "UINavigationController+Theme.h"
  22. #import "VLCDownloadViewController.h"
  23. #import "WhiteRaccoon.h"
  24. #import "NSString+SupportedMedia.h"
  25. #import "VLCStatusLabel.h"
  26. #define kVLCServerTypeUPNP 0
  27. #define kVLCServerTypeFTP 1
  28. @interface VLCLocalServerFolderListViewController () <UITableViewDataSource, UITableViewDelegate, WRRequestDelegate, VLCLocalNetworkListCell>
  29. {
  30. /* UI */
  31. UIBarButtonItem *_backButton;
  32. /* generic data storage */
  33. NSString *_listTitle;
  34. NSArray *_objectList;
  35. NSMutableArray *_mutableObjectList;
  36. NSUInteger _serverType;
  37. /* UPNP specifics */
  38. MediaServer1Device *_UPNPdevice;
  39. NSString *_UPNProotID;
  40. /* FTP specifics */
  41. NSString *_ftpServerAddress;
  42. NSString *_ftpServerUserName;
  43. NSString *_ftpServerPassword;
  44. NSString *_ftpServerPath;
  45. WRRequestListDirectory *_FTPListDirRequest;
  46. }
  47. @end
  48. @implementation VLCLocalServerFolderListViewController
  49. - (void)loadView
  50. {
  51. _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
  52. _tableView.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  53. _tableView.delegate = self;
  54. _tableView.dataSource = self;
  55. _tableView.rowHeight = [VLCLocalNetworkListCell heightOfCell];
  56. self.view = _tableView;
  57. }
  58. - (id)initWithUPNPDevice:(MediaServer1Device*)device header:(NSString*)header andRootID:(NSString*)rootID
  59. {
  60. self = [super init];
  61. if (self) {
  62. _UPNPdevice = device;
  63. _listTitle = header;
  64. _UPNProotID = rootID;
  65. _serverType = kVLCServerTypeUPNP;
  66. _mutableObjectList = [[NSMutableArray alloc] init];
  67. }
  68. return self;
  69. }
  70. - (id)initWithFTPServer:(NSString *)serverAddress userName:(NSString *)username andPassword:(NSString *)password atPath:(NSString *)path
  71. {
  72. self = [super init];
  73. if (self) {
  74. _ftpServerAddress = serverAddress;
  75. _ftpServerUserName = username;
  76. _ftpServerPassword = password;
  77. _ftpServerPath = path;
  78. _serverType = kVLCServerTypeFTP;
  79. }
  80. return self;
  81. }
  82. - (void)viewDidLoad
  83. {
  84. [super viewDidLoad];
  85. if (_serverType == kVLCServerTypeUPNP) {
  86. NSMutableString *outResult = [[NSMutableString alloc] init];
  87. NSMutableString *outNumberReturned = [[NSMutableString alloc] init];
  88. NSMutableString *outTotalMatches = [[NSMutableString alloc] init];
  89. NSMutableString *outUpdateID = [[NSMutableString alloc] init];
  90. [[_UPNPdevice contentDirectory] BrowseWithObjectID:_UPNProotID BrowseFlag:@"BrowseDirectChildren" Filter:@"*" StartingIndex:@"0" RequestedCount:@"0" SortCriteria:@"+dc:title" OutResult:outResult OutNumberReturned:outNumberReturned OutTotalMatches:outTotalMatches OutUpdateID:outUpdateID];
  91. [_mutableObjectList removeAllObjects];
  92. NSData *didl = [outResult dataUsingEncoding:NSUTF8StringEncoding];
  93. MediaServerBasicObjectParser *parser = [[MediaServerBasicObjectParser alloc] initWithMediaObjectArray:_mutableObjectList itemsOnly:NO];
  94. [parser parseFromData:didl];
  95. } else if (_serverType == kVLCServerTypeFTP) {
  96. if ([_ftpServerPath isEqualToString:@"/"])
  97. _listTitle = _ftpServerAddress;
  98. else
  99. _listTitle = [_ftpServerPath lastPathComponent];
  100. [self _listFTPDirectory];
  101. }
  102. self.tableView.separatorColor = [UIColor colorWithWhite:.122 alpha:1.];
  103. self.view.backgroundColor = [UIColor colorWithWhite:.122 alpha:1.];
  104. self.title = _listTitle;
  105. }
  106. - (BOOL)shouldAutorotate
  107. {
  108. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  109. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  110. return NO;
  111. return YES;
  112. }
  113. #pragma mark - Table view data source
  114. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  115. {
  116. return 1;
  117. }
  118. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  119. {
  120. if (_serverType == kVLCServerTypeUPNP)
  121. return _mutableObjectList.count;
  122. return _objectList.count;
  123. }
  124. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  125. {
  126. static NSString *CellIdentifier = @"LocalNetworkCellDetail";
  127. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  128. if (cell == nil)
  129. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  130. if (_serverType == kVLCServerTypeUPNP) {
  131. MediaServer1BasicObject *item = _mutableObjectList[indexPath.row];
  132. if (![item isContainer]) {
  133. MediaServer1ItemObject *mediaItem = _mutableObjectList[indexPath.row];
  134. [cell setSubtitle: [NSString stringWithFormat:@"%0.2f MB (%@)", (float)([mediaItem.size intValue] / 1e6), mediaItem.duration]];
  135. [cell setIsDirectory:NO];
  136. cell.isDownloadable = YES;
  137. if (![mediaItem.albumArt isEqualToString:NULL]) {
  138. NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mediaItem.albumArt]];
  139. UIImage* image = [[UIImage alloc] initWithData:imageData];
  140. [cell setIcon:image];
  141. }
  142. else
  143. [cell setIcon:[UIImage imageNamed:@"blank"]];
  144. cell.delegate = self;
  145. } else {
  146. [cell setIsDirectory:YES];
  147. [cell setIcon:[UIImage imageNamed:@"folder"]];
  148. }
  149. [cell setTitle:[item title]];
  150. } else if (_serverType == kVLCServerTypeFTP) {
  151. cell.title = [_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  152. if ([[_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  153. cell.isDirectory = YES;
  154. cell.icon = [UIImage imageNamed:@"folder"];
  155. } else {
  156. cell.isDirectory = NO;
  157. cell.icon = [UIImage imageNamed:@"blank"];
  158. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([[_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceSize] intValue] / 1e6)];
  159. cell.isDownloadable = YES;
  160. cell.delegate = self;
  161. }
  162. }
  163. return cell;
  164. }
  165. #pragma mark - Table view delegate
  166. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  169. }
  170. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  171. {
  172. if (_serverType == kVLCServerTypeUPNP) {
  173. MediaServer1BasicObject *item = _mutableObjectList[indexPath.row];
  174. if ([item isContainer]) {
  175. MediaServer1ContainerObject *container = _mutableObjectList[indexPath.row];
  176. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:_UPNPdevice header:[container title] andRootID:[container objectID]];
  177. [[self navigationController] pushViewController:targetViewController animated:YES];
  178. } else {
  179. MediaServer1ItemObject *item = _mutableObjectList[indexPath.row];
  180. MediaServer1ItemRes *resource = nil;
  181. NSEnumerator *e = [[item resources] objectEnumerator];
  182. NSURL *itemURL;
  183. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  184. APLog(@"%@ - %d, %@, %d, %lld, %d, %@ (%@)", [item title], [resource bitrate], [resource duration], [resource nrAudioChannels], [resource size], [resource durationInSeconds], [resource protocolInfo], [item uri]);
  185. itemURL = [NSURL URLWithString:[item uri]];
  186. }
  187. if (itemURL && ([itemURL.scheme isEqualToString:@"http"] || [itemURL.scheme isEqualToString:@"rtsp"] || [itemURL.scheme isEqualToString:@"rtp"] || [itemURL.scheme isEqualToString:@"mms"] || [itemURL.scheme isEqualToString:@"mmsh"])) {
  188. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  189. [appDelegate openMovieFromURL:itemURL];
  190. }
  191. }
  192. } else if (_serverType == kVLCServerTypeFTP) {
  193. if ([[_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  194. NSString *newPath = [NSString stringWithFormat:@"%@/%@", _ftpServerPath, [_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceName]];
  195. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithFTPServer:_ftpServerAddress userName:_ftpServerUserName andPassword:_ftpServerPassword atPath:newPath];
  196. [self.navigationController pushViewController:targetViewController animated:YES];
  197. } else {
  198. NSString *objectName = [_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  199. if (![objectName isSupportedFormat]) {
  200. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), objectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  201. [alert show];
  202. } else
  203. [self _openURLStringAndDismiss:[_FTPListDirRequest.fullURLString stringByAppendingString:objectName]];
  204. }
  205. }
  206. [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
  207. }
  208. #pragma mark - FTP specifics
  209. - (void)_listFTPDirectory
  210. {
  211. if (_FTPListDirRequest)
  212. return;
  213. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  214. _FTPListDirRequest.delegate = self;
  215. _FTPListDirRequest.hostname = _ftpServerAddress;
  216. _FTPListDirRequest.username = _ftpServerUserName;
  217. _FTPListDirRequest.password = _ftpServerPassword;
  218. _FTPListDirRequest.path = _ftpServerPath;
  219. _FTPListDirRequest.passive = YES;
  220. [_FTPListDirRequest start];
  221. }
  222. - (NSString *)_credentials
  223. {
  224. NSString * cred;
  225. if (_ftpServerUserName.length > 0) {
  226. if (_ftpServerPassword.length > 0)
  227. cred = [NSString stringWithFormat:@"%@:%@@", _ftpServerUserName, _ftpServerPassword];
  228. else
  229. cred = [NSString stringWithFormat:@"%@@", _ftpServerPassword];
  230. } else
  231. cred = @"";
  232. return [cred stringByStandardizingPath];
  233. }
  234. - (void)_downloadFTPFile:(NSString *)fileName
  235. {
  236. NSURL *URLToQueue = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  237. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:URLToQueue fileNameOfMedia:nil];
  238. }
  239. - (void)_downloadUPNPFile:(NSURL *)url fileNameOfMedia:(NSString*) fileName;
  240. {
  241. fileName = [[fileName stringByAppendingString:@"."] stringByAppendingString:[[url absoluteString] pathExtension]];
  242. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:url fileNameOfMedia:fileName];
  243. }
  244. - (void)requestCompleted:(WRRequest *)request
  245. {
  246. if (request == _FTPListDirRequest) {
  247. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  248. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  249. NSUInteger count = rawList.count;
  250. for (NSUInteger x = 0; x < count; x++) {
  251. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  252. [filteredList addObject:rawList[x]];
  253. }
  254. _objectList = [NSArray arrayWithArray:filteredList];
  255. [self.tableView reloadData];
  256. } else
  257. APLog(@"unknown request %@ completed", request);
  258. }
  259. - (void)requestFailed:(WRRequest *)request
  260. {
  261. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:NSLocalizedString(@"ERROR_NUMBER", @""), request.error.errorCode] message:request.error.message delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  262. [alert show];
  263. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  264. }
  265. #pragma mark - VLCLocalNetworkListCell delegation
  266. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  267. {
  268. if (_serverType == kVLCServerTypeUPNP) {
  269. MediaServer1ItemObject *item = _mutableObjectList[[self.tableView indexPathForCell:cell].row];
  270. MediaServer1ItemRes *resource = nil;
  271. NSEnumerator *e = [[item resources] objectEnumerator];
  272. NSURL *itemURL;
  273. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  274. itemURL = [NSURL URLWithString:[[item uri] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  275. }
  276. if (![[item uri] isSupportedFormat]) {
  277. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), [item uri]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  278. [alert show];
  279. } else {
  280. [self _downloadUPNPFile:itemURL fileNameOfMedia:[item title]];
  281. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  282. }
  283. }else if (_serverType == kVLCServerTypeFTP) {
  284. NSString *objectName = [_objectList[[self.tableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  285. if (![objectName isSupportedFormat]) {
  286. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", @""), objectName] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  287. [alert show];
  288. } else {
  289. [self _downloadFTPFile:objectName];
  290. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  291. }
  292. }
  293. }
  294. #pragma mark - communication with playback engine
  295. - (void)_openURLStringAndDismiss:(NSString *)url
  296. {
  297. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  298. [appDelegate openMovieFromURL:[NSURL URLWithString:url]];
  299. }
  300. @end