]> granicus.if.org Git - handbrake/commitdiff
MacGui: check if a file overwrites its source before adding it to the queue.
authorDamiano Galassi <damiog@gmail.com>
Sat, 31 Dec 2016 11:04:06 +0000 (12:04 +0100)
committerDamiano Galassi <damiog@gmail.com>
Sat, 31 Dec 2016 11:06:28 +0000 (12:06 +0100)
(cherry picked from commit bcb1729a994d1e508584340c8fd82075223b3824)

macosx/HBController.m

index f28b15c035f7fa6db18bfc78005b3a4a7c606576..ae165cd34d8bb954099ce81ae0620ba61e5436ea 100644 (file)
 #pragma mark - Job Handling
 
 /**
- *  Actually adds a job to the queue
- */
-- (void)doAddToQueue
-{
-    [fQueueController addJob:[self.job copy]];
-}
+ Check if the job destination if a valid one,
+ if so, call the didEndSelector
+ Note: rework this to use a block in the future
 
-/**
- * Puts up an alert before ultimately calling doAddToQueue
+ @param job the job
+ @param didEndSelector the selector to call if the check is successful
  */
-- (IBAction)addToQueue:(id)sender
+- (void)runDestinationAlerts:(HBJob *)job didEndSelector:(SEL)didEndSelector
 {
-       // We get the destination directory from the destination field here
-       NSString *destinationDirectory = self.job.destURL.path.stringByDeletingLastPathComponent;
-       // We check for a valid destination here
-       if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
-       {
+    NSString *destinationDirectory = job.destURL.path.stringByDeletingLastPathComponent;
+
+    if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0)
+    {
         NSAlert *alert = [[NSAlert alloc] init];
         [alert setMessageText:NSLocalizedString(@"Warning!", @"")];
         [alert setInformativeText:NSLocalizedString(@"This is not a valid destination directory!", @"")];
-        [alert runModal];
-        return;
-       }
-
-       if ([[NSFileManager defaultManager] fileExistsAtPath:self.job.destURL.path])
+        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:didEndSelector contextInfo:NULL];
+    }
+    else if ([job.fileURL isEqual:job.destURL])
     {
-        // File exist, warn user
         NSAlert *alert = [[NSAlert alloc] init];
-        [alert setMessageText:NSLocalizedString(@"File already exists.", @"")];
-        [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to overwrite %@?", @""), self.job.destURL.path]];
+        [alert setMessageText:NSLocalizedString(@"A file already exists at the selected destination.", @"")];
+        [alert setInformativeText:NSLocalizedString(@"The destination is the same as the source, you can not overwrite your source file!", @"")];
+        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:didEndSelector contextInfo:NULL];
+    }
+    else if ([[NSFileManager defaultManager] fileExistsAtPath:job.destURL.path])
+    {
+        NSAlert *alert = [[NSAlert alloc] init];
+        [alert setMessageText:NSLocalizedString(@"A file already exists at the selected destination.", @"")];
+        [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to overwrite %@?", @""), job.destURL.path]];
         [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
         [alert addButtonWithTitle:NSLocalizedString(@"Overwrite", @"")];
         [alert setAlertStyle:NSCriticalAlertStyle];
 
-        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(overwriteAddToQueueAlertDone:returnCode:contextInfo:) contextInfo:NULL];
+        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:didEndSelector contextInfo:NULL];
     }
-    else if ([fQueueController jobExistAtURL:self.job.destURL])
+    else if ([fQueueController jobExistAtURL:job.destURL])
     {
-        // File exist in queue, warn user
         NSAlert *alert = [[NSAlert alloc] init];
         [alert setMessageText:NSLocalizedString(@"There is already a queue item for this destination.", @"")];
-        [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to overwrite %@?", @""), self.job.destURL.path]];
+        [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to overwrite %@?", @""), job.destURL.path]];
         [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
         [alert addButtonWithTitle:NSLocalizedString(@"Overwrite", @"")];
         [alert setAlertStyle:NSCriticalAlertStyle];
 
-        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(overwriteAddToQueueAlertDone:returnCode:contextInfo:) contextInfo:NULL];
+        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:didEndSelector contextInfo:NULL];
     }
     else
     {
-        [self doAddToQueue];
+        NSInteger returnCode = NSAlertSecondButtonReturn;
+        NSMethodSignature *methodSignature = [self methodSignatureForSelector:didEndSelector];
+        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
+        [invocation setTarget:self];
+        [invocation setSelector:didEndSelector];
+        [invocation setArgument:&returnCode atIndex:3];
+        [invocation invoke];
     }
 }
 
