VLCHTTPDownloadViewController.m 5.3 KB

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