2018-09-18 15:36:38 +00:00
|
|
|
//
|
|
|
|
// SwiftMainDelegate.swift
|
|
|
|
// I2PLauncher
|
|
|
|
//
|
|
|
|
// Created by Mikal Villa on 17/09/2018.
|
|
|
|
// Copyright © 2018 The I2P Project. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Cocoa
|
2019-05-02 22:50:12 +00:00
|
|
|
import MBPopup
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2019-01-18 16:30:03 +00:00
|
|
|
extension Notification.Name {
|
|
|
|
static let killLauncher = Notification.Name("killStartupLauncher")
|
2019-05-02 22:50:12 +00:00
|
|
|
static let upgradeRouter = Notification.Name("upgradeRouter")
|
|
|
|
static let startRouter = Notification.Name("startRouter")
|
|
|
|
static let stopRouter = Notification.Name("stopRouter")
|
2019-01-18 16:30:03 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 17:01:21 +00:00
|
|
|
class Logger {
|
|
|
|
static func MLog<T>(level:Int32, _ object: T?,file:String = #file, function:String = #function, line:Int = #line) {
|
2019-05-02 22:50:12 +00:00
|
|
|
SBridge.logProxy(level, formattedMsg: "\(makeTag(function: function, file: file, line: line)) : \(String(describing: object))")
|
2018-10-11 17:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static func makeTag(function: String, file: String, line: Int) -> String{
|
|
|
|
let url = NSURL(fileURLWithPath: file)
|
|
|
|
let className:String! = url.lastPathComponent == nil ? file: url.lastPathComponent!
|
2019-05-02 22:50:12 +00:00
|
|
|
return "\(String(describing: className)) \(function)[\(line)]"
|
2018-10-11 17:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-05-02 22:50:12 +00:00
|
|
|
@objc class SwiftApplicationDelegate : NSObject, NSApplicationDelegate, NSMenuDelegate {
|
2018-09-18 15:36:38 +00:00
|
|
|
|
|
|
|
let statusBarController = StatusBarController()
|
2018-09-22 22:13:40 +00:00
|
|
|
let sharedRouterMgmr = RouterManager.shared()
|
2019-05-02 22:50:12 +00:00
|
|
|
//let popupController: MBPopupController
|
|
|
|
//let serviceTableViewController = ServiceTableViewController()
|
|
|
|
//let editorTableViewController: EditorTableViewController
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
// Constructor, think of it like an early entrypoint.
|
2018-09-18 15:36:38 +00:00
|
|
|
override init() {
|
2019-05-02 22:50:12 +00:00
|
|
|
//self.popupController = MBPopupController(contentView: serviceTableViewController.contentView)
|
|
|
|
//self.editorTableViewController = serviceTableViewController.editorTableViewController
|
2018-09-18 15:36:38 +00:00
|
|
|
super.init()
|
2019-03-10 11:16:56 +00:00
|
|
|
|
2018-10-11 17:01:21 +00:00
|
|
|
if (!DetectJava.shared().isJavaFound()) {
|
|
|
|
DetectJava.shared().findIt()
|
|
|
|
if (!DetectJava.shared().isJavaFound()) {
|
|
|
|
Logger.MLog(level:3, "Could not find java....")
|
2018-09-20 02:38:44 +00:00
|
|
|
terminate("No java..")
|
|
|
|
}
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
2018-10-11 17:01:21 +00:00
|
|
|
let javaBinPath = DetectJava.shared().javaBinary
|
|
|
|
Logger.MLog(level:1, "".appendingFormat("Found java home = %@", javaBinPath!))
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2019-05-02 22:50:12 +00:00
|
|
|
let (portIsNotTaken, _) = NetworkUtil.checkTcpPortForListen(port: 7657)
|
2018-09-18 15:36:38 +00:00
|
|
|
if (!portIsNotTaken) {
|
|
|
|
RouterProcessStatus.isRouterRunning = true
|
|
|
|
RouterProcessStatus.isRouterChildProcess = false
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level:2, "I2P Router seems to be running")
|
2018-09-18 15:36:38 +00:00
|
|
|
} else {
|
|
|
|
RouterProcessStatus.isRouterRunning = false
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level:2, "I2P Router seems to NOT be running")
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
2018-09-19 00:37:16 +00:00
|
|
|
} // End of init()
|
2018-09-18 15:36:38 +00:00
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
// A function which detects the current installed I2P router version
|
2019-05-02 22:50:12 +00:00
|
|
|
// NOTE: The return value tells if the function fails to detect I2P or not, and not if I2P is installed or not.
|
|
|
|
@objc func findInstalledI2PVersion() -> Bool {
|
|
|
|
let jarPath = Preferences.shared().i2pBaseDirectory + "/lib/i2p.jar"
|
|
|
|
let subCmd = Preferences.shared().javaCommandPath + "-cp " + jarPath + " net.i2p.CoreVersion"
|
2018-09-19 00:37:16 +00:00
|
|
|
let cmdArgs:[String] = ["-c", subCmd]
|
2019-05-02 22:50:12 +00:00
|
|
|
|
2018-09-18 15:36:38 +00:00
|
|
|
let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
|
|
|
|
let results:ExecutionResult = sub.execute(captureOutput: true)!
|
2019-05-02 22:50:12 +00:00
|
|
|
|
2018-09-18 15:36:38 +00:00
|
|
|
if (results.didCaptureOutput) {
|
2018-09-26 20:42:58 +00:00
|
|
|
if (results.status == 0) {
|
|
|
|
let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level: 1, "".appendingFormat("I2P version detected: %@",i2pVersion ?? "Unknown"))
|
2018-09-26 20:42:58 +00:00
|
|
|
RouterProcessStatus.routerVersion = i2pVersion
|
|
|
|
RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
|
2019-05-02 22:50:12 +00:00
|
|
|
return true
|
2018-09-26 20:42:58 +00:00
|
|
|
} else {
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level: 2, "Non zero exit code from subprocess while trying to detect version number!")
|
2018-09-26 20:42:58 +00:00
|
|
|
for line in results.errorsLines {
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level: 2, line)
|
2018-09-26 20:42:58 +00:00
|
|
|
}
|
2019-05-02 22:50:12 +00:00
|
|
|
return false
|
2018-09-26 20:42:58 +00:00
|
|
|
}
|
2018-09-18 15:36:38 +00:00
|
|
|
} else {
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level: 1, "Warning: Version Detection did NOT captured output")
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
2019-05-02 22:50:12 +00:00
|
|
|
return false
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
|
|
|
|
// Helper functions for the optional dock icon
|
2018-12-08 09:16:28 +00:00
|
|
|
func triggerDockIconShowHide(showIcon state: Bool) -> Bool {
|
|
|
|
var result: Bool
|
|
|
|
if state {
|
2019-05-02 22:50:12 +00:00
|
|
|
result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular)
|
2018-12-08 09:16:28 +00:00
|
|
|
} else {
|
2019-05-02 22:50:12 +00:00
|
|
|
result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.accessory)
|
2018-12-08 09:16:28 +00:00
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
// Helper functions for the optional dock icon
|
2018-12-08 09:16:28 +00:00
|
|
|
func getDockIconStateIsShowing() -> Bool {
|
2019-05-02 22:50:12 +00:00
|
|
|
if NSApp.activationPolicy() == NSApplication.ActivationPolicy.regular {
|
2018-12-08 09:16:28 +00:00
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 22:50:12 +00:00
|
|
|
@objc func updateServices() {
|
|
|
|
/*
|
|
|
|
serviceTableViewController.updateServices { [weak self] in
|
|
|
|
let title = self?.serviceTableViewController.generalStatus == .crashed ? "-.-" : "I2PLauncher"
|
|
|
|
self?.popupController.statusItem.title = title
|
|
|
|
|
|
|
|
if Preferences.shared().notifyOnStatusChange {
|
|
|
|
self?.serviceTableViewController.services.forEach { $0.notifyIfNecessary() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This is the swift "entrypoint". In C it would been "main(argc,argv)"
|
|
|
|
*
|
|
|
|
*/
|
2018-09-18 15:36:38 +00:00
|
|
|
@objc func applicationDidFinishLaunching() {
|
2018-12-08 09:16:28 +00:00
|
|
|
switch Preferences.shared().showAsIconMode {
|
|
|
|
case .bothIcon, .dockIcon:
|
|
|
|
if (!getDockIconStateIsShowing()) {
|
2019-05-02 22:50:12 +00:00
|
|
|
let _ = triggerDockIconShowHide(showIcon: true)
|
2018-12-08 09:16:28 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
if (getDockIconStateIsShowing()) {
|
2019-05-02 22:50:12 +00:00
|
|
|
let _ = triggerDockIconShowHide(showIcon: false)
|
2018-12-08 09:16:28 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-02 22:50:12 +00:00
|
|
|
let runningApps = NSWorkspace.shared.runningApplications
|
|
|
|
let isRunning = !runningApps.filter { $0.bundleIdentifier == Identifiers.launcherApplicationBundleId }.isEmpty
|
2019-01-18 16:30:03 +00:00
|
|
|
// SMLoginItemSetEnabled(launcherAppId as CFString, true)
|
|
|
|
|
|
|
|
if isRunning {
|
|
|
|
DistributedNotificationCenter.default().post(name: .killLauncher, object: Bundle.main.bundleIdentifier!)
|
|
|
|
}
|
2019-03-10 11:16:56 +00:00
|
|
|
|
2019-05-02 22:50:12 +00:00
|
|
|
if (Preferences.shared().alsoStartFirefoxOnLaunch) {
|
|
|
|
DispatchQueue.delay(.seconds(120)) {
|
|
|
|
print("two minutes has passed, executing firefox manager")
|
|
|
|
let _ = FirefoxManager.shared().executeFirefox()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if #available(OSX 10.14, *) {
|
|
|
|
Appearance.addObserver(self)
|
|
|
|
} else {
|
|
|
|
//popupController.backgroundView.backgroundColor = .white
|
|
|
|
}
|
|
|
|
|
|
|
|
NSUserNotificationCenter.default.delegate = self
|
|
|
|
/*
|
|
|
|
popupController.statusItem.button?.image = NSImage(named:"StatusBarButtonImage")
|
|
|
|
popupController.statusItem.button?.toolTip = "I2P Launch Manager"
|
|
|
|
popupController.statusItem.button?.font = NSFont(name: "SF Mono Regular", size: 10) ?? NSFont.systemFont(ofSize: 12)
|
|
|
|
popupController.statusItem.length = 30
|
|
|
|
|
|
|
|
popupController.contentView.wantsLayer = true
|
|
|
|
popupController.contentView.layer?.masksToBounds = true
|
|
|
|
|
|
|
|
serviceTableViewController.setup()
|
|
|
|
|
|
|
|
popupController.willOpenPopup = { [weak self] _ in
|
|
|
|
if self?.editorTableViewController.hidden == true {
|
|
|
|
self?.serviceTableViewController.willOpenPopup()
|
|
|
|
} else {
|
|
|
|
self?.editorTableViewController.willOpenPopup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
popupController.didOpenPopup = { [weak self] in
|
|
|
|
if self?.editorTableViewController.hidden == false {
|
|
|
|
self?.editorTableViewController.didOpenPopup()
|
2019-03-10 11:16:56 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-02 22:50:12 +00:00
|
|
|
*/
|
2018-09-23 03:33:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) {
|
|
|
|
RouterManager.shared().eventManager.listenTo(eventName: eventName, action: callbackActionFn )
|
|
|
|
}
|
|
|
|
|
|
|
|
@objc func triggerEvent(en: String, details: String? = nil) {
|
|
|
|
RouterManager.shared().eventManager.trigger(eventName: en, information: details)
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc static func openLink(url: String) {
|
2018-10-13 03:54:01 +00:00
|
|
|
NSLog("Trying to open \(url)")
|
2019-05-02 22:50:12 +00:00
|
|
|
NSWorkspace.shared.open(NSURL(string: url)! as URL)
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
2019-03-10 11:16:56 +00:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* This function will execute when the launcher shuts down for some reason.
|
|
|
|
* Could be either OS or user triggered.
|
|
|
|
*
|
|
|
|
*/
|
2018-09-18 15:36:38 +00:00
|
|
|
@objc func applicationWillTerminate() {
|
|
|
|
// Shutdown stuff
|
2018-12-08 09:16:28 +00:00
|
|
|
if (Preferences.shared().stopRouterOnLauncherShutdown) {
|
2018-10-11 17:01:21 +00:00
|
|
|
RouterManager.shared().routerRunner.TeardownLaunchd()
|
|
|
|
sleep(2)
|
|
|
|
let status: AgentStatus? = RouterRunner.launchAgent?.status()
|
|
|
|
if status != .unloaded {
|
|
|
|
Logger.MLog(level:2, "Router service not yet stopped")
|
|
|
|
RouterManager.shared().routerRunner.TeardownLaunchd()
|
|
|
|
sleep(5)
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@objc func terminate(_ why: Any?) {
|
2018-10-11 17:01:21 +00:00
|
|
|
Logger.MLog(level:2, "".appendingFormat("Stopping cause of ", why! as! CVarArg))
|
2018-09-18 15:36:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 22:50:12 +00:00
|
|
|
extension SwiftApplicationDelegate: NSUserNotificationCenterDelegate {
|
|
|
|
func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
|
|
|
|
//popupController.openPopup()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension SwiftApplicationDelegate: AppearanceObserver {
|
|
|
|
func changeAppearance(to newAppearance: NSAppearance) {
|
|
|
|
//popupController.backgroundView.backgroundColor = newAppearance.isDarkMode ? .windowBackgroundColor : .white
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|