+/**
+ *  Actually adds a job to the queue
+ */
+- (void)doAddToQueue
+{
+    [fQueueController addJob:[self.job copy]];
+}
+
+/**
+ * Puts up an alert before ultimately calling doAddToQueue
+ */
+- (IBAction)addToQueue:(id)sender
+{
+    [self runDestinationAlerts:self.job
+                didEndSelector:@selector(overwriteAddToQueueAlertDone:returnCode:contextInfo:)];
+}
+
 /**
  * Called from the alert posted by addToQueue
  * that asks the user if they want to overwrite an exiting movie file.
        {
         // Displays an alert asking user if the want to cancel encoding of current job.
         [fQueueController cancelRip:self];
-        return;
     }
-
     // If there are pending jobs in the queue, then this is a rip the queue
-    if (fQueueController.pendingItemsCount > 0)
+    else if (fQueueController.pendingItemsCount > 0)
     {
         [fQueueController rip:self];
-        return;
-    }
-
-    // Before adding jobs to the queue, check for a valid destination.
-    NSString *destinationDirectory = self.job.destURL.path.stringByDeletingLastPathComponent;
-    if ([[NSFileManager defaultManager] fileExistsAtPath:destinationDirectory] == 0) 
-    {
-        NSAlert *alert = [[NSAlert alloc] init];
-        [alert setMessageText:NSLocalizedString(@"Invalid destination.", @"")];
-        [alert setInformativeText:NSLocalizedString(@"The current destination folder is not a valid.", @"")];
-        [alert runModal];
-        return;
-    }
-
-    // We check for duplicate name here
-    if ([[NSFileManager defaultManager] fileExistsAtPath:self.job.destURL.path])
-    {
-        NSAlert *alert = [[NSAlert alloc] init];
-        [alert setMessageText:NSLocalizedString(@"A file already exists at the selected destination.", @"")];
-        [alert setInformativeText:[NSString stringWithFormat:NSLocalizedString(@"Do you want to overwrite %@?", @""), self.job.destURL.path]];
-        [alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"")];
-        [alert addButtonWithTitle:NSLocalizedString(@"Overwrite", @"")];
-        [alert setAlertStyle:NSCriticalAlertStyle];
-
-        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(overWriteAlertDone:returnCode:contextInfo:) contextInfo:NULL];
-        // overWriteAlertDone: will be called when the alert is dismissed. It will call doRip.
     }
     else
     {
-        [self doRip];
+        [self runDestinationAlerts:self.job
+                    didEndSelector:@selector(overWriteAlertDone:returnCode:contextInfo:)];
     }
 }
 
 {
     NSMutableArray<HBJob *> *jobs = [[NSMutableArray alloc] init];
     BOOL fileExists = NO;
+    BOOL fileOverwritesSource = NO;
 
     // Get the preset from the loaded job.
     HBPreset *preset = [self createPresetFromCurrentSettings];
         }
     }
 
-    if (fileExists)
+    for (HBJob *job in jobs)
+    {
+        if ([job.fileURL isEqual:job.destURL]) {
+            fileOverwritesSource = YES;
+            break;
+        }
+    }
+
+    if (fileOverwritesSource)
+    {
+        NSAlert *alert = [[NSAlert alloc] init];
+        [alert setMessageText:NSLocalizedString(@"A file already exists at the selected destination.", @"")];
+        [alert setInformativeText:NSLocalizedString(@"The destination is the same as the source, you can not overwrite your source file!", @"")];
+        [alert beginSheetModalForWindow:self.window modalDelegate:self didEndSelector:@selector(overwriteAddTitlesToQueueAlertDone:returnCode:contextInfo:) contextInfo:NULL];
+    }
+    else if (fileExists)
     {
         // File exist, warn user
         NSAlert *alert = [[NSAlert alloc] init];