VLCLocalServerFolderListViewController.m 13 KB

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