VLCMenuViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. //
  2. // VLCMenuViewController.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. // Refer to the COPYING file of the official project for license.
  9. //
  10. #import "VLCMenuViewController.h"
  11. #import "VLCAppDelegate.h"
  12. #import "VLCPlaylistViewController.h"
  13. #import "VLCAboutViewController.h"
  14. #import "VLCMovieViewController.h"
  15. #import "VLCHTTPUploaderController.h"
  16. #import "VLCSettingsController.h"
  17. #import "HTTPServer.h"
  18. #import "Reachability.h"
  19. #import "VLCHTTPFileDownloader.h"
  20. #import "IASKAppSettingsViewController.h"
  21. #import <ifaddrs.h>
  22. #import <arpa/inet.h>
  23. @interface VLCMenuViewController () {
  24. VLCHTTPUploaderController *_uploadController;
  25. Reachability *_reachability;
  26. VLCHTTPFileDownloader *_httpDownloader;
  27. }
  28. - (void)_presentOpenURLViewFromView:(UIView *)view forSelector:(SEL)selector;
  29. @end
  30. @implementation VLCMenuViewController
  31. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  32. {
  33. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  34. return self;
  35. }
  36. - (void)dealloc
  37. {
  38. [_reachability stopNotifier];
  39. [[NSNotificationCenter defaultCenter] removeObserver:self];
  40. }
  41. - (void)viewDidLoad
  42. {
  43. [super viewDidLoad];
  44. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  45. UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"BUTTON_DONE", @"")
  46. style:UIBarButtonItemStyleBordered
  47. target:self
  48. action:@selector(dismiss:)];
  49. [dismissButton setBackgroundImage:[UIImage imageNamed:@"doneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  50. [dismissButton setBackgroundImage:[UIImage imageNamed:@"doneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  51. [dismissButton setTitleTextAttributes:@{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} forState:UIControlStateNormal];
  52. dismissButton.width = 80.;
  53. self.navigationItem.rightBarButtonItem = dismissButton;
  54. self.scrollView.contentSize = self.view.frame.size;
  55. }
  56. [self.aboutButton setTitle:NSLocalizedString(@"ABOUT_APP", @"") forState:UIControlStateNormal];
  57. [self.openNetworkStreamButton setTitle:NSLocalizedString(@"OPEN_NETWORK", @"") forState:UIControlStateNormal];
  58. [self.downloadFromHTTPServerButton setTitle:NSLocalizedString(@"DOWNLOAD_FROM_HTTP", @"") forState:UIControlStateNormal];
  59. [self.openURLButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  60. self.httpUploadLabel.text = NSLocalizedString(@"HTTP_UPLOAD", @"");
  61. [self.settingsButton setTitle:NSLocalizedString(@"Settings", @"") forState:UIControlStateNormal]; // plain text key to keep compatibility with InAppSettingsKit's upstream
  62. _reachability = [Reachability reachabilityForLocalWiFi];
  63. [_reachability startNotifier];
  64. [self netReachabilityChanged:nil];
  65. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged:) name:kReachabilityChangedNotification object:nil];
  66. }
  67. - (void)netReachabilityChanged:(NSNotification *)notification
  68. {
  69. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  70. self.httpUploadServerSwitch.enabled = YES;
  71. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  72. } else {
  73. self.httpUploadServerSwitch.enabled = NO;
  74. self.httpUploadServerSwitch.on = NO;
  75. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  76. }
  77. }
  78. - (void)viewWillAppear:(BOOL)animated
  79. {
  80. [self.openURLButton sizeToFit];
  81. if (self.openURLView.superview)
  82. [self.openURLView removeFromSuperview];
  83. [super viewWillAppear:animated];
  84. }
  85. - (void)_hideAnimated:(BOOL)animated
  86. {
  87. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  88. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  89. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  90. } else
  91. [self dismissViewControllerAnimated:animated completion:NULL];
  92. }
  93. - (IBAction)dismiss:(id)sender
  94. {
  95. [self _hideAnimated:YES];
  96. }
  97. - (IBAction)openAboutPanel:(id)sender
  98. {
  99. UIViewController *aboutController = [[VLCAboutViewController alloc] initWithNibName:nil bundle:nil];
  100. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:aboutController];
  101. navController.navigationBar.barStyle = UIBarStyleBlack;
  102. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  103. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  104. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  105. [self presentModalViewController:navController animated:YES];
  106. }
  107. - (IBAction)openNetworkStream:(id)sender
  108. {
  109. if (sender == self.openNetworkStreamButton) {
  110. [self _presentOpenURLViewFromView:self.openNetworkStreamButton forSelector:@selector(openNetworkStream:)];
  111. } else {
  112. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  113. [appDelegate.playlistViewController openMovieFromURL:[NSURL URLWithString:self.openURLField.text]];
  114. [self _hideAnimated:YES];
  115. }
  116. }
  117. - (IBAction)downloadFromHTTPServer:(id)sender
  118. {
  119. if (_httpDownloader) {
  120. if (_httpDownloader.downloadInProgress)
  121. return;
  122. }
  123. if (sender == self.downloadFromHTTPServerButton) {
  124. [self _presentOpenURLViewFromView:self.downloadFromHTTPServerButton forSelector:@selector(downloadFromHTTPServer:)];
  125. } else {
  126. NSURL *URLtoSave = [NSURL URLWithString:self.openURLField.text];
  127. if (([URLtoSave.scheme isEqualToString:@"http"] || [URLtoSave.scheme isEqualToString:@"https"]) && ![URLtoSave.lastPathComponent.pathExtension isEqualToString:@""]) {
  128. if (!_httpDownloader) {
  129. _httpDownloader = [[VLCHTTPFileDownloader alloc] init];
  130. _httpDownloader.mediaViewController = self;
  131. }
  132. [_httpDownloader downloadFileFromURL:URLtoSave];
  133. [self.openURLView removeFromSuperview];
  134. } else {
  135. APLog(@"URL is not a file download");
  136. [self _hideAnimated:YES];
  137. }
  138. }
  139. }
  140. - (IBAction)showSettings:(id)sender
  141. {
  142. if (!self.settingsViewController)
  143. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  144. if (!self.settingsController)
  145. self.settingsController = [[VLCSettingsController alloc] init];
  146. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  147. self.settingsViewController.delegate = self.settingsController;
  148. self.settingsViewController.showDoneButton = YES;
  149. self.settingsViewController.showCreditsFooter = NO;
  150. self.settingsController.viewController = self.settingsViewController;
  151. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.settingsViewController];
  152. navController.navigationBarHidden = NO;
  153. navController.navigationBar.barStyle = UIBarStyleBlack;
  154. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  155. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  156. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  157. [self presentModalViewController:navController animated:YES];
  158. UIBarButtonItem *doneButton = self.settingsViewController.navigationItem.rightBarButtonItem;
  159. [doneButton setBackgroundImage:[UIImage imageNamed:@"doneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  160. [doneButton setBackgroundImage:[UIImage imageNamed:@"doneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  161. doneButton.style = UIBarButtonItemStyleBordered;
  162. [doneButton setTitleTextAttributes:@{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} forState:UIControlStateNormal];
  163. }
  164. - (NSString *)_currentIPAddress
  165. {
  166. NSString *address = @"";
  167. struct ifaddrs *interfaces = NULL;
  168. struct ifaddrs *temp_addr = NULL;
  169. int success = getifaddrs(&interfaces);
  170. if (success == 0) {
  171. temp_addr = interfaces;
  172. while(temp_addr != NULL) {
  173. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  174. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  175. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  176. }
  177. temp_addr = temp_addr->ifa_next;
  178. }
  179. }
  180. // Free memory
  181. freeifaddrs(interfaces);
  182. return address;
  183. }
  184. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  185. {
  186. _uploadController = [[VLCHTTPUploaderController alloc] init];
  187. [_uploadController changeHTTPServerState: sender.on];
  188. HTTPServer *server = _uploadController.httpServer;
  189. if (server.isRunning)
  190. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  191. else
  192. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  193. }
  194. - (IBAction)showDropbox:(id)sender
  195. {
  196. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  197. appDelegate.dropboxTableViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  198. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:appDelegate.dropboxTableViewController];
  199. navController.navigationBarHidden = NO;
  200. navController.navigationBar.barStyle = UIBarStyleBlack;
  201. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  202. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  203. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  204. [self presentModalViewController:navController animated:YES];
  205. }
  206. #pragma mark - Private methods
  207. - (void)_presentOpenURLViewFromView:(UIView *)view forSelector:(SEL)selector
  208. {
  209. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  210. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  211. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  212. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  213. pasteURL = [NSURL URLWithString:pasteString];
  214. }
  215. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  216. self.openURLField.text = [pasteURL absoluteString];
  217. }
  218. if (self.openURLView.superview)
  219. [self.openURLView removeFromSuperview];
  220. [self.openURLButton removeTarget:nil action:NULL forControlEvents:UIControlEventTouchUpInside];
  221. [self.openURLButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
  222. [view addSubview:self.openURLView];
  223. }
  224. @end