Skip to content

Commit

Permalink
Parse incoming URLs to detect if they're png or jpg to show thumbnails
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinBriza committed Jul 2, 2024
1 parent 0900cd9 commit cf33051
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
17 changes: 17 additions & 0 deletions modules/Lith/Core/formattedstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 3 additions & 0 deletions modules/Lith/Core/formattedstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -109,6 +111,7 @@ class LITHCORE_EXPORT FormattedString {
int length() const;

QStringList urls() const;
QStringList urlsWithPreviews() const;

private:
QList<Part> m_parts {};
Expand Down
4 changes: 2 additions & 2 deletions modules/Lith/UI/ChannelMessage.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion modules/Lith/UI/ChannelMessageThumbnail.qml
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit cf33051

Please sign in to comment.