// // ApplicationDelegate.swift // Cyberlock // // Created by Jura on 8/13/19. // Copyright © 2019 Omicronmedia. All rights reserved. // import AppKit import ServiceManagement protocol ApplicationDelegateInput { func openRoute(_ route: Route) } class ApplicationDelegate: NSObject, NSApplicationDelegate, ApplicationDelegateInput { private struct Constants { static let notification = Notification.Name(PrivadoConstants.Launcher.notificationName) } private var terminateRequired = false private var appCoordinator: ApplicationCoordinator? private var menu: PreferencesWindowController? // MARK: - Application Lifecycle func applicationWillFinishLaunching(_ notification: Notification) { self.openRoute(.show) } func applicationDidFinishLaunching(_ aNotification: Notification) { let environments: [String: Any] = [ "environment": Environment.environment, "arguments": Environment.arguments, "operationSystem": Environment.operationSystem, "currentUUID": Environment.currentUUID, "shortVersion": Environment.shortVersion, "bundleVersion": Environment.bundleVersion ] CometLogger.shared.event(name: PrivadoConstants.Event.environment, attributes: environments, secured: nil) self.killLauncherApplication() self.setupNetworkPlugins() self.setupLoggers() // TrayIcon let trayController = TrayIconModuleBuilder.build(output: nil) if UserDefaults.isFirstLaunch() { UserSettings.shared.isDocked = false } // if docked create popover, else create window let windowStyle: AppWindow = UserSettings.shared.isDocked ? ApplicationPopover(size: CGSize(width: PrivadoConstants.Window.sizeWidth, height: PrivadoConstants.Window.sizeHeight)) : ApplicationWindowController(size: CGSize(width: PrivadoConstants.Window.sizeWidth, height: PrivadoConstants.Window.sizeHeight)) // appWindow.isReleasedWhenClosed = false // Start application coordinator let coordinator = ApplicationCoordinator(windowStyle: windowStyle, trayController: trayController, keychain: KeychainSwift()) self.appCoordinator = coordinator coordinator.start() DispatchQueue.main.asyncAfter(deadline: .now() + 1) { openApplicationRoute(.show) } } func application(_ application: NSApplication, open urls: [URL]) { guard let url = urls.first , let route = Inception.handle(url: url) else { return } self.openRoute(.show) // Show that we login self.openRoute(route) } /// Invoked when the user closes the last window the application has open. func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply { guard !self.terminateRequired else { return .terminateNow } self.terminateRequired = true self.openRoute(.quit) return .terminateNow } func applicationWillTerminate(_ aNotification: Notification) { // Insert code here to tear down your application } func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool { self.openRoute(.show) return true } func applicationWillResignActive(_ notification: Notification) { print("resign active") } // MARK: - Utilities methods // MARK: - Launching application private func killLauncherApplication() { let runningApps = NSWorkspace.shared.runningApplications let isRunning = !runningApps.filter { $0.bundleIdentifier == PrivadoConstants.Launcher.bundle }.isEmpty if isRunning { DistributedNotificationCenter.default().post(name: Constants.notification, object: Bundle.main.bundleIdentifier!) } } private func setupNetworkPlugins() { NetworkRouter.addPlugin(NetworkLoggerPlugin(logger: CometLogger.shared)) NetworkRouter.addPlugin(ErrorNetworkPlugin()) } private func setupLoggers() { let coordinator = DatabaseAnswer.commonDB let loggers: [Comet] = [ SentryLogger(), SnowplowLogger(), NetworkLogger(uuid: Environment.currentUUID, using: coordinator), VPNLogger(uuid: Environment.currentUUID, using: coordinator), EventLogger(uuid: Environment.currentUUID, using: coordinator) ] CometLogger.shared.add(loggers: loggers) #if INTERNAL CometLogger.shared.level = .debug #else CometLogger.shared.level = .info #endif } // MARK: - AppDelegateInput func openRoute(_ route: Route) { self.appCoordinator?.handleRoute(route) } }