+
+ NSDictionary *entries = serialization[@"entries"];
+ Config *cfg = [[Config alloc] initWithName:serialization[@"name"]];
+ for (id key in entries) {
+ NSDictionary *value = entries[key];
+ if ([key isKindOfClass:NSString.class]) {
+ Target *target = [Target targetDeserialize:value
+ withConfigs:_configs];
+ if (target)
+ cfg.entries[key] = target;
+ }
+ }
+ return cfg;
+}
+
+- (void)importPressed:(id)sender {
+ NSOpenPanel *panel = [NSOpenPanel openPanel];
+ panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ];
+ NSWindow *window = NSApplication.sharedApplication.keyWindow;
+ [panel beginSheetModalForWindow:window
+ completionHandler:^(NSInteger result) {
+ if (result != NSFileHandlingPanelOKButton)
+ return;
+
+ [panel close];
+ NSError *error;
+ Config *cfg = [self configWithURL:panel.URL error:&error];
+
+ if (!error) {
+ BOOL conflict;
+ Config *mergeInto = self[cfg.name];
+ for (id key in cfg.entries) {
+ if (mergeInto.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?", cfg.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:cfg.entries];
+ cfg = mergeInto;
+ } else {
+ [_configs addObject:cfg];
+ [tableView reloadData];
+ }
+
+ [self save];
+ [(ApplicationController *)NSApplication.sharedApplication.delegate configsChanged];
+ [self activateConfig:cfg];
+ [targetController loadCurrent];
+
+ if (conflict && !mergeInto) {
+ [tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:_configs.count - 1] byExtendingSelection:NO];
+ [tableView editColumn:0 row:_configs.count - 1 withEvent:nil select:YES];
+ }
+ }
+
+ if (error) {
+ [window presentError:error
+ modalForWindow:window
+ delegate:nil
+ didPresentSelector:nil
+ contextInfo:nil];
+ }
+ }];
+
+}
+
+- (void)exportPressed:(id)sender {
+ NSSavePanel *panel = [NSSavePanel savePanel];
+ panel.allowedFileTypes = @[ @"enjoyable" ];
+ Config *cfg = _currentConfig;
+ panel.nameFieldStringValue = cfg.name;
+ NSWindow *window = NSApplication.sharedApplication.keyWindow;
+ [panel beginSheetModalForWindow:window
+ completionHandler:^(NSInteger result) {
+ if (result != NSFileHandlingPanelOKButton)
+ return;
+ [panel close];
+ NSError *error;
+ NSDictionary *serialization = [cfg serialize];
+ NSData *json = [NSJSONSerialization dataWithJSONObject:serialization
+ options:NSJSONWritingPrettyPrinted
+ error:&error];
+ if (!error)
+ [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error];
+
+ if (error) {
+ [window presentError:error
+ modalForWindow:window
+ delegate:nil
+ didPresentSelector:nil
+ contextInfo:nil];
+ }
+ }];