-- (void)addMappingWithContentsOfURL:(NSURL *)url {
- NSWindow *window = popoverActivate.window;
- NSError *error;
- NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
- mappings:_mappings
- error:&error];
-
- if (mapping && !error) {
- BOOL conflict = NO;
- NJMapping *mergeInto = self[mapping.name];
- for (id key in mapping.entries) {
- if (mergeInto.entries[key]
- && ![mergeInto.entries[key] isEqual:mapping.entries[key]]) {
- conflict = YES;
- break;
- }
- }
-
- if (conflict) {
- NSAlert *conflictAlert = [[NSAlert alloc] init];
- conflictAlert.messageText = @"Replace existing mappings?";
- conflictAlert.informativeText =
- [NSString stringWithFormat:
- @"This file contains inputs you've already mapped in \"%@\". Do you "
- @"want to merge them and replace your existing mappings, or import this "
- @"as a separate mapping?", mapping.name];
- [conflictAlert addButtonWithTitle:@"Merge"];
- [conflictAlert addButtonWithTitle:@"Cancel"];
- [conflictAlert addButtonWithTitle:@"New Mapping"];
- NSInteger res = [conflictAlert runModal];
- if (res == NSAlertSecondButtonReturn)
- return;
- else if (res == NSAlertThirdButtonReturn)
- mergeInto = nil;
- }
-
- if (mergeInto) {
- [mergeInto.entries addEntriesFromDictionary:mapping.entries];
- mapping = mergeInto;
- } else {
- [_mappings addObject:mapping];
- }
-
- [self activateMapping:mapping];
- [self mappingsChanged];
-
- if (conflict && !mergeInto) {
- [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
- [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
- }
- }
-
- if (error) {
- [window presentError:error
- modalForWindow:window
- delegate:nil
- didPresentSelector:nil
- contextInfo:nil];
+- (void)mappingConflictDidResolve:(NSAlert *)alert
+ returnCode:(NSInteger)returnCode
+ contextInfo:(void *)contextInfo {
+ NSDictionary *userInfo = CFBridgingRelease(contextInfo);
+ NJMapping *oldMapping = userInfo[@"old mapping"];
+ NJMapping *newMapping = userInfo[@"new mapping"];
+ switch (returnCode) {
+ case NSAlertFirstButtonReturn: // Merge
+ [oldMapping mergeEntriesFrom:newMapping];
+ _currentMapping = nil;
+ [self activateMapping:oldMapping];
+ [self mappingsChanged];
+ break;
+ case NSAlertThirdButtonReturn: // New Mapping
+ [self.mvc.mappingList beginUpdates];
+ [_mappings addObject:newMapping];
+ [self.mvc addedMappingAtIndex:_mappings.count - 1 startEditing:NO];
+ [self.mvc.mappingList endUpdates];
+ [self activateMapping:newMapping];
+ [self mappingsChanged];
+ break;
+ default: // Cancel, other.
+ break;