source: launchers/macosx/I2PLauncher/SwiftMainDelegate.swift @ 7a72049

Last change on this file since 7a72049 was 7a72049, checked in by meeh <meeh@…>, 2 years ago

Mac OS X Launcher:

  • Bugfixes as always
  • Added Sparkle (native updater, https://sparkle-project.org/ )
  • The launcher will now extract and overwrite older versions if found
  • Rewrite of the java extraction part (to enable overwrite)
  • Move more functionality to use EventManager? as it works quite well
  • Added check for updates menu item
  • Property mode set to 100644
File size: 3.1 KB
Line 
1//
2//  SwiftMainDelegate.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 17/09/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10import Cocoa
11
12@objc class SwiftMainDelegate : NSObject {
13 
14  let statusBarController = StatusBarController()
15  let sharedRouterMgmr = RouterManager.shared()
16  static let javaDetector = DetectJava()
17 
18  override init() {
19    super.init()
20    if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
21    SwiftMainDelegate.javaDetector.findIt()
22      if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
23        print("Could not find java....")
24        terminate("No java..")
25      }
26    }
27    let javaBinPath = SwiftMainDelegate.javaDetector.javaHome
28    RouterProcessStatus.knownJavaBinPath = javaBinPath
29    print("Found java home = ", javaBinPath)
30   
31    let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
32    if (!portIsNotTaken) {
33      RouterProcessStatus.isRouterRunning = true
34      RouterProcessStatus.isRouterChildProcess = false
35      print("I2P Router seems to be running")
36    } else {
37      RouterProcessStatus.isRouterRunning = false
38      print("I2P Router seems to NOT be running")
39    }
40  } // End of init()
41 
42  @objc func findInstalledI2PVersion() {
43    var i2pPath = NSHomeDirectory()
44    i2pPath += "/Library/I2P"
45    var jExecPath:String = RouterProcessStatus.knownJavaBinPath!
46   
47    // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
48    // If JRE is detected, binary follows - if it's JDK, home follows.
49    if (jExecPath.hasSuffix("Home")) {
50      jExecPath += "/jre/bin/java"
51    }
52   
53    let jarPath = i2pPath + "/lib/i2p.jar"
54   
55    let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
56   
57    let cmdArgs:[String] = ["-c", subCmd]
58    print(cmdArgs)
59    let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
60    let results:ExecutionResult = sub.execute(captureOutput: true)!
61    if (results.didCaptureOutput) {
62      let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
63      NSLog("I2P version detected: %@",i2pVersion ?? "Unknown")
64      RouterProcessStatus.routerVersion = i2pVersion
65      RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
66    } else {
67      print("Warning: Version Detection did NOT captured output")
68    }
69  }
70 
71  @objc func applicationDidFinishLaunching() {
72    var i2pPath = NSHomeDirectory()
73    i2pPath += "/Library/I2P"
74   
75  }
76 
77  @objc func listenForEvent(eventName: String, callbackActionFn: @escaping ((Any?)->()) ) {
78    RouterManager.shared().eventManager.listenTo(eventName: eventName, action: callbackActionFn )
79  }
80 
81  @objc func triggerEvent(en: String, details: String? = nil) {
82    RouterManager.shared().eventManager.trigger(eventName: en, information: details)
83  }
84 
85  @objc static func openLink(url: String) {
86    SBridge.sharedInstance().openUrl(url)
87  }
88 
89  @objc func applicationWillTerminate() {
90    // Shutdown stuff
91  }
92 
93  @objc func terminate(_ why: Any?) {
94    print("Stopping cause of ", why!)
95  }
96}
97
Note: See TracBrowser for help on using the repository browser.