VLCLocalServerFolderListViewController.m 13 KB

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