focalboard/mac/Focalboard/AppDelegate.swift

159 lines
4.7 KiB
Swift
Raw Normal View History

2021-01-26 19:32:36 +01:00
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
2020-10-26 20:45:40 +01:00
import Cocoa
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
static let serverStartedNotification = NSNotification.Name("serverStarted")
private var serverProcess: Process?
2021-04-12 23:02:22 +02:00
private weak var whatsnewWindow: NSWindow?
var isServerStarted: Bool {
get { return serverProcess != nil }
}
2020-10-26 20:45:40 +01:00
var serverPort = 8088
2021-02-09 21:27:34 +01:00
var sessionToken: String = ""
2020-10-26 20:45:40 +01:00
func applicationDidFinishLaunching(_ aNotification: Notification) {
copyResources()
startServer()
2021-04-12 23:02:22 +02:00
showWhatsNewDialogIfNeeded()
2020-10-26 20:45:40 +01:00
NotificationCenter.default.post(name: AppDelegate.serverStartedNotification, object: nil)
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
guard flag else {
openNewWindow(nil)
return false
}
return true
}
2020-10-26 20:45:40 +01:00
func applicationWillTerminate(_ aNotification: Notification) {
stopServer()
}
@IBAction func openNewWindow(_ sender: Any?) {
let mainStoryBoard = NSStoryboard(name: "Main", bundle: nil)
let windowController = mainStoryBoard.instantiateController(withIdentifier: "WindowController") as! NSWindowController
windowController.showWindow(self)
}
2021-04-12 23:02:22 +02:00
private func showWhatsNewDialogIfNeeded() {
2021-05-24 18:59:30 +02:00
if Globals.currentWhatsNewVersion > 0 && Globals.currentWhatsNewVersion < Globals.WhatsNewVersion {
2021-04-12 23:02:22 +02:00
Globals.currentWhatsNewVersion = Globals.WhatsNewVersion
showWhatsNew(self)
}
}
@IBAction func showWhatsNew(_: AnyObject) {
if let whatsnewWindow = self.whatsnewWindow {
whatsnewWindow.close()
self.whatsnewWindow = nil
}
let controller: WhatsNewViewController = NSStoryboard.main!.instantiateController(withIdentifier: "WhatsNewViewController") as! WhatsNewViewController
let window = NSWindow(contentViewController: controller)
self.whatsnewWindow = window
window.makeKeyAndOrderFront(self)
let vc = NSWindowController(window: window)
vc.showWindow(self)
}
2020-10-26 20:45:40 +01:00
private func webFolder() -> URL {
let url = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
2021-01-27 00:28:38 +01:00
return url.appendingPathComponent("Focalboard").appendingPathComponent("server")
2020-10-26 20:45:40 +01:00
}
private func copyResources() {
let destBaseUrl = webFolder()
do {
try FileManager.default.createDirectory(atPath: destBaseUrl.path, withIntermediateDirectories: true, attributes: nil)
} catch {
NSLog("copyResources createDirectory ERROR")
}
copyResource(destBaseUrl: destBaseUrl, resourceRelativePath: "pack")
copyResource(destBaseUrl: destBaseUrl, resourceRelativePath: "config.json")
NSLog("copyResources OK")
}
private func copyResource(destBaseUrl: URL, resourceRelativePath: String, fileExtension: String = "") {
let srcUrl = Bundle.main.url(forResource: "resources/" + resourceRelativePath, withExtension: fileExtension)!
let destUrl = destBaseUrl.appendingPathComponent(resourceRelativePath)
let fileManager = FileManager.default
if fileManager.fileExists(atPath: destUrl.path) {
do {
try fileManager.removeItem(at: destUrl)
} catch {
NSLog("copyResource removeItem ERROR")
}
}
do {
try fileManager.copyItem(at: srcUrl, to: destUrl)
} catch {
NSLog("copyResource copyItem ERROR")
return
}
}
2021-02-09 21:42:57 +01:00
private func generateSessionToken() -> String {
let bytesCount = 16
var randomNumber = ""
var randomBytes = [UInt8](repeating: 0, count: bytesCount)
let status = SecRandomCopyBytes(kSecRandomDefault, bytesCount, &randomBytes)
if status != errSecSuccess {
fatalError("SecRandomCopyBytes ERROR: \(status)")
}
randomNumber = randomBytes.map({String(format: "%02hhx", $0)}).joined(separator: "")
2021-02-10 00:51:35 +01:00
return "su-" + randomNumber
2021-02-09 21:42:57 +01:00
}
2021-04-01 17:15:06 +02:00
private func getFreePort() {
if PortUtils.isPortFree(in_port_t(serverPort)) {
return
}
serverPort = Int(PortUtils.getFreePort())
}
2020-10-26 20:45:40 +01:00
private func startServer() {
2021-04-01 17:15:06 +02:00
getFreePort()
2021-02-09 21:42:57 +01:00
sessionToken = generateSessionToken()
2021-02-09 21:27:34 +01:00
2020-10-26 20:45:40 +01:00
let cwdUrl = webFolder()
2021-01-28 23:23:52 +01:00
let executablePath = Bundle.main.path(forResource: "resources/bin/focalboard-server", ofType: "")
2020-10-26 20:45:40 +01:00
let pid = ProcessInfo.processInfo.processIdentifier
NSLog("pid: \(pid)")
let serverProcess = Process()
serverProcess.currentDirectoryPath = cwdUrl.path
serverProcess.arguments = ["-monitorpid", "\(pid)", "-port", "\(serverPort)", "-single-user"]
serverProcess.environment = ["FOCALBOARD_SINGLE_USER_TOKEN": sessionToken]
2020-10-26 20:45:40 +01:00
serverProcess.launchPath = executablePath
serverProcess.launch()
self.serverProcess = serverProcess
NSLog("startServer OK")
NSLog("cwd: \(cwdUrl)")
}
private func stopServer() {
guard let serverProcess = self.serverProcess else { return }
serverProcess.terminate()
self.serverProcess = nil
NSLog("stopServer OK")
}
}