fix code preview components

- fix them vanishing after "opening" a file (they get downloaded to
  /tmp/ without extension, which caused them to lose preview)
- make all text/plain mimetype files preview
- don't show them in Replies (in consistency with media components)
This commit is contained in:
Andreas Gattringer
2024-07-15 11:03:15 +02:00
parent 0fd578e6aa
commit 029bda5734

View File

@@ -437,16 +437,23 @@ QList<MessageComponent> MessageContentModel::componentsForType(MessageComponentT
QList<MessageComponent> components; QList<MessageComponent> components;
components += MessageComponent{MessageComponentType::File, QString(), {}}; components += MessageComponent{MessageComponentType::File, QString(), {}};
const auto event = eventCast<const Quotient::RoomMessageEvent>(m_event); const auto event = eventCast<const Quotient::RoomMessageEvent>(m_event);
auto body = EventHandler::rawMessageBody(*event);
components += TextHandler().textComponents(body, EventHandler::messageBodyInputFormat(*event), m_room, event, event->isReplaced());
if (m_emptyItinerary) { if (m_emptyItinerary) {
if (!m_isReply) {
auto fileTransferInfo = fileInfo(); auto fileTransferInfo = fileInfo();
#ifndef Q_OS_ANDROID #ifndef Q_OS_ANDROID
Q_ASSERT(event->content() != nullptr && event->content()->fileInfo() != nullptr);
const QMimeType mimeType = event->content()->fileInfo()->mimeType;
if (mimeType.name() == QStringLiteral("text/plain") || mimeType.parentMimeTypes().contains(QStringLiteral("text/plain"))) {
KSyntaxHighlighting::Repository repository; KSyntaxHighlighting::Repository repository;
const auto definitionForFile = repository.definitionForFileName(fileTransferInfo.localPath.toString()); KSyntaxHighlighting::Definition definitionForFile = repository.definitionForFileName(fileTransferInfo.localPath.path());
if (definitionForFile.isValid() || QFileInfo(fileTransferInfo.localPath.path()).suffix() == QStringLiteral("txt")) { if (!definitionForFile.isValid()) {
definitionForFile = repository.definitionForFileName(event->content()->fileInfo()->originalName);
}
if (!definitionForFile.isValid()) {
definitionForFile = repository.definitionForMimeType(mimeType.name());
}
QFile file(fileTransferInfo.localPath.path()); QFile file(fileTransferInfo.localPath.path());
file.open(QIODevice::ReadOnly); file.open(QIODevice::ReadOnly);
components += MessageComponent{MessageComponentType::Code, components += MessageComponent{MessageComponentType::Code,
@@ -459,6 +466,7 @@ QList<MessageComponent> MessageContentModel::componentsForType(MessageComponentT
QImageReader reader(fileTransferInfo.localPath.path()); QImageReader reader(fileTransferInfo.localPath.path());
components += MessageComponent{MessageComponentType::Pdf, QString(), {{QStringLiteral("size"), reader.size()}}}; components += MessageComponent{MessageComponentType::Pdf, QString(), {{QStringLiteral("size"), reader.size()}}};
} }
}
} else if (m_itineraryModel != nullptr) { } else if (m_itineraryModel != nullptr) {
components += MessageComponent{MessageComponentType::Itinerary, QString(), {}}; components += MessageComponent{MessageComponentType::Itinerary, QString(), {}};
if (m_itineraryModel->rowCount() > 0) { if (m_itineraryModel->rowCount() > 0) {
@@ -467,6 +475,8 @@ QList<MessageComponent> MessageContentModel::componentsForType(MessageComponentT
} else { } else {
updateItineraryModel(); updateItineraryModel();
} }
auto body = EventHandler::rawMessageBody(*event);
components += TextHandler().textComponents(body, EventHandler::messageBodyInputFormat(*event), m_room, event, event->isReplaced());
return components; return components;
} }
case MessageComponentType::Image: case MessageComponentType::Image: