VLCAddMediaViewController.m 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //
  2. // VLCAddMediaViewController.m
  3. // VLC for iOS
  4. //
  5. // Created by Felix Paul Kühne on 19.05.13.
  6. // Copyright (c) 2013 VideoLAN. All rights reserved.
  7. //
  8. #import "VLCAddMediaViewController.h"
  9. #import "VLCAppDelegate.h"
  10. #import "VLCPlaylistViewController.h"
  11. #import "VLCAboutViewController.h"
  12. #import "VLCMovieViewController.h"
  13. #import "VLCHTTPUploaderController.h"
  14. #import "VLCSettingsViewController.h"
  15. #import "HTTPServer.h"
  16. #import "Reachability.h"
  17. #import "VLCHTTPFileDownloader.h"
  18. #import <ifaddrs.h>
  19. #import <arpa/inet.h>
  20. @interface VLCAddMediaViewController () {
  21. VLCHTTPUploaderController *_uploadController;
  22. Reachability *_reachability;
  23. VLCHTTPFileDownloader *_httpDownloader;
  24. }
  25. @end
  26. @implementation VLCAddMediaViewController
  27. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  28. {
  29. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  30. return self;
  31. }
  32. - (void)dealloc
  33. {
  34. [_reachability stopNotifier];
  35. [[NSNotificationCenter defaultCenter] removeObserver:self];
  36. }
  37. - (void)viewDidLoad
  38. {
  39. [super viewDidLoad];
  40. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  41. [self.dismissButton setTitle:NSLocalizedString(@"BUTTON_DONE", @"") forState:UIControlStateNormal];
  42. [self.aboutButton setTitle:NSLocalizedString(@"ABOUT_APP", @"") forState:UIControlStateNormal];
  43. [self.openNetworkStreamButton setTitle:NSLocalizedString(@"OPEN_NETWORK", @"") forState:UIControlStateNormal];
  44. [self.downloadFromHTTPServerButton setTitle:NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"") forState:UIControlStateNormal];
  45. [self.openURLButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  46. self.httpUploadLabel.text = NSLocalizedString(@"HTTP_UPLOAD", @"");
  47. _reachability = [Reachability reachabilityForLocalWiFi];
  48. [_reachability startNotifier];
  49. [self netReachabilityChanged:nil];
  50. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  51. }
  52. - (void)netReachabilityChanged:(NSNotification *)notification
  53. {
  54. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  55. self.httpUploadServerSwitch.enabled = YES;
  56. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  57. } else {
  58. self.httpUploadServerSwitch.enabled = NO;
  59. self.httpUploadServerSwitch.on = NO;
  60. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  61. }
  62. }
  63. - (void)viewWillAppear:(BOOL)animated
  64. {
  65. [self.openURLButton sizeToFit];
  66. if (self.openURLView.superview)
  67. [self.openURLView removeFromSuperview];
  68. [super viewWillAppear:animated];
  69. }
  70. - (void)_hideAnimated:(BOOL)animated
  71. {
  72. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  73. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  74. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  75. } else
  76. [self dismissViewControllerAnimated:animated completion:NULL];
  77. }
  78. - (IBAction)dismiss:(id)sender
  79. {
  80. [self _hideAnimated:YES];
  81. }
  82. - (IBAction)openAboutPanel:(id)sender
  83. {
  84. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  85. if (!appDelegate.playlistViewController.aboutViewController)
  86. appDelegate.playlistViewController.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  87. [appDelegate.playlistViewController.navigationController pushViewController:appDelegate.playlistViewController.aboutViewController animated:YES];
  88. [self _hideAnimated:NO];
  89. }
  90. - (IBAction)openNetworkStream:(id)sender
  91. {
  92. if (sender == self.openNetworkStreamButton) {
  93. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  94. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  95. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  96. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  97. pasteURL = [NSURL URLWithString:pasteString];
  98. }
  99. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  100. self.openURLField.text = [pasteURL absoluteString];
  101. }
  102. if (self.openURLView.superview)
  103. [self.openURLView removeFromSuperview];
  104. [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  105. [self.openURLButton addTarget:self action:@selector(openNetworkStream:) forControlEvents:UIControlEventTouchUpInside];
  106. [self.openNetworkStreamButton addSubview:self.openURLView];
  107. } else {
  108. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  109. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
  110. [self _hideAnimated:YES];
  111. }
  112. }
  113. - (IBAction)downloadFromHTTPServer:(id)sender
  114. {
  115. if (_httpDownloader) {
  116. if (_httpDownloader.downloadInProgress)
  117. return;
  118. }
  119. if (sender == self.downloadFromHTTPServerButton) {
  120. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  121. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  122. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  123. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  124. pasteURL = [NSURL URLWithString:pasteString];
  125. }
  126. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  127. self.openURLField.text = [pasteURL absoluteString];
  128. }
  129. if (self.openURLView.superview)
  130. [self.openURLView removeFromSuperview];
  131. [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  132. [self.openURLButton addTarget:self action:@selector(downloadFromHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
  133. [self.downloadFromHTTPServerButton addSubview:self.openURLView];
  134. } else {
  135. NSURL *URLtoSave = [NSURL URLWithString:self.openURLField.text];
  136. if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
  137. if (!_httpDownloader) {
  138. _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
  139. _httpDownloader.mediaViewController = self;
  140. }
  141. [_httpDownloader downloadFileFromURL:URLtoSave];
  142. [self.openURLView removeFromSuperview];
  143. } else {
  144. APLog(@"URL is not a file download");
  145. [self _hideAnimated:YES];
  146. }
  147. }
  148. }
  149. - (IBAction)showSettings:(id)sender
  150. {
  151. if (!self.settingsViewController)
  152. self.settingsViewController = [[VLCSettingsViewController alloc] initWithNibName:@"VLCSettingsViewController" bundle:nil];
  153. [self _hideAnimated:NO];
  154. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  155. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  156. [appDelegate.playlistViewController.navigationController presentModalViewController:self.settingsViewController animated:YES];
  157. }
  158. - (NSString *)_currentIPAddress
  159. {
  160. NSString *address = @"";
  161. struct ifaddrs *interfaces = NULL;
  162. struct ifaddrs *temp_addr = NULL;
  163. int success = getifaddrs(&interfaces);
  164. if (success == 0) {
  165. temp_addr = interfaces;
  166. while(temp_addr != NULL) {
  167. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  168. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  169. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  170. }
  171. temp_addr = temp_addr->ifa_next;
  172. }
  173. }
  174. // Free memory
  175. freeifaddrs(interfaces);
  176. return address;
  177. }
  178. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  179. {
  180. _uploadController = [[VLCHTTPUploaderController alloc] init];
  181. [_uploadController changeHTTPServerState: sender.on];
  182. HTTPServer *server = _uploadController.httpServer;
  183. if (server.isRunning)
  184. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  185. else
  186. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  187. }
  188. @end