VLCAppDelegate.m 9.9 KB

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