Add openMessagesLinksOrFilesExternally keybind
This required us to set the media downloaded local path on events entirely from python instead of simply lazy-fetching them when needed from QML, due to pyotherside's async nature and files that must be open in a certain order.
This commit is contained in:
@@ -141,26 +141,15 @@ HColumnLayout {
|
||||
|
||||
property var media: []
|
||||
property string link: ""
|
||||
property var localPath: null
|
||||
property Future getLocalFuture: null
|
||||
|
||||
readonly property bool isEncryptedMedia:
|
||||
Object.keys(JSON.parse(model.media_crypt_dict)).length > 0
|
||||
|
||||
onClosed: {
|
||||
if (getLocalFuture) getLocalFuture.cancel()
|
||||
media = []
|
||||
link = ""
|
||||
}
|
||||
|
||||
onOpened: if (media.length === 3 && media[1].startsWith("mxc://")) {
|
||||
getLocalFuture = py.callCoro(
|
||||
"media_cache.get_local_media",
|
||||
[media[1], media[2]],
|
||||
path => { localPath = path; getLocalFuture = null },
|
||||
)
|
||||
}
|
||||
|
||||
HMenuItem {
|
||||
icon.name: "toggle-select-message"
|
||||
text: eventDelegate.checked ? qsTr("Deselect") : qsTr("Select")
|
||||
@@ -184,10 +173,10 @@ HColumnLayout {
|
||||
HMenuItem {
|
||||
icon.name: "copy-local-path"
|
||||
text: qsTr("Copy local path")
|
||||
visible: Boolean(contextMenu.localPath)
|
||||
visible: Boolean(model.media_local_path)
|
||||
onTriggered:
|
||||
Clipboard.text =
|
||||
contextMenu.localPath.replace(/^file:\/\//, "")
|
||||
model.media_local_path.replace(/^file:\/\//, "")
|
||||
}
|
||||
|
||||
HMenuItem {
|
||||
|
@@ -127,26 +127,10 @@ Rectangle {
|
||||
HShortcut {
|
||||
sequences: window.settings.keys.openMessagesLinksOrFiles
|
||||
onActivated: {
|
||||
let indice = []
|
||||
const indice =
|
||||
eventList.getFocusedOrSelectedOrLastMediaEvents(true)
|
||||
|
||||
if (eventList.selectedCount) {
|
||||
indice = eventList.checkedIndice
|
||||
} else if (eventList.currentIndex !== -1) {
|
||||
indice = [eventList.currentIndex]
|
||||
} else {
|
||||
// Find most recent event that's a media or contains links
|
||||
for (let i = 0; i < eventList.model.count && i <= 1000; i++) {
|
||||
const ev = eventList.model.get(i)
|
||||
const links = JSON.parse(ev.links)
|
||||
|
||||
if (ev.media_url || ev.thumbnail_url || links.length) {
|
||||
indice = [i]
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (const i of indice.sort().reverse()) {
|
||||
for (const i of Array.from(indice).sort().reverse()) {
|
||||
const event = eventList.model.get(i)
|
||||
|
||||
if (event.media_url || event.thumbnail_url) {
|
||||
@@ -166,6 +150,26 @@ Rectangle {
|
||||
}
|
||||
}
|
||||
|
||||
HShortcut {
|
||||
sequences: window.settings.keys.openMessagesLinksOrFilesExternally
|
||||
onActivated: {
|
||||
const indice =
|
||||
eventList.getFocusedOrSelectedOrLastMediaEvents(true)
|
||||
|
||||
for (const i of Array.from(indice).sort().reverse()) {
|
||||
const event = eventList.model.get(i)
|
||||
|
||||
if (event.media_url) {
|
||||
eventList.openMediaExternally(event)
|
||||
continue
|
||||
}
|
||||
|
||||
for (const url of JSON.parse(event.links))
|
||||
Qt.openUrlExternally(url)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HShortcut {
|
||||
active: eventList.currentItem
|
||||
sequences: window.settings.keys.debugFocusedMessage
|
||||
@@ -318,6 +322,19 @@ Rectangle {
|
||||
}
|
||||
}
|
||||
|
||||
function getFocusedOrSelectedOrLastMediaEvents(acceptLinks=false) {
|
||||
if (eventList.selectedCount) return eventList.checkedIndice
|
||||
if (eventList.currentIndex !== -1) return [eventList.currentIndex]
|
||||
|
||||
// Find most recent event that's a media or contains links
|
||||
for (let i = 0; i < eventList.model.count && i <= 1000; i++) {
|
||||
const ev = eventList.model.get(i)
|
||||
const links = JSON.parse(ev.links)
|
||||
|
||||
if (ev.media_url || (acceptLinks && links.length)) return [i]
|
||||
}
|
||||
}
|
||||
|
||||
function getMediaType(event) {
|
||||
if (event.event_type === "RoomAvatarEvent")
|
||||
return Utils.Media.Image
|
||||
@@ -402,6 +419,11 @@ Rectangle {
|
||||
}
|
||||
|
||||
function getLocalOrDownloadMedia(event, callback) {
|
||||
if (event.media_local_path) {
|
||||
callback(event.media_local_path)
|
||||
return
|
||||
}
|
||||
|
||||
print("Downloading " + event.media_url + " ...")
|
||||
|
||||
const args = [
|
||||
|
Reference in New Issue
Block a user