From 9584becb2e5469e8f482d73e6f0c9c3bf879e0db Mon Sep 17 00:00:00 2001 From: Joe Wreschnig Date: Fri, 1 Mar 2013 13:59:58 +0100 Subject: [PATCH] Allow JSON export of configurations. --- Config.h | 1 + Config.m | 7 ++ ConfigsController.h | 4 +- ConfigsController.m | 34 ++++++--- English.lproj/MainMenu.xib | 148 ++++++++++++++++++++++++++----------- 5 files changed, 138 insertions(+), 56 deletions(-) diff --git a/Config.h b/Config.h index cc96f2a..72f0084 100644 --- a/Config.h +++ b/Config.h @@ -17,5 +17,6 @@ - (id)initWithName:(NSString *)name; - (Target *)objectForKeyedSubscript:(JSAction *)action; - (void)setObject:(Target *)target forKeyedSubscript:(JSAction *)action; +- (NSDictionary *)serialize; @end diff --git a/Config.m b/Config.m index 5a83ed3..a02c4ce 100644 --- a/Config.m +++ b/Config.m @@ -32,4 +32,11 @@ } } +- (NSDictionary *)serialize { + NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:_entries.count]; + for (id key in _entries) + cfgEntries[key] = [_entries[key] serialize]; + return @{ @"name": _name, @"entries": cfgEntries }; +} + @end diff --git a/ConfigsController.h b/ConfigsController.h index 9d6d9a1..fac1972 100644 --- a/ConfigsController.h +++ b/ConfigsController.h @@ -9,10 +9,11 @@ @class Config; @class TargetController; -@interface ConfigsController : NSObject { +@interface ConfigsController : NSObject { IBOutlet NSButton *removeButton; IBOutlet NSTableView *tableView; IBOutlet TargetController *targetController; + IBOutlet NSButton *exportButton; } @property (readonly) Config *currentConfig; @@ -20,6 +21,7 @@ - (IBAction)addPressed:(id)sender; - (IBAction)removePressed:(id)sender; +- (IBAction)exportPressed:(id)sender; - (void)activateConfig:(Config *)config; - (void)activateConfigForProcess:(NSString *)processName; diff --git a/ConfigsController.m b/ConfigsController.m index 8870fab..17cf8fd 100644 --- a/ConfigsController.m +++ b/ConfigsController.m @@ -113,21 +113,14 @@ - (NSDictionary *)dumpAll { NSMutableArray *ary = [[NSMutableArray alloc] initWithCapacity:_configs.count]; - for (Config *config in _configs) { - NSMutableDictionary* cfgEntries = [[NSMutableDictionary alloc] initWithCapacity:config.entries.count]; - for (id key in config.entries) - cfgEntries[key] = [config.entries[key] serialize]; - [ary addObject:@{ @"name": config.name, - @"entries": cfgEntries, - }]; - } + for (Config *config in _configs) + [ary addObject:[config serialize]]; NSUInteger current = _currentConfig ? [_configs indexOfObject:_currentConfig] : 0; - return @{ @"configurationList": ary, - @"selectedConfiguration": @(current) }; + return @{ @"configurations": ary, @"selected": @(current) }; } - (void)loadAllFrom:(NSDictionary*) envelope{ - NSArray *storedConfigs = envelope[@"configurationList"]; + NSArray *storedConfigs = envelope[@"configurations"]; NSMutableArray* newConfigs = [[NSMutableArray alloc] initWithCapacity:storedConfigs.count]; // have to do two passes in case config1 refers to config2 via a TargetConfig @@ -145,7 +138,7 @@ } if (newConfigs.count) { - unsigned current = [envelope[@"selectedConfiguration"] unsignedIntValue]; + unsigned current = [envelope[@"selected"] unsignedIntValue]; if (current >= newConfigs.count) current = 0; _configs = newConfigs; @@ -155,4 +148,21 @@ } } +- (void)exportPressed:(id)sender { + NSSavePanel *panel = [NSSavePanel savePanel]; + panel.allowedFileTypes = @[ @"enjoyable" ]; + if ([panel runModal] == NSFileHandlingPanelOKButton) { + NSError *error; + NSDictionary *serialization = [_currentConfig serialize]; + NSData *json = [NSJSONSerialization dataWithJSONObject:serialization + options:NSJSONWritingPrettyPrinted + error:&error]; + if (!error) + [json writeToURL:panel.URL options:NSDataWritingAtomic error:&error]; + + if (error) + [[NSAlert alertWithError:error] runModal]; + } +} + @end diff --git a/English.lproj/MainMenu.xib b/English.lproj/MainMenu.xib index 1b4989d..056a907 100644 --- a/English.lproj/MainMenu.xib +++ b/English.lproj/MainMenu.xib @@ -189,7 +189,8 @@ Enable mapping - + . + 1048576 2147483647 @@ -429,7 +430,7 @@ {640, 300} - + 256 @@ -453,7 +454,8 @@ 256 {200, 298} - + + YES NO YES @@ -550,6 +552,7 @@ {{1, 1}, {200, 298}} + @@ -560,6 +563,7 @@ -2147483392 {{1, 1}, {8, 298}} + NO @@ -571,6 +575,7 @@ -2147483392 {{-100, -100}, {473, 15}} + NO 1 @@ -581,7 +586,8 @@ {202, 300} - + + 133650 @@ -594,6 +600,7 @@ {202, 300} + NSView @@ -606,6 +613,7 @@ 268 {{227, 123}, {180, 24}} + _NS:9 YES @@ -642,6 +650,8 @@ 268 {{227, 55}, {180, 24}} + + _NS:9 YES @@ -673,6 +683,7 @@ 268 {{227, 89}, {180, 24}} + _NS:9 YES @@ -705,6 +716,8 @@ 268 {{20, 16}, {201, 236}} + + NO 7 1 @@ -848,6 +861,7 @@ 268 {{226, 156}, {182, 26}} + YES @@ -880,6 +894,7 @@ 266 {{0, 269}, {429, 17}} + YES @@ -903,6 +918,7 @@ 12 {{0, 258}, {465, 5}} + {0, 0} @@ -931,23 +947,29 @@ 265 {{229, 190}, {176, 24}} + + _NS:9 KeyInputTextView {{211, 0}, {429, 300}} + NSView {640, 300} + YES {640, 300} + + {{0, 0}, {1440, 878}} @@ -956,9 +978,34 @@ YES - + 256 + + + 297 + {{226, 1}, {82, 32}} + + + + _NS:9 + YES + + -2080374784 + 134217728 + Export + + _NS:9 + + -2034876416 + 129 + + + 400 + 75 + + NO + 274 @@ -972,6 +1019,7 @@ 256 {320, 418} + YES NO @@ -1030,6 +1078,7 @@ {{1, 1}, {320, 418}} + @@ -1040,6 +1089,7 @@ -2147483392 {{306, 1}, {15, 403}} + NO @@ -1051,6 +1101,7 @@ -2147483392 {{1, 404}, {305, 15}} + NO 1 @@ -1061,6 +1112,7 @@ {{0, 34}, {322, 420}} + 133682 @@ -1076,6 +1128,7 @@ 260 {{10, 4}, {39, 28}} + YES @@ -1102,6 +1155,8 @@ 260 {{57, 4}, {39, 28}} + + YES 603979776 @@ -1124,13 +1179,15 @@ {322, 454} + + NSView {200, 100} - {150, 0} + {200, 0} {300, 10000} 2 0.0 @@ -1150,28 +1207,6 @@ TargetController - - - 268 - {82, 32} - _NS:9 - YES - - 67108864 - 134217728 - Button - - _NS:9 - - -2038284288 - 129 - - - 200 - 25 - - NO - @@ -1439,6 +1474,22 @@ 697 + + + exportPressed: + + + + 784 + + + + exportButton + + + + 785 + dataSource @@ -1776,6 +1827,7 @@ + Drawer Content View @@ -2138,22 +2190,22 @@ - 765 - - - - - + 778 + + - 766 - - + 782 + + + + + - 778 - - + 783 + + @@ -2243,9 +2295,9 @@ 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 @@ -2254,7 +2306,7 @@ - 781 + 785 @@ -2326,6 +2378,7 @@ NSObject id + id id @@ -2333,17 +2386,26 @@ addPressed: id + + exportPressed: + id + removePressed: id + NSButton NSButton NSTableView TargetController + + exportButton + NSButton + removeButton NSButton -- 2.30.2