2021-09-10 08:56:20 +00:00
|
|
|
import NimQml, chronicles, os, strformat, times, md5, json
|
2020-05-29 19:54:35 +00:00
|
|
|
|
2021-03-15 19:24:45 +00:00
|
|
|
import status_go
|
2021-11-16 14:56:12 +00:00
|
|
|
import app/core/main
|
2021-09-09 19:09:17 +00:00
|
|
|
import constants
|
2020-05-16 23:46:46 +00:00
|
|
|
|
2021-11-16 13:49:51 +00:00
|
|
|
import app/global/global_singleton
|
2021-10-06 20:05:13 +00:00
|
|
|
import app/boot/app_controller
|
|
|
|
|
2020-05-21 19:07:55 +00:00
|
|
|
logScope:
|
2021-11-17 15:22:26 +00:00
|
|
|
topics = "status-app"
|
2021-09-07 19:14:56 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
var signalsManagerQObjPointer: pointer
|
2020-05-29 19:54:35 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc isExperimental(): string =
|
|
|
|
result = if getEnv("EXPERIMENTAL") == "1": "1" else: "0" # value explicity passed to avoid trusting input
|
2020-06-23 19:31:52 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc determineResourcePath(): string =
|
|
|
|
result = if defined(windows) and defined(production): "/../resources/resources.rcc" else: "/../resources.rcc"
|
2021-07-21 05:50:35 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc determineFleetsPath(): string =
|
|
|
|
result = if defined(windows) and defined(production): "/../resources/fleets.json" else: "/../fleets.json"
|
2021-10-06 20:05:13 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc determineOpenUri(): string =
|
2021-09-10 08:56:20 +00:00
|
|
|
if OPENURI.len > 0:
|
2021-11-17 15:22:26 +00:00
|
|
|
result = $(%* { "uri": OPENURI })
|
|
|
|
|
|
|
|
proc determineStatusAppIconPath(): string =
|
|
|
|
if defined(production):
|
|
|
|
if defined(macosx):
|
|
|
|
return "" # not used in macOS
|
|
|
|
elif defined(windows):
|
|
|
|
return "/../resources/status.svg"
|
2020-09-17 20:11:31 +00:00
|
|
|
else:
|
2021-11-17 15:22:26 +00:00
|
|
|
return "/../status.svg"
|
|
|
|
else:
|
|
|
|
if defined(macosx):
|
|
|
|
return "" # not used in macOS
|
|
|
|
else:
|
|
|
|
return "/../status-dev.svg"
|
2020-05-18 18:48:20 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc prepareLogging() =
|
2021-09-14 19:39:29 +00:00
|
|
|
when compiles(defaultChroniclesStream.output.writer):
|
|
|
|
defaultChroniclesStream.output.writer =
|
|
|
|
proc (logLevel: LogLevel, msg: LogOutputStr) {.gcsafe, raises: [Defect].} =
|
|
|
|
try:
|
2021-11-17 12:57:19 +00:00
|
|
|
if signalsManagerQObjPointer != nil:
|
|
|
|
signal_handler(signalsManagerQObjPointer, ($(%* {"type": "chronicles-log", "event": msg})).cstring, "receiveSignal")
|
2021-09-14 19:39:29 +00:00
|
|
|
except:
|
|
|
|
logLoggingFailure(cstring(msg), getCurrentException())
|
2021-09-24 12:03:57 +00:00
|
|
|
|
2021-09-14 19:39:29 +00:00
|
|
|
let logFile = fmt"app_{getTime().toUnix}.log"
|
|
|
|
discard defaultChroniclesStream.outputs[1].open(LOGDIR & logFile, fmAppend)
|
|
|
|
|
2022-02-09 09:43:23 +00:00
|
|
|
proc setupRemoteSignalsHandling() =
|
2021-11-17 15:22:26 +00:00
|
|
|
# Please note that this must use the `cdecl` calling convention because
|
|
|
|
# it will be passed as a regular C function to statusgo_backend. This means that
|
|
|
|
# we cannot capture any local variables here (we must rely on globals)
|
|
|
|
var callback: SignalCallback = proc(p0: cstring) {.cdecl.} =
|
|
|
|
if signalsManagerQObjPointer != nil:
|
|
|
|
signal_handler(signalsManagerQObjPointer, p0, "receiveSignal")
|
2021-08-13 20:04:04 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
status_go.setSignalEventCallback(callback)
|
2020-05-11 21:24:08 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
proc mainProc() =
|
|
|
|
if defined(macosx) and defined(production):
|
|
|
|
setCurrentDir(getAppDir())
|
2020-05-20 17:11:30 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
ensureDirectories(DATADIR, TMPDIR, LOGDIR)
|
2020-09-15 19:47:13 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
let isExperimental = isExperimental()
|
|
|
|
let resourcesPath = determineResourcePath()
|
|
|
|
let fleetsPath = determineFleetsPath()
|
|
|
|
let openUri = determineOpenUri()
|
|
|
|
let statusAppIconPath = determineStatusAppIconPath()
|
2022-02-09 09:43:23 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
let fleetConfig = readFile(joinPath(getAppDir(), fleetsPath))
|
|
|
|
let statusFoundation = newStatusFoundation(fleetConfig)
|
|
|
|
let uiScaleFilePath = joinPath(DATADIR, "ui-scale")
|
|
|
|
enableHDPI(uiScaleFilePath)
|
|
|
|
initializeOpenGL()
|
2020-08-25 20:19:46 +00:00
|
|
|
|
2022-02-08 16:28:25 +00:00
|
|
|
let imageCert = imageServerTLSCert()
|
|
|
|
installSelfSignedCertificate(imageCert)
|
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
let app = newQGuiApplication()
|
|
|
|
let appController = newAppController(statusFoundation)
|
|
|
|
let singleInstance = newSingleInstance($toMD5(DATADIR), openUri)
|
|
|
|
let networkAccessFactory = newQNetworkAccessManagerFactory(TMPDIR & "netcache")
|
2022-02-09 09:43:23 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
let isProductionQVariant = newQVariant(if defined(production): true else: false)
|
|
|
|
let isExperimentalQVariant = newQVariant(isExperimental)
|
|
|
|
let signalsManagerQVariant = newQVariant(statusFoundation.signalsManager)
|
2021-09-22 08:10:54 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
QResource.registerResource(app.applicationDirPath & resourcesPath)
|
|
|
|
# Register events objects
|
|
|
|
let dockShowAppEvent = newStatusDockShowAppEventObject(singletonInstance.engine)
|
|
|
|
let osThemeEvent = newStatusOSThemeEventObject(singletonInstance.engine)
|
2021-07-05 12:34:56 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
if not defined(macosx):
|
|
|
|
app.icon(app.applicationDirPath & statusAppIconPath)
|
2020-09-11 17:23:57 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
prepareLogging()
|
2021-08-12 11:26:08 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
singletonInstance.engine.addImportPath("qrc:/./StatusQ/src")
|
|
|
|
singletonInstance.engine.addImportPath("qrc:/./imports")
|
2022-03-03 22:50:53 +00:00
|
|
|
singletonInstance.engine.addImportPath("qrc:/./app");
|
2021-11-17 15:22:26 +00:00
|
|
|
singletonInstance.engine.setNetworkAccessManagerFactory(networkAccessFactory)
|
|
|
|
singletonInstance.engine.setRootContextProperty("uiScaleFilePath", newQVariant(uiScaleFilePath))
|
2021-10-06 20:05:13 +00:00
|
|
|
singletonInstance.engine.setRootContextProperty("singleInstance", newQVariant(singleInstance))
|
2021-11-17 15:22:26 +00:00
|
|
|
singletonInstance.engine.setRootContextProperty("isExperimental", isExperimentalQVariant)
|
2021-11-17 12:57:19 +00:00
|
|
|
singletonInstance.engine.setRootContextProperty("signals", signalsManagerQVariant)
|
2021-11-17 15:22:26 +00:00
|
|
|
singletonInstance.engine.setRootContextProperty("production", isProductionQVariant)
|
2020-05-18 15:07:30 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
app.installEventFilter(dockShowAppEvent)
|
|
|
|
app.installEventFilter(osThemeEvent)
|
2022-02-09 09:43:23 +00:00
|
|
|
|
|
|
|
defer:
|
2021-11-17 15:22:26 +00:00
|
|
|
info "shutting down..."
|
|
|
|
signalsManagerQObjPointer = nil
|
|
|
|
isProductionQVariant.delete()
|
|
|
|
isExperimentalQVariant.delete()
|
|
|
|
signalsManagerQVariant.delete()
|
|
|
|
networkAccessFactory.delete()
|
|
|
|
dockShowAppEvent.delete()
|
|
|
|
osThemeEvent.delete()
|
|
|
|
statusFoundation.delete()
|
|
|
|
appController.delete()
|
|
|
|
singleInstance.delete()
|
|
|
|
app.delete()
|
|
|
|
|
|
|
|
# Checks below must be always after "defer", in case anything fails destructors will freed a memory.
|
|
|
|
if singleInstance.secondInstance():
|
|
|
|
info "Terminating the app as the second instance"
|
|
|
|
quit()
|
2022-02-09 09:43:23 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
info "starting application controller..."
|
2021-10-15 19:58:14 +00:00
|
|
|
appController.start()
|
2022-01-27 15:29:17 +00:00
|
|
|
|
|
|
|
# We need this global variable in order to be able to access the application
|
|
|
|
# from the non-closure callback passed to `statusgo_backend.setSignalEventCallback`
|
|
|
|
signalsManagerQObjPointer = cast[pointer](statusFoundation.signalsManager.vptr)
|
|
|
|
setupRemoteSignalsHandling()
|
2022-02-09 09:43:23 +00:00
|
|
|
|
2021-11-17 15:22:26 +00:00
|
|
|
info "starting application..."
|
2020-05-06 17:40:00 +00:00
|
|
|
app.exec()
|
|
|
|
|
|
|
|
when isMainModule:
|
|
|
|
mainProc()
|