From 53508d57a481fa1e67ac66541581f925dda7fd6e Mon Sep 17 00:00:00 2001 From: ritsuka Date: Sat, 21 Feb 2015 07:28:48 +0000 Subject: MacGui: add a preference to toggle the log cleanup. Use NSURL directly where possible. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6934 b64f7644-9d1e-0410-96f1-a4d463321fa5 --- macosx/English.lproj/Preferences.xib | 66 +++++++++++++++---------- macosx/HBAppDelegate.m | 95 ++++++++++++++++++++++-------------- macosx/HBJobOutputFileWriter.m | 11 +---- macosx/HBOutputFileWriter.m | 6 +++ macosx/HBOutputPanelController.m | 27 +++------- macosx/HBPreferencesController.m | 1 + macosx/HBPreviewGenerator.m | 17 +++---- macosx/HBQueueController.m | 2 +- macosx/HBUtilities.h | 5 -- macosx/HBUtilities.m | 13 ----- 10 files changed, 120 insertions(+), 123 deletions(-) (limited to 'macosx') diff --git a/macosx/English.lproj/Preferences.xib b/macosx/English.lproj/Preferences.xib index f6abd81a0..d68b90d3c 100644 --- a/macosx/English.lproj/Preferences.xib +++ b/macosx/English.lproj/Preferences.xib @@ -1,9 +1,9 @@ - + - + @@ -379,14 +379,15 @@ + - + - - + + @@ -403,8 +404,8 @@ - - + + @@ -437,7 +438,7 @@ - + @@ -468,7 +469,7 @@ - + @@ -477,7 +478,7 @@ - + @@ -486,7 +487,7 @@ - + @@ -495,16 +496,16 @@ - + - + - + @@ -513,7 +514,7 @@ - + @@ -522,7 +523,7 @@ - + @@ -531,7 +532,7 @@ - + @@ -540,8 +541,8 @@ - - + + @@ -552,8 +553,8 @@ - + diff --git a/macosx/HBAppDelegate.m b/macosx/HBAppDelegate.m index 42c18435a..1a78e0815 100644 --- a/macosx/HBAppDelegate.m +++ b/macosx/HBAppDelegate.m @@ -89,26 +89,6 @@ // Get the number of HandBrake instances currently running NSUInteger instances = [NSRunningApplication runningApplicationsWithBundleIdentifier:[[NSBundle mainBundle] bundleIdentifier]].count; - // If we are a single instance it is safe to clean up the previews if there are any - // left over. This is a bit of a kludge but will prevent a build up of old instance - // live preview cruft. No danger of removing an active preview directory since they - // are created later in HBPreviewController if they don't exist at the moment a live - // preview encode is initiated. - if (instances == 1) - { - NSString *previewDirectory = [[HBUtilities appSupportPath] stringByAppendingPathComponent:@"Previews"]; - NSError *error = nil; - NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:previewDirectory error:&error]; - for (NSString *file in files) - { - BOOL result = [[NSFileManager defaultManager] removeItemAtPath:[previewDirectory stringByAppendingPathComponent:file] error:&error]; - if (result == NO && error) - { - [HBUtilities writeToActivityLog: "Could not remove existing preview at : %s", file.UTF8String]; - } - } - } - [self showMainWindow:self]; // Now we re-check the queue array to see if there are @@ -199,9 +179,22 @@ if ([[NSUserDefaults standardUserDefaults] boolForKey:@"QueueWindowIsOpen"]) [self showQueueWindow:nil]; - // Remove encodes logs older than a month dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{ - [self cleanEncodeLogs]; + // Remove encodes logs older than a month + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"HBClearOldLogs"]) + { + [self cleanEncodeLogs]; + } + + // If we are a single instance it is safe to clean up the previews if there are any + // left over. This is a bit of a kludge but will prevent a build up of old instance + // live preview cruft. No danger of removing an active preview directory since they + // are created later in HBPreviewController if they don't exist at the moment a live + // preview encode is initiated. + if (instances == 1) + { + [self cleanPreviews]; + } }); } @@ -300,27 +293,55 @@ { NSURL *directoryUrl = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"EncodeLogs"]; - NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:directoryUrl - includingPropertiesForKeys:nil - options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | - NSDirectoryEnumerationSkipsHiddenFiles | - NSDirectoryEnumerationSkipsPackageDescendants - error:NULL]; + if (directoryUrl) + { + NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:directoryUrl + includingPropertiesForKeys:nil + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | + NSDirectoryEnumerationSkipsHiddenFiles | + NSDirectoryEnumerationSkipsPackageDescendants + error:NULL]; - NSDate *limit = [NSDate dateWithTimeIntervalSinceNow: -(60 * 60 * 24 * 30)]; - NSFileManager *manager = [[NSFileManager alloc] init]; + NSDate *limit = [NSDate dateWithTimeIntervalSinceNow: -(60 * 60 * 24 * 30)]; + NSFileManager *manager = [[NSFileManager alloc] init]; - for (NSURL *fileURL in contents) - { - NSDate *creationDate = nil; - [fileURL getResourceValue:&creationDate forKey:NSURLCreationDateKey error:NULL]; - if ([creationDate isLessThan:limit]) + for (NSURL *fileURL in contents) { - [manager removeItemAtURL:fileURL error:NULL]; + NSDate *creationDate = nil; + [fileURL getResourceValue:&creationDate forKey:NSURLCreationDateKey error:NULL]; + if ([creationDate isLessThan:limit]) + { + [manager removeItemAtURL:fileURL error:NULL]; + } } + [manager release]; } +} - [manager release]; +- (void)cleanPreviews +{ + NSURL *previewDirectory = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"Previews"]; + + if (previewDirectory) + { + NSArray *contents = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:previewDirectory + includingPropertiesForKeys:nil + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | + NSDirectoryEnumerationSkipsPackageDescendants + error:NULL]; + + NSFileManager *manager = [[NSFileManager alloc] init]; + for (NSURL *url in contents) + { + NSError *error = nil; + BOOL result = [manager removeItemAtURL:url error:&error]; + if (result == NO && error) + { + [HBUtilities writeToActivityLog: "Could not remove existing preview at : %s", url.lastPathComponent.UTF8String]; + } + } + [manager release]; + } } #pragma mark - Menu actions diff --git a/macosx/HBJobOutputFileWriter.m b/macosx/HBJobOutputFileWriter.m index 2ae3c161d..fa84350ec 100644 --- a/macosx/HBJobOutputFileWriter.m +++ b/macosx/HBJobOutputFileWriter.m @@ -37,15 +37,8 @@ else { // if we are putting it in the default ~/Libraries/Application Support/HandBrake/EncodeLogs logs directory - NSString *encodeLogDirectory = [[HBUtilities appSupportPath] stringByAppendingPathComponent:@"EncodeLogs"]; - if( ![[NSFileManager defaultManager] fileExistsAtPath:encodeLogDirectory] ) - { - [[NSFileManager defaultManager] createDirectoryAtPath:encodeLogDirectory - withIntermediateDirectories:NO - attributes:nil - error:nil]; - } - outputURL = [[NSURL fileURLWithPath:encodeLogDirectory] URLByAppendingPathComponent:outputDateFileName]; + NSURL *encodeLogDirectory = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"EncodeLogs"]; + outputURL = [encodeLogDirectory URLByAppendingPathComponent:outputDateFileName]; } self = [super initWithFileURL:outputURL]; diff --git a/macosx/HBOutputFileWriter.m b/macosx/HBOutputFileWriter.m index f01293f12..919ce04dc 100644 --- a/macosx/HBOutputFileWriter.m +++ b/macosx/HBOutputFileWriter.m @@ -17,6 +17,12 @@ self = [super init]; if (self) { + + [[NSFileManager defaultManager] createDirectoryAtPath:url.URLByDeletingLastPathComponent.path + withIntermediateDirectories:YES + attributes:nil + error:NULL]; + _url = [url copy]; f = fopen(url.fileSystemRepresentation, "w"); f = freopen(NULL, "a", f); diff --git a/macosx/HBOutputPanelController.m b/macosx/HBOutputPanelController.m index 53a27c703..909fbe736 100644 --- a/macosx/HBOutputPanelController.m +++ b/macosx/HBOutputPanelController.m @@ -49,17 +49,9 @@ (void)[self window]; // Additionally, redirect the output to a file on the disk. - NSFileManager *fileManager = [NSFileManager defaultManager]; - NSString *outputLogFile = [[HBUtilities appSupportPath] stringByAppendingPathComponent:@"HandBrake-activitylog.txt"]; + NSURL *outputLogFile = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"HandBrake-activitylog.txt"]; - // We check for an existing output log file here - if ([fileManager fileExistsAtPath:outputLogFile] == NO) - { - // if not, then we create a new blank one - [fileManager createFileAtPath:outputLogFile contents:nil attributes:nil]; - } - - _outputFile = [[HBOutputFileWriter alloc] initWithFileURL:[NSURL fileURLWithPath:outputLogFile]]; + _outputFile = [[HBOutputFileWriter alloc] initWithFileURL:outputLogFile]; [[HBOutputRedirect stderrRedirect] addListener:_outputFile]; [[HBOutputRedirect stdoutRedirect] addListener:_outputFile]; @@ -166,9 +158,7 @@ - (IBAction)openActivityLogFile:(id)sender { /* Opens the activity window log file in the users default text editor */ - NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat: @"%@%@%@", @"tell application \"Finder\" to open (POSIX file \"", self.outputFile.url.path, @"\")"]]; - [myScript executeAndReturnError: nil]; - [myScript release]; + [[NSWorkspace sharedWorkspace] openURL:self.outputFile.url]; } /** @@ -177,18 +167,15 @@ - (IBAction)openEncodeLogDirectory:(id)sender { /* Opens the activity window log file in the users default text editor */ - NSString *encodeLogDirectory = [[HBUtilities appSupportPath] stringByAppendingPathComponent:@"EncodeLogs"]; - if( ![[NSFileManager defaultManager] fileExistsAtPath:encodeLogDirectory] ) + NSURL *encodeLogDirectory = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"EncodeLogs"]; + if( ![[NSFileManager defaultManager] fileExistsAtPath:encodeLogDirectory.path] ) { - [[NSFileManager defaultManager] createDirectoryAtPath:encodeLogDirectory + [[NSFileManager defaultManager] createDirectoryAtPath:encodeLogDirectory.path withIntermediateDirectories:NO attributes:nil error:nil]; } - - NSAppleScript *myScript = [[NSAppleScript alloc] initWithSource: [NSString stringWithFormat: @"%@%@%@", @"tell application \"Finder\" to open (POSIX file \"", encodeLogDirectory, @"\")"]]; - [myScript executeAndReturnError: nil]; - [myScript release]; + [[NSWorkspace sharedWorkspace] openURL:encodeLogDirectory]; } - (IBAction)clearActivityLogFile:(id)sender diff --git a/macosx/HBPreferencesController.m b/macosx/HBPreferencesController.m index 22d9fcab8..8bbb58015 100644 --- a/macosx/HBPreferencesController.m +++ b/macosx/HBPreferencesController.m @@ -70,6 +70,7 @@ @"AlertWhenDone": @"Alert Window", @"AlertWhenDoneSound": @"YES", @"LoggingLevel": @"1", + @"HBClearOldLogs": @YES, @"EncodeLogLocation": @"NO", @"MinTitleScanSeconds": @"10", @"PreviewsNumber": @"10", diff --git a/macosx/HBPreviewGenerator.m b/macosx/HBPreviewGenerator.m index a7efb4d2f..46352aa55 100644 --- a/macosx/HBPreviewGenerator.m +++ b/macosx/HBPreviewGenerator.m @@ -100,19 +100,17 @@ + (NSURL *) generateFileURLForType:(NSString *) type { - NSString *previewDirectory = [NSString stringWithFormat:@"%@/Previews/%d", [HBUtilities appSupportPath], getpid()]; + NSURL *previewDirectory = [[HBUtilities appSupportURL] URLByAppendingPathComponent:[NSString stringWithFormat:@"/Previews/%d", getpid()] isDirectory:YES]; - if (![[NSFileManager defaultManager] fileExistsAtPath:previewDirectory]) - { - if (![[NSFileManager defaultManager] createDirectoryAtPath:previewDirectory + if (![[NSFileManager defaultManager] createDirectoryAtPath:previewDirectory.path withIntermediateDirectories:YES attributes:nil error:nil]) - return nil; + { + return nil; } - return [[NSURL fileURLWithPath:previewDirectory] - URLByAppendingPathComponent:[NSString stringWithFormat:@"preview_temp.%@", type]]; + return [previewDirectory URLByAppendingPathComponent:[NSString stringWithFormat:@"preview_temp.%@", type]]; } /** @@ -149,10 +147,7 @@ } // See if there is an existing preview file, if so, delete it. - if (![[NSFileManager defaultManager] fileExistsAtPath:destURL.path]) - { - [[NSFileManager defaultManager] removeItemAtPath:destURL.path error:NULL]; - } + [[NSFileManager defaultManager] removeItemAtURL:destURL error:NULL]; HBJob *job = [[self.job copy] autorelease]; job.title = self.job.title; diff --git a/macosx/HBQueueController.m b/macosx/HBQueueController.m index b4b4c0e76..0963a453e 100644 --- a/macosx/HBQueueController.m +++ b/macosx/HBQueueController.m @@ -234,7 +234,7 @@ - (void)loadQueueFile { - NSURL *queueURL = [NSURL fileURLWithPath:[[HBUtilities appSupportPath] stringByAppendingPathComponent:@"Queue.hbqueue"]]; + NSURL *queueURL = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"Queue.hbqueue"]; _jobs = [[HBDistributedArray alloc] initWithURL:queueURL]; [self reloadQueue]; diff --git a/macosx/HBUtilities.h b/macosx/HBUtilities.h index dac8e5085..eed1cafcd 100644 --- a/macosx/HBUtilities.h +++ b/macosx/HBUtilities.h @@ -13,11 +13,6 @@ */ + (NSString *)handBrakeVersion; -/** - * Returns the path of the current /Library/Application Support/HandBrake folder. - */ -+ (NSString *)appSupportPath; - /** * Returns the url of the current /Library/Application Support/HandBrake folder. */ diff --git a/macosx/HBUtilities.m b/macosx/HBUtilities.m index 6a61e1ab1..3e4fdb272 100644 --- a/macosx/HBUtilities.m +++ b/macosx/HBUtilities.m @@ -17,19 +17,6 @@ infoDictionary[@"CFBundleVersion"]]; } -+ (NSString *)appSupportPath -{ - NSString *appSupportPath = [[NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, - NSUserDomainMask, - YES) firstObject] stringByAppendingPathComponent:@"HandBrake"]; - - NSFileManager *fileManager = [NSFileManager defaultManager]; - if (![fileManager fileExistsAtPath:appSupportPath]) - [fileManager createDirectoryAtPath:appSupportPath withIntermediateDirectories:YES attributes:nil error:NULL]; - - return appSupportPath; -} - + (NSURL *)appSupportURL { NSFileManager *fileManager = [NSFileManager defaultManager]; -- cgit v1.2.3