5 // Created by Hori,Masaki on 2013/12/31.
6 // Copyright (c) 2013年 Hori,Masaki. All rights reserved.
9 #import "HMAppDelegate.h"
11 #import "HMUserDefaults.h"
12 #import "HMBroserWindowController.h"
13 #import "HMHistoryWindowController.h"
14 #import "HMSlotItemWindowController.h"
15 #import "HMPreferencePanelController.h"
16 #import "HMUpgradableShipsWindowController.h"
17 #import "HMScreenshotListWindowController.h"
18 #import "HMShipMasterDetailWindowController.h"
20 #import "HMExternalBrowserWindowController.h"
21 #import "HMBrowserContentAdjuster.h"
23 #import "HMFleetManager.h"
25 #import "HMTSVSupport.h"
27 #import "CustomHTTPProtocol.h"
31 #import "HMShipWindowController.h"
32 #import "HMEquipmentWindowController.h"
33 #import "HMMapWindowController.h"
35 #import "HMUITestWindowController.h"
42 //@interface NSObject (HMM_NSUserNotificationCenterPrivateMethods)
43 //- (void)_removeDisplayedNotification:(id)obj;
46 @interface HMAppDelegate () <NSUserNotificationCenterDelegate>
48 @property (strong) HMBroserWindowController *browserWindowController;
49 @property (strong) HMHistoryWindowController *historyWindowController;
50 @property (strong) HMSlotItemWindowController *slotItemWindowController;
51 @property (strong) HMPreferencePanelController *preferencePanelController;
52 @property (strong) HMUpgradableShipsWindowController *upgradableShipWindowController;
54 //@property (strong) HMExternalBrowserWindowController *externalBrowserWindowController;
55 @property (strong) HMBrowserContentAdjuster *browserContentAdjuster;
57 @property (strong) NSMutableArray *browserWindowControllers;
59 @property (strong) NSMutableArray *updaters;
61 @property (strong) HMFleetManager *fleetManager;
64 @property (strong) HMShipWindowController *shipWindowController;
65 @property (strong) HMShipMasterDetailWindowController *shipMDWindowController;
66 @property (strong) HMEquipmentWindowController *equipmentWindowController;
67 @property (strong) HMMapWindowController *mapWindowController;
71 @property (strong) HMUITestWindowController *uiTestWindowController;
74 @property (strong) HMJSONViewWindowController *logedJSONViewWindowController;
78 @implementation HMAppDelegate
80 @synthesize screenshotListWindowController = _screenshotListWindowController;
82 - (void)logLineReturn:(NSString *)format, ...
84 @synchronized (self) {
87 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
88 fprintf(stderr, "%s\n", [str UTF8String]);
92 - (void)log:(NSString *)format, ...
94 @synchronized (self) {
97 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
98 fprintf(stderr, "%s", [str UTF8String]);
107 self.updaters = [NSMutableArray new];
108 _fleetManager = [HMFleetManager new];
113 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
115 [CustomHTTPProtocol setupCache];
117 NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
118 [unc setDelegate:self];
120 self.browserWindowControllers = [NSMutableArray new];
122 [NSTimer scheduledTimerWithTimeInterval:0.33
124 selector:@selector(fire:)
131 self.browserWindowController = [HMBroserWindowController new];
132 [self.browserWindowController showWindow:nil];
135 self.jsonViewWindowController = [HMJSONViewWindowController new];
136 [self.jsonViewWindowController showWindow:nil];
140 self.uiTestWindowController = [HMUITestWindowController new];
141 [self.uiTestWindowController showWindow:nil];
143 if(!HMStandardDefaults.showsDebugMenu) {
144 [self.debugMenuItem setHidden:YES];
149 - (void)addCounterUpdateBlock:(void(^)())updater
151 [self.updaters addObject:updater];
153 - (void)fire:(NSTimer *)timer
155 for(void (^updater)() in self.updaters) {
162 [CustomHTTPProtocol clearCache];
165 - (HMScreenshotListWindowController *)screenshotListWindowController
167 if(_screenshotListWindowController) return _screenshotListWindowController;
168 _screenshotListWindowController = [HMScreenshotListWindowController new];
169 return _screenshotListWindowController;
172 - (NSArray *)shipTypeCategories
174 static NSArray *categories = nil;
176 if(categories) return categories;
181 @[@3, @4], // leght cruiser
182 @[@5,@6], // heavy crusier
183 @[@7, @11, @16, @18], // aircraft carrier
184 @[@8, @9, @10, @12], // battle ship
185 @[@13, @14], // submarine
190 - (NSPredicate *)predicateForShipType:(HMShipType)shipType
192 NSPredicate *predicate = nil;
193 NSArray *categories = [self shipTypeCategories];
199 case kHMLightCruiser:
200 case kHMHeavyCruiser:
201 case kHMAircraftCarrier:
204 predicate = [NSPredicate predicateWithFormat:@"master_ship.stype.id IN %@", categories[shipType]];
209 NSMutableArray *ommitTypes = [NSMutableArray new];
210 for(int i = kHMDestroyer; i < kHMOtherType; i++) {
211 [ommitTypes addObjectsFromArray:categories[i]];
213 predicate = [NSPredicate predicateWithFormat:@"NOT master_ship.stype.id IN %@", ommitTypes];
221 - (void)setScreenShotSaveDirectory:(NSString *)screenShotSaveDirectory
223 HMStandardDefaults.screenShotSaveDirectory = screenShotSaveDirectory;
225 - (NSString *)screenShotSaveDirectory
227 NSString *path = HMStandardDefaults.screenShotSaveDirectory;
229 path = [[self picturesDirectory] path];
234 - (NSURL *)documentsFilesDirectory
236 NSFileManager *fileManager = [NSFileManager defaultManager];
237 return [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
239 - (NSURL *)picturesDirectory
241 NSFileManager *fileManager = [NSFileManager defaultManager];
242 return [[fileManager URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask] lastObject];
244 - (NSURL *)supportDirectory
246 NSFileManager *fileManager = [NSFileManager defaultManager];
247 NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
248 NSURL *ownAppSuportURL = [appSupportURL URLByAppendingPathComponent:@"com.masakih.KCD"];
249 return ownAppSuportURL;
253 - (NSString *)appNameForUserAgent
255 return @"Version/8.0.8 Safari/600.8.9";
258 - (NSFont *)monospaceSystemFont11
260 NSFont *font11 = nil;
261 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
262 font11 = [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular];
264 font11 = [NSFont systemFontOfSize:11];
269 - (NSFont *)monospaceSystemFont12
271 NSFont *font12 = nil;
272 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
273 font12 = [NSFont monospacedDigitSystemFontOfSize:12 weight:NSFontWeightRegular];
275 font12 = [NSFont systemFontOfSize:12];
280 - (NSFont *)monospaceSystemFont13
282 NSFont *font13 = nil;
283 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
284 font13 = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightRegular];
286 font13 = [NSFont systemFontOfSize:13];
293 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
295 SEL action = [menuItem action];
296 if(action == @selector(showHideHistory:)) {
297 NSWindow *window = self.historyWindowController.window;
298 if(!window.isVisible || !window.isMainWindow) {
299 [menuItem setTitle:NSLocalizedString(@"Show History", @"")];
301 [menuItem setTitle:NSLocalizedString(@"Hide History", @"")];
304 } else if(action == @selector(showHideSlotItemWindow:)) {
305 NSWindow *window = self.slotItemWindowController.window;
306 if(!window.isVisible || !window.isMainWindow) {
307 [menuItem setTitle:NSLocalizedString(@"Show Slot Item", @"")];
309 [menuItem setTitle:NSLocalizedString(@"Hide Slot Item", @"")];
312 } else if(action == @selector(showHideUpgradableShipWindow:)) {
313 NSWindow *window = self.upgradableShipWindowController.window;
314 if(!window.isVisible || !window.isMainWindow) {
315 [menuItem setTitle:NSLocalizedString(@"Show Upgradable Ships", @"")];
317 [menuItem setTitle:NSLocalizedString(@"Hide Upgradable Ships", @"")];
320 } else if(action == @selector(showHideScreenshotListWindow:)) {
321 NSWindow *window = self.screenshotListWindowController.window;
322 if(!window.isVisible || !window.isMainWindow) {
323 [menuItem setTitle:NSLocalizedString(@"Show Screenshot List", @"")];
325 [menuItem setTitle:NSLocalizedString(@"Hide Screenshot List", @"")];
328 } else if(action == @selector(saveLocalData:) || action == @selector(loadLocalData:)) {
330 } else if(action == @selector(showHidePreferencePanle:)) {
332 } else if(action == @selector(openNewBrowser:) || action == @selector(selectBookmark:)) {
334 } else if(action == @selector(showWindowAduster:)) {
338 else if(action == @selector(saveDocument:) || action == @selector(openDocument:)) {
340 } else if(action == @selector(removeDatabaseFile:)) {
345 else if(action == @selector(showShipWindow:) || action == @selector(showEquipmentWindow:)
346 || action == @selector(showMapWindow:) || action == @selector(showOwnershipShipWindow:) ) {
353 - (IBAction)showHideHistory:(id)sender
355 if(!self.historyWindowController) {
356 self.historyWindowController = [HMHistoryWindowController new];
359 NSWindow *window = self.historyWindowController.window;
360 if(!window.isVisible || !window.isMainWindow) {
361 [window makeKeyAndOrderFront:nil];
363 [window orderOut:nil];
367 - (IBAction)showHideSlotItemWindow:(id)sender
369 if(!self.slotItemWindowController) {
370 self.slotItemWindowController = [HMSlotItemWindowController new];
373 NSWindow *window = self.slotItemWindowController.window;
374 if(!window.isVisible || !window.isMainWindow) {
375 [window makeKeyAndOrderFront:nil];
377 [window orderOut:nil];
381 - (IBAction)showHidePreferencePanle:(id)sender
383 if(!self.preferencePanelController) {
384 self.preferencePanelController = [HMPreferencePanelController new];
387 NSWindow *window = self.preferencePanelController.window;
388 if(!window.isVisible || !window.isMainWindow) {
389 [window makeKeyAndOrderFront:nil];
391 [window orderOut:nil];
395 - (IBAction)showHideUpgradableShipWindow:(id)sender
397 if(!self.upgradableShipWindowController) {
398 self.upgradableShipWindowController = [HMUpgradableShipsWindowController new];
401 NSWindow *window = self.upgradableShipWindowController.window;
402 if(!window.isVisible || !window.isMainWindow) {
403 [window makeKeyAndOrderFront:nil];
405 [window orderOut:nil];
409 - (IBAction)showHideScreenshotListWindow:(id)sender
411 NSWindow *window = self.screenshotListWindowController.window;
412 if(!window.isVisible || !window.isMainWindow) {
413 [window makeKeyAndOrderFront:nil];
415 [window orderOut:nil];
419 - (IBAction)openNewBrowser:(id)sender
421 [self createNewBrowser];
423 - (IBAction)selectBookmark:(id)sender
425 HMExternalBrowserWindowController *browser = [self createNewBrowser];
427 [browser selectBookmark:sender];
429 - (HMExternalBrowserWindowController *)createNewBrowser
431 HMExternalBrowserWindowController *browser = [HMExternalBrowserWindowController new];
432 [self.browserWindowControllers addObject:browser];
433 [browser.window makeKeyAndOrderFront:nil];
435 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
437 selector:@selector(windowWillClose:)
438 name:NSWindowWillCloseNotification
439 object:browser.window];
443 - (IBAction)showWindowAduster:(id)sender
445 if(! self.browserContentAdjuster) {
446 self.browserContentAdjuster = [HMBrowserContentAdjuster new];
448 [self.browserContentAdjuster showWindow:nil];
451 - (void)windowWillClose:(NSNotification *)notification
453 id object = [notification object];
454 if([self.browserWindowControllers containsObject:object]) {
455 [self.browserWindowControllers removeObject:object];
456 [[NSNotificationCenter defaultCenter] removeObserver:self
457 name:NSWindowWillCloseNotification
459 [self.browserWindowControllers removeObject:object];
463 - (IBAction)removeDatabaseFile:(id)sender
465 NSBundle *mainBundle = [NSBundle mainBundle];
466 NSString *appleScriptPath = [mainBundle pathForResource:@"RemoveDatabaseFileAndRestart"
468 NSTask *task = [NSTask new];
469 task.launchPath = @"/usr/bin/open";
470 task.arguments = @[appleScriptPath];
476 - (IBAction)showShipWindow:(id)sender
478 if(!_shipWindowController) {
479 self.shipWindowController = [HMShipWindowController new];
481 [self.shipWindowController showWindow:nil];
483 - (IBAction)showEquipmentWindow:(id)sender
485 if(!_equipmentWindowController) {
486 self.equipmentWindowController = [HMEquipmentWindowController new];
488 [self.equipmentWindowController showWindow:nil];
490 - (IBAction)showMapWindow:(id)sender
492 if(!_mapWindowController) {
493 self.mapWindowController = [HMMapWindowController new];
495 [self.mapWindowController showWindow:nil];
497 - (IBAction)showOwnershipShipWindow:(id)sender
499 if(!_shipMDWindowController) {
500 self.shipMDWindowController = [HMShipMasterDetailWindowController new];
502 [self.shipMDWindowController showWindow:nil];
506 #pragma mark - NSApplicationDelegate
507 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
512 #pragma mark - NSUserNotificationCenterDelegate
513 //- (void)removeUserNotification:(NSDictionary *)dict
515 // NSUserNotificationCenter *center = [dict objectForKey:@"center"];
516 // NSUserNotification *notification = [dict objectForKey:@"notification"];
517 // [center removeDeliveredNotification:notification];
518 // // [center _removeDisplayedNotification:notification];
521 - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
526 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
528 // [self performSelector:@selector(removeUserNotification:)
529 // withObject:@{@"center":center, @"notification":notification}
532 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
534 // [center removeDeliveredNotification:notification];
537 - (IBAction)saveDocument:(id)sender
539 NSSavePanel *panel = [NSSavePanel savePanel];
540 [panel setAllowedFileTypes:@[@"plist"]];
541 [panel setPrompt:@"Save log"];
542 [panel setTitle:@"Save log"];
543 [panel beginWithCompletionHandler:^(NSInteger result) {
544 if(result == NSModalResponseOK) {
545 NSArray *array = [self.jsonViewWindowController.commands copy];
546 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
548 NSLog(@"can not convert log.");
551 NSError *error = nil;
552 [data writeToURL:panel.URL
553 options:NSDataWritingAtomic
556 NSLog(@"can not save property list.: %@", error);
562 - (IBAction)openDocument:(id)sender
564 NSOpenPanel *panel = [NSOpenPanel openPanel];
565 [panel setAllowedFileTypes:@[@"plist"]];
566 [panel setAllowsMultipleSelection:NO];
567 [panel setPrompt:@"Open log"];
568 [panel setTitle:@"Open log"];
569 [panel beginWithCompletionHandler:^(NSInteger result) {
570 if(result == NSModalResponseOK) {
571 NSData *data = [NSData dataWithContentsOfURL:panel.URL];
572 id array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
573 if(!array || ![array isKindOfClass:[NSArray class]]) {
574 NSLog(@"Can not convert data to log.");
578 self.logedJSONViewWindowController = [HMJSONViewWindowController new];
579 [self.logedJSONViewWindowController setCommandArray:array];
580 [[self.logedJSONViewWindowController window] setTitle:@"SAVED LOG FILE VIEWER"];
582 [self.logedJSONViewWindowController showWindow:nil];
588 - (IBAction)saveLocalData:(id)sender
590 [[HMTSVSupport new] save:sender];
592 - (IBAction)loadLocalData:(id)sender
594 [[HMTSVSupport new] load:sender];