VLCAddMediaViewController.m 8.5 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. _reachability = [Reachability reachabilityForLocalWiFi];
  47. [_reachability startNotifier];
  48. [self netReachabilityChanged:nil];
  49. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  50. }
  51. - (void)netReachabilityChanged:(NSNotification *)notification
  52. {
  53. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  54. self.httpUploadServerSwitch.enabled = YES;
  55. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  56. } else {
  57. self.httpUploadServerSwitch.enabled = NO;
  58. self.httpUploadServerSwitch.on = NO;
  59. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  60. }
  61. }
  62. - (void)viewWillAppear:(BOOL)animated
  63. {
  64. [self.openURLButton sizeToFit];
  65. if (self.openURLView.superview)
  66. [self.openURLView removeFromSuperview];
  67. [super viewWillAppear:animated];
  68. }
  69. - (void)_hideAnimated:(BOOL)animated
  70. {
  71. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  72. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  73. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  74. } else
  75. [self dismissViewControllerAnimated:animated completion:NULL];
  76. }
  77. - (IBAction)dismiss:(id)sender
  78. {
  79. [self _hideAnimated:YES];
  80. }
  81. - (IBAction)openAboutPanel:(id)sender
  82. {
  83. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  84. if (!appDelegate.playlistViewController.aboutViewController)
  85. appDelegate.playlistViewController.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  86. [appDelegate.playlistViewController.navigationController pushViewController:appDelegate.playlistViewController.aboutViewController animated:YES];
  87. [self _hideAnimated:NO];
  88. }
  89. - (IBAction)openNetworkStream:(id)sender
  90. {
  91. if (sender == self.openNetworkStreamButton) {
  92. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  93. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  94. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  95. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  96. pasteURL = [NSURL URLWithString:pasteString];
  97. }
  98. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  99. self.openURLField.text = [pasteURL absoluteString];
  100. }
  101. if (self.openURLView.superview)
  102. [self.openURLView removeFromSuperview];
  103. [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  104. [self.openURLButton addTarget:self action:@selector(openNetworkStream:) forControlEvents:UIControlEventTouchUpInside];
  105. [self.openNetworkStreamButton addSubview:self.openURLView];
  106. } else {
  107. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  108. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
  109. [self _hideAnimated:YES];
  110. }
  111. }
  112. - (IBAction)downloadFromHTTPServer:(id)sender
  113. {
  114. if (_httpDownloader) {
  115. if (_httpDownloader.downloadInProgress)
  116. return;
  117. }
  118. if (sender == self.downloadFromHTTPServerButton) {
  119. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  120. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  121. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  122. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  123. pasteURL = [NSURL URLWithString:pasteString];
  124. }
  125. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  126. self.openURLField.text = [pasteURL absoluteString];
  127. }
  128. if (self.openURLView.superview)
  129. [self.openURLView removeFromSuperview];
  130. [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  131. [self.openURLButton addTarget:self action:@selector(downloadFromHTTPServer:) forControlEvents:UIControlEventTouchUpInside];
  132. [self.downloadFromHTTPServerButton addSubview:self.openURLView];
  133. } else {
  134. NSURL *URLtoSave = [NSURL URLWithString:self.openURLField.text];
  135. if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
  136. if (!_httpDownloader) {
  137. _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
  138. _httpDownloader.mediaViewController = self;
  139. }
  140. [_httpDownloader downloadFileFromURL:URLtoSave];
  141. [self.openURLView removeFromSuperview];
  142. } else {
  143. APLog(@"URL is not a file download");
  144. [self _hideAnimated:YES];
  145. }
  146. }
  147. }
  148. - (IBAction)showSettings:(id)sender
  149. {
  150. if (!self.settingsViewController)
  151. self.settingsViewController = [[VLCSettingsViewController alloc] initWithNibName:@"VLCSettingsViewController" bundle:nil];
  152. [self _hideAnimated:NO];
  153. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  154. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  155. [appDelegate.playlistViewController.navigationController presentModalViewController:self.settingsViewController animated:YES];
  156. }
  157. - (NSString *)_currentIPAddress
  158. {
  159. NSString *address = @"";
  160. struct ifaddrs *interfaces = NULL;
  161. struct ifaddrs *temp_addr = NULL;
  162. int success = getifaddrs(&interfaces);
  163. if (success == 0) {
  164. temp_addr = interfaces;
  165. while(temp_addr != NULL) {
  166. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  167. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  168. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  169. }
  170. temp_addr = temp_addr->ifa_next;
  171. }
  172. }
  173. // Free memory
  174. freeifaddrs(interfaces);
  175. return address;
  176. }
  177. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  178. {
  179. _uploadController = [[VLCHTTPUploaderController alloc] init];
  180. [_uploadController changeHTTPServerState: sender.on];
  181. HTTPServer *server = _uploadController.httpServer;
  182. if (server.isRunning)
  183. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  184. else
  185. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  186. }
  187. @end