mirror of
https://github.com/telegramdesktop/tdesktop
synced 2025-01-13 18:52:54 +00:00
Move MimeType from utils to core/mime_type.
Also move to Core namespace.
This commit is contained in:
parent
2b36dd660b
commit
0e9793b845
@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "history/history_media_types.h"
|
||||
#include "chat_helpers/message_field.h"
|
||||
#include "core/file_utilities.h"
|
||||
#include "core/mime_type.h"
|
||||
#include "ui/widgets/checkbox.h"
|
||||
#include "ui/widgets/buttons.h"
|
||||
#include "ui/widgets/input_fields.h"
|
||||
@ -801,7 +802,7 @@ void SingleFilePreview::preparePreview(const Storage::PreparedFile &file) {
|
||||
} else {
|
||||
auto fileinfo = QFileInfo(filepath);
|
||||
auto filename = fileinfo.fileName();
|
||||
_fileIsImage = fileIsImage(filename, mimeTypeForFile(fileinfo).name());
|
||||
_fileIsImage = fileIsImage(filename, Core::MimeTypeForFile(fileinfo).name());
|
||||
|
||||
auto songTitle = QString();
|
||||
auto songPerformer = QString();
|
||||
|
93
Telegram/SourceFiles/core/mime_type.cpp
Normal file
93
Telegram/SourceFiles/core/mime_type.cpp
Normal file
@ -0,0 +1,93 @@
|
||||
/*
|
||||
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"
|
||||
|
||||
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"));
|
||||
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)");
|
||||
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");
|
||||
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) {
|
||||
if (mime == qsl("image/webp")) {
|
||||
return MimeType(MimeType::Known::WebP);
|
||||
} else if (mime == qsl("application/x-tdesktop-theme")) {
|
||||
return MimeType(MimeType::Known::TDesktopTheme);
|
||||
} else if (mime == qsl("application/x-tdesktop-palette")) {
|
||||
return MimeType(MimeType::Known::TDesktopPalette);
|
||||
}
|
||||
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);
|
||||
} 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
|
41
Telegram/SourceFiles/core/mime_type.h
Normal file
41
Telegram/SourceFiles/core/mime_type.h
Normal file
@ -0,0 +1,41 @@
|
||||
/*
|
||||
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
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <QtCore/QString>
|
||||
#include <QtCore/QStringList>
|
||||
#include <QtCore/QMimeType>
|
||||
|
||||
namespace Core {
|
||||
|
||||
class MimeType {
|
||||
public:
|
||||
enum class Known {
|
||||
Unknown,
|
||||
TDesktopTheme,
|
||||
TDesktopPalette,
|
||||
WebP,
|
||||
};
|
||||
|
||||
explicit MimeType(const QMimeType &type);
|
||||
explicit MimeType(Known type);
|
||||
QStringList globPatterns() const;
|
||||
QString filterString() const;
|
||||
QString name() const;
|
||||
|
||||
private:
|
||||
QMimeType _typeStruct;
|
||||
Known _type = Known::Unknown;
|
||||
|
||||
};
|
||||
|
||||
MimeType MimeTypeForName(const QString &mime);
|
||||
MimeType MimeTypeForFile(const QFileInfo &file);
|
||||
MimeType MimeTypeForData(const QByteArray &data);
|
||||
|
||||
} // namespace Core
|
@ -1005,76 +1005,3 @@ QString rusKeyboardLayoutSwitch(const QString &from) {
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
QStringList MimeType::globPatterns() const {
|
||||
switch (_type) {
|
||||
case Known::WebP: return QStringList(qsl("*.webp"));
|
||||
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)");
|
||||
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");
|
||||
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) {
|
||||
if (mime == qsl("image/webp")) {
|
||||
return MimeType(MimeType::Known::WebP);
|
||||
} else if (mime == qsl("application/x-tdesktop-theme")) {
|
||||
return MimeType(MimeType::Known::TDesktopTheme);
|
||||
} else if (mime == qsl("application/x-tdesktop-palette")) {
|
||||
return MimeType(MimeType::Known::TDesktopPalette);
|
||||
}
|
||||
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);
|
||||
} 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));
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <set>
|
||||
#include "logs.h"
|
||||
#include "core/basic_types.h"
|
||||
@ -435,35 +436,6 @@ enum DBIPeerReportSpamStatus {
|
||||
dbiprsRequesting = 5, // requesting the cloud setting right now
|
||||
};
|
||||
|
||||
class MimeType {
|
||||
public:
|
||||
enum class Known {
|
||||
Unknown,
|
||||
TDesktopTheme,
|
||||
TDesktopPalette,
|
||||
WebP,
|
||||
};
|
||||
|
||||
MimeType(const QMimeType &type) : _typeStruct(type) {
|
||||
}
|
||||
MimeType(Known type) : _type(type) {
|
||||
}
|
||||
QStringList globPatterns() const;
|
||||
QString filterString() const;
|
||||
QString name() const;
|
||||
|
||||
private:
|
||||
QMimeType _typeStruct;
|
||||
Known _type = Known::Unknown;
|
||||
|
||||
};
|
||||
|
||||
MimeType mimeTypeForName(const QString &mime);
|
||||
MimeType mimeTypeForFile(const QFileInfo &file);
|
||||
MimeType mimeTypeForData(const QByteArray &data);
|
||||
|
||||
#include <cmath>
|
||||
|
||||
inline int rowscount(int fullCount, int countPerRow) {
|
||||
return (fullCount + countPerRow - 1) / countPerRow;
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "inline_bots/inline_bot_layout_item.h"
|
||||
#include "mainwidget.h"
|
||||
#include "core/file_utilities.h"
|
||||
#include "core/mime_type.h"
|
||||
#include "media/media_audio.h"
|
||||
#include "storage/localstorage.h"
|
||||
#include "platform/platform_specific.h"
|
||||
@ -192,7 +193,7 @@ QString documentSaveFilename(const DocumentData *data, bool forceSavingAs = fals
|
||||
}
|
||||
|
||||
QString name, filter, caption, prefix;
|
||||
MimeType mimeType = mimeTypeForName(data->mimeString());
|
||||
const auto mimeType = Core::MimeTypeForName(data->mimeString());
|
||||
QStringList p = mimeType.globPatterns();
|
||||
QString pattern = p.isEmpty() ? QString() : p.front();
|
||||
if (data->isVoiceMessage()) {
|
||||
|
@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "inline_bots/inline_bot_send_data.h"
|
||||
#include "storage/file_download.h"
|
||||
#include "core/file_utilities.h"
|
||||
#include "core/mime_type.h"
|
||||
#include "mainwidget.h"
|
||||
#include "auth_session.h"
|
||||
|
||||
@ -427,7 +428,7 @@ MTPVector<MTPDocumentAttribute> Result::adjustAttributes(
|
||||
const auto &fields = audio->c_documentAttributeAudio();
|
||||
if (!exists(mtpc_documentAttributeFilename)
|
||||
&& !(fields.vflags.v & Flag::f_voice)) {
|
||||
const auto p = mimeTypeForName(mime).globPatterns();
|
||||
const auto p = Core::MimeTypeForName(mime).globPatterns();
|
||||
auto pattern = p.isEmpty() ? QString() : p.front();
|
||||
const auto extension = pattern.isEmpty()
|
||||
? qsl(".unknown")
|
||||
|
@ -12,6 +12,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
#include "mainwindow.h"
|
||||
#include "application.h"
|
||||
#include "core/file_utilities.h"
|
||||
#include "core/mime_type.h"
|
||||
#include "ui/widgets/popup_menu.h"
|
||||
#include "ui/widgets/buttons.h"
|
||||
#include "ui/text_options.h"
|
||||
@ -807,7 +808,7 @@ void MediaView::onSaveAs() {
|
||||
QFileInfo alreadyInfo(location.name());
|
||||
QDir alreadyDir(alreadyInfo.dir());
|
||||
QString name = alreadyInfo.fileName(), filter;
|
||||
MimeType mimeType = mimeTypeForName(_doc->mimeString());
|
||||
const auto mimeType = Core::MimeTypeForName(_doc->mimeString());
|
||||
QStringList p = mimeType.globPatterns();
|
||||
QString pattern = p.isEmpty() ? QString() : p.front();
|
||||
if (name.isEmpty()) {
|
||||
|
@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
|
||||
#include "data/data_document.h"
|
||||
#include "core/file_utilities.h"
|
||||
#include "core/mime_type.h"
|
||||
#include "media/media_audio.h"
|
||||
#include "boxes/send_files_box.h"
|
||||
#include "media/media_clip_reader.h"
|
||||
@ -392,7 +393,7 @@ void FileLoadTask::process() {
|
||||
filesize = info.size();
|
||||
filename = info.fileName();
|
||||
if (!_information) {
|
||||
_information = readMediaInformation(mimeTypeForFile(info).name());
|
||||
_information = readMediaInformation(Core::MimeTypeForFile(info).name());
|
||||
}
|
||||
filemime = _information->filemime;
|
||||
if (auto image = base::get_if<FileMediaInformation::Image>(
|
||||
@ -415,7 +416,7 @@ void FileLoadTask::process() {
|
||||
fullimage = base::take(image->data);
|
||||
}
|
||||
}
|
||||
auto mimeType = mimeTypeForData(_content);
|
||||
const auto mimeType = Core::MimeTypeForData(_content);
|
||||
filemime = mimeType.name();
|
||||
if (filemime != stickerMime) {
|
||||
fullimage = Images::prepareOpaque(std::move(fullimage));
|
||||
@ -444,14 +445,14 @@ void FileLoadTask::process() {
|
||||
if (_type == SendMediaType::Photo) {
|
||||
if (ValidateThumbDimensions(fullimage.width(), fullimage.height())) {
|
||||
filesize = -1; // Fill later.
|
||||
filemime = mimeTypeForName("image/jpeg").name();
|
||||
filemime = Core::MimeTypeForName("image/jpeg").name();
|
||||
filename = filedialogDefaultName(qsl("image"), qsl(".jpg"), QString(), true);
|
||||
} else {
|
||||
_type = SendMediaType::File;
|
||||
}
|
||||
}
|
||||
if (_type == SendMediaType::File) {
|
||||
filemime = mimeTypeForName("image/png").name();
|
||||
filemime = Core::MimeTypeForName("image/png").name();
|
||||
filename = filedialogDefaultName(qsl("image"), qsl(".png"), QString(), true);
|
||||
{
|
||||
QBuffer buffer(&_content);
|
||||
|
@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
||||
|
||||
#include "platform/platform_file_utilities.h"
|
||||
#include "storage/localimageloader.h"
|
||||
#include "core/mime_type.h"
|
||||
|
||||
namespace Storage {
|
||||
namespace {
|
||||
@ -48,13 +49,13 @@ bool PrepareAlbumMediaIsWaiting(
|
||||
crl::async([=, &semaphore, &file] {
|
||||
const auto guard = gsl::finally([&] { semaphore.release(); });
|
||||
if (!file.path.isEmpty()) {
|
||||
file.mime = mimeTypeForFile(QFileInfo(file.path)).name();
|
||||
file.mime = Core::MimeTypeForFile(QFileInfo(file.path)).name();
|
||||
file.information = FileLoadTask::ReadMediaInformation(
|
||||
file.path,
|
||||
QByteArray(),
|
||||
file.mime);
|
||||
} else if (!file.content.isEmpty()) {
|
||||
file.mime = mimeTypeForData(file.content).name();
|
||||
file.mime = Core::MimeTypeForData(file.content).name();
|
||||
file.information = FileLoadTask::ReadMediaInformation(
|
||||
QString(),
|
||||
file.content,
|
||||
|
@ -154,6 +154,8 @@
|
||||
<(src_loc)/core/launcher.h
|
||||
<(src_loc)/core/main_queue_processor.cpp
|
||||
<(src_loc)/core/main_queue_processor.h
|
||||
<(src_loc)/core/mime_type.cpp
|
||||
<(src_loc)/core/mime_type.h
|
||||
<(src_loc)/core/single_timer.cpp
|
||||
<(src_loc)/core/single_timer.h
|
||||
<(src_loc)/core/tl_help.h
|
||||
|
Loading…
Reference in New Issue
Block a user