VLCDownloadViewController.m 10 KB

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