VLCDownloadViewController.m 10 KB

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