2019-12-19 22:46:16 +11:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
|
|
|
|
2019-11-04 04:48:12 +11:00
|
|
|
import QtQuick 2.12
|
2020-03-13 17:52:38 +11:00
|
|
|
import "../PythonBridge"
|
2019-11-04 04:48:12 +11:00
|
|
|
|
|
|
|
HImage {
|
|
|
|
id: image
|
2019-12-16 04:02:40 +11:00
|
|
|
inderterminateProgressBar: isMxc
|
2019-11-04 04:48:12 +11:00
|
|
|
source: sourceOverride || (show ? cachedPath : "")
|
2020-03-13 17:52:38 +11:00
|
|
|
|
2019-11-04 04:48:12 +11:00
|
|
|
onWidthChanged: Qt.callLater(update)
|
|
|
|
onHeightChanged: Qt.callLater(update)
|
|
|
|
onVisibleChanged: Qt.callLater(update)
|
2019-11-16 23:06:56 +11:00
|
|
|
onMxcChanged: Qt.callLater(update)
|
2020-03-13 17:52:38 +11:00
|
|
|
Component.onDestruction: if (getFuture) getFuture.cancel()
|
2019-11-04 04:48:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
property string mxc
|
2020-03-10 02:46:08 +11:00
|
|
|
property string title
|
2019-11-04 04:48:12 +11:00
|
|
|
property string sourceOverride: ""
|
2019-11-05 01:29:41 +11:00
|
|
|
property bool thumbnail: true
|
2019-11-05 05:37:25 +11:00
|
|
|
property var cryptDict: ({})
|
2019-11-04 04:48:12 +11:00
|
|
|
|
|
|
|
property bool show: false
|
|
|
|
property string cachedPath: ""
|
2020-03-13 17:52:38 +11:00
|
|
|
|
|
|
|
property Future getFuture: null
|
|
|
|
|
2019-11-07 06:47:18 +11:00
|
|
|
readonly property bool isMxc: mxc.startsWith("mxc://")
|
2019-11-04 04:48:12 +11:00
|
|
|
|
|
|
|
|
|
|
|
function update() {
|
2020-03-08 19:27:51 +11:00
|
|
|
if (! py) return // component was destroyed
|
|
|
|
|
2020-03-08 19:46:20 +11:00
|
|
|
const w = sourceSize.width || width
|
|
|
|
const h = sourceSize.height || height
|
2019-11-04 04:48:12 +11:00
|
|
|
|
|
|
|
if (! image.mxc || w < 1 || h < 1 ) {
|
|
|
|
show = false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-07 06:47:18 +11:00
|
|
|
if (! isMxc) {
|
2019-12-10 02:35:50 +11:00
|
|
|
if (source !== mxc) source = mxc
|
2019-11-06 00:19:48 +11:00
|
|
|
show = image.visible
|
2019-11-04 22:00:28 +11:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-03-08 19:46:20 +11:00
|
|
|
const method = image.thumbnail ? "get_thumbnail" : "get_media"
|
|
|
|
const args = image.thumbnail ?
|
2020-03-10 02:46:08 +11:00
|
|
|
[image.mxc, image.title, w, h, cryptDict] :
|
|
|
|
[image.mxc, image.title, cryptDict]
|
2019-11-05 01:29:41 +11:00
|
|
|
|
2020-03-13 17:52:38 +11:00
|
|
|
getFuture = py.callCoro("media_cache." + method, args, path => {
|
2019-11-04 22:00:28 +11:00
|
|
|
if (! image) return
|
2019-12-10 02:35:50 +11:00
|
|
|
if (image.cachedPath !== path) image.cachedPath = path
|
2019-11-13 00:10:00 +11:00
|
|
|
|
|
|
|
image.broken = false
|
|
|
|
image.show = image.visible
|
|
|
|
|
2020-03-13 17:52:38 +11:00
|
|
|
}, (type, args, error, traceback) => {
|
|
|
|
print(`Error retrieving ${mxc} (${title}):\n${traceback}`)
|
|
|
|
if (image) image.broken = true
|
2019-11-13 00:10:00 +11:00
|
|
|
},
|
2019-11-04 22:00:28 +11:00
|
|
|
)
|
2019-11-04 04:48:12 +11:00
|
|
|
}
|
|
|
|
}
|