From cf33051ba5c2fefe48633c52485bc6272aa070c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20B=C5=99=C3=ADza?= Date: Tue, 2 Jul 2024 16:42:57 +0200 Subject: [PATCH] Parse incoming URLs to detect if they're png or jpg to show thumbnails --- modules/Lith/Core/formattedstring.cpp | 17 +++++++++++++++++ modules/Lith/Core/formattedstring.h | 3 +++ modules/Lith/UI/ChannelMessage.qml | 4 ++-- modules/Lith/UI/ChannelMessageThumbnail.qml | 2 +- 4 files changed, 23 insertions(+), 3 deletions(-) diff --git a/modules/Lith/Core/formattedstring.cpp b/modules/Lith/Core/formattedstring.cpp index bff9f51..cc04991 100644 --- a/modules/Lith/Core/formattedstring.cpp +++ b/modules/Lith/Core/formattedstring.cpp @@ -324,6 +324,13 @@ void FormattedString::prune() { Part prefix {it->pos + previousEnd, reMatch.capturedStart() - previousEnd}; Part url = Part {it->pos + reMatch.capturedStart(), reMatch.capturedLength()}; url.hyperlink = true; + + auto parsedUrl = QUrl(url.text(m_fullText, -1).toString()); + auto file = parsedUrl.fileName(); + if (file.endsWith(QStringLiteral(".jpg")) || file.endsWith(QStringLiteral(".jpeg")) || + file.endsWith(QStringLiteral(".png"))) { + url.containsImage = true; + } if (prefix.n > 0) { segments.emplace_back(std::move(prefix)); } @@ -383,6 +390,16 @@ QStringList FormattedString::urls() const { return ret; } +QStringList FormattedString::urlsWithPreviews() const { + QStringList ret; + for (auto& url : m_parts) { + if (url.hyperlink && url.containsImage) { + ret.append(url.text(m_fullText, -1).toString()); + } + } + return ret; +} + bool FormattedString::operator!=(const FormattedString& o) const { return !operator==(o); } diff --git a/modules/Lith/Core/formattedstring.h b/modules/Lith/Core/formattedstring.h index 4b1696b..80e7a24 100644 --- a/modules/Lith/Core/formattedstring.h +++ b/modules/Lith/Core/formattedstring.h @@ -29,6 +29,7 @@ class LITHCORE_EXPORT FormattedString { QML_NAMED_ELEMENT(formattedString) Q_PROPERTY(int length READ length CONSTANT) Q_PROPERTY(QStringList urls READ urls CONSTANT) + Q_PROPERTY(QStringList urlsWithPreviews READ urlsWithPreviews CONSTANT) public: struct Part { struct Color { @@ -59,6 +60,7 @@ class LITHCORE_EXPORT FormattedString { Color foreground {-1, false}; Color background {-1, false}; bool hyperlink {false}; + bool containsImage {false}; bool bold {false}; bool underline {false}; bool italic {false}; @@ -109,6 +111,7 @@ class LITHCORE_EXPORT FormattedString { int length() const; QStringList urls() const; + QStringList urlsWithPreviews() const; private: QList m_parts {}; diff --git a/modules/Lith/UI/ChannelMessage.qml b/modules/Lith/UI/ChannelMessage.qml index 976fea2..c6c0fbb 100644 --- a/modules/Lith/UI/ChannelMessage.qml +++ b/modules/Lith/UI/ChannelMessage.qml @@ -100,7 +100,7 @@ Item { ListView { id: previewListView - visible: Lith.settings.showImageThumbnails && messageModel.message.urls.length > 0 + visible: Lith.settings.showImageThumbnails && messageModel.message.urlsWithPreviews.length > 0 y: (headerLabel.visible ? headerLabel.height : 0) + Math.max(dateAndPrefixLabel.height, messageText.height) height: 192 @@ -110,7 +110,7 @@ Item { spacing: 12 reuseItems: true - model: messageModel.message.urls + model: messageModel.message.urlsWithPreviews delegate: ChannelMessageThumbnail { width: previewListView.height height: previewListView.height diff --git a/modules/Lith/UI/ChannelMessageThumbnail.qml b/modules/Lith/UI/ChannelMessageThumbnail.qml index f4cdf8e..ae797e6 100644 --- a/modules/Lith/UI/ChannelMessageThumbnail.qml +++ b/modules/Lith/UI/ChannelMessageThumbnail.qml @@ -25,7 +25,7 @@ Rectangle { z: -1 anchors.fill: parent fillMode: Image.PreserveAspectFit - source: root.thumbnailUrl.endsWith(".jpg") || root.thumbnailUrl.endsWith(".png") ? root.thumbnailUrl : "" + source: root.thumbnailUrl asynchronous: true } Label {