Skip to content

Commit

Permalink
MacGui: check if a file overwrites its source before adding it to the…
Browse files Browse the repository at this point in the history
… queue.
  • Loading branch information
galad87 committed Dec 31, 2016
1 parent 7ba7532 commit bcb1729
Showing 1 changed file with 70 additions and 59 deletions.
129 changes: 70 additions & 59 deletions macosx/HBController.m
Original file line number Diff line number Diff line change
Expand Up @@ -961,60 +961,82 @@ - (void)setQueueInfo:(NSString *)info progress:(double)progress hidden:(BOOL)hid
#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.
Expand Down Expand Up @@ -1051,43 +1073,16 @@ - (IBAction)rip:(id)sender
{
// 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:)];
}
}

Expand Down Expand Up @@ -1138,6 +1133,7 @@ - (void)doAddTitlesAtIndexesToQueue:(NSIndexSet *)indexes;
{
NSMutableArray<HBJob *> *jobs = [[NSMutableArray alloc] init];
BOOL fileExists = NO;
BOOL fileOverwritesSource = NO;

// Get the preset from the loaded job.
HBPreset *preset = [self createPresetFromCurrentSettings];
Expand Down Expand Up @@ -1173,7 +1169,22 @@ - (void)doAddTitlesAtIndexesToQueue:(NSIndexSet *)indexes;
}
}

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];
Expand Down

0 comments on commit bcb1729

Please sign in to comment.