VLCDownloadViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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_LONG", @""), 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. } else if (_currentDownloadType == kVLCDownloadViaFTP) {
  111. if (_FTPDownloadRequest) {
  112. NSURL *target = _FTPDownloadRequest.downloadLocation;
  113. [_FTPDownloadRequest destroy];
  114. [self requestCompleted:_FTPDownloadRequest];
  115. /* remove partially downloaded content */
  116. NSFileManager *fileManager = [NSFileManager defaultManager];
  117. if ([fileManager fileExistsAtPath:target.path])
  118. [fileManager removeItemAtPath:target.path error:nil];
  119. }
  120. }
  121. }
  122. #pragma mark - VLC HTTP Downloader delegate
  123. - (void)downloadStarted
  124. {
  125. [self.activityIndicator stopAnimating];
  126. self.currentDownloadLabel.text = _humanReadableFilename;
  127. self.progressView.progress = 0.;
  128. self.currentDownloadLabel.hidden = NO;
  129. self.progressView.hidden = NO;
  130. self.cancelButton.hidden = NO;
  131. APLog(@"download started");
  132. }
  133. - (void)downloadEnded
  134. {
  135. self.currentDownloadLabel.hidden = YES;
  136. self.progressView.hidden = YES;
  137. self.cancelButton.hidden = YES;
  138. APLog(@"download ended");
  139. [self _triggerNextDownload];
  140. }
  141. - (void)downloadFailedWithErrorDescription:(NSString *)description
  142. {
  143. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DOWNLOAD_FAILED", @"") message:description delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:nil];
  144. [alert show];
  145. }
  146. - (void)progressUpdatedTo:(CGFloat)percentage
  147. {
  148. [self.progressView setProgress:percentage animated:YES];
  149. }
  150. #pragma mark - ftp networking
  151. - (void)_downloadFTPFile:(NSURL *)URLToFile
  152. {
  153. if (_FTPDownloadRequest)
  154. return;
  155. _FTPDownloadRequest = [[WRRequestDownload alloc] init];
  156. _FTPDownloadRequest.delegate = self;
  157. _FTPDownloadRequest.passive = YES;
  158. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  159. NSString *directoryPath = searchPaths[0];
  160. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, URLToFile.lastPathComponent]];
  161. _FTPDownloadRequest.downloadLocation = destinationURL;
  162. [_FTPDownloadRequest startWithFullURL:URLToFile];
  163. }
  164. - (void)requestStarted:(WRRequest *)request
  165. {
  166. [self downloadStarted];
  167. }
  168. - (void)requestCompleted:(WRRequest *)request
  169. {
  170. _FTPDownloadRequest = nil;
  171. [self downloadEnded];
  172. }
  173. - (void)requestFailed:(WRRequest *)request
  174. {
  175. _FTPDownloadRequest = nil;
  176. [self downloadEnded];
  177. 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];
  178. [alert show];
  179. }
  180. #pragma mark - table view data source
  181. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  182. {
  183. return 1;
  184. }
  185. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  186. {
  187. return _currentDownloads.count;
  188. }
  189. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  190. {
  191. static NSString *CellIdentifier = @"ScheduledDownloadsCell";
  192. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  193. if (cell == nil) {
  194. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  195. cell.textLabel.textColor = [UIColor whiteColor];
  196. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  197. }
  198. NSInteger row = indexPath.row;
  199. cell.textLabel.text = [_currentDownloads[row] lastPathComponent];
  200. cell.detailTextLabel.text = [_currentDownloads[row] absoluteString];
  201. return cell;
  202. }
  203. #pragma mark - table view delegate
  204. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  205. {
  206. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  207. }
  208. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  209. {
  210. return YES;
  211. }
  212. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  213. {
  214. if (editingStyle == UITableViewCellEditingStyleDelete) {
  215. [_currentDownloads removeObjectAtIndex:indexPath.row];
  216. [tableView reloadData];
  217. }
  218. }
  219. #pragma mark - communication with other VLC objects
  220. - (void)addURLToDownloadList:(NSURL *)aURL
  221. {
  222. [_currentDownloads addObject:aURL];
  223. [self _triggerNextDownload];
  224. }
  225. @end