2018-06-13 22:06:21 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "core/mime_type.h"
|
|
|
|
|
2019-09-04 07:19:15 +00:00
|
|
|
#include <QtCore/QMimeDatabase>
|
|
|
|
|
2018-06-13 22:06:21 +00:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
MimeType::MimeType(const QMimeType &type) : _typeStruct(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeType::MimeType(Known type) : _type(type) {
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList MimeType::globPatterns() const {
|
|
|
|
switch (_type) {
|
|
|
|
case Known::WebP: return QStringList(qsl("*.webp"));
|
2019-07-03 11:03:01 +00:00
|
|
|
case Known::Tgs: return QStringList(qsl("*.tgs"));
|
2018-06-13 22:06:21 +00:00
|
|
|
case Known::TDesktopTheme: return QStringList(qsl("*.tdesktop-theme"));
|
|
|
|
case Known::TDesktopPalette: return QStringList(qsl("*.tdesktop-palette"));
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return _typeStruct.globPatterns();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString MimeType::filterString() const {
|
|
|
|
switch (_type) {
|
|
|
|
case Known::WebP: return qsl("WebP image (*.webp)");
|
2019-07-03 11:03:01 +00:00
|
|
|
case Known::Tgs: return qsl("Telegram sticker (*.tgs)");
|
2018-06-13 22:06:21 +00:00
|
|
|
case Known::TDesktopTheme: return qsl("Theme files (*.tdesktop-theme)");
|
|
|
|
case Known::TDesktopPalette: return qsl("Palette files (*.tdesktop-palette)");
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return _typeStruct.filterString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString MimeType::name() const {
|
|
|
|
switch (_type) {
|
|
|
|
case Known::WebP: return qsl("image/webp");
|
2019-07-03 11:03:01 +00:00
|
|
|
case Known::Tgs: return qsl("application/x-tgsticker");
|
2018-06-13 22:06:21 +00:00
|
|
|
case Known::TDesktopTheme: return qsl("application/x-tdesktop-theme");
|
|
|
|
case Known::TDesktopPalette: return qsl("application/x-tdesktop-palette");
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
return _typeStruct.name();
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeType MimeTypeForName(const QString &mime) {
|
2018-12-03 15:32:07 +00:00
|
|
|
if (mime == qstr("image/webp")) {
|
2018-06-13 22:06:21 +00:00
|
|
|
return MimeType(MimeType::Known::WebP);
|
2019-07-03 11:03:01 +00:00
|
|
|
} else if (mime == qstr("application/x-tgsticker")) {
|
|
|
|
return MimeType(MimeType::Known::Tgs);
|
2019-09-09 20:58:30 +00:00
|
|
|
} else if (mime == qstr("application/x-tdesktop-theme")
|
|
|
|
|| mime == qstr("application/x-tgtheme-tdesktop")) {
|
2018-06-13 22:06:21 +00:00
|
|
|
return MimeType(MimeType::Known::TDesktopTheme);
|
2018-12-03 15:32:07 +00:00
|
|
|
} else if (mime == qstr("application/x-tdesktop-palette")) {
|
2018-06-13 22:06:21 +00:00
|
|
|
return MimeType(MimeType::Known::TDesktopPalette);
|
2018-12-03 15:32:07 +00:00
|
|
|
} else if (mime == qstr("audio/mpeg3")) {
|
|
|
|
return MimeType(QMimeDatabase().mimeTypeForName("audio/mp3"));
|
2018-06-13 22:06:21 +00:00
|
|
|
}
|
|
|
|
return MimeType(QMimeDatabase().mimeTypeForName(mime));
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeType MimeTypeForFile(const QFileInfo &file) {
|
|
|
|
QString path = file.absoluteFilePath();
|
|
|
|
if (path.endsWith(qstr(".webp"), Qt::CaseInsensitive)) {
|
|
|
|
return MimeType(MimeType::Known::WebP);
|
2019-07-03 11:03:01 +00:00
|
|
|
} else if (path.endsWith(qstr(".tgs"), Qt::CaseInsensitive)) {
|
|
|
|
return MimeType(MimeType::Known::Tgs);
|
2018-06-13 22:06:21 +00:00
|
|
|
} else if (path.endsWith(qstr(".tdesktop-theme"), Qt::CaseInsensitive)) {
|
|
|
|
return MimeType(MimeType::Known::TDesktopTheme);
|
|
|
|
} else if (path.endsWith(qstr(".tdesktop-palette"), Qt::CaseInsensitive)) {
|
|
|
|
return MimeType(MimeType::Known::TDesktopPalette);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
QFile f(path);
|
|
|
|
if (f.open(QIODevice::ReadOnly)) {
|
|
|
|
QByteArray magic = f.read(12);
|
|
|
|
if (magic.size() >= 12) {
|
|
|
|
if (!memcmp(magic.constData(), "RIFF", 4) && !memcmp(magic.constData() + 8, "WEBP", 4)) {
|
|
|
|
return MimeType(MimeType::Known::WebP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MimeType(QMimeDatabase().mimeTypeForFile(file));
|
|
|
|
}
|
|
|
|
|
|
|
|
MimeType MimeTypeForData(const QByteArray &data) {
|
|
|
|
if (data.size() >= 12) {
|
|
|
|
if (!memcmp(data.constData(), "RIFF", 4) && !memcmp(data.constData() + 8, "WEBP", 4)) {
|
|
|
|
return MimeType(MimeType::Known::WebP);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return MimeType(QMimeDatabase().mimeTypeForData(data));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Core
|