mirror of
https://git.jami.net/savoirfairelinux/jami-client-qt.git
synced 2025-08-09 01:05:39 +02:00
messagesadapter: better file's detection
use QMimeDatabase to detect file's extension. GitLab: #738 Change-Id: I47c74ef3011881d9694c9c54ab4de7f8a2af86e3
This commit is contained in:
parent
d490e84089
commit
9adddd0d89
2 changed files with 27 additions and 27 deletions
|
@ -29,16 +29,18 @@
|
||||||
#include <api/datatransfermodel.h>
|
#include <api/datatransfermodel.h>
|
||||||
|
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QBuffer>
|
||||||
#include <QClipboard>
|
#include <QClipboard>
|
||||||
#include <QDesktopServices>
|
#include <QDesktopServices>
|
||||||
|
#include <QDir>
|
||||||
#include <QFileInfo>
|
#include <QFileInfo>
|
||||||
#include <QImageReader>
|
#include <QImageReader>
|
||||||
#include <QList>
|
#include <QList>
|
||||||
#include <QUrl>
|
|
||||||
#include <QMimeData>
|
#include <QMimeData>
|
||||||
#include <QBuffer>
|
#include <QMimeDatabase>
|
||||||
|
#include <QUrl>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
#include <QDir>
|
#include <QRegExp>
|
||||||
|
|
||||||
MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
|
MessagesAdapter::MessagesAdapter(AppSettingsManager* settingsManager,
|
||||||
PreviewEngine* previewEngine,
|
PreviewEngine* previewEngine,
|
||||||
|
@ -476,21 +478,17 @@ MessagesAdapter::conversationTypersUrlToName(const QSet<QString>& typersSet)
|
||||||
}
|
}
|
||||||
|
|
||||||
QVariantMap
|
QVariantMap
|
||||||
MessagesAdapter::isLocalImage(const QString& msg)
|
MessagesAdapter::isLocalImage(const QString& mimename)
|
||||||
{
|
{
|
||||||
|
if (mimename.startsWith("image/")) {
|
||||||
|
QString fileFormat = mimename;
|
||||||
|
fileFormat.replace("image/", "");
|
||||||
QImageReader reader;
|
QImageReader reader;
|
||||||
reader.setDecideFormatFromContent(true);
|
|
||||||
reader.setFileName(msg);
|
|
||||||
QByteArray fileFormat = reader.format();
|
|
||||||
if (fileFormat == "gif") {
|
|
||||||
return {{"isAnimatedImage", true}};
|
|
||||||
}
|
|
||||||
QList<QByteArray> supportedFormats = reader.supportedImageFormats();
|
QList<QByteArray> supportedFormats = reader.supportedImageFormats();
|
||||||
auto iterator = std::find_if(supportedFormats.begin(),
|
auto iterator = std::find_if(supportedFormats.begin(),
|
||||||
supportedFormats.end(),
|
supportedFormats.end(),
|
||||||
[fileFormat](QByteArray format) { return format == fileFormat; });
|
[fileFormat](QByteArray format) { return format == fileFormat; });
|
||||||
if (iterator != supportedFormats.end()) {
|
return {{"isImage", iterator != supportedFormats.end()}};
|
||||||
return {{"isImage", true}};
|
|
||||||
}
|
}
|
||||||
return {{"isImage", false}};
|
return {{"isImage", false}};
|
||||||
}
|
}
|
||||||
|
@ -504,26 +502,28 @@ MessagesAdapter::getMediaInfo(const QString& msg)
|
||||||
"<%1 style='width:100%;height:%2;outline:none;background-color:#f1f3f4;"
|
"<%1 style='width:100%;height:%2;outline:none;background-color:#f1f3f4;"
|
||||||
"object-fit:cover;' "
|
"object-fit:cover;' "
|
||||||
"controls controlsList='nodownload' src='file://%3' type='%4'/></body>";
|
"controls controlsList='nodownload' src='file://%3' type='%4'/></body>";
|
||||||
QVariantMap fileInfo = isLocalImage(msg);
|
QMimeDatabase db;
|
||||||
|
QMimeType mime = db.mimeTypeForFile(filePath);
|
||||||
|
QVariantMap fileInfo = isLocalImage(mime.name());
|
||||||
if (fileInfo["isImage"].toBool() || fileInfo["isAnimatedImage"].toBool()) {
|
if (fileInfo["isImage"].toBool() || fileInfo["isAnimatedImage"].toBool()) {
|
||||||
return fileInfo;
|
return fileInfo;
|
||||||
}
|
}
|
||||||
QRegularExpression vPattern("[^\\s]+(.*?)\\.(avi|mov|webm|webp|rmvb)$",
|
static const QRegExp vPattern("(video/)(avi|mov|webm|webp|rmvb)$", Qt::CaseInsensitive);
|
||||||
QRegularExpression::CaseInsensitiveOption);
|
auto match = vPattern.indexIn(mime.name());
|
||||||
QString type = vPattern.match(filePath).captured(2);
|
QString type = vPattern.capturedTexts().size() == 3 ? vPattern.capturedTexts()[1] : "";
|
||||||
if (!type.isEmpty()) {
|
if (!type.isEmpty()) {
|
||||||
return {
|
return {
|
||||||
{"isVideo", true},
|
{"isVideo", true},
|
||||||
{"html", html.arg("video", "100%", filePath, "video/" + type)},
|
{"html", html.arg("video", "100%", filePath, mime.name())},
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
QRegularExpression aPattern("[^\\s]+(.*?)\\.(ogg|flac|wav|mpeg|mp3)$",
|
static const QRegExp aPattern("(audio/)(ogg|flac|wav|mpeg|mp3)$", Qt::CaseInsensitive);
|
||||||
QRegularExpression::CaseInsensitiveOption);
|
match = aPattern.indexIn(mime.name());
|
||||||
type = aPattern.match(filePath).captured(2);
|
type = aPattern.capturedTexts().size() == 3 ? aPattern.capturedTexts()[1] : "";
|
||||||
if (!type.isEmpty()) {
|
if (!type.isEmpty()) {
|
||||||
return {
|
return {
|
||||||
{"isVideo", false},
|
{"isVideo", false},
|
||||||
{"html", html.arg("audio", "54px", filePath, "audio/" + type)},
|
{"html", html.arg("audio", "54px", filePath, mime.name())},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,7 +101,7 @@ protected:
|
||||||
Q_INVOKABLE void deleteInteraction(const QString& interactionId);
|
Q_INVOKABLE void deleteInteraction(const QString& interactionId);
|
||||||
Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName);
|
Q_INVOKABLE void copyToDownloads(const QString& interactionId, const QString& displayName);
|
||||||
Q_INVOKABLE void userIsComposing(bool isComposing);
|
Q_INVOKABLE void userIsComposing(bool isComposing);
|
||||||
Q_INVOKABLE QVariantMap isLocalImage(const QString& msg);
|
Q_INVOKABLE QVariantMap isLocalImage(const QString& mimeName);
|
||||||
Q_INVOKABLE QVariantMap getMediaInfo(const QString& msg);
|
Q_INVOKABLE QVariantMap getMediaInfo(const QString& msg);
|
||||||
Q_INVOKABLE bool isRemoteImage(const QString& msg);
|
Q_INVOKABLE bool isRemoteImage(const QString& msg);
|
||||||
Q_INVOKABLE QString getFormattedTime(const quint64 timestamp);
|
Q_INVOKABLE QString getFormattedTime(const quint64 timestamp);
|
||||||
|
|
Loading…
Add table
Reference in a new issue