VLCAddMediaViewController.m 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. [self.openNetworkStreamButton addSubview:self.openURLView];
  100. } else {
  101. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  102. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
  103. [self _hideAnimated:YES];
  104. }
  105. }
  106. - (IBAction)downloadFromHTTPServer:(id)sender
  107. {
  108. //TODO
  109. }
  110. - (IBAction)showSettings:(id)sender
  111. {
  112. if (!self.settingsViewController)
  113. self.settingsViewController = [[VLCSettingsViewController alloc] initWithNibName:@"VLCSettingsViewController" bundle:nil];
  114. [self _hideAnimated:NO];
  115. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  116. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  117. [appDelegate.playlistViewController.navigationController presentModalViewController:self.settingsViewController animated:YES];
  118. }
  119. - (NSString *)_currentIPAddress
  120. {
  121. NSString *address = @"";
  122. struct ifaddrs *interfaces = NULL;
  123. struct ifaddrs *temp_addr = NULL;
  124. int success = getifaddrs(&interfaces);
  125. if (success == 0) {
  126. temp_addr = interfaces;
  127. while(temp_addr != NULL) {
  128. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  129. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  130. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  131. }
  132. temp_addr = temp_addr->ifa_next;
  133. }
  134. }
  135. // Free memory
  136. freeifaddrs(interfaces);
  137. return address;
  138. }
  139. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  140. {
  141. _uploadController = [[VLCHTTPUploaderController alloc] init];
  142. [_uploadController changeHTTPServerState: sender.on];
  143. HTTPServer *server = _uploadController.httpServer;
  144. if (server.isRunning)
  145. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  146. else
  147. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  148. }
  149. @end