VLCHTTPDownloadViewController.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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:@"Download" forState:UIControlStateNormal]; //FIXME l10n
  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 _triggerNextDownload];
  58. }
  59. }
  60. }
  61. #pragma mark - download management
  62. - (void)_triggerNextDownload
  63. {
  64. if (!_httpDownloader.downloadInProgress && _currentDownloads.count > 0) {
  65. [_httpDownloader downloadFileFromURL:_currentDownloads[0]];
  66. [self.activityIndicator startAnimating];
  67. [_currentDownloads removeObjectAtIndex:0];
  68. [self.downloadsTable reloadData];
  69. }
  70. }
  71. - (IBAction)cancelDownload:(id)sender
  72. {
  73. if (_httpDownloader.downloadInProgress)
  74. [_httpDownloader cancelDownload];
  75. }
  76. #pragma mark - VLC HTTP Downloader delegate
  77. - (void)downloadStarted
  78. {
  79. [self.activityIndicator stopAnimating];
  80. self.currentDownloadLabel.text = _httpDownloader.userReadableDownloadName;
  81. self.progressView.progress = 0.;
  82. self.currentDownloadLabel.hidden = NO;
  83. self.progressView.hidden = NO;
  84. self.cancelButton.hidden = NO;
  85. APLog(@"download started");
  86. }
  87. - (void)downloadEnded
  88. {
  89. self.currentDownloadLabel.hidden = YES;
  90. self.progressView.hidden = YES;
  91. self.cancelButton.hidden = YES;
  92. APLog(@"download ended");
  93. [self _triggerNextDownload];
  94. }
  95. - (void)downloadFailedWithErrorDescription:(NSString *)description
  96. {
  97. APLog(@"download failed: %@", description);
  98. }
  99. - (void)progressUpdatedTo:(CGFloat)percentage
  100. {
  101. [self.progressView setProgress:percentage animated:YES];
  102. }
  103. #pragma mark - table view data source
  104. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  105. {
  106. return 1;
  107. }
  108. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  109. {
  110. return _currentDownloads.count;
  111. }
  112. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  113. {
  114. static NSString *CellIdentifier = @"ScheduledDownloadsCell";
  115. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  116. if (cell == nil) {
  117. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  118. cell.textLabel.textColor = [UIColor whiteColor];
  119. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  120. }
  121. NSInteger row = indexPath.row;
  122. cell.textLabel.text = [_currentDownloads[row] lastPathComponent];
  123. cell.detailTextLabel.text = _currentDownloads[row];
  124. return cell;
  125. }
  126. #pragma mark - table view delegate
  127. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  128. {
  129. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  130. }
  131. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  132. {
  133. return YES;
  134. }
  135. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  136. {
  137. if (editingStyle == UITableViewCellEditingStyleDelete) {
  138. [_currentDownloads removeObjectAtIndex:indexPath.row];
  139. [tableView reloadData];
  140. }
  141. }
  142. @end