VLCDownloadViewController.m 8.2 KB

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