VLCHTTPDownloadViewController.m 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // VLCHTTPDownloadViewController.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 "VLCHTTPDownloadViewController.h"
  11. #import "VLCHTTPFileDownloader.h"
  12. #import "UIBarButtonItem+Theme.h"
  13. @interface VLCHTTPDownloadViewController ()
  14. {
  15. VLCHTTPFileDownloader *_httpDownloader;
  16. NSMutableArray *_currentDownloads;
  17. }
  18. @end
  19. @implementation VLCHTTPDownloadViewController
  20. - (void)viewDidLoad
  21. {
  22. [self.downloadButton setTitle:NSLocalizedString(@"BUTTON_DOWNLOAD",@"") forState:UIControlStateNormal];
  23. _currentDownloads = [[NSMutableArray alloc] init];
  24. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  25. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedBackButtonWithTarget:self andSelector:@selector(goBack:)];
  26. self.title = NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"");
  27. [super viewDidLoad];
  28. }
  29. - (void)viewWillAppear:(BOOL)animated
  30. {
  31. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  32. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  33. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  34. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  35. pasteURL = [NSURL URLWithString:pasteString];
  36. }
  37. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  38. self.urlField.text = [pasteURL absoluteString];
  39. }
  40. [super viewWillAppear:animated];
  41. }
  42. #pragma mark - UI interaction
  43. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
  44. {
  45. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  46. return NO;
  47. return YES;
  48. }
  49. - (IBAction)goBack:(id)sender
  50. {
  51. [self.navigationController popViewControllerAnimated:YES];
  52. }
  53. - (IBAction)downloadAction:(id)sender
  54. {
  55. if ([self.urlField.text length] > 0) {
  56. NSURL *URLtoSave = [NSURL URLWithString:self.urlField.text];
  57. if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
  58. if (!_httpDownloader) {
  59. _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
  60. _httpDownloader.delegate = self;
  61. }
  62. [_currentDownloads addObject:URLtoSave];
  63. self.urlField.text = @"";
  64. [self.downloadsTable reloadData];
  65. [self _triggerNextDownload];
  66. }
  67. }
  68. }
  69. #pragma mark - download management
  70. - (void)_triggerNextDownload
  71. {
  72. if (!_httpDownloader.downloadInProgress && _currentDownloads.count > 0) {
  73. [_httpDownloader downloadFileFromURL:_currentDownloads[0]];
  74. [self.activityIndicator startAnimating];
  75. [_currentDownloads removeObjectAtIndex:0];
  76. [self.downloadsTable reloadData];
  77. }
  78. }
  79. - (IBAction)cancelDownload:(id)sender
  80. {
  81. if (_httpDownloader.downloadInProgress)
  82. [_httpDownloader cancelDownload];
  83. }
  84. #pragma mark - VLC HTTP Downloader delegate
  85. - (void)downloadStarted
  86. {
  87. [self.activityIndicator stopAnimating];
  88. self.currentDownloadLabel.text = _httpDownloader.userReadableDownloadName;
  89. self.progressView.progress = 0.;
  90. self.currentDownloadLabel.hidden = NO;
  91. self.progressView.hidden = NO;
  92. self.cancelButton.hidden = NO;
  93. APLog(@"download started");
  94. }
  95. - (void)downloadEnded
  96. {
  97. self.currentDownloadLabel.hidden = YES;
  98. self.progressView.hidden = YES;
  99. self.cancelButton.hidden = YES;
  100. APLog(@"download ended");
  101. [self _triggerNextDownload];
  102. }
  103. - (void)downloadFailedWithErrorDescription:(NSString *)description
  104. {
  105. APLog(@"download failed: %@", description);
  106. }
  107. - (void)progressUpdatedTo:(CGFloat)percentage
  108. {
  109. [self.progressView setProgress:percentage animated:YES];
  110. }
  111. #pragma mark - table view data source
  112. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  113. {
  114. return 1;
  115. }
  116. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  117. {
  118. return _currentDownloads.count;
  119. }
  120. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  121. {
  122. static NSString *CellIdentifier = @"ScheduledDownloadsCell";
  123. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  124. if (cell == nil) {
  125. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  126. cell.textLabel.textColor = [UIColor whiteColor];
  127. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  128. }
  129. NSInteger row = indexPath.row;
  130. cell.textLabel.text = [_currentDownloads[row] lastPathComponent];
  131. cell.detailTextLabel.text = [_currentDownloads[row] absoluteString];
  132. return cell;
  133. }
  134. #pragma mark - table view delegate
  135. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  138. }
  139. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  140. {
  141. return YES;
  142. }
  143. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  144. {
  145. if (editingStyle == UITableViewCellEditingStyleDelete) {
  146. [_currentDownloads removeObjectAtIndex:indexPath.row];
  147. [tableView reloadData];
  148. }
  149. }
  150. @end