-- (void)addMappingWithContentsOfURL:(NSURL *)url {
- NSWindow *window = popoverActivate.window;
- NSError *error;
- NJMapping *mapping = [NJMapping mappingWithContentsOfURL:url
- mappings:_mappings
- error:&error];
-
- if (mapping && !error) {
- NJMapping *mergeInto = self[mapping.name];
- if ([mergeInto hasConflictWith:mapping]) {
- 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"];
- [conflictAlert beginSheetModalForWindow:popoverActivate.window
- modalDelegate:self
- didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:)
- contextInfo:(void *)CFBridgingRetain(@{ @"old mapping": mergeInto,
- @"new mapping": mapping })];
- } else {
- [_mappings addObject:mapping];
- [self activateMapping:mapping];
- [self mappingsChanged];
- }
- }
-
- if (error) {
- [window presentError:error
- modalForWindow:window
- delegate:nil
- didPresentSelector:nil
- contextInfo:nil];
- }
-}
-
-- (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];
- [self addMappingWithContentsOfURL:panel.URL];
- }];
-
-}
-
-- (void)exportPressed:(id)sender {
- NSSavePanel *panel = [NSSavePanel savePanel];
- panel.allowedFileTypes = @[ @"enjoyable" ];
- NJMapping *mapping = _currentMapping;
- panel.nameFieldStringValue = [mapping.name stringByFixingPathComponent];
- NSWindow *window = NSApplication.sharedApplication.keyWindow;
- [panel beginSheetModalForWindow:window
- completionHandler:^(NSInteger result) {
- if (result != NSFileHandlingPanelOKButton)
- return;
- [panel close];
- NSError *error;
- [mapping writeToURL:panel.URL error:&error];
- if (error) {
- [window presentError:error
- modalForWindow:window
- delegate:nil
- didPresentSelector:nil
- contextInfo:nil];
- }
- }];
-}
-
-- (IBAction)mappingPressed:(id)sender {
- [popover showRelativeToRect:popoverActivate.bounds
- ofView:popoverActivate
- preferredEdge:NSMinXEdge];
-}
-
-- (void)popoverWillShow:(NSNotification *)notification {
- popoverActivate.state = NSOnState;
-}
-
-- (void)popoverWillClose:(NSNotification *)notification {
- popoverActivate.state = NSOffState;