From 7fd91ae86082895956de0aa70ef64e7bcea5cc16 Mon Sep 17 00:00:00 2001 From: masakih Date: Tue, 11 Feb 2014 10:45:51 +0900 Subject: [PATCH] =?utf8?q?=E3=81=84=E3=82=8D=E3=81=84=E3=82=8D=E8=BF=BD?= =?utf8?q?=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- KCD.xcodeproj/project.pbxproj | 16 +++ KCD/Base.lproj/MainMenu.xib | 7 +- KCD/HMAppDelegate.h | 6 + KCD/HMAppDelegate.m | 3 + KCD/HMJSONCommand.h | 12 +- KCD/HMJSONCommand.m | 61 ++++++++- KCD/HMJSONReciever.m | 6 +- KCD/HMJSONTracker.m | 17 ++- KCD/HMJSONViewCommand.h | 13 ++ KCD/HMJSONViewCommand.m | 50 ++++++++ KCD/HMJSONViewWindowController.h | 25 ++++ KCD/HMJSONViewWindowController.m | 81 ++++++++++++ KCD/HMJSONViewWindowController.xib | 256 +++++++++++++++++++++++++++++++++++++ 13 files changed, 541 insertions(+), 12 deletions(-) create mode 100644 KCD/HMJSONViewCommand.h create mode 100644 KCD/HMJSONViewCommand.m create mode 100644 KCD/HMJSONViewWindowController.h create mode 100644 KCD/HMJSONViewWindowController.m create mode 100644 KCD/HMJSONViewWindowController.xib diff --git a/KCD.xcodeproj/project.pbxproj b/KCD.xcodeproj/project.pbxproj index a37b28c1..f9ca6356 100644 --- a/KCD.xcodeproj/project.pbxproj +++ b/KCD.xcodeproj/project.pbxproj @@ -7,6 +7,9 @@ objects = { /* Begin PBXBuildFile section */ + F46F103618A7CE310063E503 /* HMJSONViewCommand.m in Sources */ = {isa = PBXBuildFile; fileRef = F46F103518A7CE310063E503 /* HMJSONViewCommand.m */; }; + F47B78B218A7B42B0011386E /* HMJSONViewWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = F47B78B018A7B42B0011386E /* HMJSONViewWindowController.m */; }; + F47B78B318A7B42B0011386E /* HMJSONViewWindowController.xib in Resources */ = {isa = PBXBuildFile; fileRef = F47B78B118A7B42B0011386E /* HMJSONViewWindowController.xib */; }; F4BDEB47187252F30069D0CE /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4BDEB46187252F30069D0CE /* Cocoa.framework */; }; F4BDEB51187252F30069D0CE /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F4BDEB4F187252F30069D0CE /* InfoPlist.strings */; }; F4BDEB53187252F30069D0CE /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F4BDEB52187252F30069D0CE /* main.m */; }; @@ -40,6 +43,11 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + F46F103418A7CE310063E503 /* HMJSONViewCommand.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONViewCommand.h; sourceTree = ""; }; + F46F103518A7CE310063E503 /* HMJSONViewCommand.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONViewCommand.m; sourceTree = ""; }; + F47B78AF18A7B42B0011386E /* HMJSONViewWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HMJSONViewWindowController.h; sourceTree = ""; }; + F47B78B018A7B42B0011386E /* HMJSONViewWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HMJSONViewWindowController.m; sourceTree = ""; }; + F47B78B118A7B42B0011386E /* HMJSONViewWindowController.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = HMJSONViewWindowController.xib; sourceTree = ""; }; F4BDEB43187252F30069D0CE /* KCD.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = KCD.app; sourceTree = BUILT_PRODUCTS_DIR; }; F4BDEB46187252F30069D0CE /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; }; F4BDEB49187252F30069D0CE /* AppKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AppKit.framework; path = System/Library/Frameworks/AppKit.framework; sourceTree = SDKROOT; }; @@ -153,11 +161,16 @@ F4C1190718A746EB005D5B25 /* HMJSONTracker.m */, F4C1190918A74DF0005D5B25 /* HMJSONCommand.h */, F4C1190A18A74DF0005D5B25 /* HMJSONCommand.m */, + F46F103418A7CE310063E503 /* HMJSONViewCommand.h */, + F46F103518A7CE310063E503 /* HMJSONViewCommand.m */, F4C1190018A6FFC2005D5B25 /* HMQueue.h */, F4C1190118A6FFC2005D5B25 /* HMQueue.m */, F4BDEB5B187252F30069D0CE /* MainMenu.xib */, F4BDEB5E187252F30069D0CE /* Images.xcassets */, F4BDEB4D187252F30069D0CE /* Supporting Files */, + F47B78AF18A7B42B0011386E /* HMJSONViewWindowController.h */, + F47B78B018A7B42B0011386E /* HMJSONViewWindowController.m */, + F47B78B118A7B42B0011386E /* HMJSONViewWindowController.xib */, ); path = KCD; sourceTree = ""; @@ -283,6 +296,7 @@ buildActionMask = 2147483647; files = ( F4BDEB51187252F30069D0CE /* InfoPlist.strings in Resources */, + F47B78B318A7B42B0011386E /* HMJSONViewWindowController.xib in Resources */, F4BDEB5F187252F30069D0CE /* Images.xcassets in Resources */, F4BDEB57187252F30069D0CE /* Credits.rtf in Resources */, F4BDEB5D187252F30069D0CE /* MainMenu.xib in Resources */, @@ -305,10 +319,12 @@ buildActionMask = 2147483647; files = ( F4C1190218A6FFC2005D5B25 /* HMQueue.m in Sources */, + F46F103618A7CE310063E503 /* HMJSONViewCommand.m in Sources */, F4C118FA18A67B1C005D5B25 /* CacheStoragePolicy.m in Sources */, F4C1190518A71AEA005D5B25 /* HMJSONReciever.m in Sources */, F4C1190B18A74DF0005D5B25 /* HMJSONCommand.m in Sources */, F4BDEB53187252F30069D0CE /* main.m in Sources */, + F47B78B218A7B42B0011386E /* HMJSONViewWindowController.m in Sources */, F4C118FC18A67B1C005D5B25 /* CustomHTTPProtocol.m in Sources */, F4C1190818A746EB005D5B25 /* HMJSONTracker.m in Sources */, F4C118FB18A67B1C005D5B25 /* CanonicalRequest.m in Sources */, diff --git a/KCD/Base.lproj/MainMenu.xib b/KCD/Base.lproj/MainMenu.xib index c4a28909..cf0e7918 100644 --- a/KCD/Base.lproj/MainMenu.xib +++ b/KCD/Base.lproj/MainMenu.xib @@ -1,6 +1,7 @@ + @@ -647,7 +648,7 @@ - + @@ -659,7 +660,9 @@ - + + + diff --git a/KCD/HMAppDelegate.h b/KCD/HMAppDelegate.h index 79266599..04505f4c 100644 --- a/KCD/HMAppDelegate.h +++ b/KCD/HMAppDelegate.h @@ -10,6 +10,9 @@ #import +#import "HMJSONViewWindowController.h" + + @interface HMAppDelegate : NSObject @property (assign) IBOutlet NSWindow *window; @@ -20,4 +23,7 @@ - (void)log:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2); +@property (retain) HMJSONViewWindowController *jsonViewWindowController; + + @end diff --git a/KCD/HMAppDelegate.m b/KCD/HMAppDelegate.m index 33926423..ee13bdd6 100644 --- a/KCD/HMAppDelegate.m +++ b/KCD/HMAppDelegate.m @@ -64,6 +64,9 @@ static FILE* logFileP = NULL; self.tracker = [HMJSONTracker new]; + + self.jsonViewWindowController = [HMJSONViewWindowController new]; + [self.jsonViewWindowController showWindow:nil]; } @end diff --git a/KCD/HMJSONCommand.h b/KCD/HMJSONCommand.h index ae97440b..51a9f8dd 100644 --- a/KCD/HMJSONCommand.h +++ b/KCD/HMJSONCommand.h @@ -12,7 +12,17 @@ + (HMJSONCommand *)commandForAPI:(NSString *)api; -@property (retain) NSString *argumentsString; - (void)doCommand:(id)json; +@property (copy) NSString *argumentsString; +@property (retain) NSArray *arguments; +@property (copy, readonly) NSString *api; +@property (retain) id json; + + +// for subclass ++ (BOOL)canExcuteAPI:(NSString *)api; + ++ (void)registerClass:(Class)commandClass; + @end diff --git a/KCD/HMJSONCommand.m b/KCD/HMJSONCommand.m index ee402336..3ff17b2a 100644 --- a/KCD/HMJSONCommand.m +++ b/KCD/HMJSONCommand.m @@ -11,21 +11,80 @@ #import "HMAppDelegate.h" +static NSMutableArray *registerdCommands = nil; + + +@interface HMJSONCommand () +@property (copy, readwrite) NSString *api; +@end @implementation HMJSONCommand +@synthesize argumentsString = _argumentsString; + ++ (void)load +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + registerdCommands = [NSMutableArray new]; + }); +} + (HMJSONCommand *)commandForAPI:(NSString *)api { - [[NSApp delegate] logLineReturn:@"%@", api]; + for(Class commandClass in registerdCommands) { + if([commandClass canExcuteAPI:api]) { + HMJSONCommand *command = [commandClass new]; + command.api = api; + + return command; + } + } + return nil; } ++ (void)registerClass:(Class)commandClass +{ + if(!commandClass) return; + if([registerdCommands containsObject:commandClass]) return; + [registerdCommands addObject:commandClass]; +} + + +- (void)setArgumentsString:(NSString *)argumentsString +{ + _argumentsString = [argumentsString copy]; + + NSString *unescape = [_argumentsString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; + NSArray *pair = [unescape componentsSeparatedByString:@"&"]; + NSMutableArray *array = [NSMutableArray new]; + for(NSString *p in pair) { + NSArray *pp = [p componentsSeparatedByString:@"="]; + if([pp count] != 2) { + NSLog(@"API (%@): Bat Argument: pair is odd.", self.api); + continue; + } + [array addObject:@{@"key": pp[0], @"value": pp[1]}]; + } + self.arguments = array; +} +- (NSString *)argumentsString +{ + return [_argumentsString copy]; +} + +// abstruct - (void)doCommand:(id)json { NSLog(@"Enter %s", __PRETTY_FUNCTION__); assert(NO); } ++ (BOOL)canExcuteAPI:(NSString *)api +{ + return NO; +} + @end diff --git a/KCD/HMJSONReciever.m b/KCD/HMJSONReciever.m index d2bd9e24..b0cb1c26 100644 --- a/KCD/HMJSONReciever.m +++ b/KCD/HMJSONReciever.m @@ -87,14 +87,14 @@ NSData *JSONData = [elements[1] dataUsingEncoding:NSUTF8StringEncoding]; NSError *error = nil; id json = [NSJSONSerialization JSONObjectWithData:JSONData - options:NSJSONReadingAllowFragments - error:&error]; + options:NSJSONReadingAllowFragments + error:&error]; if(error) { [[NSApp delegate] logLineReturn:@"\e[1m\e[31mFail decode JSON data\e[39m\e[22m %@", error]; } else { NSData *requestBodyData = [protocol.request HTTPBody]; NSString *requestBodyString = [[NSString alloc] initWithData:requestBodyData encoding:NSUTF8StringEncoding]; - [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"arg": requestBodyString, @"json" : json}]; + [self.queueu enqueue:@{@"api" : protocol.request.URL.path, @"argument": requestBodyString, @"json" : json}]; } #endif diff --git a/KCD/HMJSONTracker.m b/KCD/HMJSONTracker.m index 400d2866..37a13453 100644 --- a/KCD/HMJSONTracker.m +++ b/KCD/HMJSONTracker.m @@ -38,11 +38,18 @@ dispatch_queue_t queue = dispatch_queue_create("HMJSONTracker", 0); dispatch_async(queue, ^{ while(YES) { - NSDictionary *item = [self.queue dequeue]; - HMJSONCommand *command = [HMJSONCommand commandForAPI:[item objectForKey:@"api"]]; - command.argumentsString = [item objectForKey:@"arg"]; - [command doCommand:[item objectForKey:@"json"]]; - + @try { + NSDictionary *item = [self.queue dequeue]; + dispatch_sync(dispatch_get_main_queue(), ^{ + // + HMJSONCommand *command = [HMJSONCommand commandForAPI:[item objectForKey:@"api"]]; + command.argumentsString = [item objectForKey:@"argument"]; + [command doCommand:[item objectForKey:@"json"]]; + }); + } + @catch (id e) { + NSLog(@"Cought Exception -> %@", e); + } } }); } diff --git a/KCD/HMJSONViewCommand.h b/KCD/HMJSONViewCommand.h new file mode 100644 index 00000000..aac351e6 --- /dev/null +++ b/KCD/HMJSONViewCommand.h @@ -0,0 +1,13 @@ +// +// HMJSONViewCommand.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONCommand.h" + +@interface HMJSONViewCommand : HMJSONCommand + +@end diff --git a/KCD/HMJSONViewCommand.m b/KCD/HMJSONViewCommand.m new file mode 100644 index 00000000..2ecef0bc --- /dev/null +++ b/KCD/HMJSONViewCommand.m @@ -0,0 +1,50 @@ +// +// HMJSONViewCommand.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONViewCommand.h" +#import "HMAppDelegate.h" + + +@interface HMJSONViewCommand () + +@end + +@implementation HMJSONViewCommand ++ (void)load +{ + static dispatch_once_t onceToken; + dispatch_once(&onceToken, ^{ + [HMJSONCommand registerClass:self]; + }); +} + ++ (BOOL)canExcuteAPI:(NSString *)api +{ + return YES;// [api isEqualToString:@"/kcsapi/api_req_member/get_incentive"]; +} + +- (id)init +{ + self = [super init]; + if(self) { + + } + + return self; +} + +- (void)doCommand:(id)json +{ + self.json = json; + + HMAppDelegate *appDelegate = [NSApp delegate]; + [appDelegate.jsonViewWindowController setCommand:@{@"api":self.api, @"argument":self.arguments, @"json":self.json, @"date": [NSDate date]}]; +} + + +@end diff --git a/KCD/HMJSONViewWindowController.h b/KCD/HMJSONViewWindowController.h new file mode 100644 index 00000000..65f87001 --- /dev/null +++ b/KCD/HMJSONViewWindowController.h @@ -0,0 +1,25 @@ +// +// HMJSONViewWindowController.h +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import + +@interface HMJSONViewWindowController : NSWindowController +@property (nonatomic, assign) IBOutlet NSTableView *argumentsView; +@property (nonatomic, assign) IBOutlet NSOutlineView *jsonView; + +@property (nonatomic, assign) IBOutlet NSArrayController *apis; + + +@property (assign) NSArray *arguments; +@property (retain, readonly) NSMutableArray *commands; +@property (assign, readonly) id json; + +- (void)setCommand:(NSDictionary *)command; + + +@end diff --git a/KCD/HMJSONViewWindowController.m b/KCD/HMJSONViewWindowController.m new file mode 100644 index 00000000..f3e4cd4f --- /dev/null +++ b/KCD/HMJSONViewWindowController.m @@ -0,0 +1,81 @@ +// +// HMJSONViewWindowController.m +// KCD +// +// Created by Hori,Masaki on 2014/02/09. +// Copyright (c) 2014å¹´ Hori,Masaki. All rights reserved. +// + +#import "HMJSONViewWindowController.h" + +@interface HMJSONViewWindowController () +@property (retain, readwrite) NSMutableArray *commands; + +@property (assign, readwrite) id json; +@end + +@implementation HMJSONViewWindowController + +- (id)init +{ + self = [super initWithWindowNibName:NSStringFromClass([self class])]; + if(self) { + _commands = [NSMutableArray new]; + } + return self; +} + +- (void)awakeFromNib +{ + [self.apis addObserver:self + forKeyPath:@"selection" + options:NSKeyValueObservingOptionNew + context:NULL]; +} + +- (void)setCommand:(NSDictionary *)command +{ + [self willChangeValueForKey:@"commands"]; + [self.commands addObject:command]; + [self didChangeValueForKey:@"commands"]; +} + +- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context +{ + if([keyPath isEqualToString:@"selection"]) { + [self willChangeValueForKey:@"arguments"]; + self.arguments = [self.apis valueForKeyPath:@"selection.argument"]; + [self didChangeValueForKey:@"arguments"]; + + [self willChangeValueForKey:@"json"]; + self.json = [self.apis valueForKeyPath:@"selection.json"]; + [self didChangeValueForKey:@"json"]; + +// [self.argumentsView reloadData]; + [self.jsonView reloadData]; + + return; + } + + [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; +} + + +- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item +{ + return 0; +} +- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item +{ + return nil; +} +- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item +{ + return NO; +} +- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item +{ + return nil; +} + +@end diff --git a/KCD/HMJSONViewWindowController.xib b/KCD/HMJSONViewWindowController.xib new file mode 100644 index 00000000..7a28a582 --- /dev/null +++ b/KCD/HMJSONViewWindowController.xib @@ -0,0 +1,256 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + API + api contains $value + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file -- 2.11.0