VLCOpenNetworkStreamViewController.m 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. if (SYSTEM_RUNS_IOS7_OR_LATER)
  36. [self.openButton setTitle:NSLocalizedString(@"OPEN_NETWORK", @"") forState:UIControlStateNormal];
  37. else
  38. [self.openButton setTitle:NSLocalizedString(@"BUTTON_OPEN", @"") forState:UIControlStateNormal];
  39. [self.privateModeLabel setText:NSLocalizedString(@"PRIVATE_PLAYBACK_TOGGLE", @"")];
  40. self.title = NSLocalizedString(@"OPEN_NETWORK", @"");
  41. self.navigationItem.leftBarButtonItem = [UIBarButtonItem themedRevealMenuButtonWithTarget:self andSelector:@selector(goBack:)];
  42. [self.whatToOpenHelpLabel setText:NSLocalizedString(@"OPEN_NETWORK_HELP", @"")];
  43. self.urlField.delegate = self;
  44. self.urlField.keyboardType = UIKeyboardTypeURL;
  45. if (SYSTEM_RUNS_IOS7_OR_LATER)
  46. self.edgesForExtendedLayout = UIRectEdgeNone;
  47. }
  48. - (void)viewWillAppear:(BOOL)animated
  49. {
  50. if ([[UIPasteboard generalPasteboard] containsPasteboardTypes:@[@"public.url", @"public.text"]]) {
  51. NSURL *pasteURL = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.url"];
  52. if (!pasteURL || [[pasteURL absoluteString] isEqualToString:@""]) {
  53. NSString *pasteString = [[UIPasteboard generalPasteboard] valueForPasteboardType:@"public.text"];
  54. pasteURL = [NSURL URLWithString:pasteString];
  55. }
  56. if (pasteURL && ![[pasteURL scheme] isEqualToString:@""] && ![[pasteURL absoluteString] isEqualToString:@""])
  57. self.urlField.text = [pasteURL absoluteString];
  58. }
  59. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  60. _recentURLs = [NSMutableArray arrayWithArray:[defaults objectForKey:kVLCRecentURLs]];
  61. self.privateToggleSwitch.on = [defaults boolForKey:kVLCPrivateWebStreaming];
  62. [super viewWillAppear:animated];
  63. }
  64. - (void)viewWillDisappear:(BOOL)animated
  65. {
  66. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  67. [defaults setObject:[NSArray arrayWithArray:_recentURLs] forKey:kVLCRecentURLs];
  68. [defaults setBool:self.privateToggleSwitch.on forKey:kVLCPrivateWebStreaming];
  69. [defaults synchronize];
  70. [super viewWillDisappear:animated];
  71. }
  72. - (CGSize)contentSizeForViewInPopover {
  73. return [self.view sizeThatFits:CGSizeMake(320, 800)];
  74. }
  75. #pragma mark - UI interaction
  76. - (BOOL)shouldAutorotate
  77. {
  78. UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
  79. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone && toInterfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
  80. return NO;
  81. return YES;
  82. }
  83. - (IBAction)goBack:(id)sender
  84. {
  85. [[(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController] toggleSidebar:![(VLCAppDelegate*)[UIApplication sharedApplication].delegate revealController].sidebarShowing duration:kGHRevealSidebarDefaultAnimationDuration];
  86. }
  87. - (IBAction)openButtonAction:(id)sender
  88. {
  89. if ([self.urlField.text length] > 0) {
  90. if (!self.privateToggleSwitch.on) {
  91. if ([_recentURLs indexOfObject:self.urlField.text] != NSNotFound)
  92. [_recentURLs removeObject:self.urlField.text];
  93. if (_recentURLs.count >= 15)
  94. [_recentURLs removeLastObject];
  95. [_recentURLs addObject:self.urlField.text];
  96. [self.historyTableView reloadData];
  97. }
  98. [self _openURLStringAndDismiss:self.urlField.text];
  99. }
  100. }
  101. #pragma mark - table view data source
  102. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  103. {
  104. return 1;
  105. }
  106. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  107. {
  108. return _recentURLs.count;
  109. }
  110. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  111. {
  112. static NSString *CellIdentifier = @"StreamingHistoryCell";
  113. UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
  114. if (cell == nil) {
  115. cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
  116. cell.textLabel.textColor = [UIColor whiteColor];
  117. cell.detailTextLabel.textColor = [UIColor colorWithWhite:.72 alpha:1.];
  118. }
  119. NSInteger row = indexPath.row;
  120. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  121. cell.detailTextLabel.text = _recentURLs[row];
  122. return cell;
  123. }
  124. #pragma mark - table view delegate
  125. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  126. {
  127. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor colorWithWhite:.122 alpha:1.];
  128. }
  129. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  130. {
  131. return YES;
  132. }
  133. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  134. {
  135. if (editingStyle == UITableViewCellEditingStyleDelete) {
  136. [_recentURLs removeObjectAtIndex:indexPath.row];
  137. [tableView reloadData];
  138. }
  139. }
  140. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  143. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  144. }
  145. #pragma mark - internals
  146. - (void)_openURLStringAndDismiss:(NSString *)url
  147. {
  148. NSURL *URLscheme = [NSURL URLWithString:url];
  149. NSString *URLofSubtitle = nil;
  150. if ([URLscheme.scheme isEqualToString:@"http"])
  151. URLofSubtitle = [self _checkURLofSubtitle:url];
  152. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMovieWithExternalSubtitleFromURL:[NSURL URLWithString:url] externalSubURL:URLofSubtitle];
  153. }
  154. - (NSString *)_checkURLofSubtitle:(NSString *)url
  155. {
  156. NSString *SubtitleFileExtensions = kSupportedSubtitleFileExtensions;
  157. NSCharacterSet *characterFilter = [NSCharacterSet characterSetWithCharactersInString:@"\\.():$"];
  158. SubtitleFileExtensions = [[SubtitleFileExtensions componentsSeparatedByCharactersInSet:characterFilter] componentsJoinedByString:@""];
  159. NSArray *arraySubtitleFileExtensions = [SubtitleFileExtensions componentsSeparatedByString:@"|"];
  160. NSString *urlTemp = [[url stringByDeletingPathExtension] stringByAppendingString:@"."];
  161. for (int cnt = 0; cnt < arraySubtitleFileExtensions.count; cnt++) {
  162. NSString *CheckURL = [urlTemp stringByAppendingString:arraySubtitleFileExtensions[cnt]];
  163. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckURL]];
  164. NSURLResponse *response = nil;
  165. NSError *error = nil;
  166. NSData *receivedData = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];
  167. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  168. if (httpStatus == 200) {
  169. NSString *receivedSub = [NSString stringWithContentsOfURL:[NSURL URLWithString:CheckURL] encoding:NSASCIIStringEncoding error:nil];
  170. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  171. NSString *directoryPath = searchPaths[0];
  172. NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[CheckURL lastPathComponent]];
  173. NSFileManager *fileManager = [NSFileManager defaultManager];
  174. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  175. //create local subtitle file
  176. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  177. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  178. APLog(@"file creation failed, no data was saved");
  179. }
  180. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  181. return FileSubtitlePath;
  182. }
  183. }
  184. return nil;
  185. }
  186. #pragma mark - text view delegate
  187. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  188. {
  189. [self.urlField resignFirstResponder];
  190. return NO;
  191. }
  192. @end