5 // Created by Hori,Masaki on 2013/12/31.
6 // Copyright (c) 2013年 Hori,Masaki. All rights reserved.
9 #import "HMAppDelegate.h"
13 #import "HMUserDefaults.h"
14 #import "HMBroserWindowController.h"
15 #import "HMSlotItemWindowController.h"
16 #import "HMScreenshotListWindowController.h"
17 #import "HMShipMasterDetailWindowController.h"
18 #import "HMAirBaseWindowController.h"
21 #import "HMExternalBrowserWindowController.h"
22 #import "HMBrowserContentAdjuster.h"
24 #import "HMFleetManager.h"
26 #import "HMPeriodicNotifier.h"
28 #import "HMTSVSupport.h"
30 #import "CustomHTTPProtocol.h"
34 #import "HMShipWindowController.h"
35 #import "HMEquipmentWindowController.h"
36 #import "HMMapWindowController.h"
38 #import "HMUITestWindowController.h"
45 //@interface NSObject (HMM_NSUserNotificationCenterPrivateMethods)
46 //- (void)_removeDisplayedNotification:(id)obj;
49 @interface HMAppDelegate () <NSUserNotificationCenterDelegate, NSTouchBarProvider>
51 @property (nonatomic, strong) HMBroserWindowController *browserWindowController;
52 @property (nonatomic, strong) HMHistoryWindowController *historyWindowController;
53 @property (nonatomic, strong) HMSlotItemWindowController *slotItemWindowController;
54 @property (nonatomic, strong) HMPreferencePanelController *preferencePanelController;
55 @property (nonatomic, strong) HMUpgradableShipsWindowController *upgradableShipWindowController;
56 @property (nonatomic, strong) HMAirBaseWindowController *airBaseWindowController;
58 //@property (strong) HMExternalBrowserWindowController *externalBrowserWindowController;
59 @property (nonatomic, strong) HMBrowserContentAdjuster *browserContentAdjuster;
61 @property (nonatomic, strong) NSMutableArray *browserWindowControllers;
63 @property (nonatomic, strong) NSMutableArray *updaters;
65 @property (nonatomic, strong) HMFleetManager *fleetManager;
67 @property (nonatomic, strong) HMPeriodicNotifier *historyCleanNotifer;
69 @property (nonatomic, strong) IBOutlet NSTouchBar *mainTouchBar;
72 @property (nonatomic, strong) HMShipWindowController *shipWindowController;
73 @property (nonatomic, strong) HMShipMasterDetailWindowController *shipMDWindowController;
74 @property (nonatomic, strong) HMEquipmentWindowController *equipmentWindowController;
75 @property (nonatomic, strong) HMMapWindowController *mapWindowController;
79 @property (nonatomic, strong) HMUITestWindowController *uiTestWindowController;
82 @property (nonatomic, strong) HMJSONViewWindowController *logedJSONViewWindowController;
86 @implementation HMAppDelegate
88 @synthesize screenshotListWindowController = _screenshotListWindowController;
90 - (void)logLineReturn:(NSString *)format, ...
92 @synchronized (self) {
95 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
96 fprintf(stderr, "%s\n", [str UTF8String]);
100 - (void)log:(NSString *)format, ...
102 @synchronized (self) {
104 va_start(ap, format);
105 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
106 fprintf(stderr, "%s", [str UTF8String]);
115 self.updaters = [NSMutableArray new];
116 _fleetManager = [HMFleetManager new];
121 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
123 [CustomHTTPProtocol setupCache];
125 NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
126 [unc setDelegate:self];
128 self.browserWindowControllers = [NSMutableArray new];
130 [NSTimer scheduledTimerWithTimeInterval:0.33
132 selector:@selector(fire:)
139 if(self.browserWindowController) return;
141 self.browserWindowController = [HMBroserWindowController new];
142 [self.browserWindowController showWindow:nil];
145 self.jsonViewWindowController = [HMJSONViewWindowController new];
146 [self.jsonViewWindowController showWindow:nil];
150 self.uiTestWindowController = [HMUITestWindowController new];
151 [self.uiTestWindowController showWindow:nil];
153 if(!HMStandardDefaults.showsDebugMenu) {
154 [self.debugMenuItem setHidden:YES];
157 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
158 _historyCleanNotifer = [HMPeriodicNotifier periodicNotifierWithHour:0 minutes:7];
159 [nc addObserverForName:HMPeriodicNotification
160 object:_historyCleanNotifer
162 usingBlock:^(NSNotification * _Nonnull note) {
163 HMHistoryItemCleaner *historyItemCleaner = [HMHistoryItemCleaner new];
164 [historyItemCleaner cleanOldHistoryItems];
168 - (void)addCounterUpdateBlock:(void(^)())updater
170 [self.updaters addObject:updater];
172 - (void)fire:(NSTimer *)timer
174 for(void (^updater)() in self.updaters) {
181 [CustomHTTPProtocol clearCache];
184 - (HMScreenshotListWindowController *)screenshotListWindowController
186 if(_screenshotListWindowController) return _screenshotListWindowController;
187 _screenshotListWindowController = [HMScreenshotListWindowController new];
188 return _screenshotListWindowController;
191 - (void)setScreenShotSaveDirectory:(NSString *)screenShotSaveDirectory
193 HMStandardDefaults.screenShotSaveDirectory = screenShotSaveDirectory;
195 - (NSString *)screenShotSaveDirectory
197 NSString *path = HMStandardDefaults.screenShotSaveDirectory;
199 path = [[self picturesDirectory] path];
204 - (NSURL *)documentsFilesDirectory
206 NSFileManager *fileManager = [NSFileManager defaultManager];
207 return [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
209 - (NSURL *)picturesDirectory
211 NSFileManager *fileManager = [NSFileManager defaultManager];
212 return [[fileManager URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask] lastObject];
214 - (NSURL *)supportDirectory
216 NSFileManager *fileManager = [NSFileManager defaultManager];
217 NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
218 NSURL *ownAppSuportURL = [appSupportURL URLByAppendingPathComponent:@"com.masakih.KCD"];
219 return ownAppSuportURL;
223 - (NSString *)appNameForUserAgent
225 return @"Version/9.1.2 Safari/601.7.7";
228 - (NSFont *)monospaceSystemFont11
230 NSFont *font11 = nil;
231 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
232 font11 = [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular];
234 font11 = [NSFont systemFontOfSize:11];
239 - (NSFont *)monospaceSystemFont12
241 NSFont *font12 = nil;
242 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
243 font12 = [NSFont monospacedDigitSystemFontOfSize:12 weight:NSFontWeightRegular];
245 font12 = [NSFont systemFontOfSize:12];
250 - (NSFont *)monospaceSystemFont13
252 NSFont *font13 = nil;
253 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
254 font13 = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightRegular];
256 font13 = [NSFont systemFontOfSize:13];
263 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
265 SEL action = [menuItem action];
266 if(action == @selector(showHideHistory:)) {
267 NSWindow *window = self.historyWindowController.window;
268 if(!window.isVisible || !window.isMainWindow) {
269 [menuItem setTitle:NSLocalizedString(@"Show History", @"")];
271 [menuItem setTitle:NSLocalizedString(@"Hide History", @"")];
274 } else if(action == @selector(showHideSlotItemWindow:)) {
275 NSWindow *window = self.slotItemWindowController.window;
276 if(!window.isVisible || !window.isMainWindow) {
277 [menuItem setTitle:NSLocalizedString(@"Show Slot Item", @"")];
279 [menuItem setTitle:NSLocalizedString(@"Hide Slot Item", @"")];
282 } else if(action == @selector(showHideUpgradableShipWindow:)) {
283 NSWindow *window = self.upgradableShipWindowController.window;
284 if(!window.isVisible || !window.isMainWindow) {
285 [menuItem setTitle:NSLocalizedString(@"Show Upgradable Ships", @"")];
287 [menuItem setTitle:NSLocalizedString(@"Hide Upgradable Ships", @"")];
290 } else if(action == @selector(showHideScreenshotListWindow:)) {
291 NSWindow *window = self.screenshotListWindowController.window;
292 if(!window.isVisible || !window.isMainWindow) {
293 [menuItem setTitle:NSLocalizedString(@"Show Screenshot List", @"")];
295 [menuItem setTitle:NSLocalizedString(@"Hide Screenshot List", @"")];
298 } else if(action == @selector(showHideAirBaseInfoWindow:)) {
299 NSWindow *window = self.airBaseWindowController.window;
300 if(!window.isVisible || !window.isMainWindow) {
301 [menuItem setTitle:NSLocalizedString(@"Show Air Base Info", @"")];
303 [menuItem setTitle:NSLocalizedString(@"Hide Air Base Info", @"")];
306 } else if(action == @selector(saveLocalData:) || action == @selector(loadLocalData:)) {
308 } else if(action == @selector(showHidePreferencePanle:)) {
310 } else if(action == @selector(openNewBrowser:) || action == @selector(selectBookmark:)) {
312 } else if(action == @selector(showWindowAduster:)) {
316 else if(action == @selector(saveDocument:) || action == @selector(openDocument:)) {
318 } else if(action == @selector(removeDatabaseFile:)) {
323 else if(action == @selector(showShipWindow:) || action == @selector(showEquipmentWindow:)
324 || action == @selector(showMapWindow:) || action == @selector(showOwnershipShipWindow:) ) {
331 - (IBAction)showHideHistory:(id)sender
333 if(!self.historyWindowController) {
334 self.historyWindowController = [HMHistoryWindowController new];
337 NSWindow *window = self.historyWindowController.window;
338 if(!window.isVisible || !window.isMainWindow) {
339 [window makeKeyAndOrderFront:nil];
341 [window orderOut:nil];
345 - (IBAction)showHideSlotItemWindow:(id)sender
347 if(!self.slotItemWindowController) {
348 self.slotItemWindowController = [HMSlotItemWindowController new];
351 NSWindow *window = self.slotItemWindowController.window;
352 if(!window.isVisible || !window.isMainWindow) {
353 [window makeKeyAndOrderFront:nil];
355 [window orderOut:nil];
359 - (IBAction)showHidePreferencePanle:(id)sender
361 if(!self.preferencePanelController) {
362 self.preferencePanelController = [HMPreferencePanelController new];
365 NSWindow *window = self.preferencePanelController.window;
366 if(!window.isVisible || !window.isMainWindow) {
367 [window makeKeyAndOrderFront:nil];
369 [window orderOut:nil];
373 - (IBAction)showHideUpgradableShipWindow:(id)sender
375 if(!self.upgradableShipWindowController) {
376 self.upgradableShipWindowController = [HMUpgradableShipsWindowController new];
379 NSWindow *window = self.upgradableShipWindowController.window;
380 if(!window.isVisible || !window.isMainWindow) {
381 [window makeKeyAndOrderFront:nil];
383 [window orderOut:nil];
387 - (IBAction)showHideScreenshotListWindow:(id)sender
389 NSWindow *window = self.screenshotListWindowController.window;
390 if(!window.isVisible || !window.isMainWindow) {
391 [window makeKeyAndOrderFront:nil];
393 [window orderOut:nil];
396 - (IBAction)showHideAirBaseInfoWindow:(id)sender
398 if(!self.airBaseWindowController) {
399 self.airBaseWindowController = [HMAirBaseWindowController new];
402 NSWindow *window = self.airBaseWindowController.window;
403 if(!window.isVisible || !window.isMainWindow) {
404 [window makeKeyAndOrderFront:nil];
406 [window orderOut:nil];
410 - (IBAction)openNewBrowser:(id)sender
412 [self createNewBrowser];
414 - (IBAction)selectBookmark:(id)sender
416 HMExternalBrowserWindowController *browser = [self createNewBrowser];
418 [browser selectBookmark:sender];
420 - (HMExternalBrowserWindowController *)createNewBrowser
422 HMExternalBrowserWindowController *browser = [HMExternalBrowserWindowController new];
423 [self.browserWindowControllers addObject:browser];
424 [browser.window makeKeyAndOrderFront:nil];
426 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
428 selector:@selector(windowWillClose:)
429 name:NSWindowWillCloseNotification
430 object:browser.window];
434 - (IBAction)showWindowAduster:(id)sender
436 if(! self.browserContentAdjuster) {
437 self.browserContentAdjuster = [HMBrowserContentAdjuster new];
439 [self.browserContentAdjuster showWindow:nil];
442 - (void)windowWillClose:(NSNotification *)notification
444 id object = [notification object];
445 if([self.browserWindowControllers containsObject:object]) {
446 [[NSNotificationCenter defaultCenter] removeObserver:self
447 name:NSWindowWillCloseNotification
449 [self.browserWindowControllers removeObject:object];
453 - (IBAction)removeDatabaseFile:(id)sender
455 NSBundle *mainBundle = [NSBundle mainBundle];
456 NSString *appleScriptPath = [mainBundle pathForResource:@"RemoveDatabaseFileAndRestart"
458 NSTask *task = [NSTask new];
459 task.launchPath = @"/usr/bin/open";
460 task.arguments = @[appleScriptPath];
464 - (IBAction)showMainBrowser:(id)sender
466 [self.browserWindowController showWindow:nil];
471 - (IBAction)showShipWindow:(id)sender
473 if(!_shipWindowController) {
474 self.shipWindowController = [HMShipWindowController new];
476 [self.shipWindowController showWindow:nil];
478 - (IBAction)showEquipmentWindow:(id)sender
480 if(!_equipmentWindowController) {
481 self.equipmentWindowController = [HMEquipmentWindowController new];
483 [self.equipmentWindowController showWindow:nil];
485 - (IBAction)showMapWindow:(id)sender
487 if(!_mapWindowController) {
488 self.mapWindowController = [HMMapWindowController new];
490 [self.mapWindowController showWindow:nil];
492 - (IBAction)showOwnershipShipWindow:(id)sender
494 if(!_shipMDWindowController) {
495 self.shipMDWindowController = [HMShipMasterDetailWindowController new];
497 [self.shipMDWindowController showWindow:nil];
501 - (NSTouchBar *)touchBar
503 if(NSClassFromString(@"NSTouchBar") == Nil) return nil;
505 NSWindow *mainWindow = [NSApplication sharedApplication].mainWindow;
506 if(mainWindow == self.browserWindowController.window) return nil;
508 if(self.mainTouchBar) return self.mainTouchBar;
510 NSArray *toplevel = nil;
511 NSBundle *mainBundle = [NSBundle mainBundle];
512 [mainBundle loadNibNamed:@"MainTouchBar"
514 topLevelObjects:&toplevel];
516 return self.mainTouchBar;
519 #pragma mark - NSApplicationDelegate
520 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
525 #pragma mark - NSUserNotificationCenterDelegate
526 //- (void)removeUserNotification:(NSDictionary *)dict
528 // NSUserNotificationCenter *center = [dict objectForKey:@"center"];
529 // NSUserNotification *notification = [dict objectForKey:@"notification"];
530 // [center removeDeliveredNotification:notification];
531 // // [center _removeDisplayedNotification:notification];
534 - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
539 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
541 // [self performSelector:@selector(removeUserNotification:)
542 // withObject:@{@"center":center, @"notification":notification}
545 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
547 // [center removeDeliveredNotification:notification];
550 - (IBAction)saveDocument:(id)sender
552 NSSavePanel *panel = [NSSavePanel savePanel];
553 [panel setAllowedFileTypes:@[@"plist"]];
554 [panel setPrompt:@"Save log"];
555 [panel setTitle:@"Save log"];
556 [panel beginWithCompletionHandler:^(NSInteger result) {
557 if(result == NSModalResponseOK) {
558 NSArray *array = [self.jsonViewWindowController.commands copy];
559 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
561 NSLog(@"can not convert log.");
564 NSError *error = nil;
565 [data writeToURL:panel.URL
566 options:NSDataWritingAtomic
569 NSLog(@"can not save property list.: %@", error);
575 - (IBAction)openDocument:(id)sender
577 NSOpenPanel *panel = [NSOpenPanel openPanel];
578 [panel setAllowedFileTypes:@[@"plist"]];
579 [panel setAllowsMultipleSelection:NO];
580 [panel setPrompt:@"Open log"];
581 [panel setTitle:@"Open log"];
582 [panel beginWithCompletionHandler:^(NSInteger result) {
583 if(result == NSModalResponseOK) {
584 NSData *data = [NSData dataWithContentsOfURL:panel.URL];
585 id array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
586 if(!array || ![array isKindOfClass:[NSArray class]]) {
587 NSLog(@"Can not convert data to log.");
591 self.logedJSONViewWindowController = [HMJSONViewWindowController new];
592 [self.logedJSONViewWindowController setCommandArray:array];
593 [[self.logedJSONViewWindowController window] setTitle:@"SAVED LOG FILE VIEWER"];
595 [self.logedJSONViewWindowController showWindow:nil];
601 - (IBAction)saveLocalData:(id)sender
603 [[HMTSVSupport new] save:sender];
605 - (IBAction)loadLocalData:(id)sender
607 [[HMTSVSupport new] load:sender];