VLCHTTPDownloadViewController.m 5.1 KB

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