VLCLocalServerFolderListViewController.m 13 KB

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