VLCOpenNetworkStreamViewController.m 9.6 KB

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