From 888e42df34b49ff923f84cf47d7654b984afe27f Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 9 Apr 2020 13:41:30 +0400 Subject: [PATCH] Remove data_document_good_thumbnail module. --- Telegram/CMakeLists.txt | 2 - Telegram/SourceFiles/data/data_document.cpp | 1 - .../data/data_document_good_thumbnail.cpp | 285 ------------------ .../data/data_document_good_thumbnail.h | 73 ----- .../SourceFiles/data/data_document_media.cpp | 1 - 5 files changed, 362 deletions(-) delete mode 100644 Telegram/SourceFiles/data/data_document_good_thumbnail.cpp delete mode 100644 Telegram/SourceFiles/data/data_document_good_thumbnail.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 0fcffa305..38ea4b989 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -358,8 +358,6 @@ PRIVATE data/data_countries.h data/data_document.cpp data/data_document.h - data/data_document_good_thumbnail.cpp - data/data_document_good_thumbnail.h data/data_document_media.cpp data/data_document_media.h data/data_drafts.cpp diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 846206791..2e33c18a2 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -9,7 +9,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_session.h" #include "data/data_streaming.h" -#include "data/data_document_good_thumbnail.h" #include "data/data_document_media.h" #include "data/data_reply_preview.h" #include "lang/lang_keys.h" diff --git a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp b/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp deleted file mode 100644 index 428256a08..000000000 --- a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp +++ /dev/null @@ -1,285 +0,0 @@ -/* -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 "data/data_document_good_thumbnail.h" - -#include "data/data_session.h" -#include "data/data_document.h" -#include "data/data_file_origin.h" -#include "data/data_cloud_themes.h" -#include "media/clip/media_clip_reader.h" -#include "lottie/lottie_animation.h" -#include "window/themes/window_theme_preview.h" -#include "main/main_session.h" -#include "app.h" - -#include -#include - -namespace Data { -namespace { - -constexpr auto kGoodThumbQuality = 87; -constexpr auto kWallPaperSize = 960; - -enum class FileType { - Video, - AnimatedSticker, - WallPaper, - Theme, -}; - -QImage Prepare( - const QString &path, - QByteArray data, - FileType type) { - if (type == FileType::Video) { - return ::Media::Clip::PrepareForSending(path, data).thumbnail; - } else if (type == FileType::AnimatedSticker) { - return Lottie::ReadThumbnail(Lottie::ReadContent(data, path)); - } else if (type == FileType::Theme) { - return Window::Theme::GeneratePreview(data, path); - } - const auto validateSize = [](QSize size) { - return (size.width() + size.height()) < 10'000; - }; - auto buffer = QBuffer(&data); - auto file = QFile(path); - auto device = data.isEmpty() ? static_cast(&file) : &buffer; - auto reader = QImageReader(device); -#ifndef OS_MAC_OLD - reader.setAutoTransform(true); -#endif // OS_MAC_OLD - if (!reader.canRead() || !validateSize(reader.size())) { - return QImage(); - } - auto result = reader.read(); - if (!result.width() || !result.height()) { - return QImage(); - } - return (result.width() > kWallPaperSize - || result.height() > kWallPaperSize) - ? result.scaled( - kWallPaperSize, - kWallPaperSize, - Qt::KeepAspectRatio, - Qt::SmoothTransformation) - : result; -} - -} // namespace - -GoodThumbSource::GoodThumbSource(not_null document) -: _document(document) { -} - -void GoodThumbSource::generate(base::binary_guard &&guard) { - if (!guard) { - return; - } - const auto data = _document->data(); - const auto type = _document->isWallPaper() - ? FileType::WallPaper - : _document->isTheme() - ? FileType::Theme - : _document->sticker() - ? FileType::AnimatedSticker - : FileType::Video; - auto location = _document->location().isEmpty() - ? nullptr - : std::make_unique(_document->location()); - if (data.isEmpty() && !location) { - _empty = true; - return; - } - crl::async([ - =, - guard = std::move(guard), - location = std::move(location) - ]() mutable { - const auto filepath = (location && location->accessEnable()) - ? location->name() - : QString(); - auto result = Prepare(filepath, data, type); - auto bytes = QByteArray(); - if (!result.isNull()) { - auto buffer = QBuffer(&bytes); - const auto format = (type == FileType::AnimatedSticker) - ? "WEBP" - : (type == FileType::WallPaper && result.hasAlphaChannel()) - ? "PNG" - : "JPG"; - result.save(&buffer, format, kGoodThumbQuality); - } - if (!filepath.isEmpty()) { - location->accessDisable(); - } - const auto bytesSize = bytes.size(); - ready( - std::move(guard), - std::move(result), - bytesSize, - std::move(bytes)); - }); -} - -// NB: This method is called from crl::async(), 'this' is unreliable. -void GoodThumbSource::ready( - base::binary_guard &&guard, - QImage &&image, - int bytesSize, - QByteArray &&bytesForCache) { - crl::on_main(std::move(guard), [ - =, - image = std::move(image), - bytes = std::move(bytesForCache) - ]() mutable { - if (image.isNull()) { - _empty = true; - return; - } - _loaded = std::move(image); - _width = _loaded.width(); - _height = _loaded.height(); - _bytesSize = bytesSize; - if (!bytes.isEmpty()) { - Auth().data().cache().put( - _document->goodThumbnailCacheKey(), - Storage::Cache::Database::TaggedValue{ - std::move(bytes), - kImageCacheTag }); - } - Auth().downloaderTaskFinished().notify(); - }); -} - -void GoodThumbSource::load(FileOrigin origin) { - if (loading() || _empty) { - return; - } - auto callback = [=, guard = _loading.make_guard()]( - QByteArray &&value) mutable { - if (value.isEmpty()) { - crl::on_main([=, guard = std::move(guard)]() mutable { - generate(std::move(guard)); - }); - return; - } - crl::async([ - =, - guard = std::move(guard), - value = std::move(value) - ]() mutable { - ready( - std::move(guard), - App::readImage(value, nullptr, false), - value.size()); - }); - }; - - Auth().data().cache().get( - _document->goodThumbnailCacheKey(), - std::move(callback)); -} - -void GoodThumbSource::loadEvenCancelled(FileOrigin origin) { - _empty = false; - load(origin); -} - -QImage GoodThumbSource::takeLoaded() { - return std::move(_loaded); -} - -void GoodThumbSource::unload() { - _loaded = QImage(); - cancel(); -} - -void GoodThumbSource::automaticLoad( - FileOrigin origin, - const HistoryItem *item) { -} - -void GoodThumbSource::automaticLoadSettingsChanged() { -} - -bool GoodThumbSource::loading() { - return _loading.alive(); -} - -bool GoodThumbSource::displayLoading() { - return false; -} - -void GoodThumbSource::cancel() { - _loading = nullptr; -} - -float64 GoodThumbSource::progress() { - return 1.; -} - -int GoodThumbSource::loadOffset() { - return 0; -} - -const StorageImageLocation &GoodThumbSource::location() { - return StorageImageLocation::Invalid(); -} - -void GoodThumbSource::refreshFileReference(const QByteArray &data) { -} - -std::optional GoodThumbSource::cacheKey() { - return _document->goodThumbnailCacheKey(); -} - -void GoodThumbSource::setDelayedStorageLocation( - const StorageImageLocation &location) { -} - -void GoodThumbSource::performDelayedLoad(FileOrigin origin) { -} - -bool GoodThumbSource::isDelayedStorageImage() const { - return false; -} - -void GoodThumbSource::setImageBytes(const QByteArray &bytes) { - if (!bytes.isEmpty()) { - cancel(); - _loaded = App::readImage(bytes); - _width = _loaded.width(); - _height = _loaded.height(); - _bytesSize = bytes.size(); - } -} - -int GoodThumbSource::width() { - return _width; -} - -int GoodThumbSource::height() { - return _height; -} - -int GoodThumbSource::bytesSize() { - return _bytesSize; -} - -void GoodThumbSource::setInformation(int size, int width, int height) { - _width = width; - _height = height; - _bytesSize = size; -} - -QByteArray GoodThumbSource::bytesForCache() { - return QByteArray(); -} - -} // namespace Data diff --git a/Telegram/SourceFiles/data/data_document_good_thumbnail.h b/Telegram/SourceFiles/data/data_document_good_thumbnail.h deleted file mode 100644 index a13aeb5bb..000000000 --- a/Telegram/SourceFiles/data/data_document_good_thumbnail.h +++ /dev/null @@ -1,73 +0,0 @@ -/* -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 "ui/image/image.h" -#include "base/binary_guard.h" - -class DocumentData; - -namespace Data { - -class GoodThumbSource : public Images::Source { -public: - explicit GoodThumbSource(not_null document); - - void load(FileOrigin origin) override; - void loadEvenCancelled(FileOrigin origin) override; - QImage takeLoaded() override; - void unload() override; - - void automaticLoad( - FileOrigin origin, - const HistoryItem *item) override; - void automaticLoadSettingsChanged() override; - - bool loading() override; - bool displayLoading() override; - void cancel() override; - float64 progress() override; - int loadOffset() override; - - const StorageImageLocation &location() override; - void refreshFileReference(const QByteArray &data) override; - std::optional cacheKey() override; - void setDelayedStorageLocation( - const StorageImageLocation &location) override; - void performDelayedLoad(FileOrigin origin) override; - bool isDelayedStorageImage() const override; - void setImageBytes(const QByteArray &bytes) override; - - int width() override; - int height() override; - int bytesSize() override; - void setInformation(int size, int width, int height) override; - - QByteArray bytesForCache() override; - -private: - void generate(base::binary_guard &&guard); - - // NB: This method is called from crl::async(), 'this' is unreliable. - void ready( - base::binary_guard &&guard, - QImage &&image, - int bytesSize, - QByteArray &&bytesForCache = {}); - - not_null _document; - QImage _loaded; - base::binary_guard _loading; - int _width = 0; - int _height = 0; - int _bytesSize = 0; - bool _empty = false; - -}; - -} // namespace Data diff --git a/Telegram/SourceFiles/data/data_document_media.cpp b/Telegram/SourceFiles/data/data_document_media.cpp index 2f70da8d7..2e1ab3410 100644 --- a/Telegram/SourceFiles/data/data_document_media.cpp +++ b/Telegram/SourceFiles/data/data_document_media.cpp @@ -8,7 +8,6 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_document_media.h" #include "data/data_document.h" -#include "data/data_document_good_thumbnail.h" #include "data/data_session.h" #include "data/data_cloud_themes.h" #include "data/data_file_origin.h"