+- (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];
+ }
+}
+