VLCAddMediaViewController.m 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 <ifaddrs.h>
  18. #import <arpa/inet.h>
  19. @interface VLCAddMediaViewController () {
  20. VLCHTTPUploaderController *_uploadController;
  21. Reachability *_reachability;
  22. }
  23. @end
  24. @implementation VLCAddMediaViewController
  25. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  26. {
  27. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  28. return self;
  29. }
  30. - (void)dealloc
  31. {
  32. [_reachability stopNotifier];
  33. [[NSNotificationCenter defaultCenter] removeObserver:self];
  34. }
  35. - (void)viewDidLoad
  36. {
  37. [super viewDidLoad];
  38. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  39. [self.dismissButton setTitle:NSLocalizedString(@"BUTTON_DONE", @"") forState:UIControlStateNormal];
  40. [self.aboutButton setTitle:NSLocalizedString(@"ABOUT_APP", @"") forState:UIControlStateNormal];
  41. [self.openNetworkStreamButton setTitle:NSLocalizedString(@"OPEN_NETWORK", @"") forState:UIControlStateNormal];
  42. [self.downloadFromHTTPServerButton setTitle:NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"") forState:UIControlStateNormal];
  43. [self.openURLButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  44. _reachability = [Reachability reachabilityForLocalWiFi];
  45. [_reachability startNotifier];
  46. [self netReachabilityChanged:nil];
  47. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  48. }
  49. - (void)netReachabilityChanged:(NSNotification *)notification
  50. {
  51. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  52. self.httpUploadServerSwitch.enabled = YES;
  53. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  54. } else {
  55. self.httpUploadServerSwitch.enabled = NO;
  56. self.httpUploadServerSwitch.on = NO;
  57. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  58. }
  59. }
  60. - (void)viewWillAppear:(BOOL)animated
  61. {
  62. [self.openURLButton sizeToFit];
  63. if (self.openURLView.superview)
  64. [self.openURLView removeFromSuperview];
  65. [super viewWillAppear:animated];
  66. }
  67. - (void)_hideAnimated:(BOOL)animated
  68. {
  69. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  70. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  71. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  72. } else
  73. [self dismissViewControllerAnimated:animated completion:NULL];
  74. }
  75. - (IBAction)dismiss:(id)sender
  76. {
  77. [self _hideAnimated:YES];
  78. }
  79. - (IBAction)openAboutPanel:(id)sender
  80. {
  81. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  82. if (!appDelegate.playlistViewController.aboutViewController)
  83. appDelegate.playlistViewController.aboutViewController = [[VLCAboutViewController alloc] initWithNibName:@"VLCAboutViewController" bundle:nil];
  84. [appDelegate.playlistViewController.navigationController pushViewController:appDelegate.playlistViewController.aboutViewController animated:YES];
  85. [self _hideAnimated:NO];
  86. }
  87. - (IBAction)openNetworkStream:(id)sender
  88. {
  89. if (sender == self.openNetworkStreamButton) {
  90. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  91. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  92. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  93. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  94. pasteURL = [NSURL URLWithString:pasteString];
  95. }
  96. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  97. self.openURLField.text = [pasteURL absoluteString];
  98. }
  99. if (self.openURLView.superview)
  100. [self.openURLView removeFromSuperview];
  101. [self.openNetworkStreamButton addSubview:self.openURLView];
  102. } else {
  103. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  104. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
  105. [self _hideAnimated:YES];
  106. }
  107. }
  108. - (IBAction)downloadFromHTTPServer:(id)sender
  109. {
  110. if (sender == self.downloadFromHTTPServerButton) {
  111. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  112. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  113. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  114. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  115. pasteURL = [NSURL URLWithString:pasteString];
  116. }
  117. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  118. self.openURLField.text = [pasteURL absoluteString];
  119. }
  120. if (self.openURLView.superview)
  121. [self.openURLView removeFromSuperview];
  122. [self.downloadFromHTTPServerButton addSubview:self.openURLView];
  123. } else {
  124. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  125. NSURL *URLtoSave = [NSURL URLWithString:self.openURLField.text];
  126. if ([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"])
  127. [appDelegate application:[UIApplication sharedApplication] openURL:URLtoSave sourceApplication:@"self" annotation:nil];
  128. }
  129. }
  130. - (IBAction)showSettings:(id)sender
  131. {
  132. if (!self.settingsViewController)
  133. self.settingsViewController = [[VLCSettingsViewController alloc] initWithNibName:@"VLCSettingsViewController" bundle:nil];
  134. [self _hideAnimated:NO];
  135. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  136. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  137. [appDelegate.playlistViewController.navigationController presentModalViewController:self.settingsViewController animated:YES];
  138. }
  139. - (NSString *)_currentIPAddress
  140. {
  141. NSString *address = @"";
  142. struct ifaddrs *interfaces = NULL;
  143. struct ifaddrs *temp_addr = NULL;
  144. int success = getifaddrs(&interfaces);
  145. if (success == 0) {
  146. temp_addr = interfaces;
  147. while(temp_addr != NULL) {
  148. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  149. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  150. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  151. }
  152. temp_addr = temp_addr->ifa_next;
  153. }
  154. }
  155. // Free memory
  156. freeifaddrs(interfaces);
  157. return address;
  158. }
  159. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  160. {
  161. _uploadController = [[VLCHTTPUploaderController alloc] init];
  162. [_uploadController changeHTTPServerState: sender.on];
  163. HTTPServer *server = _uploadController.httpServer;
  164. if (server.isRunning)
  165. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  166. else
  167. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  168. }
  169. @end