2020-09-23 19:57:54 -04:00
|
|
|
// Copyright Mirage authors & contributors <https://github.com/mirukana/mirage>
|
2019-12-19 07:46:16 -04:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-07-13 05:39:01 -04:00
|
|
|
import QtQuick 2.12
|
2019-06-27 02:31:03 -04:00
|
|
|
import io.thp.pyotherside 1.5
|
2019-12-27 08:58:24 -04:00
|
|
|
import CppUtils 0.1
|
2020-05-15 03:03:47 -04:00
|
|
|
import "."
|
2019-06-27 02:31:03 -04:00
|
|
|
|
|
|
|
Python {
|
|
|
|
id: py
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-05-15 03:06:59 -04:00
|
|
|
readonly property var pendingCoroutines: Globals.pendingCoroutines
|
|
|
|
|
2020-05-15 03:03:47 -04:00
|
|
|
function makeFuture(callback) {
|
|
|
|
return Qt.createComponent("Future.qml").createObject(py, {bridge: py})
|
2019-12-07 18:33:33 -04:00
|
|
|
}
|
|
|
|
|
2019-12-06 08:44:45 -04:00
|
|
|
function setattr(obj, attr, value, callback=null) {
|
|
|
|
py.call(py.getattr(obj, "__setattr__"), [attr, value], callback)
|
|
|
|
}
|
|
|
|
|
2019-10-28 06:26:02 -04:00
|
|
|
function callCoro(name, args=[], onSuccess=null, onError=null) {
|
2020-03-08 04:27:43 -04:00
|
|
|
const uuid = name + "." + CppUtils.uuid()
|
2020-05-15 03:03:47 -04:00
|
|
|
const future = makeFuture()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-03-08 04:27:43 -04:00
|
|
|
Globals.pendingCoroutines[uuid] = {future, onSuccess, onError}
|
2020-05-06 13:39:53 -04:00
|
|
|
Globals.pendingCoroutinesChanged()
|
2020-05-21 01:26:12 -04:00
|
|
|
// if (name === "models.ensure_exists_from_qml") { print("r"); return}
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2019-12-18 06:44:18 -04:00
|
|
|
call("BRIDGE.call_backend_coro", [name, uuid, args], pyFuture => {
|
2019-12-18 06:16:24 -04:00
|
|
|
future.privates.pythonFuture = pyFuture
|
2019-12-07 18:33:33 -04:00
|
|
|
})
|
|
|
|
|
2019-12-18 06:16:24 -04:00
|
|
|
return future
|
2019-06-27 02:31:03 -04:00
|
|
|
}
|
|
|
|
|
2019-10-28 06:26:02 -04:00
|
|
|
function callClientCoro(
|
|
|
|
accountId, name, args=[], onSuccess=null, onError=null
|
|
|
|
) {
|
2020-05-15 03:03:47 -04:00
|
|
|
const future = makeFuture()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-09-16 19:16:14 -04:00
|
|
|
callCoro("get_client", [accountId, [name, args]], () => {
|
2020-03-08 04:27:43 -04:00
|
|
|
const uuid = accountId + "." + name + "." + CppUtils.uuid()
|
2019-06-28 18:12:45 -04:00
|
|
|
|
2019-12-02 16:29:29 -04:00
|
|
|
Globals.pendingCoroutines[uuid] = {onSuccess, onError}
|
2020-05-06 13:39:53 -04:00
|
|
|
Globals.pendingCoroutinesChanged()
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2020-03-08 04:27:43 -04:00
|
|
|
const call_args = [accountId, name, uuid, args]
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2019-12-18 06:44:18 -04:00
|
|
|
call("BRIDGE.call_client_coro", call_args, pyFuture => {
|
2019-12-18 06:16:24 -04:00
|
|
|
future.privates.pythonFuture = pyFuture
|
2019-12-07 18:33:33 -04:00
|
|
|
})
|
2019-07-21 08:38:49 -04:00
|
|
|
})
|
2019-12-07 18:33:33 -04:00
|
|
|
|
2019-12-18 06:16:24 -04:00
|
|
|
return future
|
2019-06-28 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
2019-07-21 07:14:16 -04:00
|
|
|
function saveConfig(backend_attribute, data, callback=null) {
|
2019-07-18 21:58:21 -04:00
|
|
|
if (! py.ready) { return } // config not loaded yet
|
2019-12-07 18:33:33 -04:00
|
|
|
return callCoro(backend_attribute + ".write", [data], callback)
|
2019-07-18 21:58:21 -04:00
|
|
|
}
|
|
|
|
|
2019-07-24 17:26:40 -04:00
|
|
|
function loadSettings(callback=null) {
|
2020-03-08 04:27:43 -04:00
|
|
|
const func = "load_settings"
|
2019-12-09 13:21:12 -04:00
|
|
|
|
|
|
|
return callCoro(func, [], ([settings, uiState, history, theme]) => {
|
2019-07-24 17:26:40 -04:00
|
|
|
window.settings = settings
|
|
|
|
window.uiState = uiState
|
2019-12-09 13:21:12 -04:00
|
|
|
window.history = history
|
2019-07-24 17:26:40 -04:00
|
|
|
window.theme = Qt.createQmlObject(theme, window, "theme")
|
2020-08-03 01:29:37 -04:00
|
|
|
utils.theme = window.theme
|
2019-07-24 17:26:40 -04:00
|
|
|
|
|
|
|
if (callback) { callback(settings, uiState, theme) }
|
|
|
|
})
|
|
|
|
}
|
2020-08-03 01:19:08 -04:00
|
|
|
|
|
|
|
function showError(type, traceback, sourceIndication="", message="") {
|
|
|
|
console.error(`python: ${sourceIndication}\n${traceback}`)
|
|
|
|
|
|
|
|
if (Globals.hideErrorTypes.has(type)) {
|
|
|
|
console.info("Not showing popup for ignored error type " + type)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-03 01:26:35 -04:00
|
|
|
window.makePopup(
|
2020-08-03 01:19:08 -04:00
|
|
|
"Popups/UnexpectedErrorPopup.qml",
|
|
|
|
{ errorType: type, message, traceback },
|
|
|
|
)
|
|
|
|
}
|
2019-06-27 02:31:03 -04:00
|
|
|
}
|