From 794561a4606fa9e31e3b2b077891f5b1e6084698 Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Fri, 1 Mar 2013 15:51:55 +0100 Subject: [PATCH] Re-import exported files. --- ApplicationController.m | 6 +- Config.m | 2 +- ConfigsController.h | 4 + ConfigsController.m | 74 +++++++++ English.lproj/MainMenu.xib | 239 ++++++++++++---------------- Enjoyable.xcodeproj/project.pbxproj | 6 + Enjoyable_Prefix.pch | 1 + NSError+Description.h | 9 ++ NSError+Description.m | 13 ++ 9 files changed, 212 insertions(+), 142 deletions(-) create mode 100644 NSError+Description.h create mode 100644 NSError+Description.m diff --git a/ApplicationController.m b/ApplicationController.m index b86a5f2..4454565 100644 --- a/ApplicationController.m +++ b/ApplicationController.m @@ -44,17 +44,15 @@ - (IBAction)toggleActivity:(id)sender { BOOL sendRealEvents = !self.jsController.sendingRealEvents; self.jsController.sendingRealEvents = sendRealEvents; - activeButton.label = sendRealEvents ? @"Stop" : @"Start"; activeButton.image = [NSImage imageNamed:sendRealEvents ? @"NSStopProgressFreestandingTemplate" : @"NSGoRightTemplate"]; activeMenuItem.state = sendRealEvents; } - (NSInteger)firstConfigMenuIndex { - NSInteger count = dockMenuBase.numberOfItems; - for (int i = 0; i < count; ++i) + for (NSInteger i = dockMenuBase.numberOfItems - 1; i >= 0; --i) if ([dockMenuBase itemAtIndex:i].isSeparatorItem) return i + 1; - return count; + return dockMenuBase.numberOfItems; } - (void)configsChanged { diff --git a/Config.m b/Config.m index a02c4ce..55162b0 100644 --- a/Config.m +++ b/Config.m @@ -13,7 +13,7 @@ - (id)initWithName:(NSString *)name { if ((self = [super init])) { - self.name = name; + self.name = name ? name : @"Untitled"; _entries = [[NSMutableDictionary alloc] init]; } return self; diff --git a/ConfigsController.h b/ConfigsController.h index fac1972..369d7f7 100644 --- a/ConfigsController.h +++ b/ConfigsController.h @@ -19,8 +19,12 @@ @property (readonly) Config *currentConfig; @property (readonly) NSArray *configs; +- (Config *)objectForKeyedSubscript:(NSString *)name; + + - (IBAction)addPressed:(id)sender; - (IBAction)removePressed:(id)sender; +- (IBAction)importPressed:(id)sender; - (IBAction)exportPressed:(id)sender; - (void)activateConfig:(Config *)config; - (void)activateConfigForProcess:(NSString *)processName; diff --git a/ConfigsController.m b/ConfigsController.m index 17cf8fd..9693985 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -148,6 +148,80 @@ } } +- (void)importPressed:(id)sender { + NSOpenPanel *panel = [NSOpenPanel openPanel]; + panel.allowedFileTypes = @[ @"enjoyable", @"json", @"txt" ]; + if ([panel runModal] == NSFileHandlingPanelOKButton) { + NSError *error; + NSInputStream *stream = [NSInputStream inputStreamWithURL:panel.URL]; + [stream open]; + NSDictionary *serialization = !error + ? [NSJSONSerialization JSONObjectWithStream:stream options:0 error:&error] + : nil; + [stream close]; + + if (!([serialization isKindOfClass:[NSDictionary class]] + && serialization[@"entries"])) { + error = [NSError errorWithDomain:@"Enjoyable" + code:0 + description:@"This isn't a valid mapping file."]; + } + + + if (!error) { + NSDictionary *entries = serialization[@"entries"]; + Config *cfg = [[Config alloc] initWithName:serialization[@"name"]]; + Config *mergeInto = self[cfg.name]; + BOOL conflict = NO; + for (id key in entries) { + cfg.entries[key] = [Target targetDeserialize:entries[key] + withConfigs:_configs]; + if (mergeInto.entries[key]) + conflict = YES; + } + + 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) + [[NSAlert alertWithError:error] runModal]; + } +} + - (void)exportPressed:(id)sender { NSSavePanel *panel = [NSSavePanel savePanel]; panel.allowedFileTypes = @[ @"enjoyable" ]; diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index 056a907..9c7050a 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -35,8 +35,6 @@ NSToolbar NSToolbarFlexibleSpaceItem NSToolbarItem - NSToolbarSeparatorItem - NSToolbarSpaceItem NSView NSWindowTemplate @@ -176,7 +174,7 @@ - Configurations + Mappings 1048576 2147483647 @@ -184,12 +182,49 @@ submenuAction: - Configurations + Mappings - Enable mapping - . + Enable + r + 1048576 + 2147483647 + + + + + + YES + YES + + + 2147483647 + + + + + + List + l + 1048576 + 2147483647 + + + + + + Import... + o + 1048576 + 2147483647 + + + + + + Export... + s 1048576 2147483647 @@ -291,7 +326,7 @@ 42CA6E7F-AC4A-4681-98B6-B9901269E463 - Start + Start/Stop @@ -309,17 +344,17 @@ YES 0 - + - EC49C240-1D96-41C2-AD75-D75EEB29EDEA + A03535DA-B573-44B2-86A0-5E261E68FC16 - Configurations + Configurations NSImage - NSMultipleDocuments + NSListViewTemplate @@ -358,72 +393,16 @@ - - NSToolbarSeparatorItem - - Separator - - - - - - {12, 5} - {12, 1000} - YES - YES - -1 - YES - 0 - - YES - YES - - - 1048576 - 2147483647 - - - - - - NSToolbarSpaceItem - - Space - - - - - - {32, 5} - {32, 32} - YES - YES - -1 - YES - 0 - - YES - YES - - - 1048576 - 2147483647 - - - - - - - + - - + + @@ -981,31 +960,6 @@ 256 - - - 297 - {{226, 1}, {82, 32}} - - - - _NS:9 - YES - - -2080374784 - 134217728 - Export - - _NS:9 - - -2034876416 - 129 - - - 400 - 75 - - NO - 274 @@ -1156,7 +1110,7 @@ {{57, 4}, {39, 28}} - + YES 603979776 @@ -1314,6 +1268,14 @@ 498 + + + toggle: + + + + 811 + delegate @@ -1478,17 +1440,17 @@ exportPressed: - + - 784 + 815 - - exportButton + + importPressed: - + - 785 + 816 @@ -1702,6 +1664,10 @@ + + + + @@ -1827,7 +1793,6 @@ - Drawer Content View @@ -1896,29 +1861,12 @@ 487 - - - + - - 490 - - - - - 492 - - - - - 493 - - - 495 @@ -2195,17 +2143,29 @@ - 782 - - - - - + 790 + + + + + 810 + + + + + 812 + + + + + 813 + + - 783 - - + 814 + + @@ -2245,9 +2205,6 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2296,9 +2253,12 @@ com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin - com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin @@ -2306,7 +2266,7 @@ - 785 + 816 @@ -2379,6 +2339,7 @@ id id + id id @@ -2390,6 +2351,10 @@ exportPressed: id + + importPressed: + id + removePressed: id @@ -2568,9 +2533,9 @@ {8, 8} {9, 9} + {11, 10} {11, 11} {10, 3} - {32, 32} {16, 15} {8, 8} diff --git a/Enjoyable.xcodeproj/project.pbxproj b/Enjoyable.xcodeproj/project.pbxproj index aac5494..2c79e25 100644 --- a/Enjoyable.xcodeproj/project.pbxproj +++ b/Enjoyable.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ D5F809710FB093400006A4DE /* TargetConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = D5F809700FB093400006A4DE /* TargetConfig.m */; }; D5F80A9D0FB0A2FF0006A4DE /* icon.icns in Resources */ = {isa = PBXBuildFile; fileRef = D5617A080FAEAF8300928B3A /* icon.icns */; }; EE1D7C9216E01E7000B000EB /* NSView+FirstResponder.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1D7C9116E01E7000B000EB /* NSView+FirstResponder.m */; }; + EE1D7C9616E0ECCF00B000EB /* NSError+Description.m in Sources */ = {isa = PBXBuildFile; fileRef = EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */; }; /* End PBXBuildFile section */ /* Begin PBXFileReference section */ @@ -88,6 +89,8 @@ D5F809700FB093400006A4DE /* TargetConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TargetConfig.m; sourceTree = ""; }; EE1D7C9016E01E7000B000EB /* NSView+FirstResponder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSView+FirstResponder.h"; sourceTree = ""; }; EE1D7C9116E01E7000B000EB /* NSView+FirstResponder.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSView+FirstResponder.m"; sourceTree = ""; }; + EE1D7C9416E0ECCF00B000EB /* NSError+Description.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSError+Description.h"; sourceTree = ""; }; + EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSError+Description.m"; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -189,6 +192,8 @@ 29B97315FDCFA39411CA2CEA /* Other Sources */ = { isa = PBXGroup; children = ( + EE1D7C9416E0ECCF00B000EB /* NSError+Description.h */, + EE1D7C9516E0ECCF00B000EB /* NSError+Description.m */, 32CA4F630368D1EE00C91783 /* Enjoyable_Prefix.pch */, 29B97316FDCFA39411CA2CEA /* main.m */, EE1D7C9016E01E7000B000EB /* NSView+FirstResponder.h */, @@ -305,6 +310,7 @@ 8BEFAD9C15C46BFF00823AEC /* TargetMouseScroll.m in Sources */, 8BEFADA015C476DC00823AEC /* TargetToggleMouseScope.m in Sources */, EE1D7C9216E01E7000B000EB /* NSView+FirstResponder.m in Sources */, + EE1D7C9616E0ECCF00B000EB /* NSError+Description.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/Enjoyable_Prefix.pch b/Enjoyable_Prefix.pch index 1f44b2f..86806fe 100644 --- a/Enjoyable_Prefix.pch +++ b/Enjoyable_Prefix.pch @@ -9,3 +9,4 @@ #import #import "NSView+FirstResponder.h" +#import "NSError+Description.h" diff --git a/NSError+Description.h b/NSError+Description.h new file mode 100644 index 0000000..a722e9e --- /dev/null +++ b/NSError+Description.h @@ -0,0 +1,9 @@ +#import + +@interface NSError (Description) + ++ (NSError *)errorWithDomain:(NSString *)domain + code:(NSInteger)code + description:(NSString *)description; + +@end diff --git a/NSError+Description.m b/NSError+Description.m new file mode 100644 index 0000000..126bb17 --- /dev/null +++ b/NSError+Description.m @@ -0,0 +1,13 @@ +#import "NSError+Description.h" + +@implementation NSError (Description) + ++ (NSError *)errorWithDomain:(NSString *)domain + code:(NSInteger)code + description:(NSString *)description { + NSDictionary *errorDict = @{ NSLocalizedDescriptionKey : description }; + return [NSError errorWithDomain:domain code:code userInfo:errorDict]; + +} + +@end -- 2.20.1