From a4a411cf1f7588113e4533d80c53dd2b9fde050d Mon Sep 17 00:00:00 2001 From: Andreas Gattringer Date: Mon, 15 Jul 2024 12:34:11 +0200 Subject: [PATCH] correctly display filename in MimeComponents (previously it would include the caption text and save with it as filename) Don't append filename to the caption. Relevant spec: https://spec.matrix.org/latest/client-server-api/#mfile --- src/eventhandler.cpp | 33 ++++++++++++------- src/timeline/FileComponent.qml | 3 +- src/timeline/ReplyMessageComponentChooser.qml | 7 ++-- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/eventhandler.cpp b/src/eventhandler.cpp index 46f25e3f8..44749b101 100644 --- a/src/eventhandler.cpp +++ b/src/eventhandler.cpp @@ -241,17 +241,19 @@ Qt::TextFormat EventHandler::messageBodyInputFormat(const Quotient::RoomMessageE QString EventHandler::rawMessageBody(const Quotient::RoomMessageEvent &event) { + QString body; + if (event.hasFileContent()) { - auto fileCaption = event.content()->fileInfo()->originalName; - if (fileCaption.isEmpty()) { - fileCaption = event.plainBody(); - } else if (event.content()->fileInfo()->originalName != event.plainBody()) { - fileCaption = event.plainBody() + " | "_ls + fileCaption; + // if filename is given or body is equal to filename, + // then body is a caption + QString fileName = event.content()->fileInfo()->originalName; + QString body = event.plainBody(); + if (fileName.isEmpty() || fileName == body) { + return QString(); } - return fileCaption; + return body; } - QString body; if (event.hasTextContent() && event.content()) { body = static_cast(event.content())->body; } else { @@ -660,23 +662,30 @@ QVariantMap EventHandler::getMediaInfoForEvent(const Quotient::RoomEvent *event) QString eventId = event->id(); // Get the file info for the event. - const EventContent::FileInfo *fileInfo; - bool isSticker = false; if (event->is()) { auto roomMessageEvent = eventCast(event); if (!roomMessageEvent->hasFileContent()) { return {}; } + + const EventContent::FileInfo *fileInfo; fileInfo = roomMessageEvent->content()->fileInfo(); + QVariantMap mediaInfo = getMediaInfoFromFileInfo(fileInfo, eventId, false, false); + // if filename isn't specifically given, it is in body + // https://spec.matrix.org/latest/client-server-api/#mfile + mediaInfo["filename"_ls] = (fileInfo->originalName.isEmpty()) ? roomMessageEvent->plainBody() : fileInfo->originalName; + + return mediaInfo; } else if (event->is()) { + const EventContent::FileInfo *fileInfo; + auto stickerEvent = eventCast(event); fileInfo = &stickerEvent->image(); - isSticker = true; + + return getMediaInfoFromFileInfo(fileInfo, eventId, false, true); } else { return {}; } - - return getMediaInfoFromFileInfo(fileInfo, eventId, false, isSticker); } QVariantMap EventHandler::getMediaInfoFromFileInfo(const EventContent::FileInfo *fileInfo, const QString &eventId, bool isThumbnail, bool isSticker) const diff --git a/src/timeline/FileComponent.qml b/src/timeline/FileComponent.qml index 214d288c8..1bbfe2021 100644 --- a/src/timeline/FileComponent.qml +++ b/src/timeline/FileComponent.qml @@ -45,6 +45,7 @@ ColumnLayout { * - width - The width in pixels of the audio media. * - height - The height in pixels of the audio media. * - tempInfo - mediaInfo (with the same properties as this except no tempInfo) for a temporary image while the file downloads. + * - filename */ required property var mediaInfo @@ -155,7 +156,7 @@ ColumnLayout { spacing: 0 QQC2.Label { Layout.fillWidth: true - text: root.display + text: root.mediaInfo.filename wrapMode: Text.Wrap elide: Text.ElideRight } diff --git a/src/timeline/ReplyMessageComponentChooser.qml b/src/timeline/ReplyMessageComponentChooser.qml index d54bb6017..0e41e637c 100644 --- a/src/timeline/ReplyMessageComponentChooser.qml +++ b/src/timeline/ReplyMessageComponentChooser.qml @@ -69,13 +69,12 @@ DelegateChooser { DelegateChoice { roleValue: MessageComponentType.Video delegate: MimeComponent { - required property string display required property var mediaInfo mimeIconSource: mediaInfo.mimeIcon size: mediaInfo.size duration: mediaInfo.duration - label: display + label: mediaInfo.filename } } @@ -116,7 +115,7 @@ DelegateChooser { mimeIconSource: mediaInfo.mimeIcon size: mediaInfo.size duration: mediaInfo.duration - label: display + label: mediaInfo.filename } } @@ -128,7 +127,7 @@ DelegateChooser { mimeIconSource: mediaInfo.mimeIcon size: mediaInfo.size - label: display + label: mediaInfo.filename } }