VLCOpenNetworkStreamViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*****************************************************************************
  2. * VLCOpenNetworkStreamViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Gleb Pinigin <gpinigin # gmail.com>
  10. * Pierre Sagaspe <pierre.sagaspe # me.com>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCOpenNetworkStreamViewController.h"
  15. #import "VLCAppDelegate.h"
  16. #import "VLCPlaylistViewController.h"
  17. #import "UIBarButtonItem+Theme.h"
  18. #import "UINavigationController+Theme.h"
  19. #import "VLCMenuTableViewController.h"
  20. @interface VLCOpenNetworkStreamViewController () <UITableViewDataSource, UITableViewDelegate, UITextFieldDelegate>
  21. {
  22. NSMutableArray *_recentURLs;
  23. }
  24. @end
  25. @implementation VLCOpenNetworkStreamViewController
  26. + (void)initialize
  27. {
  28. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  29. NSDictionary *appDefaults = @{kVLCRecentURLs : @[], kVLCPrivateWebStreaming : @(NO)};
  30. [defaults registerDefaults:appDefaults];
  31. }
  32. - (void)viewDidLoad
  33. {
  34. [super viewDidLoad];
  35. [self.openButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  36. [self.privateModeLabel setText:NSLocalizedString(@"PRIVATE_PLAYBACK_TOGGLE", @"")];
  37. self.title = NSLocalizedString(@"OPEN_NETWORK", @"");
  38. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  39. [self.whatToOpenHelpLabel setText:NSLocalizedString(@"OPEN_NETWORK_HELP", @"")];
  40. self.urlField.delegate = self;
  41. self.urlField.keyboardType = UIKeyboardTypeURL;
  42. if (SYSTEM_RUNS_IOS7_OR_LATER)
  43. self.edgesForExtendedLayout = UIRectEdgeNone;
  44. }
  45. - (void)viewWillAppear:(BOOL)animated
  46. {
  47. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  48. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  49. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  50. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  51. pasteURL = [NSURL URLWithString:pasteString];
  52. }
  53. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  54. self.urlField.text = [pasteURL absoluteString];
  55. }
  56. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  57. _recentURLs = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCRecentURLs]];
  58. self.privateToggleSwitch.on = [defaults boolForKey:kVLCPrivateWebStreaming];
  59. [super viewWillAppear:animated];
  60. }
  61. - (void)viewWillDisappear:(BOOL)animated
  62. {
  63. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  64. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  65. [defaults setBool:self.privateToggleSwitch.on forKey:kVLCPrivateWebStreaming];
  66. [defaults synchronize];
  67. [super viewWillDisappear:animated];
  68. }
  69. - (CGSize)contentSizeForViewInPopover {
  70. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  71. }
  72. #pragma mark - UI interaction
  73. - (BOOL)shouldAutorotate
  74. {
  75. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  76. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  77. return NO;
  78. return YES;
  79. }
  80. - (IBAction)goBack:(id)sender
  81. {
  82. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  83. }
  84. - (IBAction)openButtonAction:(id)sender
  85. {
  86. if ([self.urlField.text length] > 0) {
  87. if (!self.privateToggleSwitch.on) {
  88. if ([_recentURLs indexOfObject:self.urlField.text] != NSNotFound)
  89. [_recentURLs removeObject:self.urlField.text];
  90. if (_recentURLs.count >= 15)
  91. [_recentURLs removeLastObject];
  92. [_recentURLs addObject:self.urlField.text];
  93. [self.historyTableView reloadData];
  94. }
  95. [self _openURLStringAndDismiss:self.urlField.text];
  96. }
  97. }
  98. #pragma mark - table view data source
  99. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  100. {
  101. return 1;
  102. }
  103. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  104. {
  105. return _recentURLs.count;
  106. }
  107. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  108. {
  109. static NSString *CellIdentifier = @"StreamingHistoryCell";
  110. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  111. if (cell == nil) {
  112. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  113. cell.textLabel.textColor = [UIColor whiteColor];
  114. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  115. }
  116. NSInteger row = indexPath.row;
  117. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  118. cell.detailTextLabel.text = _recentURLs[row];
  119. return cell;
  120. }
  121. #pragma mark - table view delegate
  122. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  123. {
  124. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  125. }
  126. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  127. {
  128. return YES;
  129. }
  130. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  131. {
  132. if (editingStyle == UITableViewCellEditingStyleDelete) {
  133. [_recentURLs removeObjectAtIndex:indexPath.row];
  134. [tableView reloadData];
  135. }
  136. }
  137. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  138. {
  139. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  140. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  141. }
  142. #pragma mark - internals
  143. - (void)_openURLStringAndDismiss:(NSString *)url
  144. {
  145. VLCAppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
  146. [appDelegate.menuViewController selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionNone];
  147. [appDelegate openMovieFromURL:[NSURL URLWithString:url]];
  148. }
  149. #pragma mark - text view delegate
  150. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  151. [self.urlField resignFirstResponder];
  152. return NO;
  153. }
  154. @end