-- (NJMapping *)mappingWithURL:(NSURL *)url error:(NSError **)error {
- NSInputStream *stream = [NSInputStream inputStreamWithURL:url];
- [stream open];
- NSDictionary *serialization = !*error
- ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:error]
- : nil;
- [stream close];
-
- if (!([serialization isKindOfClass:NSDictionary.class]
- && [serialization[@"name"] isKindOfClass:NSString.class]
- && [serialization[@"entries"] isKindOfClass:NSDictionary.class])) {
- *error = [NSError errorWithDomain:@"Enjoyable"
- code:0
- description:@"This isn't a valid mapping file."];
- return nil;
- }
-
- NSDictionary *entries = serialization[@"entries"];
- NJMapping *mapping = [[NJMapping alloc] initWithName:serialization[@"name"]];
- for (id key in entries) {
- NSDictionary *value = entries[key];
- if ([key isKindOfClass:NSString.class]) {
- NJOutput *output = [NJOutput outputDeserialize:value
- withMappings:_mappings];
- if (output)
- mapping.entries[key] = output;
- }
+- (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];
+ [self activateMapping:oldMapping];
+ [self mappingsChanged];
+ break;
+ case NSAlertThirdButtonReturn: // New Mapping
+ [_mappings addObject:newMapping];
+ [self activateMapping:newMapping];
+ [self mappingsChanged];
+ [self mappingPressed:alert];
+ [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_mappings.count - 1] byExtendingSelection:NO];
+ [tableView editColumn:0 row:_mappings.count - 1 withEvent:nil select:YES];
+ break;
+ default: // Cancel, other.
+ break;