source: launchers/macosx/I2PLauncher/SwiftMainDelegate.swift @ 7615b92

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

Adding all new code, removed a lot obsolete code and fixed import paths etc.
Mac OS X launcher:

  • UI built on Swift
    • Why?
      • Apple seems to on purpose make it harder to get into Objective-C these days
      • Swift is compiled to native code, but has easiness of Javascript in programming
      • Perfect for the OS X UI, many guides & tutorials as well
  • "Backend" in Objective-C++ / C++14
    • Why?
      • Originally written in Objective-C / C++14 with C++17 backports
      • Only for backend because of the time the development takes *

Short summary of features:

  • Java
    • It can detect java from:
      • JAVA_HOME environment variable
      • "Internet Plug-Ins" Apple stuff
      • By the /usr/libexec/java_home binary helper
    • It can unpack a new version of I2P
      • Unpacks to ~/Library/I2P
      • Can check currently unpacked version in ~/Library/I2P via i2p.jar's "net.i2p.CoreVersion?"
  • User Interface (a popover, see https://youtu.be/k8L3lQ5rUq0 for example of this concept)
    • Router control tab view
      • It can start the router
      • It can stop the router
      • It can detect already running router, then avoid fireing up one
      • It can show basic information about the router state & version
    • Log view tab (not yet done)
  • While left-click triggers popover, right-click draws a minimal context menu
  • Property mode set to 100644
File size: 3.0 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  let javaDetector = DetectJava()
17  static let objCBridge = SBridge()
18 
19  override init() {
20    super.init()
21   
22    self.javaDetector.findIt()
23    if (!javaDetector.isJavaFound()) {
24      print("Could not find java....")
25      terminate("No java..")
26    }
27    let javaBinPath = self.javaDetector.javaHome
28    print("Found java home = ", javaBinPath)
29   
30    let (portIsNotTaken, descPort) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
31    if (!portIsNotTaken) {
32      RouterProcessStatus.isRouterRunning = true
33      RouterProcessStatus.isRouterChildProcess = false
34      print("I2P Router seems to be running")
35    } else {
36      RouterProcessStatus.isRouterRunning = false
37      print("I2P Router seems to NOT be running")
38     
39    }
40   
41   
42  }
43 
44  func findInstalledI2PVersion(jarPath: String, javaBin: String) {
45    var i2pPath = NSHomeDirectory()
46    i2pPath += "/Library/I2P"
47    var jExecPath:String = javaBin
48   
49    // Sometimes, home will return the binary, sometimes the actual home dir. This fixes the diverge.
50    // If JRE is detected, binary follows - if it's JDK, home follows.
51    if (jExecPath.hasSuffix("Home")) {
52      jExecPath += "/jre/bin/java"
53    }
54   
55    let subCmd = jExecPath + " -cp " + jarPath + " net.i2p.CoreVersion"
56   
57    var 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      print("captured output")
63      let i2pVersion = results.outputLines.first?.replace(target: "I2P Core version: ", withString: "")
64      print("I2P version detected: ",i2pVersion!)
65      RouterProcessStatus.routerVersion = i2pVersion
66    } else {
67      print("did NOT captured output")
68     
69    }
70  }
71 
72  @objc func applicationDidFinishLaunching() {
73    print("Hello from swift!")
74    var i2pPath = NSHomeDirectory()
75    i2pPath += "/Library/I2P"
76   
77    let javaBinPath = self.javaDetector.javaHome.replace(target: " ", withString: "\\ ")
78   
79    let fileManager = FileManager()
80    var ok = ObjCBool(true)
81    let doesI2PDirExists = fileManager.fileExists(atPath: i2pPath, isDirectory: &ok)
82   
83    if (!doesI2PDirExists) {
84      // Deploy
85    }
86   
87    let i2pJarPath = i2pPath + "/lib/i2p.jar"
88   
89    findInstalledI2PVersion(jarPath: i2pJarPath, javaBin: javaBinPath)
90  }
91 
92  @objc static func openLink(url: String) {
93    objCBridge.openUrl(url)
94  }
95 
96  @objc func applicationWillTerminate() {
97    // Shutdown stuff
98  }
99 
100  @objc func terminate(_ why: Any?) {
101    print("Stopping cause of ", why!)
102  }
103}
104
Note: See TracBrowser for help on using the repository browser.