VLCHTTPDownloadViewController.m 5.6 KB

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