VLCHTTPDownloadViewController.m 6.0 KB

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