VLCDownloadViewController.m 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // VLCDownloadViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 16.06.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 "VLCDownloadViewController.h"
  11. #import "VLCHTTPFileDownloader.h"
  12. #import "VLCAppDelegate.h"
  13. #import "UIBarButtonItem+Theme.h"
  14. #import "WhiteRaccoon.h"
  15. #import "NSString+SupportedMedia.h"
  16. #define kVLCDownloadViaHTTP 1
  17. #define kVLCDownloadViaFTP 2
  18. @interface VLCDownloadViewController () <WRRequestDelegate>
  19. {
  20. NSMutableArray *_currentDownloads;
  21. NSUInteger _currentDownloadType;
  22. NSString *_humanReadableFilename;
  23. VLCHTTPFileDownloader *_httpDownloader;
  24. WRRequestDownload *_FTPDownloadRequest;
  25. }
  26. @end
  27. @implementation VLCDownloadViewController
  28. - (void)viewDidLoad
  29. {
  30. [self.downloadButton setTitle:NSLocalizedString(@"BUTTON_DOWNLOAD",@"") forState:UIControlStateNormal];
  31. _currentDownloads = [[NSMutableArray alloc] init];
  32. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  33. self.title = NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"");
  34. self.whatToDownloadHelpLabel.text = [NSString stringWithFormat:NSLocalizedString(@"DOWNLOAD_FROM_HTTP_HELP", @""), [[UIDevice currentDevice] model]];
  35. [super viewDidLoad];
  36. }
  37. - (void)viewWillAppear:(BOOL)animated
  38. {
  39. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  40. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  41. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  42. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  43. pasteURL = [NSURL URLWithString:pasteString];
  44. }
  45. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  46. self.urlField.text = [pasteURL absoluteString];
  47. }
  48. [super viewWillAppear:animated];
  49. }
  50. #pragma mark - UI interaction
  51. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  52. {
  53. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  54. return NO;
  55. return YES;
  56. }
  57. - (IBAction)goBack:(id)sender
  58. {
  59. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  60. }
  61. - (IBAction)downloadAction:(id)sender
  62. {
  63. if ([self.urlField.text length] > 0) {
  64. NSURL *URLtoSave = [NSURL URLWithString:self.urlField.text];
  65. if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"] || [URLtoSave.scheme isEqualToString:@"ftp"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
  66. if ([URLtoSave.lastPathComponent isSupportedFormat]) {
  67. [_currentDownloads addObject:URLtoSave];
  68. self.urlField.text = @"";
  69. [self.downloadsTable reloadData];
  70. [self _triggerNextDownload];
  71. } else {
  72. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", @"") message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED", @""), URLtoSave.lastPathComponent] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  73. [alert show];
  74. }
  75. }
  76. }
  77. }
  78. #pragma mark - download management
  79. - (void)_triggerNextDownload
  80. {
  81. if (_currentDownloads.count > 0) {
  82. NSString *downloadScheme = [_currentDownloads[0] scheme];
  83. if ([downloadScheme isEqualToString:@"http"] || [downloadScheme isEqualToString:@"https"]) {
  84. if (!_httpDownloader) {
  85. _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
  86. _httpDownloader.delegate = self;
  87. }
  88. if (!_httpDownloader.downloadInProgress) {
  89. _currentDownloadType = kVLCDownloadViaHTTP;
  90. [_httpDownloader downloadFileFromURL:_currentDownloads[0]];
  91. _humanReadableFilename = _httpDownloader.userReadableDownloadName;
  92. }
  93. } else if ([downloadScheme isEqualToString:@"ftp"]) {
  94. _currentDownloadType = kVLCDownloadViaFTP;
  95. [self _downloadFTPFile:_currentDownloads[0]];
  96. _humanReadableFilename = [_currentDownloads[0] lastPathComponent];
  97. } else
  98. APLog(@"Unknown download scheme '%@'", downloadScheme);
  99. [self.activityIndicator startAnimating];
  100. [_currentDownloads removeObjectAtIndex:0];
  101. [self.downloadsTable reloadData];
  102. } else
  103. _currentDownloadType = 0;
  104. }
  105. - (IBAction)cancelDownload:(id)sender
  106. {
  107. if (_currentDownloadType == kVLCDownloadViaHTTP) {
  108. if (_httpDownloader.downloadInProgress)
  109. [_httpDownloader cancelDownload];
  110. }
  111. }
  112. #pragma mark - VLC HTTP Downloader delegate
  113. - (void)downloadStarted
  114. {
  115. [self.activityIndicator stopAnimating];
  116. self.currentDownloadLabel.text = _humanReadableFilename;
  117. self.progressView.progress = 0.;
  118. self.currentDownloadLabel.hidden = NO;
  119. self.progressView.hidden = NO;
  120. self.cancelButton.hidden = NO;
  121. APLog(@"download started");
  122. }
  123. - (void)downloadEnded
  124. {
  125. self.currentDownloadLabel.hidden = YES;
  126. self.progressView.hidden = YES;
  127. self.cancelButton.hidden = YES;
  128. APLog(@"download ended");
  129. [self _triggerNextDownload];
  130. }
  131. - (void)downloadFailedWithErrorDescription:(NSString *)description
  132. {
  133. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DOWNLOAD_FAILED", @"") message:description delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  134. [alert show];
  135. }
  136. - (void)progressUpdatedTo:(CGFloat)percentage
  137. {
  138. [self.progressView setProgress:percentage animated:YES];
  139. }
  140. #pragma mark - ftp networking
  141. - (void)_downloadFTPFile:(NSURL *)URLToFile
  142. {
  143. if (_FTPDownloadRequest)
  144. return;
  145. _FTPDownloadRequest = [[WRRequestDownload alloc] init];
  146. _FTPDownloadRequest.delegate = self;
  147. _FTPDownloadRequest.passive = YES;
  148. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  149. NSString *directoryPath = searchPaths[0];
  150. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, URLToFile.lastPathComponent]];
  151. _FTPDownloadRequest.downloadLocation = destinationURL;
  152. [_FTPDownloadRequest startWithFullURL:URLToFile];
  153. }
  154. - (void)requestStarted:(WRRequest *)request
  155. {
  156. [self downloadStarted];
  157. }
  158. - (void)requestCompleted:(WRRequest *)request
  159. {
  160. _FTPDownloadRequest = nil;
  161. [self downloadEnded];
  162. }
  163. - (void)requestFailed:(WRRequest *)request
  164. {
  165. _FTPDownloadRequest = nil;
  166. [self downloadEnded];
  167. 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];
  168. [alert show];
  169. }
  170. #pragma mark - table view data source
  171. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  172. {
  173. return 1;
  174. }
  175. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  176. {
  177. return _currentDownloads.count;
  178. }
  179. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  180. {
  181. static NSString *CellIdentifier = @"ScheduledDownloadsCell";
  182. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  183. if (cell == nil) {
  184. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  185. cell.textLabel.textColor = [UIColor whiteColor];
  186. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  187. }
  188. NSInteger row = indexPath.row;
  189. cell.textLabel.text = [_currentDownloads[row] lastPathComponent];
  190. cell.detailTextLabel.text = [_currentDownloads[row] absoluteString];
  191. return cell;
  192. }
  193. #pragma mark - table view delegate
  194. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  195. {
  196. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  197. }
  198. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  199. {
  200. return YES;
  201. }
  202. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  203. {
  204. if (editingStyle == UITableViewCellEditingStyleDelete) {
  205. [_currentDownloads removeObjectAtIndex:indexPath.row];
  206. [tableView reloadData];
  207. }
  208. }
  209. #pragma mark - communication with other VLC objects
  210. - (void)addURLToDownloadList:(NSURL *)aURL
  211. {
  212. [_currentDownloads addObject:aURL];
  213. [self _triggerNextDownload];
  214. }
  215. @end