VLCLocalServerFolderListViewController.m 13 KB

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