VLCOpenNetworkStreamViewController.m 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.textLabel.highlightedTextColor = [UIColor blackColor];
  123. cell.detailTextLabel.textColor = [UIColor VLCLightTextColor];
  124. cell.detailTextLabel.highlightedTextColor = [UIColor blackColor];
  125. }
  126. NSInteger row = indexPath.row;
  127. cell.textLabel.text = [_recentURLs[row] lastPathComponent];
  128. cell.detailTextLabel.text = _recentURLs[row];
  129. return cell;
  130. }
  131. #pragma mark - table view delegate
  132. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  133. {
  134. cell.backgroundColor = (indexPath.row % 2 == 0)? [UIColor blackColor]: [UIColor VLCDarkBackgroundColor];
  135. }
  136. - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  137. {
  138. return YES;
  139. }
  140. - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  141. {
  142. if (editingStyle == UITableViewCellEditingStyleDelete) {
  143. [_recentURLs removeObjectAtIndex:indexPath.row];
  144. [tableView reloadData];
  145. }
  146. }
  147. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  148. {
  149. [self _openURLStringAndDismiss:_recentURLs[indexPath.row]];
  150. [self.historyTableView deselectRowAtIndexPath:indexPath animated:NO];
  151. }
  152. - (void)tableView:(UITableView *)tableView
  153. performAction:(SEL)action
  154. forRowAtIndexPath:(NSIndexPath *)indexPath
  155. withSender:(id)sender
  156. {
  157. NSString *actionText = NSStringFromSelector(action);
  158. if ([actionText isEqualToString:@"copy:"])
  159. {
  160. [UIPasteboard generalPasteboard].string = _recentURLs[indexPath.row];
  161. }
  162. }
  163. - (BOOL)tableView:(UITableView *)tableView
  164. canPerformAction:(SEL)action
  165. forRowAtIndexPath:(NSIndexPath *)indexPath
  166. withSender:(id)sender
  167. {
  168. NSString *actionText = NSStringFromSelector(action);
  169. if ([actionText isEqualToString:@"copy:"])
  170. {
  171. return YES;
  172. }
  173. return NO;
  174. }
  175. - (BOOL)tableView:(UITableView *)tableView shouldShowMenuForRowAtIndexPath:(NSIndexPath *)indexPath
  176. {
  177. return YES;
  178. }
  179. #pragma mark - internals
  180. - (void)_openURLStringAndDismiss:(NSString *)url
  181. {
  182. NSURL *URLscheme = [NSURL URLWithString:url];
  183. NSString *URLofSubtitle = nil;
  184. if ([URLscheme.scheme isEqualToString:@"http"])
  185. if (self.ScanSubToggleSwitch.on)
  186. URLofSubtitle = [self _checkURLofSubtitle:url];
  187. [(VLCAppDelegate*)[UIApplication sharedApplication].delegate openMovieWithExternalSubtitleFromURL:[NSURL URLWithString:url] externalSubURL:URLofSubtitle];
  188. }
  189. - (NSString *)_checkURLofSubtitle:(NSString *)url
  190. {
  191. NSString *SubtitleFileExtensions = kSupportedSubtitleFileExtensions;
  192. NSCharacterSet *characterFilter = [NSCharacterSet characterSetWithCharactersInString:@"\\.():$"];
  193. SubtitleFileExtensions = [[SubtitleFileExtensions componentsSeparatedByCharactersInSet:characterFilter] componentsJoinedByString:@""];
  194. NSArray *arraySubtitleFileExtensions = [SubtitleFileExtensions componentsSeparatedByString:@"|"];
  195. NSString *urlTemp = [[url stringByDeletingPathExtension] stringByAppendingString:@"."];
  196. for (int cnt = 0; cnt < arraySubtitleFileExtensions.count; cnt++) {
  197. NSString *CheckURL = [urlTemp stringByAppendingString:arraySubtitleFileExtensions[cnt]];
  198. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:CheckURL]];
  199. NSURLResponse *response = nil;
  200. NSError *error = nil;
  201. NSData *receivedData = [[NSData alloc] initWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];
  202. NSInteger httpStatus = [(NSHTTPURLResponse *)response statusCode];
  203. receivedData = nil;
  204. if (httpStatus == 200) {
  205. NSString *receivedSub = [NSString stringWithContentsOfURL:[NSURL URLWithString:CheckURL] encoding:NSASCIIStringEncoding error:nil];
  206. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  207. NSString *directoryPath = searchPaths[0];
  208. NSString *FileSubtitlePath = [directoryPath stringByAppendingPathComponent:[CheckURL lastPathComponent]];
  209. NSFileManager *fileManager = [NSFileManager defaultManager];
  210. if (![fileManager fileExistsAtPath:FileSubtitlePath]) {
  211. //create local subtitle file
  212. [fileManager createFileAtPath:FileSubtitlePath contents:nil attributes:nil];
  213. if (![fileManager fileExistsAtPath:FileSubtitlePath])
  214. APLog(@"file creation failed, no data was saved");
  215. }
  216. [receivedSub writeToFile:FileSubtitlePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
  217. return FileSubtitlePath;
  218. }
  219. }
  220. return nil;
  221. }
  222. #pragma mark - text view delegate
  223. - (BOOL)textFieldShouldReturn:(UITextField *)textField
  224. {
  225. [self.urlField resignFirstResponder];
  226. return NO;
  227. }
  228. @end