2018-01-16 18:30:28 +01:00
|
|
|
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
|
2017-01-29 20:22:45 +01:00
|
|
|
// This file is part of Parity.
|
|
|
|
|
|
|
|
// Parity is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
|
|
// Parity is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Parity. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import Cocoa
|
|
|
|
|
|
|
|
@NSApplicationMain
|
|
|
|
@available(macOS, deprecated: 10.11)
|
|
|
|
|
|
|
|
class AppDelegate: NSObject, NSApplicationDelegate {
|
|
|
|
@IBOutlet weak var statusMenu: NSMenu!
|
|
|
|
@IBOutlet weak var startAtLogonMenuItem: NSMenuItem!
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
let statusItem = NSStatusBar.system().statusItem(withLength: NSVariableStatusItemLength)
|
|
|
|
var parityPid: Int32? = nil
|
|
|
|
var commandLine: [String] = []
|
|
|
|
let defaultDefaults = "{\"fat_db\":false,\"mode\":\"passive\",\"mode.alarm\":3600,\"mode.timeout\":300,\"pruning\":\"fast\",\"tracing\":false}"
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func menuAppPath() -> String {
|
|
|
|
return Bundle.main.executablePath!
|
|
|
|
}
|
|
|
|
|
|
|
|
func parityPath() -> String {
|
|
|
|
return Bundle.main.bundlePath + "/Contents/MacOS/parity"
|
|
|
|
}
|
|
|
|
|
|
|
|
func isAlreadyRunning() -> Bool {
|
|
|
|
return NSRunningApplication.runningApplications(withBundleIdentifier: Bundle.main.bundleIdentifier!).count > 1
|
|
|
|
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func isParityRunning() -> Bool {
|
|
|
|
if let pid = self.parityPid {
|
|
|
|
return kill(pid, 0) == 0
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func killParity() {
|
|
|
|
if let pid = self.parityPid {
|
2017-03-23 13:25:31 +01:00
|
|
|
kill(pid, SIGKILL)
|
2017-01-29 20:22:45 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func openUI() {
|
|
|
|
let parity = Process()
|
|
|
|
parity.launchPath = self.parityPath()
|
|
|
|
parity.arguments = self.commandLine
|
|
|
|
parity.arguments!.append("ui")
|
|
|
|
parity.launch()
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func writeConfigFiles() {
|
|
|
|
let basePath = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first?
|
|
|
|
.appendingPathComponent(Bundle.main.bundleIdentifier!, isDirectory: true)
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
if FileManager.default.fileExists(atPath: basePath!.path) {
|
|
|
|
return
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
do {
|
|
|
|
let defaultsFileDir = basePath?.appendingPathComponent("chains").appendingPathComponent("ethereum")
|
|
|
|
let defaultsFile = defaultsFileDir?.appendingPathComponent("user_defaults")
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
try FileManager.default.createDirectory(atPath: (defaultsFileDir?.path)!, withIntermediateDirectories: true, attributes: nil)
|
|
|
|
if !FileManager.default.fileExists(atPath: defaultsFile!.path) {
|
|
|
|
try defaultDefaults.write(to: defaultsFile!, atomically: false, encoding: String.Encoding.utf8)
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
let configFile = basePath?.appendingPathComponent("config.toml")
|
|
|
|
}
|
|
|
|
catch {}
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func autostartEnabled() -> Bool {
|
|
|
|
return itemReferencesInLoginItems().existingReference != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func itemReferencesInLoginItems() -> (existingReference: LSSharedFileListItem?, lastReference: LSSharedFileListItem?) {
|
|
|
|
let itemUrl: UnsafeMutablePointer<Unmanaged<CFURL>?> = UnsafeMutablePointer<Unmanaged<CFURL>?>.allocate(capacity: 1)
|
|
|
|
if let appUrl: NSURL = NSURL.fileURL(withPath: Bundle.main.bundlePath) as NSURL? {
|
|
|
|
let loginItemsRef = LSSharedFileListCreate(
|
|
|
|
nil,
|
|
|
|
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
|
|
|
|
nil
|
|
|
|
).takeRetainedValue() as LSSharedFileList?
|
|
|
|
if loginItemsRef != nil {
|
|
|
|
let loginItems: NSArray = LSSharedFileListCopySnapshot(loginItemsRef, nil).takeRetainedValue() as NSArray
|
|
|
|
if(loginItems.count > 0)
|
|
|
|
{
|
|
|
|
let lastItemRef: LSSharedFileListItem = loginItems.lastObject as! LSSharedFileListItem
|
|
|
|
for i in 0 ..< loginItems.count {
|
|
|
|
let currentItemRef: LSSharedFileListItem = loginItems.object(at: i) as! LSSharedFileListItem
|
|
|
|
if LSSharedFileListItemResolve(currentItemRef, 0, itemUrl, nil) == noErr {
|
|
|
|
if let urlRef: NSURL = itemUrl.pointee?.takeRetainedValue() {
|
|
|
|
if urlRef.isEqual(appUrl) {
|
|
|
|
return (currentItemRef, lastItemRef)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//The application was not found in the startup list
|
|
|
|
return (nil, lastItemRef)
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
let addAtStart: LSSharedFileListItem = kLSSharedFileListItemBeforeFirst.takeRetainedValue()
|
|
|
|
return(nil, addAtStart)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (nil, nil)
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func toggleLaunchAtStartup() {
|
|
|
|
let itemReferences = itemReferencesInLoginItems()
|
|
|
|
let shouldBeToggled = (itemReferences.existingReference == nil)
|
|
|
|
let loginItemsRef = LSSharedFileListCreate(
|
|
|
|
nil,
|
|
|
|
kLSSharedFileListSessionLoginItems.takeRetainedValue(),
|
|
|
|
nil
|
|
|
|
).takeRetainedValue() as LSSharedFileList?
|
|
|
|
if loginItemsRef != nil {
|
|
|
|
if shouldBeToggled {
|
|
|
|
if let appUrl : CFURL = NSURL.fileURL(withPath: Bundle.main.bundlePath) as CFURL? {
|
|
|
|
LSSharedFileListInsertItemURL(
|
|
|
|
loginItemsRef,
|
|
|
|
itemReferences.lastReference,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
appUrl,
|
|
|
|
nil,
|
|
|
|
nil
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let itemRef = itemReferences.existingReference {
|
|
|
|
LSSharedFileListItemRemove(loginItemsRef,itemRef)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func launchParity() {
|
|
|
|
self.commandLine = CommandLine.arguments.dropFirst().filter({ $0 != "ui"})
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
let processes = GetBSDProcessList()!
|
|
|
|
let parityProcess = processes.index(where: {
|
|
|
|
var name = $0.kp_proc.p_comm
|
|
|
|
let str = withUnsafePointer(to: &name) {
|
|
|
|
$0.withMemoryRebound(to: UInt8.self, capacity: MemoryLayout.size(ofValue: name)) {
|
|
|
|
String(cString: $0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return str == "parity"
|
|
|
|
})
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
if parityProcess == nil {
|
|
|
|
let parity = Process()
|
|
|
|
let p = self.parityPath()
|
|
|
|
parity.launchPath = p//self.parityPath()
|
|
|
|
parity.arguments = self.commandLine
|
|
|
|
parity.launch()
|
|
|
|
self.parityPid = parity.processIdentifier
|
|
|
|
} else {
|
|
|
|
self.parityPid = processes[parityProcess!].kp_proc.p_pid
|
|
|
|
}
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
|
|
if self.isAlreadyRunning() {
|
|
|
|
openUI()
|
|
|
|
NSApplication.shared().terminate(self)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self.writeConfigFiles()
|
|
|
|
self.launchParity()
|
2018-01-16 18:30:28 +01:00
|
|
|
Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true, block: {_ in
|
2017-01-29 20:22:45 +01:00
|
|
|
if !self.isParityRunning() {
|
|
|
|
NSApplication.shared().terminate(self)
|
|
|
|
}
|
|
|
|
})
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
let icon = NSImage(named: "statusIcon")
|
|
|
|
icon?.isTemplate = true // best for dark mode
|
|
|
|
statusItem.image = icon
|
|
|
|
statusItem.menu = statusMenu
|
|
|
|
}
|
|
|
|
|
|
|
|
override func validateMenuItem(_ menuItem: NSMenuItem) -> Bool {
|
|
|
|
if menuItem == self.startAtLogonMenuItem! {
|
|
|
|
menuItem.state = self.autostartEnabled() ? NSOnState : NSOffState
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
@IBAction func quitClicked(_ sender: NSMenuItem) {
|
|
|
|
self.killParity()
|
|
|
|
NSApplication.shared().terminate(self)
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
@IBAction func openClicked(_ sender: NSMenuItem) {
|
|
|
|
self.openUI()
|
|
|
|
}
|
2018-01-16 18:30:28 +01:00
|
|
|
|
2017-01-29 20:22:45 +01:00
|
|
|
@IBAction func startAtLogonClicked(_ sender: NSMenuItem) {
|
|
|
|
self.toggleLaunchAtStartup()
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|