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 "HMPeriodicNotifier.h"
26 #import "HMHistoryItemCleaner.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>
51 @property (strong) HMBroserWindowController *browserWindowController;
52 @property (strong) HMHistoryWindowController *historyWindowController;
53 @property (strong) HMSlotItemWindowController *slotItemWindowController;
54 @property (strong) HMPreferencePanelController *preferencePanelController;
55 @property (strong) HMUpgradableShipsWindowController *upgradableShipWindowController;
57 //@property (strong) HMExternalBrowserWindowController *externalBrowserWindowController;
58 @property (strong) HMBrowserContentAdjuster *browserContentAdjuster;
60 @property (strong) NSMutableArray *browserWindowControllers;
62 @property (strong) NSMutableArray *updaters;
64 @property (strong) HMFleetManager *fleetManager;
66 @property (strong) HMPeriodicNotifier *historyCleanNotifer;
69 @property (strong) HMShipWindowController *shipWindowController;
70 @property (strong) HMShipMasterDetailWindowController *shipMDWindowController;
71 @property (strong) HMEquipmentWindowController *equipmentWindowController;
72 @property (strong) HMMapWindowController *mapWindowController;
76 @property (strong) HMUITestWindowController *uiTestWindowController;
79 @property (strong) HMJSONViewWindowController *logedJSONViewWindowController;
83 @implementation HMAppDelegate
85 @synthesize screenshotListWindowController = _screenshotListWindowController;
87 - (void)logLineReturn:(NSString *)format, ...
89 @synchronized (self) {
92 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
93 fprintf(stderr, "%s\n", [str UTF8String]);
97 - (void)log:(NSString *)format, ...
99 @synchronized (self) {
101 va_start(ap, format);
102 NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
103 fprintf(stderr, "%s", [str UTF8String]);
112 self.updaters = [NSMutableArray new];
113 _fleetManager = [HMFleetManager new];
118 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
120 [CustomHTTPProtocol setupCache];
122 NSUserNotificationCenter *unc = [NSUserNotificationCenter defaultUserNotificationCenter];
123 [unc setDelegate:self];
125 self.browserWindowControllers = [NSMutableArray new];
127 [NSTimer scheduledTimerWithTimeInterval:0.33
129 selector:@selector(fire:)
136 self.browserWindowController = [HMBroserWindowController new];
137 [self.browserWindowController showWindow:nil];
140 self.jsonViewWindowController = [HMJSONViewWindowController new];
141 [self.jsonViewWindowController showWindow:nil];
145 self.uiTestWindowController = [HMUITestWindowController new];
146 [self.uiTestWindowController showWindow:nil];
148 if(!HMStandardDefaults.showsDebugMenu) {
149 [self.debugMenuItem setHidden:YES];
152 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
153 _historyCleanNotifer = [HMPeriodicNotifier periodicNotifierWithHour:0 minutes:7];
154 [nc addObserverForName:HMPeriodicNotification
155 object:_historyCleanNotifer
157 usingBlock:^(NSNotification * _Nonnull note) {
158 HMHistoryItemCleaner *historyItemCleaner = [HMHistoryItemCleaner new];
159 [historyItemCleaner cleanOldHistoryItems];
163 - (void)addCounterUpdateBlock:(void(^)())updater
165 [self.updaters addObject:updater];
167 - (void)fire:(NSTimer *)timer
169 for(void (^updater)() in self.updaters) {
176 [CustomHTTPProtocol clearCache];
179 - (HMScreenshotListWindowController *)screenshotListWindowController
181 if(_screenshotListWindowController) return _screenshotListWindowController;
182 _screenshotListWindowController = [HMScreenshotListWindowController new];
183 return _screenshotListWindowController;
186 - (NSArray *)shipTypeCategories
188 static NSArray *categories = nil;
190 if(categories) return categories;
195 @[@3, @4], // leght cruiser
196 @[@5,@6], // heavy crusier
197 @[@7, @11, @16, @18], // aircraft carrier
198 @[@8, @9, @10, @12], // battle ship
199 @[@13, @14], // submarine
204 - (NSPredicate *)predicateForShipType:(HMShipType)shipType
206 NSPredicate *predicate = nil;
207 NSArray *categories = [self shipTypeCategories];
213 case kHMLightCruiser:
214 case kHMHeavyCruiser:
215 case kHMAircraftCarrier:
218 predicate = [NSPredicate predicateWithFormat:@"master_ship.stype.id IN %@", categories[shipType]];
223 NSMutableArray *ommitTypes = [NSMutableArray new];
224 for(int i = kHMDestroyer; i < kHMOtherType; i++) {
225 [ommitTypes addObjectsFromArray:categories[i]];
227 predicate = [NSPredicate predicateWithFormat:@"NOT master_ship.stype.id IN %@", ommitTypes];
235 - (void)setScreenShotSaveDirectory:(NSString *)screenShotSaveDirectory
237 HMStandardDefaults.screenShotSaveDirectory = screenShotSaveDirectory;
239 - (NSString *)screenShotSaveDirectory
241 NSString *path = HMStandardDefaults.screenShotSaveDirectory;
243 path = [[self picturesDirectory] path];
248 - (NSURL *)documentsFilesDirectory
250 NSFileManager *fileManager = [NSFileManager defaultManager];
251 return [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
253 - (NSURL *)picturesDirectory
255 NSFileManager *fileManager = [NSFileManager defaultManager];
256 return [[fileManager URLsForDirectory:NSPicturesDirectory inDomains:NSUserDomainMask] lastObject];
258 - (NSURL *)supportDirectory
260 NSFileManager *fileManager = [NSFileManager defaultManager];
261 NSURL *appSupportURL = [[fileManager URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask] lastObject];
262 NSURL *ownAppSuportURL = [appSupportURL URLByAppendingPathComponent:@"com.masakih.KCD"];
263 return ownAppSuportURL;
267 - (NSString *)appNameForUserAgent
269 return @"Version/9.0.3 Safari/601.4.4";
272 - (NSFont *)monospaceSystemFont11
274 NSFont *font11 = nil;
275 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
276 font11 = [NSFont monospacedDigitSystemFontOfSize:11 weight:NSFontWeightRegular];
278 font11 = [NSFont systemFontOfSize:11];
283 - (NSFont *)monospaceSystemFont12
285 NSFont *font12 = nil;
286 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
287 font12 = [NSFont monospacedDigitSystemFontOfSize:12 weight:NSFontWeightRegular];
289 font12 = [NSFont systemFontOfSize:12];
294 - (NSFont *)monospaceSystemFont13
296 NSFont *font13 = nil;
297 if([NSFont respondsToSelector:@selector(monospacedDigitSystemFontOfSize:weight:)]) {
298 font13 = [NSFont monospacedDigitSystemFontOfSize:13 weight:NSFontWeightRegular];
300 font13 = [NSFont systemFontOfSize:13];
307 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
309 SEL action = [menuItem action];
310 if(action == @selector(showHideHistory:)) {
311 NSWindow *window = self.historyWindowController.window;
312 if(!window.isVisible || !window.isMainWindow) {
313 [menuItem setTitle:NSLocalizedString(@"Show History", @"")];
315 [menuItem setTitle:NSLocalizedString(@"Hide History", @"")];
318 } else if(action == @selector(showHideSlotItemWindow:)) {
319 NSWindow *window = self.slotItemWindowController.window;
320 if(!window.isVisible || !window.isMainWindow) {
321 [menuItem setTitle:NSLocalizedString(@"Show Slot Item", @"")];
323 [menuItem setTitle:NSLocalizedString(@"Hide Slot Item", @"")];
326 } else if(action == @selector(showHideUpgradableShipWindow:)) {
327 NSWindow *window = self.upgradableShipWindowController.window;
328 if(!window.isVisible || !window.isMainWindow) {
329 [menuItem setTitle:NSLocalizedString(@"Show Upgradable Ships", @"")];
331 [menuItem setTitle:NSLocalizedString(@"Hide Upgradable Ships", @"")];
334 } else if(action == @selector(showHideScreenshotListWindow:)) {
335 NSWindow *window = self.screenshotListWindowController.window;
336 if(!window.isVisible || !window.isMainWindow) {
337 [menuItem setTitle:NSLocalizedString(@"Show Screenshot List", @"")];
339 [menuItem setTitle:NSLocalizedString(@"Hide Screenshot List", @"")];
342 } else if(action == @selector(saveLocalData:) || action == @selector(loadLocalData:)) {
344 } else if(action == @selector(showHidePreferencePanle:)) {
346 } else if(action == @selector(openNewBrowser:) || action == @selector(selectBookmark:)) {
348 } else if(action == @selector(showWindowAduster:)) {
352 else if(action == @selector(saveDocument:) || action == @selector(openDocument:)) {
354 } else if(action == @selector(removeDatabaseFile:)) {
359 else if(action == @selector(showShipWindow:) || action == @selector(showEquipmentWindow:)
360 || action == @selector(showMapWindow:) || action == @selector(showOwnershipShipWindow:) ) {
367 - (IBAction)showHideHistory:(id)sender
369 if(!self.historyWindowController) {
370 self.historyWindowController = [HMHistoryWindowController new];
373 NSWindow *window = self.historyWindowController.window;
374 if(!window.isVisible || !window.isMainWindow) {
375 [window makeKeyAndOrderFront:nil];
377 [window orderOut:nil];
381 - (IBAction)showHideSlotItemWindow:(id)sender
383 if(!self.slotItemWindowController) {
384 self.slotItemWindowController = [HMSlotItemWindowController new];
387 NSWindow *window = self.slotItemWindowController.window;
388 if(!window.isVisible || !window.isMainWindow) {
389 [window makeKeyAndOrderFront:nil];
391 [window orderOut:nil];
395 - (IBAction)showHidePreferencePanle:(id)sender
397 if(!self.preferencePanelController) {
398 self.preferencePanelController = [HMPreferencePanelController new];
401 NSWindow *window = self.preferencePanelController.window;
402 if(!window.isVisible || !window.isMainWindow) {
403 [window makeKeyAndOrderFront:nil];
405 [window orderOut:nil];
409 - (IBAction)showHideUpgradableShipWindow:(id)sender
411 if(!self.upgradableShipWindowController) {
412 self.upgradableShipWindowController = [HMUpgradableShipsWindowController new];
415 NSWindow *window = self.upgradableShipWindowController.window;
416 if(!window.isVisible || !window.isMainWindow) {
417 [window makeKeyAndOrderFront:nil];
419 [window orderOut:nil];
423 - (IBAction)showHideScreenshotListWindow:(id)sender
425 NSWindow *window = self.screenshotListWindowController.window;
426 if(!window.isVisible || !window.isMainWindow) {
427 [window makeKeyAndOrderFront:nil];
429 [window orderOut:nil];
433 - (IBAction)openNewBrowser:(id)sender
435 [self createNewBrowser];
437 - (IBAction)selectBookmark:(id)sender
439 HMExternalBrowserWindowController *browser = [self createNewBrowser];
441 [browser selectBookmark:sender];
443 - (HMExternalBrowserWindowController *)createNewBrowser
445 HMExternalBrowserWindowController *browser = [HMExternalBrowserWindowController new];
446 [self.browserWindowControllers addObject:browser];
447 [browser.window makeKeyAndOrderFront:nil];
449 NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
451 selector:@selector(windowWillClose:)
452 name:NSWindowWillCloseNotification
453 object:browser.window];
457 - (IBAction)showWindowAduster:(id)sender
459 if(! self.browserContentAdjuster) {
460 self.browserContentAdjuster = [HMBrowserContentAdjuster new];
462 [self.browserContentAdjuster showWindow:nil];
465 - (void)windowWillClose:(NSNotification *)notification
467 id object = [notification object];
468 if([self.browserWindowControllers containsObject:object]) {
469 [self.browserWindowControllers removeObject:object];
470 [[NSNotificationCenter defaultCenter] removeObserver:self
471 name:NSWindowWillCloseNotification
473 [self.browserWindowControllers removeObject:object];
477 - (IBAction)removeDatabaseFile:(id)sender
479 NSBundle *mainBundle = [NSBundle mainBundle];
480 NSString *appleScriptPath = [mainBundle pathForResource:@"RemoveDatabaseFileAndRestart"
482 NSTask *task = [NSTask new];
483 task.launchPath = @"/usr/bin/open";
484 task.arguments = @[appleScriptPath];
490 - (IBAction)showShipWindow:(id)sender
492 if(!_shipWindowController) {
493 self.shipWindowController = [HMShipWindowController new];
495 [self.shipWindowController showWindow:nil];
497 - (IBAction)showEquipmentWindow:(id)sender
499 if(!_equipmentWindowController) {
500 self.equipmentWindowController = [HMEquipmentWindowController new];
502 [self.equipmentWindowController showWindow:nil];
504 - (IBAction)showMapWindow:(id)sender
506 if(!_mapWindowController) {
507 self.mapWindowController = [HMMapWindowController new];
509 [self.mapWindowController showWindow:nil];
511 - (IBAction)showOwnershipShipWindow:(id)sender
513 if(!_shipMDWindowController) {
514 self.shipMDWindowController = [HMShipMasterDetailWindowController new];
516 [self.shipMDWindowController showWindow:nil];
520 #pragma mark - NSApplicationDelegate
521 - (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender
526 #pragma mark - NSUserNotificationCenterDelegate
527 //- (void)removeUserNotification:(NSDictionary *)dict
529 // NSUserNotificationCenter *center = [dict objectForKey:@"center"];
530 // NSUserNotification *notification = [dict objectForKey:@"notification"];
531 // [center removeDeliveredNotification:notification];
532 // // [center _removeDisplayedNotification:notification];
535 - (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)notification
540 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDeliverNotification:(NSUserNotification *)notification
542 // [self performSelector:@selector(removeUserNotification:)
543 // withObject:@{@"center":center, @"notification":notification}
546 //- (void)userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
548 // [center removeDeliveredNotification:notification];
551 - (IBAction)saveDocument:(id)sender
553 NSSavePanel *panel = [NSSavePanel savePanel];
554 [panel setAllowedFileTypes:@[@"plist"]];
555 [panel setPrompt:@"Save log"];
556 [panel setTitle:@"Save log"];
557 [panel beginWithCompletionHandler:^(NSInteger result) {
558 if(result == NSModalResponseOK) {
559 NSArray *array = [self.jsonViewWindowController.commands copy];
560 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
562 NSLog(@"can not convert log.");
565 NSError *error = nil;
566 [data writeToURL:panel.URL
567 options:NSDataWritingAtomic
570 NSLog(@"can not save property list.: %@", error);
576 - (IBAction)openDocument:(id)sender
578 NSOpenPanel *panel = [NSOpenPanel openPanel];
579 [panel setAllowedFileTypes:@[@"plist"]];
580 [panel setAllowsMultipleSelection:NO];
581 [panel setPrompt:@"Open log"];
582 [panel setTitle:@"Open log"];
583 [panel beginWithCompletionHandler:^(NSInteger result) {
584 if(result == NSModalResponseOK) {
585 NSData *data = [NSData dataWithContentsOfURL:panel.URL];
586 id array = [NSKeyedUnarchiver unarchiveObjectWithData:data];
587 if(!array || ![array isKindOfClass:[NSArray class]]) {
588 NSLog(@"Can not convert data to log.");
592 self.logedJSONViewWindowController = [HMJSONViewWindowController new];
593 [self.logedJSONViewWindowController setCommandArray:array];
594 [[self.logedJSONViewWindowController window] setTitle:@"SAVED LOG FILE VIEWER"];
596 [self.logedJSONViewWindowController showWindow:nil];
602 - (IBAction)saveLocalData:(id)sender
604 [[HMTSVSupport new] save:sender];
606 - (IBAction)loadLocalData:(id)sender
608 [[HMTSVSupport new] load:sender];