VLCMenuViewController.m 11 KB

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