VLCLocalServerFolderListViewController.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 kVLCUPNPFileServer 0
  24. #define kVLCFTPServer 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 = kVLCUPNPFileServer;
  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 = kVLCFTPServer;
  76. }
  77. return self;
  78. }
  79. - (void)viewDidLoad
  80. {
  81. [super viewDidLoad];
  82. if (_serverType == kVLCUPNPFileServer) {
  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 == kVLCFTPServer) {
  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)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  104. {
  105. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  106. return NO;
  107. return YES;
  108. }
  109. #pragma mark - Table view data source
  110. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  111. {
  112. return 1;
  113. }
  114. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  115. {
  116. if (_serverType == kVLCUPNPFileServer)
  117. return _mutableObjectList.count;
  118. return _objectList.count;
  119. }
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. static NSString *CellIdentifier = @"LocalNetworkCellDetail";
  123. VLCLocalNetworkListCell *cell = (VLCLocalNetworkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  124. if (cell == nil)
  125. cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
  126. if (_serverType == kVLCUPNPFileServer) {
  127. MediaServer1BasicObject *item = _mutableObjectList[indexPath.row];
  128. if (![item isContainer]) {
  129. MediaServer1ItemObject *mediaItem = _mutableObjectList[indexPath.row];
  130. [cell setSubtitle: [NSString stringWithFormat:@"%0.2f MB (%@)", (float)([mediaItem.size intValue] / 1e6), mediaItem.duration]];
  131. [cell setIsDirectory:NO];
  132. cell.isDownloadable = YES;
  133. if (![mediaItem.albumArt isEqualToString:NULL]) {
  134. NSData* imageData = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mediaItem.albumArt]];
  135. UIImage* image = [[UIImage alloc] initWithData:imageData];
  136. [cell setIcon:image];
  137. }
  138. else
  139. [cell setIcon:[UIImage imageNamed:@"blank"]];
  140. cell.delegate = self;
  141. } else {
  142. [cell setIsDirectory:YES];
  143. [cell setIcon:[UIImage imageNamed:@"folder"]];
  144. }
  145. [cell setTitle:[item title]];
  146. } else if (_serverType == kVLCFTPServer) {
  147. cell.title = [_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceName];
  148. if ([[_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceType] intValue] == 4) {
  149. cell.isDirectory = YES;
  150. cell.icon = [UIImage imageNamed:@"folder"];
  151. } else {
  152. cell.isDirectory = NO;
  153. cell.icon = [UIImage imageNamed:@"blank"];
  154. cell.subtitle = [NSString stringWithFormat:@"%0.2f MB", (float)([[_objectList[indexPath.row] objectForKey:(id)kCFFTPResourceSize] intValue] / 1e6)];
  155. cell.isDownloadable = YES;
  156. cell.delegate = self;
  157. }
  158. }
  159. return cell;
  160. }
  161. #pragma mark - Table view delegate
  162. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  163. {
  164. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  165. }
  166. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  167. {
  168. if (_serverType == kVLCUPNPFileServer) {
  169. MediaServer1BasicObject *item = _mutableObjectList[indexPath.row];
  170. if ([item isContainer]) {
  171. MediaServer1ContainerObject *container = _mutableObjectList[indexPath.row];
  172. VLCLocalServerFolderListViewController *targetViewController = [[VLCLocalServerFolderListViewController alloc] initWithUPNPDevice:_UPNPdevice header:[container title] andRootID:[container objectID]];
  173. [[self navigationController] pushViewController:targetViewController animated:YES];
  174. } else {
  175. MediaServer1ItemObject *item = _mutableObjectList[indexPath.row];
  176. MediaServer1ItemRes *resource = nil;
  177. NSEnumerator *e = [[item resources] objectEnumerator];
  178. NSURL *itemURL;
  179. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  180. APLog(@"%@ - %d, %@, %d, %lld, %d, %@ (%@)", [item title], [resource bitrate], [resource duration], [resource nrAudioChannels], [resource size], [resource durationInSeconds], [resource protocolInfo], [item uri]);
  181. itemURL = [NSURL URLWithString:[item uri]];
  182. }
  183. if (itemURL && ([itemURL.scheme isEqualToString:@"http"] || [itemURL.scheme isEqualToString:@"rtsp"] || [itemURL.scheme isEqualToString:@"rtp"] || [itemURL.scheme isEqualToString:@"mms"] || [itemURL.scheme isEqualToString:@"mmsh"])) {
  184. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  185. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:appDelegate.playlistViewController];
  186. [navController loadTheme];
  187. appDelegate.revealController.contentViewController = navController;
  188. [appDelegate.revealController toggleSidebar:NO duration:kGHRevealSidebarDefaultAnimationDuration];
  189. [appDelegate.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:itemURL afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  190. }
  191. }
  192. } else if (_serverType == kVLCFTPServer) {
  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. }
  207. #pragma mark - FTP specifics
  208. - (void)_listFTPDirectory
  209. {
  210. if (_FTPListDirRequest)
  211. return;
  212. _FTPListDirRequest = [[WRRequestListDirectory alloc] init];
  213. _FTPListDirRequest.delegate = self;
  214. _FTPListDirRequest.hostname = _ftpServerAddress;
  215. _FTPListDirRequest.username = _ftpServerUserName;
  216. _FTPListDirRequest.password = _ftpServerPassword;
  217. _FTPListDirRequest.path = _ftpServerPath;
  218. _FTPListDirRequest.passive = YES;
  219. [_FTPListDirRequest start];
  220. }
  221. - (NSString *)_credentials
  222. {
  223. NSString * cred;
  224. if (_ftpServerUserName.length > 0) {
  225. if (_ftpServerPassword.length > 0)
  226. cred = [NSString stringWithFormat:@"%@:%@@", _ftpServerUserName, _ftpServerPassword];
  227. else
  228. cred = [NSString stringWithFormat:@"%@@", _ftpServerPassword];
  229. } else
  230. cred = @"";
  231. return [cred stringByStandardizingPath];
  232. }
  233. - (void)_downloadFTPFile:(NSString *)fileName
  234. {
  235. NSURL *URLToQueue = [NSURL URLWithString:[[@"ftp" stringByAppendingFormat:@"://%@%@/%@/%@", [self _credentials], _ftpServerAddress, _ftpServerPath, fileName] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  236. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:URLToQueue fileNameOfMedia:nil];
  237. }
  238. - (void)_downloadUPNPFile:(NSURL *)url fileNameOfMedia:(NSString*) fileName;
  239. {
  240. fileName = [[fileName stringByAppendingString:@"."] stringByAppendingString:[[url absoluteString] pathExtension]];
  241. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate downloadViewController] addURLToDownloadList:url fileNameOfMedia:fileName];
  242. }
  243. - (void)requestCompleted:(WRRequest *)request
  244. {
  245. if (request == _FTPListDirRequest) {
  246. NSMutableArray *filteredList = [[NSMutableArray alloc] init];
  247. NSArray *rawList = [(WRRequestListDirectory*)request filesInfo];
  248. NSUInteger count = rawList.count;
  249. for (NSUInteger x = 0; x < count; x++) {
  250. if (![[rawList[x] objectForKey:(id)kCFFTPResourceName] hasPrefix:@"."])
  251. [filteredList addObject:rawList[x]];
  252. }
  253. _objectList = [NSArray arrayWithArray:filteredList];
  254. [self.tableView reloadData];
  255. } else
  256. APLog(@"unknown request %@ completed", request);
  257. }
  258. - (void)requestFailed:(WRRequest *)request
  259. {
  260. 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];
  261. [alert show];
  262. APLog(@"request %@ failed with error %i", request, request.error.errorCode);
  263. }
  264. #pragma mark - VLCLocalNetworkListCell delegation
  265. - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
  266. {
  267. if (_serverType == kVLCUPNPFileServer) {
  268. MediaServer1ItemObject *item = _mutableObjectList[[self.tableView indexPathForCell:cell].row];
  269. MediaServer1ItemRes *resource = nil;
  270. NSEnumerator *e = [[item resources] objectEnumerator];
  271. NSURL *itemURL;
  272. while((resource = (MediaServer1ItemRes*)[e nextObject])){
  273. itemURL = [NSURL URLWithString:[[item uri] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
  274. }
  275. if (![[item uri] isSupportedFormat]) {
  276. 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];
  277. [alert show];
  278. } else {
  279. [self _downloadUPNPFile:itemURL fileNameOfMedia:[item title]];
  280. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  281. }
  282. }else if (_serverType == kVLCFTPServer) {
  283. NSString *objectName = [_objectList[[self.tableView indexPathForCell:cell].row] objectForKey:(id)kCFFTPResourceName];
  284. if (![objectName isSupportedFormat]) {
  285. 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];
  286. [alert show];
  287. } else {
  288. [self _downloadFTPFile:objectName];
  289. [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", @"")];
  290. }
  291. }
  292. }
  293. #pragma mark - communication with playback engine
  294. - (void)_openURLStringAndDismiss:(NSString *)url
  295. {
  296. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  297. [appDelegate.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  298. [appDelegate.playlistViewController performSelector:@selector(openMovieFromURL:) withObject:[NSURL URLWithString:url] afterDelay:kGHRevealSidebarDefaultAnimationDuration];
  299. }
  300. @end