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