source: launchers/macosx/I2PLauncher/SwiftMainDelegate.swift @ 3c0a8cf

Last change on this file since 3c0a8cf was 3c0a8cf, checked in by meeh <meeh@…>, 23 months ago

Mac OSX Launcher: A lot of bugfixes, refactoring and cleanup.

  • 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 statusItem = NSStatusBar.system().statusItem(withLength: NSSquareStatusItemLength )
15  let statusBarController = StatusBarController()
16  static let javaDetector = DetectJava()
17  static let objCBridge = SBridge()
18 
19  override init() {
20    super.init()
21    if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
22    SwiftMainDelegate.javaDetector.findIt()
23      if (!SwiftMainDelegate.javaDetector.isJavaFound()) {
24        print("Could not find java....")
25        terminate("No java..")
26      }
27    }
28    let javaBinPath = SwiftMainDelegate.javaDetector.javaHome
29    RouterProcessStatus.knownJavaBinPath = javaBinPath
30    print("Found java home = ", javaBinPath)
31   
32    let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
33    if (!portIsNotTaken) {
34      RouterProcessStatus.isRouterRunning = true
35      RouterProcessStatus.isRouterChildProcess = false
36      print("I2P Router seems to be running")
37    } else {
38      RouterProcessStatus.isRouterRunning = false
39      print("I2P Router seems to NOT be running")
40     
41    }
42   
43   
44  } // End of init()
45 
46  @objc func findInstalledI2PVersion() {
47    var i2pPath = NSHomeDirectory()
48    i2pPath += "/Library/I2P"
49    var jExecPath:String = RouterProcessStatus.knownJavaBinPath!
50   
51    // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
52    // If JRE is detected, binary follows - if it's JDK, home follows.
53    if (jExecPath.hasSuffix("Home")) {
54      jExecPath += "/jre/bin/java"
55    }
56   
57    let jarPath = i2pPath + "/lib/i2p.jar"
58   
59    let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
60   
61    let cmdArgs:[String] = ["-c", subCmd]
62    print(cmdArgs)
63    let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
64    let results:ExecutionResult = sub.execute(captureOutput: true)!
65    if (results.didCaptureOutput) {
66      print("captured output")
67      let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
68      NSLog("I2P version detected: %@",i2pVersion ?? "Unknown")
69      RouterProcessStatus.routerVersion = i2pVersion
70    } else {
71      print("did NOT captured output")
72     
73    }
74  }
75 
76  @objc func applicationDidFinishLaunching() {
77    print("Hello from swift!")
78    var i2pPath = NSHomeDirectory()
79    i2pPath += "/Library/I2P"
80   
81    let fileManager = FileManager()
82    var ok = ObjCBool(true)
83    let doesI2PDirExists = fileManager.fileExists(atPath: i2pPath, isDirectory: &ok)
84   
85    if (!doesI2PDirExists) {
86      // Deploy
87    }
88   
89    //let i2pJarPath = i2pPath + "/lib/i2p.jar"
90   
91    findInstalledI2PVersion()
92  }
93 
94  @objc static func openLink(url: String) {
95    objCBridge.openUrl(url)
96  }
97 
98  @objc func applicationWillTerminate() {
99    // Shutdown stuff
100  }
101 
102  @objc func terminate(_ why: Any?) {
103    print("Stopping cause of ", why!)
104  }
105}
106
Note: See TracBrowser for help on using the repository browser.