VLCAppDelegate.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // VLCAppDelegate.m
  3. // AspenProject
  4. //
  5. // Created by Felix Paul Kühne on 27.02.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 "VLCAppDelegate.h"
  11. #import "DirectoryWatcher.h"
  12. #import "NSString+SupportedMedia.h"
  13. #import "UIDevice+SpeedCategory.h"
  14. #import "VLCPlaylistViewController.h"
  15. #import "VLCMenuViewController.h"
  16. #import "VLCMovieViewController.h"
  17. #import "PAPasscodeViewController.h"
  18. #import "UINavigationController+Theme.h"
  19. @interface VLCAppDelegate () <PAPasscodeViewControllerDelegate, DirectoryWatcherDelegate> {
  20. NSURL *_tempURL;
  21. PAPasscodeViewController *_passcodeLockController;
  22. VLCDropboxTableViewController *_dropboxTableViewController;
  23. DirectoryWatcher *_directoryWatcher;
  24. NSTimer *_addMediaTimer;
  25. NSMutableDictionary *_addedFiles;
  26. }
  27. @property (nonatomic) BOOL passcodeValidated;
  28. @end
  29. @implementation VLCAppDelegate
  30. + (void)initialize
  31. {
  32. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  33. NSNumber *skipLoopFilterDefaultValue;
  34. int deviceSpeedCategory = [[UIDevice currentDevice] speedCategory];
  35. if (deviceSpeedCategory < 3)
  36. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonKey;
  37. else
  38. skipLoopFilterDefaultValue = kVLCSettingSkipLoopFilterNonRef;
  39. NSDictionary *appDefaults = @{kVLCSettingPasscodeKey : @"", kVLCSettingPasscodeOnKey : @(NO), kVLCSettingContinueAudioInBackgroundKey : @(YES), kVLCSettingStretchAudio : @(NO), kVLCSettingTextEncoding : kVLCSettingTextEncodingDefaultValue, kVLCSettingSkipLoopFilter : skipLoopFilterDefaultValue};
  40. [defaults registerDefaults:appDefaults];
  41. }
  42. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  43. {
  44. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
  45. _playlistViewController = [[VLCPlaylistViewController alloc] init];
  46. self.navigationController = [[UINavigationController alloc] initWithRootViewController:_playlistViewController];
  47. [self.navigationController loadTheme];
  48. self.window.rootViewController = self.navigationController;
  49. [self.window makeKeyAndVisible];
  50. _directoryWatcher = [DirectoryWatcher watchFolderWithPath:[self directoryPath] delegate:self];
  51. [self validatePasscode];
  52. return YES;
  53. }
  54. - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
  55. {
  56. if ([[DBSession sharedSession] handleOpenURL:url]) {
  57. [self.dropboxTableViewController updateViewAfterSessionChange];
  58. return YES;
  59. }
  60. if (_playlistViewController && url != nil) {
  61. APLog(@"%@ requested %@ to be opened", sourceApplication, url);
  62. if (url.isFileURL) {
  63. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"SAVE_FILE", @"") message:[NSString stringWithFormat:NSLocalizedString(@"SAVE_FILE_LONG", @""), url.lastPathComponent] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", @"") otherButtonTitles:NSLocalizedString(@"BUTTON_SAVE", @""), nil];
  64. _tempURL = url;
  65. [alert show];
  66. } else {
  67. NSURL *parsedUrl = [self parseOpenURL:url];
  68. [_playlistViewController openMovieFromURL:parsedUrl];
  69. }
  70. return YES;
  71. }
  72. return NO;
  73. }
  74. - (NSURL *)parseOpenURL:(NSURL *)url
  75. {
  76. NSString *receivedUrl = [url absoluteString];
  77. if ([receivedUrl length] > 6) {
  78. NSString *verifyVlcUrl = [receivedUrl substringToIndex:6];
  79. if ([verifyVlcUrl isEqualToString:@"vlc://"]) {
  80. NSString *parsedString = [receivedUrl substringFromIndex:6];
  81. // "url decode" so we can parse http:// links
  82. parsedString = [parsedString stringByReplacingOccurrencesOfString:@"+"withString:@" "];
  83. parsedString = [parsedString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  84. // add http:// if nothing is there
  85. NSDataDetector *detector = [NSDataDetector dataDetectorWithTypes:(NSTextCheckingTypes)NSTextCheckingTypeLink error:nil];
  86. NSUInteger parsedStringLength = [parsedString length];
  87. NSUInteger numberOfUrlMatches = [detector numberOfMatchesInString:parsedString options:0 range:NSMakeRange(0, parsedStringLength)];
  88. if (numberOfUrlMatches == 0) {
  89. parsedString = [@"http://" stringByAppendingString:parsedString];
  90. }
  91. NSURL *targetUrl = [NSURL URLWithString:parsedString];
  92. return targetUrl;
  93. }
  94. }
  95. return url;
  96. }
  97. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  98. {
  99. if (buttonIndex == 1) {
  100. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  101. NSString *directoryPath = searchPaths[0];
  102. NSURL *destinationURL = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/%@", directoryPath, _tempURL.lastPathComponent]];
  103. NSError *theError;
  104. [[NSFileManager defaultManager] copyItemAtURL:_tempURL toURL:destinationURL error:&theError];
  105. if (theError.code != noErr)
  106. APLog(@"saving the file failed (%i): %@", theError.code, theError.localizedDescription);
  107. [self updateMediaList];
  108. } else
  109. [_playlistViewController openMovieFromURL:_tempURL];
  110. }
  111. - (void)applicationWillResignActive:(UIApplication *)application
  112. {
  113. [[MLMediaLibrary sharedMediaLibrary] applicationWillExit];
  114. }
  115. - (void)applicationDidBecomeActive:(UIApplication *)application
  116. {
  117. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  118. [self updateMediaList];
  119. }
  120. - (void)applicationDidEnterBackground:(UIApplication *)application
  121. {
  122. [self validatePasscode]; // Lock library when going to background
  123. }
  124. #pragma mark - properties
  125. - (VLCDropboxTableViewController *)dropboxTableViewController
  126. {
  127. if (_dropboxTableViewController == nil) {
  128. _dropboxTableViewController = [[VLCDropboxTableViewController alloc] initWithNibName:nil bundle:nil];
  129. }
  130. return _dropboxTableViewController;
  131. }
  132. #pragma mark - directory watcher delegate
  133. - (void)addFileTimerFired
  134. {
  135. NSArray *allKeys = [_addedFiles allKeys];
  136. NSFileManager *fileManager = [NSFileManager defaultManager];
  137. MLMediaLibrary *sharedLibrary = [MLMediaLibrary sharedMediaLibrary];
  138. for (NSString *fileURL in allKeys) {
  139. if (![fileManager fileExistsAtPath:fileURL])
  140. continue;
  141. NSDictionary *attribs = [fileManager attributesOfItemAtPath:fileURL error:nil];
  142. NSNumber *prevFetchedSize = [_addedFiles objectForKey:fileURL];
  143. NSNumber *updatedSize = [attribs objectForKey:NSFileSize];
  144. if (!updatedSize)
  145. continue;
  146. if ([prevFetchedSize compare:updatedSize] == NSOrderedSame) {
  147. [_addedFiles removeObjectForKey:fileURL];
  148. [sharedLibrary addFilePaths:@[fileURL]];
  149. /* exclude media files from backup (QA1719) */
  150. NSURL *excludeURL = [NSURL fileURLWithPath:fileURL];
  151. [excludeURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  152. // TODO Should we update media db after adding new files?
  153. [sharedLibrary updateMediaDatabase];
  154. [_playlistViewController updateViewContents];
  155. } else
  156. [_addedFiles setObject:updatedSize forKey:fileURL];
  157. }
  158. if (_addedFiles.count == 0) {
  159. [_addMediaTimer invalidate];
  160. _addMediaTimer = nil;
  161. }
  162. }
  163. - (void)directoryDidChange:(DirectoryWatcher *)folderWatcher
  164. {
  165. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[self directoryPath] error:nil];
  166. NSMutableArray *matchedFiles = [NSMutableArray arrayWithCapacity:foundFiles.count];
  167. for (NSString *fileName in foundFiles) {
  168. if ([fileName isSupportedMediaFormat])
  169. [matchedFiles addObject:[[self directoryPath] stringByAppendingPathComponent:fileName]];
  170. }
  171. NSArray *mediaFiles = [MLFile allFiles];
  172. if (mediaFiles.count > matchedFiles.count) { // File was deleted
  173. [[MLMediaLibrary sharedMediaLibrary] updateMediaDatabase];
  174. [_playlistViewController updateViewContents];
  175. } else if (mediaFiles.count < matchedFiles.count) { // File was added
  176. NSMutableArray *addedFiles = [NSMutableArray array];
  177. for (NSString *fileName in matchedFiles) {
  178. NSURL *fileURL = [NSURL fileURLWithPath:fileName];
  179. BOOL found = NO;
  180. for (MLFile *mediaFile in mediaFiles) {
  181. if ([mediaFile.url isEqualToString:fileURL.absoluteString])
  182. found = YES;
  183. }
  184. if (!found)
  185. [addedFiles addObject:fileName];
  186. }
  187. _addedFiles = [NSMutableDictionary dictionaryWithCapacity:[addedFiles count]];
  188. for (NSString *fileURL in addedFiles)
  189. [_addedFiles setObject:@(0) forKey:fileURL];
  190. _addMediaTimer = [NSTimer scheduledTimerWithTimeInterval:2. target:self
  191. selector:@selector(addFileTimerFired)
  192. userInfo:nil repeats:YES];
  193. }
  194. }
  195. #pragma mark - media list methods
  196. - (NSString *)directoryPath
  197. {
  198. #define LOCAL_PLAYBACK_HACK 0
  199. #if LOCAL_PLAYBACK_HACK && TARGET_IPHONE_SIMULATOR
  200. NSString *directoryPath = @"/Users/fkuehne/Desktop/VideoLAN docs/Clips/sel/";
  201. #else
  202. NSArray *searchPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  203. NSString *directoryPath = searchPaths[0];
  204. #endif
  205. return directoryPath;
  206. }
  207. - (void)updateMediaList
  208. {
  209. NSString *directoryPath = [self directoryPath];
  210. NSArray *foundFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:nil];
  211. NSMutableArray *filePaths = [NSMutableArray arrayWithCapacity:[foundFiles count]];
  212. NSURL *fileURL;
  213. for (NSString *fileName in foundFiles) {
  214. if ([fileName isSupportedMediaFormat]) {
  215. [filePaths addObject:[directoryPath stringByAppendingPathComponent:fileName]];
  216. /* exclude media files from backup (QA1719) */
  217. fileURL = [NSURL fileURLWithPath:[directoryPath stringByAppendingPathComponent:fileName]];
  218. [fileURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:nil];
  219. }
  220. }
  221. [[MLMediaLibrary sharedMediaLibrary] addFilePaths:filePaths];
  222. [_playlistViewController updateViewContents];
  223. }
  224. #pragma mark - pass code validation
  225. - (void)validatePasscode
  226. {
  227. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  228. NSString *passcode = [defaults objectForKey:kVLCSettingPasscodeKey];
  229. if ([passcode isEqualToString:@""] || ![[defaults objectForKey:kVLCSettingPasscodeOnKey] boolValue]) {
  230. self.passcodeValidated = YES;
  231. return;
  232. }
  233. if (!self.passcodeValidated) {
  234. if ([self.nextPasscodeCheckDate earlierDate:[NSDate date]] == self.nextPasscodeCheckDate) {
  235. _passcodeLockController = [[PAPasscodeViewController alloc] initForAction:PasscodeActionEnter];
  236. _passcodeLockController.delegate = self;
  237. _passcodeLockController.passcode = passcode;
  238. self.window.rootViewController = _passcodeLockController;
  239. } else
  240. self.passcodeValidated = YES;
  241. }
  242. }
  243. - (void)PAPasscodeViewControllerDidEnterPasscode:(PAPasscodeViewController *)controller
  244. {
  245. // TODO add transition animation, i.e. fade
  246. self.nextPasscodeCheckDate = [NSDate dateWithTimeIntervalSinceNow:300];
  247. self.window.rootViewController = self.navigationController;
  248. }
  249. - (void)PAPasscodeViewController:(PAPasscodeViewController *)controller didFailToEnterPasscode:(NSInteger)attempts
  250. {
  251. // TODO handle error attempts
  252. }
  253. @end