VLCDownloadViewController.m 11 KB

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