Changeset 66deb5d


Ignore:
Timestamp:
May 2, 2019 10:50:12 PM (13 months ago)
Author:
meeh <meeh@…>
Branches:
master
Children:
7cb0c9b
Parents:
3e10745
Message:

Mac OSX Launcher: Cleaned, updated and renamed main swift class.

Location:
launchers/macosx
Files:
1 deleted
1 moved

Legend:

Unmodified
Added
Removed
  • launchers/macosx/I2PLauncher/SwiftApplicationDelegate.swift

    r3e10745 r66deb5d  
    99import Foundation
    1010import Cocoa
     11import MBPopup
    1112
    1213extension Notification.Name {
    1314  static let killLauncher = Notification.Name("killStartupLauncher")
     15  static let upgradeRouter = Notification.Name("upgradeRouter")
     16  static let startRouter = Notification.Name("startRouter")
     17  static let stopRouter = Notification.Name("stopRouter")
    1418}
    1519
    1620class Logger {
    1721  static func MLog<T>(level:Int32, _ object: T?,file:String = #file, function:String = #function, line:Int = #line) {
    18     SBridge.logProxy(level, formattedMsg: "\(makeTag(function: function, file: file, line: line)) : \(object)")
     22    SBridge.logProxy(level, formattedMsg: "\(makeTag(function: function, file: file, line: line)) : \(String(describing: object))")
    1923  }
    2024 
     
    2226    let url = NSURL(fileURLWithPath: file)
    2327    let className:String! = url.lastPathComponent == nil ? file: url.lastPathComponent!
    24     return "\(className) \(function)[\(line)]"
    25   }
    26  
    27 }
    28 
    29 
    30 @objc class SwiftMainDelegate : NSObject {
     28    return "\(String(describing: className)) \(function)[\(line)]"
     29  }
     30 
     31}
     32
     33
     34@objc class SwiftApplicationDelegate : NSObject, NSApplicationDelegate, NSMenuDelegate {
    3135 
    3236  let statusBarController = StatusBarController()
    3337  let sharedRouterMgmr = RouterManager.shared()
     38  //let popupController: MBPopupController
     39  //let serviceTableViewController = ServiceTableViewController()
     40  //let editorTableViewController: EditorTableViewController
    3441 
    3542  // Constructor, think of it like an early entrypoint.
    3643  override init() {
     44    //self.popupController = MBPopupController(contentView: serviceTableViewController.contentView)
     45    //self.editorTableViewController = serviceTableViewController.editorTableViewController
    3746    super.init()
    3847   
     
    4756    Logger.MLog(level:1, "".appendingFormat("Found java home = %@", javaBinPath!))
    4857   
    49     let (portIsNotTaken, _) = RouterProcessStatus.checkTcpPortForListen(port: 7657)
     58    let (portIsNotTaken, _) = NetworkUtil.checkTcpPortForListen(port: 7657)
    5059    if (!portIsNotTaken) {
    5160      RouterProcessStatus.isRouterRunning = true
     
    5968 
    6069  // A function which detects the current installed I2P router version
    61   @objc func findInstalledI2PVersion() {
    62     var i2pPath = Preferences.shared().i2pBaseDirectory
    63     let jExecPath:String = Preferences.shared().javaCommandPath
    64    
    65     let jarPath = i2pPath + "/lib/i2p.jar"
    66    
    67     let subCmd = jExecPath + "-cp " + jarPath + " net.i2p.CoreVersion"
    68    
     70  // NOTE: The return value tells if the function fails to detect I2P or not, and not if I2P is installed or not.
     71  @objc func findInstalledI2PVersion() -> Bool {
     72    let jarPath = Preferences.shared().i2pBaseDirectory + "/lib/i2p.jar"
     73    let subCmd = Preferences.shared().javaCommandPath + "-cp " + jarPath + " net.i2p.CoreVersion"
    6974    let cmdArgs:[String] = ["-c", subCmd]
    70     print(cmdArgs)
     75   
    7176    let sub:Subprocess = Subprocess.init(executablePath: "/bin/sh", arguments: cmdArgs)
    7277    let results:ExecutionResult = sub.execute(captureOutput: true)!
     78   
    7379    if (results.didCaptureOutput) {
    7480      if (results.status == 0) {
     
    7783        RouterProcessStatus.routerVersion = i2pVersion
    7884        RouterManager.shared().eventManager.trigger(eventName: "router_version", information: i2pVersion)
     85        return true
    7986      } else {
    8087        Logger.MLog(level: 2, "Non zero exit code from subprocess while trying to detect version number!")
     
    8289          Logger.MLog(level: 2, line)
    8390        }
     91        return false
    8492      }
    8593    } else {
    8694      Logger.MLog(level: 1, "Warning: Version Detection did NOT captured output")
    8795    }
     96    return false
    8897  }
    8998 
     
    93102    var result: Bool
    94103    if state {
    95       result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.regular)
    96     } else {
    97       result = NSApp.setActivationPolicy(NSApplicationActivationPolicy.accessory)
     104      result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.regular)
     105    } else {
     106      result = NSApp.setActivationPolicy(NSApplication.ActivationPolicy.accessory)
    98107    }
    99108    return result
     
    102111  // Helper functions for the optional dock icon
    103112  func getDockIconStateIsShowing() -> Bool {
    104     if NSApp.activationPolicy() == NSApplicationActivationPolicy.regular {
     113    if NSApp.activationPolicy() == NSApplication.ActivationPolicy.regular {
    105114      return true
    106115    } else {
    107116      return false
    108117    }
     118  }
     119 
     120  @objc func updateServices() {
     121    /*
     122    serviceTableViewController.updateServices { [weak self] in
     123      let title = self?.serviceTableViewController.generalStatus == .crashed ? "-.-" : "I2PLauncher"
     124      self?.popupController.statusItem.title = title
     125     
     126      if Preferences.shared().notifyOnStatusChange {
     127        self?.serviceTableViewController.services.forEach { $0.notifyIfNecessary() }
     128      }
     129    }
     130    */
    109131  }
    110132 
     
    118140    case .bothIcon, .dockIcon:
    119141      if (!getDockIconStateIsShowing()) {
    120         triggerDockIconShowHide(showIcon: true)
     142        let _ = triggerDockIconShowHide(showIcon: true)
    121143      }
    122144    default:
    123145      if (getDockIconStateIsShowing()) {
    124         triggerDockIconShowHide(showIcon: false)
    125       }
    126     }
    127     let launcherAppId = "net.i2p.bootstrap.macosx.StartupItemApp"
    128     let runningApps = NSWorkspace.shared().runningApplications
    129     let isRunning = !runningApps.filter { $0.bundleIdentifier == launcherAppId }.isEmpty
     146        let _ = triggerDockIconShowHide(showIcon: false)
     147      }
     148    }
     149    let runningApps = NSWorkspace.shared.runningApplications
     150    let isRunning = !runningApps.filter { $0.bundleIdentifier == Identifiers.launcherApplicationBundleId }.isEmpty
    130151    // SMLoginItemSetEnabled(launcherAppId as CFString, true)
    131152   
     
    134155    }
    135156   
    136     if (Preferences.shared().alsoStartFirefoxOnLaunch)
    137     {
    138       // TODO: For some reason it does not seem to obay the two minutes delay.
    139       // If set, execute i2p browser / firefox after two minutes.
    140       DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
    141         FirefoxManager.shared().executeFirefox()
    142       }
    143     }
     157    if (Preferences.shared().alsoStartFirefoxOnLaunch) {
     158      DispatchQueue.delay(.seconds(120)) {
     159        print("two minutes has passed, executing firefox manager")
     160        let _ = FirefoxManager.shared().executeFirefox()
     161      }
     162    }
     163   
     164    if #available(OSX 10.14, *) {
     165      Appearance.addObserver(self)
     166    } else {
     167      //popupController.backgroundView.backgroundColor = .white
     168    }
     169   
     170    NSUserNotificationCenter.default.delegate = self
     171    /*
     172    popupController.statusItem.button?.image = NSImage(named:"StatusBarButtonImage")
     173    popupController.statusItem.button?.toolTip = "I2P Launch Manager"
     174    popupController.statusItem.button?.font = NSFont(name: "SF Mono Regular", size: 10) ?? NSFont.systemFont(ofSize: 12)
     175    popupController.statusItem.length = 30
     176   
     177    popupController.contentView.wantsLayer = true
     178    popupController.contentView.layer?.masksToBounds = true
     179   
     180    serviceTableViewController.setup()
     181   
     182    popupController.willOpenPopup = { [weak self] _ in
     183      if self?.editorTableViewController.hidden == true {
     184        self?.serviceTableViewController.willOpenPopup()
     185      } else {
     186        self?.editorTableViewController.willOpenPopup()
     187      }
     188    }
     189   
     190    popupController.didOpenPopup = { [weak self] in
     191      if self?.editorTableViewController.hidden == false {
     192        self?.editorTableViewController.didOpenPopup()
     193      }
     194    }
     195    */
    144196  }
    145197 
     
    154206  @objc static func openLink(url: String) {
    155207    NSLog("Trying to open \(url)")
    156     NSWorkspace.shared().open(NSURL(string: url)! as URL)
     208    NSWorkspace.shared.open(NSURL(string: url)! as URL)
    157209  }
    158210 
     
    182234}
    183235
     236extension SwiftApplicationDelegate: NSUserNotificationCenterDelegate {
     237  func userNotificationCenter(_ center: NSUserNotificationCenter, didActivate notification: NSUserNotification) {
     238    //popupController.openPopup()
     239  }
     240}
     241
     242extension SwiftApplicationDelegate: AppearanceObserver {
     243  func changeAppearance(to newAppearance: NSAppearance) {
     244    //popupController.backgroundView.backgroundColor = newAppearance.isDarkMode ? .windowBackgroundColor : .white
     245  }
     246}
     247
Note: See TracChangeset for help on using the changeset viewer.