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"
16 #import "HMTSVSupport.h"
18 #import "CustomHTTPProtocol.h"
25 //@interface NSObject (HMM_NSUserNotificationCenterPrivateMethods)
26 //- (void)_removeDisplayedNotification:(id)obj;
29 @interface HMAppDelegate () <NSUserNotificationCenterDelegate, NSTouchBarProvider>
31 @property (nonatomic, strong) HMBroserWindowController *browserWindowController;
32 @property (nonatomic, strong) HMHistoryWindowController *historyWindowController;
33 @property (nonatomic, strong) HMSlotItemWindowController *slotItemWindowController;
34 @property (nonatomic, strong) HMPreferencePanelController *preferencePanelController;
35 @property (nonatomic, strong) HMUpgradableShipsWindowController *upgradableShipWindowController;
36 @property (nonatomic, strong) HMAirBaseWindowController *airBaseWindowController;
38 //@property (strong) HMExternalBrowserWindowController *externalBrowserWindowController;
39 @property (nonatomic, strong) HMBrowserContentAdjuster *browserContentAdjuster;
41 @property (nonatomic, strong) NSMutableArray *browserWindowControllers;
43 @property (nonatomic, strong) NSMutableArray *updaters;
45 @property (nonatomic, strong) HMFleetManager *fleetManager;
47 @property (nonatomic, strong) HMPeriodicNotifier *historyCleanNotifer;
49 @property (nonatomic, strong) IBOutlet NSTouchBar *mainTouchBar;
52 @property (nonatomic, strong) HMShipWindowController *shipWindowController;
53 @property (nonatomic, strong) HMShipMasterDetailWindowController *shipMDWindowController;
54 @property (nonatomic, strong) HMEquipmentWindowController *equipmentWindowController;
55 @property (nonatomic, strong) HMMapWindowController *mapWindowController;
59 @property (nonatomic, strong) HMUITestWindowController *uiTestWindowController;
62 @property (nonatomic, strong) HMJSONViewWindowController *logedJSONViewWindowController;
65 @property (nonatomic, strong) HMJSONTracker *jsonTracker;
66 @property (nonatomic, strong) HMTimeSignalNotifier *timeSignalNotifier;
67 @property (nonatomic, strong) HMResourceHistoryManager* resourceHistory;
70 @implementation HMAppDelegate
72 @synthesize screenshotListWindowController = _screenshotListWindowController;
74 - (void)logLineReturn:(NSString *)format, ...
76 @synchronized (self) {
79 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
80 fprintf(stderr, "%s\n", [str UTF8String]);
84 - (void)log:(NSString *)format, ...
86 @synchronized (self) {
89 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
90 fprintf(stderr, "%s", [str UTF8String]);
99 [ValueTransformerRegister registerAll];
100 [CommandRegister register];
101 self.updaters = [NSMutableArray new];
102 _fleetManager = [HMFleetManager new];
104 _jsonTracker = [HMJSONTracker new];
105 _timeSignalNotifier = [HMTimeSignalNotifier new];
106 _resourceHistory = [HMResourceHistoryManager new];
111 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
113 [CustomHTTPProtocol setupCache];
115 NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
116 [unc setDelegate:self];
118 self.browserWindowControllers = [NSMutableArray new];
120 [NSTimer scheduledTimerWithTimeInterval:0.33
122 selector:@selector(fire:)
129 if(self.browserWindowController) return;
131 [HMBookmarkManager shared];
133 self.browserWindowController = [HMBroserWindowController new];
134 [self.browserWindowController showWindow:nil];
137 self.jsonViewWindowController = [HMJSONViewWindowController new];
138 [self.jsonViewWindowController showWindow:nil];
142 self.uiTestWindowController = [HMUITestWindowController new];
143 [self.uiTestWindowController showWindow:nil];
145 if(!HMStandardDefaults.showsDebugMenu) {
146 [self.debugMenuItem setHidden:YES];
149 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
150 _historyCleanNotifer = [[HMPeriodicNotifier alloc] initWithHour:0 minutes:7];
151 [nc addObserverForName:[HMPeriodicNotifier notificationName]
152 object:_historyCleanNotifer
154 usingBlock:^(NSNotification * _Nonnull note) {
155 HMHistoryItemCleaner *historyItemCleaner = [HMHistoryItemCleaner new];
156 [historyItemCleaner cleanOldHistoryItems];
160 - (void)addCounterUpdateBlock:(void(^)())updater
162 [self.updaters addObject:updater];
164 - (void)fire:(NSTimer *)timer
166 for(void (^updater)() in self.updaters) {
173 [CustomHTTPProtocol clearCache];
176 - (HMScreenshotListWindowController *)screenshotListWindowController
178 if(_screenshotListWindowController) return _screenshotListWindowController;
179 _screenshotListWindowController = [HMScreenshotListWindowController new];
180 id w = _screenshotListWindowController.window;
182 NSLog(@"Can not load ScreenshotListWindow");
184 return _screenshotListWindowController;
187 - (void)setScreenShotSaveDirectory:(NSString *)screenShotSaveDirectory
189 HMStandardDefaults.screenShotSaveDirectory = screenShotSaveDirectory;
191 - (NSString *)screenShotSaveDirectory
193 NSString *path = HMStandardDefaults.screenShotSaveDirectory;
195 path = [[self picturesDirectory] path];
200 - (NSURL *)documentsFilesDirectory
202 NSFileManager *fileManager = [NSFileManager defaultManager];
203 return [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
205 - (NSURL *)picturesDirectory
207 NSFileManager *fileManager = [NSFileManager defaultManager];
208 return [[fileManager URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask] lastObject];
210 - (NSURL *)supportDirectory
212 NSFileManager *fileManager = [NSFileManager defaultManager];
213 NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
214 NSURL *ownAppSuportURL = [appSupportURL URLByAppendingPathComponent:@"com.masakih.KCD"];
215 return ownAppSuportURL;
219 - (NSString *)appNameForUserAgent
221 return @"Version/9.1.2 Safari/601.7.7";
224 - (NSFont *)monospaceSystemFont11
226 NSFont *font11 = nil;
227 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
228 font11 = [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular];
230 font11 = [NSFont systemFontOfSize:11];
235 - (NSFont *)monospaceSystemFont12
237 NSFont *font12 = nil;
238 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
239 font12 = [NSFont monospacedDigitSystemFontOfSize:12 weight:NSFontWeightRegular];
241 font12 = [NSFont systemFontOfSize:12];
246 - (NSFont *)monospaceSystemFont13
248 NSFont *font13 = nil;
249 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
250 font13 = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightRegular];
252 font13 = [NSFont systemFontOfSize:13];
259 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
261 SEL action = [menuItem action];
262 if(action == @selector(showHideHistory:)) {
263 NSWindow *window = self.historyWindowController.window;
264 if(!window.isVisible || !window.isMainWindow) {
265 [menuItem setTitle:NSLocalizedString(@"Show History", @"")];
267 [menuItem setTitle:NSLocalizedString(@"Hide History", @"")];
270 } else if(action == @selector(showHideSlotItemWindow:)) {
271 NSWindow *window = self.slotItemWindowController.window;
272 if(!window.isVisible || !window.isMainWindow) {
273 [menuItem setTitle:NSLocalizedString(@"Show Slot Item", @"")];
275 [menuItem setTitle:NSLocalizedString(@"Hide Slot Item", @"")];
278 } else if(action == @selector(showHideUpgradableShipWindow:)) {
279 NSWindow *window = self.upgradableShipWindowController.window;
280 if(!window.isVisible || !window.isMainWindow) {
281 [menuItem setTitle:NSLocalizedString(@"Show Upgradable Ships", @"")];
283 [menuItem setTitle:NSLocalizedString(@"Hide Upgradable Ships", @"")];
286 } else if(action == @selector(showHideScreenshotListWindow:)) {
287 NSWindow *window = self.screenshotListWindowController.window;
288 if(!window.isVisible || !window.isMainWindow) {
289 [menuItem setTitle:NSLocalizedString(@"Show Screenshot List", @"")];
291 [menuItem setTitle:NSLocalizedString(@"Hide Screenshot List", @"")];
294 } else if(action == @selector(showHideAirBaseInfoWindow:)) {
295 NSWindow *window = self.airBaseWindowController.window;
296 if(!window.isVisible || !window.isMainWindow) {
297 [menuItem setTitle:NSLocalizedString(@"Show Air Base Info", @"")];
299 [menuItem setTitle:NSLocalizedString(@"Hide Air Base Info", @"")];
302 } else if(action == @selector(saveLocalData:) || action == @selector(loadLocalData:)) {
304 } else if(action == @selector(showHidePreferencePanle:)) {
306 } else if(action == @selector(openNewBrowser:) || action == @selector(selectBookmark:)) {
308 } else if(action == @selector(showWindowAduster:)) {
312 else if(action == @selector(saveDocument:) || action == @selector(openDocument:)) {
314 } else if(action == @selector(removeDatabaseFile:)) {
319 else if(action == @selector(showShipWindow:) || action == @selector(showEquipmentWindow:)
320 || action == @selector(showMapWindow:) || action == @selector(showOwnershipShipWindow:) ) {
327 - (IBAction)showHideHistory:(id)sender
329 if(!self.historyWindowController) {
330 self.historyWindowController = [HMHistoryWindowController new];
333 NSWindow *window = self.historyWindowController.window;
334 if(!window.isVisible || !window.isMainWindow) {
335 [window makeKeyAndOrderFront:nil];
337 [window orderOut:nil];
341 - (IBAction)showHideSlotItemWindow:(id)sender
343 if(!self.slotItemWindowController) {
344 self.slotItemWindowController = [HMSlotItemWindowController new];
347 NSWindow *window = self.slotItemWindowController.window;
348 if(!window.isVisible || !window.isMainWindow) {
349 [window makeKeyAndOrderFront:nil];
351 [window orderOut:nil];
355 - (IBAction)showHidePreferencePanle:(id)sender
357 if(!self.preferencePanelController) {
358 self.preferencePanelController = [HMPreferencePanelController new];
361 NSWindow *window = self.preferencePanelController.window;
362 if(!window.isVisible || !window.isMainWindow) {
363 [window makeKeyAndOrderFront:nil];
365 [window orderOut:nil];
369 - (IBAction)showHideUpgradableShipWindow:(id)sender
371 if(!self.upgradableShipWindowController) {
372 self.upgradableShipWindowController = [HMUpgradableShipsWindowController new];
375 NSWindow *window = self.upgradableShipWindowController.window;
376 if(!window.isVisible || !window.isMainWindow) {
377 [window makeKeyAndOrderFront:nil];
379 [window orderOut:nil];
383 - (IBAction)showHideScreenshotListWindow:(id)sender
385 NSWindow *window = self.screenshotListWindowController.window;
386 if(!window.isVisible || !window.isMainWindow) {
387 [window makeKeyAndOrderFront:nil];
389 [window orderOut:nil];
392 - (IBAction)showHideAirBaseInfoWindow:(id)sender
394 if(!self.airBaseWindowController) {
395 self.airBaseWindowController = [HMAirBaseWindowController new];
398 NSWindow *window = self.airBaseWindowController.window;
399 if(!window.isVisible || !window.isMainWindow) {
400 [window makeKeyAndOrderFront:nil];
402 [window orderOut:nil];
406 - (IBAction)openNewBrowser:(id)sender
408 [self createNewBrowser];
410 - (IBAction)selectBookmark:(id)sender
412 HMExternalBrowserWindowController *browser = [self createNewBrowser];
414 [browser selectBookmark:sender];
416 - (HMExternalBrowserWindowController *)createNewBrowser
418 HMExternalBrowserWindowController *browser = [HMExternalBrowserWindowController new];
419 [self.browserWindowControllers addObject:browser];
420 [browser.window makeKeyAndOrderFront:nil];
422 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
424 selector:@selector(windowWillClose:)
425 name:NSWindowWillCloseNotification
426 object:browser.window];
430 - (IBAction)showWindowAduster:(id)sender
432 if(! self.browserContentAdjuster) {
433 self.browserContentAdjuster = [HMBrowserContentAdjuster new];
435 [self.browserContentAdjuster showWindow:nil];
438 - (void)windowWillClose:(NSNotification *)notification
440 id object = [notification object];
441 if([self.browserWindowControllers containsObject:object]) {
442 [[NSNotificationCenter defaultCenter] removeObserver:self
443 name:NSWindowWillCloseNotification
445 [self.browserWindowControllers removeObject:object];
449 - (IBAction)removeDatabaseFile:(id)sender
451 NSBundle *mainBundle = [NSBundle mainBundle];
452 NSString *appleScriptPath = [mainBundle pathForResource:@"RemoveDatabaseFileAndRestart"
454 NSTask *task = [NSTask new];
455 task.launchPath = @"/usr/bin/open";
456 task.arguments = @[appleScriptPath];
460 - (IBAction)showMainBrowser:(id)sender
462 [self.browserWindowController showWindow:nil];
467 - (IBAction)showShipWindow:(id)sender
469 if(!_shipWindowController) {
470 self.shipWindowController = [HMShipWindowController new];
472 [self.shipWindowController showWindow:nil];
474 - (IBAction)showEquipmentWindow:(id)sender
476 if(!_equipmentWindowController) {
477 self.equipmentWindowController = [HMEquipmentWindowController new];
479 [self.equipmentWindowController showWindow:nil];
481 - (IBAction)showMapWindow:(id)sender
483 if(!_mapWindowController) {
484 self.mapWindowController = [HMMapWindowController new];
486 [self.mapWindowController showWindow:nil];
488 - (IBAction)showOwnershipShipWindow:(id)sender
490 if(!_shipMDWindowController) {
491 self.shipMDWindowController = [HMShipMasterDetailWindowController new];
493 [self.shipMDWindowController showWindow:nil];
497 - (NSTouchBar *)touchBar
499 if(NSClassFromString(@"NSTouchBar") == Nil) return nil;
501 NSWindow *mainWindow = [NSApplication sharedApplication].mainWindow;
502 if(mainWindow == self.browserWindowController.window) return nil;
504 if(self.mainTouchBar) return self.mainTouchBar;
506 NSArray *toplevel = nil;
507 NSBundle *mainBundle = [NSBundle mainBundle];
508 [mainBundle loadNibNamed:@"MainTouchBar"
510 topLevelObjects:&toplevel];
512 return self.mainTouchBar;
515 #pragma mark - NSApplicationDelegate
516 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
521 #pragma mark - NSUserNotificationCenterDelegate
522 //- (void)removeUserNotification:(NSDictionary *)dict
524 // NSUserNotificationCenter *center = [dict objectForKey:@"center"];
525 // NSUserNotification *notification = [dict objectForKey:@"notification"];
526 // [center removeDeliveredNotification:notification];
527 // // [center _removeDisplayedNotification:notification];
530 - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
535 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
537 // [self performSelector:@selector(removeUserNotification:)
538 // withObject:@{@"center":center, @"notification":notification}
541 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
543 // [center removeDeliveredNotification:notification];
546 - (IBAction)saveDocument:(id)sender
548 NSSavePanel *panel = [NSSavePanel savePanel];
549 [panel setAllowedFileTypes:@[@"plist"]];
550 [panel setPrompt:@"Save log"];
551 [panel setTitle:@"Save log"];
552 [panel beginWithCompletionHandler:^(NSInteger result) {
553 if(result == NSModalResponseOK) {
554 NSArray *array = [self.jsonViewWindowController.commands copy];
555 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
557 NSLog(@"can not convert log.");
560 NSError *error = nil;
561 [data writeToURL:panel.URL
562 options:NSDataWritingAtomic
565 NSLog(@"can not save property list.: %@", error);
571 - (IBAction)openDocument:(id)sender
573 NSOpenPanel *panel = [NSOpenPanel openPanel];
574 [panel setAllowedFileTypes:@[@"plist"]];
575 [panel setAllowsMultipleSelection:NO];
576 [panel setPrompt:@"Open log"];
577 [panel setTitle:@"Open log"];
578 [panel beginWithCompletionHandler:^(NSInteger result) {
579 if(result == NSModalResponseOK) {
580 NSData *data = [NSData dataWithContentsOfURL:panel.URL];
581 id array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
582 if(!array || ![array isKindOfClass:[NSArray class]]) {
583 NSLog(@"Can not convert data to log.");
587 self.logedJSONViewWindowController = [HMJSONViewWindowController new];
588 [self.logedJSONViewWindowController setCommandArray:array];
589 [[self.logedJSONViewWindowController window] setTitle:@"SAVED LOG FILE VIEWER"];
591 [self.logedJSONViewWindowController showWindow:nil];
597 - (IBAction)saveLocalData:(id)sender
599 [[HMTSVSupport new] save:sender];
601 - (IBAction)loadLocalData:(id)sender
603 [[HMTSVSupport new] load:sender];