VLCMenuViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 "VLCHTTPDownloadViewController.h"
  23. #import <ifaddrs.h>
  24. #import <arpa/inet.h>
  25. @interface VLCMenuViewController () {
  26. VLCHTTPUploaderController *_uploadController;
  27. VLCHTTPDownloadViewController *_downloadViewController;
  28. Reachability *_reachability;
  29. }
  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 (!_downloadViewController)
  121. _downloadViewController = [[VLCHTTPDownloadViewController alloc] initWithNibName:nil bundle:nil];
  122. [self.navigationController pushViewController:_downloadViewController animated:YES];
  123. }
  124. - (IBAction)showSettings:(id)sender
  125. {
  126. if (!self.settingsViewController)
  127. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  128. if (!self.settingsController)
  129. self.settingsController = [[VLCSettingsController alloc] init];
  130. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  131. self.settingsViewController.delegate = self.settingsController;
  132. self.settingsViewController.showDoneButton = YES;
  133. self.settingsViewController.showCreditsFooter = NO;
  134. self.settingsController.viewController = self.settingsViewController;
  135. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.settingsViewController];
  136. navController.navigationBarHidden = NO;
  137. navController.navigationBar.barStyle = UIBarStyleBlack;
  138. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  139. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  140. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  141. [self presentModalViewController:navController animated:YES];
  142. UIBarButtonItem *doneButton = self.settingsViewController.navigationItem.rightBarButtonItem;
  143. [doneButton setBackgroundImage:[UIImage imageNamed:@"doneButton"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
  144. [doneButton setBackgroundImage:[UIImage imageNamed:@"doneButtonHighlight"] forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];
  145. doneButton.style = UIBarButtonItemStyleBordered;
  146. [doneButton setTitleTextAttributes:@{UITextAttributeTextShadowColor : [UIColor whiteColor], UITextAttributeTextColor : [UIColor blackColor]} forState:UIControlStateNormal];
  147. }
  148. - (NSString *)_currentIPAddress
  149. {
  150. NSString *address = @"";
  151. struct ifaddrs *interfaces = NULL;
  152. struct ifaddrs *temp_addr = NULL;
  153. int success = getifaddrs(&interfaces);
  154. if (success == 0) {
  155. temp_addr = interfaces;
  156. while(temp_addr != NULL) {
  157. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  158. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  159. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  160. }
  161. temp_addr = temp_addr->ifa_next;
  162. }
  163. }
  164. // Free memory
  165. freeifaddrs(interfaces);
  166. return address;
  167. }
  168. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  169. {
  170. _uploadController = [[VLCHTTPUploaderController alloc] init];
  171. [_uploadController changeHTTPServerState: sender.on];
  172. HTTPServer *server = _uploadController.httpServer;
  173. if (server.isRunning)
  174. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  175. else
  176. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  177. }
  178. - (IBAction)showDropbox:(id)sender
  179. {
  180. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  181. appDelegate.dropboxTableViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  182. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:appDelegate.dropboxTableViewController];
  183. navController.navigationBarHidden = NO;
  184. navController.navigationBar.barStyle = UIBarStyleBlack;
  185. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackground"] forBarMetrics:UIBarMetricsDefault];
  186. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
  187. [navController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBarBackgroundPhoneLandscape"] forBarMetrics:UIBarMetricsLandscapePhone];
  188. [self presentModalViewController:navController animated:YES];
  189. }
  190. @end