VLCDownloadViewController.m 11 KB

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