VLCMenuViewController.m 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 "UINavigationController+Theme.h"
  22. #import "UIBarButtonItem+Theme.h"
  23. #import "VLCOpenNetworkStreamViewController.h"
  24. #import "VLCHTTPDownloadViewController.h"
  25. #import "VLCBugreporter.h"
  26. #import <ifaddrs.h>
  27. #import <arpa/inet.h>
  28. @interface VLCMenuViewController () {
  29. VLCHTTPUploaderController *_uploadController;
  30. VLCHTTPDownloadViewController *_downloadViewController;
  31. Reachability *_reachability;
  32. }
  33. - (void)_presentViewController:(UIViewController *)viewController;
  34. - (void)_dismissModalViewController;
  35. @end
  36. @implementation VLCMenuViewController
  37. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  38. {
  39. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  40. return self;
  41. }
  42. - (void)dealloc
  43. {
  44. [_reachability stopNotifier];
  45. [[NSNotificationCenter defaultCenter] removeObserver:self];
  46. }
  47. - (void)viewDidLoad
  48. {
  49. [super viewDidLoad];
  50. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  51. UIBarButtonItem *dismissButton = [UIBarButtonItem themedDoneButtonWithTarget:self andSelector:@selector(dismiss:)];
  52. dismissButton.width = 80.;
  53. self.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"menuCone"]];
  54. self.navigationItem.rightBarButtonItem = dismissButton;
  55. [(UIScrollView *)self.view setContentSize:self.view.bounds.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.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. - (CGSize)contentSizeForViewInPopover {
  68. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  69. }
  70. - (void)netReachabilityChanged:(NSNotification *)notification
  71. {
  72. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  73. self.httpUploadServerSwitch.enabled = YES;
  74. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  75. } else {
  76. self.httpUploadServerSwitch.enabled = NO;
  77. self.httpUploadServerSwitch.on = NO;
  78. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_NO_CONNECTIVITY", @"");
  79. }
  80. }
  81. - (void)_hideAnimated:(BOOL)animated
  82. {
  83. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  84. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  85. [appDelegate.playlistViewController.addMediaPopoverController dismissPopoverAnimated:YES];
  86. } else
  87. [self dismissViewControllerAnimated:animated completion:NULL];
  88. }
  89. - (IBAction)dismiss:(id)sender
  90. {
  91. [self _hideAnimated:YES];
  92. }
  93. - (IBAction)openAboutPanel:(id)sender
  94. {
  95. UIViewController *aboutController = [[VLCAboutViewController alloc] initWithNibName:nil bundle:nil];
  96. [self _presentViewController:aboutController];
  97. }
  98. - (IBAction)openNetworkStream:(id)sender
  99. {
  100. UIViewController *openURLController = [[VLCOpenNetworkStreamViewController alloc] initWithNibName:nil bundle:nil];
  101. [self _presentViewController:openURLController];
  102. }
  103. - (IBAction)downloadFromHTTPServer:(id)sender
  104. {
  105. if (!_downloadViewController)
  106. _downloadViewController = [[VLCHTTPDownloadViewController alloc] initWithNibName:nil bundle:nil];
  107. [self _presentViewController:_downloadViewController];
  108. }
  109. - (IBAction)showSettings:(id)sender
  110. {
  111. if (!self.settingsController) {
  112. self.settingsController = [[VLCSettingsController alloc] init];
  113. }
  114. if (!self.settingsViewController) {
  115. self.settingsViewController = [[IASKAppSettingsViewController alloc] initWithStyle:UITableViewStyleGrouped];
  116. self.settingsController.viewController = self.settingsViewController;
  117. }
  118. self.settingsViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  119. self.settingsViewController.delegate = self.settingsController;
  120. self.settingsViewController.showDoneButton = NO;
  121. self.settingsViewController.showCreditsFooter = NO;
  122. [self _presentViewController:self.settingsController.viewController];
  123. }
  124. - (NSString *)_currentIPAddress
  125. {
  126. NSString *address = @"";
  127. struct ifaddrs *interfaces = NULL;
  128. struct ifaddrs *temp_addr = NULL;
  129. int success = getifaddrs(&interfaces);
  130. if (success == 0) {
  131. temp_addr = interfaces;
  132. while(temp_addr != NULL) {
  133. if(temp_addr->ifa_addr->sa_family == AF_INET) {
  134. if([@(temp_addr->ifa_name) isEqualToString:@"en0"])
  135. address = @(inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr));
  136. }
  137. temp_addr = temp_addr->ifa_next;
  138. }
  139. }
  140. // Free memory
  141. freeifaddrs(interfaces);
  142. return address;
  143. }
  144. - (IBAction)toggleHTTPServer:(UISwitch *)sender
  145. {
  146. _uploadController = [[VLCHTTPUploaderController alloc] init];
  147. [_uploadController changeHTTPServerState: sender.on];
  148. HTTPServer *server = _uploadController.httpServer;
  149. if (server.isRunning)
  150. self.httpUploadServerLocationLabel.text = [NSString stringWithFormat:@"http://%@:%i", [self _currentIPAddress], server.listeningPort];
  151. else
  152. self.httpUploadServerLocationLabel.text = NSLocalizedString(@"HTTP_UPLOAD_SERVER_OFF", @"");
  153. }
  154. - (IBAction)showDropbox:(id)sender
  155. {
  156. VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
  157. appDelegate.dropboxTableViewController.modalPresentationStyle = UIModalPresentationFormSheet;
  158. [self _presentViewController:appDelegate.dropboxTableViewController];
  159. }
  160. #pragma mark - Private methods
  161. - (void)_presentViewController:(UIViewController *)viewController
  162. {
  163. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
  164. UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:viewController];
  165. [navController loadTheme];
  166. navController.modalPresentationStyle = UIModalPresentationFormSheet;
  167. [self presentModalViewController:navController animated:YES];
  168. if (viewController.navigationItem.rightBarButtonItem == nil) {
  169. UIBarButtonItem *doneButton = [UIBarButtonItem themedDoneButtonWithTarget:self andSelector:@selector(_dismissModalViewController)];
  170. viewController.navigationItem.rightBarButtonItem = doneButton;
  171. }
  172. } else {
  173. [self.navigationController pushViewController:viewController animated:YES];
  174. }
  175. }
  176. #pragma mark - shake to support
  177. - (BOOL)canBecomeFirstResponder {
  178. return YES;
  179. }
  180. - (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
  181. if (motion == UIEventSubtypeMotionShake)
  182. [[VLCBugreporter sharedInstance] handleBugreportRequest];
  183. }
  184. - (void)_dismissModalViewController
  185. {
  186. [self dismissModalViewControllerAnimated:YES];
  187. }
  188. @end