VLCLocalServerFolderListViewController.m 15 KB

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