Fix duplicate entries when merges don't conflict.
[enjoyable.git] / Classes / NJMappingsController.m
index 0db1111..4a9c3ff 100644 (file)
@@ -22,7 +22,8 @@
 - (id)init {
     if ((self = [super init])) {
         _mappings = [[NSMutableArray alloc] init];
-        _currentMapping = [[NJMapping alloc] initWithName:@"(default)"];
+        _currentMapping = [[NJMapping alloc] initWithName:
+                           NSLocalizedString(@"(default)", @"default name for first the mapping")];
         _manualMapping = _currentMapping;
         [_mappings addObject:_currentMapping];
     }
@@ -79,7 +80,9 @@
 
     if (!found) {
         [self activateMapping:oldMapping];
-        if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]) {
+        if ([oldMapping.name.lowercaseString isEqualToString:@"@application"]
+            || [oldMapping.name.lowercaseString isEqualToString:
+                NSLocalizedString(@"@Application", nil).lowercaseString]) {
             oldMapping.name = app.bestMappingName;
             [self mappingsChanged];
         }
 }
 
 - (IBAction)addPressed:(id)sender {
-    NJMapping *newMapping = [[NJMapping alloc] initWithName:@"Untitled"];
+    [self mappingPressed:sender];
+    NJMapping *newMapping = [[NJMapping alloc] init];
     [_mappings addObject:newMapping];
     [self activateMapping:newMapping];
     [self mappingsChanged];
     }
 }
 
+- (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;
+    }
+}
+
 - (void)addMappingWithContentsOfURL:(NSURL *)url {
     NSWindow *window = popoverActivate.window;
     NSError *error;
     
     if (mapping && !error) {
         NJMapping *mergeInto = self[mapping.name];
-        BOOL conflict = [mergeInto hasConflictWith:mapping];
-        
-        if (conflict) {
+        if ([mergeInto hasConflictWith:mapping]) {
             NSAlert *conflictAlert = [[NSAlert alloc] init];
-            conflictAlert.messageText = @"Replace existing mappings?";
+            conflictAlert.messageText = NSLocalizedString(@"import conflict prompt", @"Title of import conflict alert");
             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) {
+            [NSString stringWithFormat:NSLocalizedString(@"import conflict in %@", @"Explanation of import conflict"),
+                                       mapping.name];
+            [conflictAlert addButtonWithTitle:NSLocalizedString(@"import and merge", @"button to merge imported mappings")];
+            [conflictAlert addButtonWithTitle:NSLocalizedString(@"cancel import", @"button to cancel import")];
+            [conflictAlert addButtonWithTitle:NSLocalizedString(@"import new mapping", @"button to import as new mapping")];
+            [conflictAlert beginSheetModalForWindow:popoverActivate.window
+                                      modalDelegate:self
+                                     didEndSelector:@selector(mappingConflictDidResolve:returnCode:contextInfo:)
+                                        contextInfo:(void *)CFBridgingRetain(@{ @"old mapping": mergeInto,
+                                                                                @"new mapping": mapping })];
+        } else if (mergeInto) {
             [mergeInto mergeEntriesFrom:mapping];
-            mapping = mergeInto;
+            [self activateMapping:mergeInto];
+            [self mappingsChanged];
         } 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];
+            [self activateMapping:mapping];
+            [self mappingsChanged];
         }
     }
-    
+
     if (error) {
         [window presentError:error
               modalForWindow:window