]> granicus.if.org Git - handbrake/commitdiff
MacGui: remove logs older than a month in the EncodeLogs folder.
authorritsuka <damiog@gmail.com>
Fri, 20 Feb 2015 12:23:27 +0000 (12:23 +0000)
committerritsuka <damiog@gmail.com>
Fri, 20 Feb 2015 12:23:27 +0000 (12:23 +0000)
git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@6933 b64f7644-9d1e-0410-96f1-a4d463321fa5

macosx/HBAppDelegate.m
macosx/HBUtilities.h
macosx/HBUtilities.m

index 2d8259b226467705ae6936133bcab3bc49a9ef4a..42c18435aabf89daf0d75521afc579c5d3147142 100644 (file)
@@ -54,7 +54,7 @@
         [HBUtilities writeToActivityLog: "%s", versionStringFull.UTF8String];
 
         // we init the HBPresetsManager
-        NSURL *presetsURL = [NSURL fileURLWithPath:[[HBUtilities appSupportPath] stringByAppendingPathComponent:@"UserPresets.plist"]];
+        NSURL *presetsURL = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"UserPresets.plist"];
         _presetsManager = [[HBPresetsManager alloc] initWithURL:presetsURL];
 
         _queueController = [[HBQueueController alloc] init];
     // Open queue window now if it was visible when HB was closed
     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];
+    });
 }
 
 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)app
     return YES;
 }
 
+#pragma mark - Clean ups
+
+/**
+ *  Clears the EncodeLogs folder, removes the logs
+ *  older than a month.
+ */
+- (void)cleanEncodeLogs
+{
+    NSURL *directoryUrl = [[HBUtilities appSupportURL] URLByAppendingPathComponent:@"EncodeLogs"];
+
+    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];
+
+    for (NSURL *fileURL in contents)
+    {
+        NSDate *creationDate = nil;
+        [fileURL getResourceValue:&creationDate forKey:NSURLCreationDateKey error:NULL];
+        if ([creationDate isLessThan:limit])
+        {
+            [manager removeItemAtURL:fileURL error:NULL];
+        }
+    }
+
+    [manager release];
+}
+
 #pragma mark - Menu actions
 
 - (IBAction)rip:(id)sender
index 2e073d17a7c8cb5f1a23931e70bcf64ef855b381..dac8e508575ce87e4a2cac0478b618dcd184e680 100644 (file)
  */
 + (NSString *)appSupportPath;
 
+/**
+ *  Returns the url of the current <user>/Library/Application Support/HandBrake folder.
+ */
++ (NSURL *)appSupportURL;
 /**
  *  Writes a message to standard error.
  *  The message will show up in the output panel and in the activity log.
index c661b2a261706d781ed78d32fd296a612264f9bb..6a61e1ab17884f24607910fb887cea3d841a9fbc 100644 (file)
     return appSupportPath;
 }
 
++ (NSURL *)appSupportURL
+{
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    NSURL *appSupportURL = [[[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]
+                             firstObject] URLByAppendingPathComponent:@"HandBrake"];
+
+    if (![fileManager fileExistsAtPath:appSupportURL.path])
+    {
+        [fileManager createDirectoryAtPath:appSupportURL.path withIntermediateDirectories:YES attributes:nil error:NULL];
+    }
+
+    return appSupportURL;
+}
+
 + (void)writeToActivityLog:(const char *)format, ...
 {
     va_list args;