VLCDownloadViewController.m 9.8 KB

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