From 483711771969d80cab1c0ad1bbfaf1a115f3a024 Mon Sep 17 00:00:00 2001 From: John Preston Date: Thu, 25 Oct 2018 13:24:45 +0400 Subject: [PATCH] Use photo as video thumbnail in WebPageData. --- Telegram/SourceFiles/data/data_document.cpp | 8 ++++-- Telegram/SourceFiles/data/data_document.h | 5 ++++ .../data/data_document_good_thumbnail.cpp | 18 ++++++++++--- .../data/data_document_good_thumbnail.h | 5 +++- Telegram/SourceFiles/data/data_web_page.cpp | 20 ++++++++++++++- Telegram/SourceFiles/data/data_web_page.h | 3 +++ Telegram/SourceFiles/ui/image/image.h | 4 +++ .../SourceFiles/ui/image/image_source.cpp | 25 +++++++++++++++++++ Telegram/SourceFiles/ui/image/image_source.h | 6 +++++ 9 files changed, 87 insertions(+), 7 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index 2716b0fe3d..89805afc72 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -552,11 +552,15 @@ void DocumentData::validateGoodThumbnail() { void DocumentData::refreshGoodThumbnail() { if (_goodThumbnail && hasRemoteLocation()) { - _goodThumbnail->replaceSource( - std::make_unique(this)); + replaceGoodThumbnail(std::make_unique(this)); } } +void DocumentData::replaceGoodThumbnail( + std::unique_ptr &&source) { + _goodThumbnail->replaceSource(std::move(source)); +} + void DocumentData::setGoodThumbnail(QImage &&image, QByteArray &&bytes) { Expects(uploadingData != nullptr); diff --git a/Telegram/SourceFiles/data/data_document.h b/Telegram/SourceFiles/data/data_document.h index 35a9edf359..6c64d76827 100644 --- a/Telegram/SourceFiles/data/data_document.h +++ b/Telegram/SourceFiles/data/data_document.h @@ -9,6 +9,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_types.h" +namespace Images { +class Source; +} // namespace Images + namespace Storage { namespace Cache { struct Key; @@ -158,6 +162,7 @@ public: Storage::Cache::Key goodThumbnailCacheKey() const; void setGoodThumbnail(QImage &&image, QByteArray &&bytes); void refreshGoodThumbnail(); + void replaceGoodThumbnail(std::unique_ptr &&source); void setRemoteLocation( int32 dc, diff --git a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp b/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp index 5ad2220bfa..b8925768ea 100644 --- a/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp +++ b/Telegram/SourceFiles/data/data_document_good_thumbnail.cpp @@ -52,9 +52,11 @@ void GoodThumbSource::generate(base::binary_guard &&guard) { if (!filepath.isEmpty()) { location->accessDisable(); } + const auto bytesSize = bytes.size(); ready( std::move(guard), std::move(result.thumbnail), + bytesSize, std::move(bytes)); }); } @@ -63,12 +65,13 @@ void GoodThumbSource::generate(base::binary_guard &&guard) { void GoodThumbSource::ready( base::binary_guard &&guard, QImage &&image, - QByteArray &&bytes) { + int bytesSize, + QByteArray &&bytesForCache) { crl::on_main([ =, guard = std::move(guard), image = std::move(image), - bytes = std::move(bytes) + bytes = std::move(bytesForCache) ]() mutable { if (!guard.alive()) { return; @@ -80,6 +83,7 @@ void GoodThumbSource::ready( _loaded = std::move(image); _width = _loaded.width(); _height = _loaded.height(); + _bytesSize = bytesSize; if (!bytes.isEmpty()) { Auth().data().cache().put( _document->goodThumbnailCacheKey(), @@ -114,7 +118,7 @@ void GoodThumbSource::load( guard = std::move(guard), value = std::move(value) ]() mutable { - ready(std::move(guard), App::readImage(value)); + ready(std::move(guard), App::readImage(value), value.size()); }); }; @@ -194,6 +198,9 @@ void GoodThumbSource::setImageBytes(const QByteArray &bytes) { if (!bytes.isEmpty()) { cancel(); _loaded = App::readImage(bytes); + _width = _loaded.width(); + _height = _loaded.height(); + _bytesSize = bytes.size(); } } @@ -205,9 +212,14 @@ 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() { diff --git a/Telegram/SourceFiles/data/data_document_good_thumbnail.h b/Telegram/SourceFiles/data/data_document_good_thumbnail.h index f22c36d0ee..703bbf5a24 100644 --- a/Telegram/SourceFiles/data/data_document_good_thumbnail.h +++ b/Telegram/SourceFiles/data/data_document_good_thumbnail.h @@ -50,6 +50,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; QByteArray bytesForCache() override; @@ -61,13 +62,15 @@ private: void ready( base::binary_guard &&guard, QImage &&image, - QByteArray &&bytes = {}); + 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; }; diff --git a/Telegram/SourceFiles/data/data_web_page.cpp b/Telegram/SourceFiles/data/data_web_page.cpp index 6a21153759..2ecbdfc515 100644 --- a/Telegram/SourceFiles/data/data_web_page.cpp +++ b/Telegram/SourceFiles/data/data_web_page.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_photo.h" #include "data/data_document.h" #include "ui/image/image.h" +#include "ui/image/image_source.h" #include "ui/text/text_entity.h" namespace { @@ -196,5 +197,22 @@ bool WebPageData::applyChanges( author = resultAuthor; pendingTill = newPendingTill; ++version; + + replaceDocumentGoodThumbnail(); + return true; -} \ No newline at end of file +} + +void WebPageData::replaceDocumentGoodThumbnail() { + if (!document || !photo || !document->goodThumbnail()) { + return; + } + const auto &location = photo->full->location(); + if (!location.isNull()) { + document->replaceGoodThumbnail( + std::make_unique( + location, + photo->full->bytesSize())); + } + +} diff --git a/Telegram/SourceFiles/data/data_web_page.h b/Telegram/SourceFiles/data/data_web_page.h index 16756d2848..c4a9ad0d63 100644 --- a/Telegram/SourceFiles/data/data_web_page.h +++ b/Telegram/SourceFiles/data/data_web_page.h @@ -95,4 +95,7 @@ struct WebPageData { int pendingTill = 0; int version = 0; +private: + void replaceDocumentGoodThumbnail(); + }; diff --git a/Telegram/SourceFiles/ui/image/image.h b/Telegram/SourceFiles/ui/image/image.h index 5ef58aaff1..ce770c36a7 100644 --- a/Telegram/SourceFiles/ui/image/image.h +++ b/Telegram/SourceFiles/ui/image/image.h @@ -78,6 +78,7 @@ public: virtual int width() = 0; virtual int height() = 0; + virtual int bytesSize() = 0; virtual void setInformation(int size, int width, int height) = 0; virtual QByteArray bytesForCache() = 0; @@ -194,6 +195,9 @@ public: int height() const { return _source->height(); } + int bytesSize() const { + return _source->bytesSize(); + } void setInformation(int size, int width, int height) { _source->setInformation(size, width, height); } diff --git a/Telegram/SourceFiles/ui/image/image_source.cpp b/Telegram/SourceFiles/ui/image/image_source.cpp index 1e1bf3229f..22dd4e7c5b 100644 --- a/Telegram/SourceFiles/ui/image/image_source.cpp +++ b/Telegram/SourceFiles/ui/image/image_source.cpp @@ -113,6 +113,10 @@ int ImageSource::height() { return _data.height(); } +int ImageSource::bytesSize() { + return _bytes.size(); +} + void ImageSource::setInformation(int size, int width, int height) { } @@ -242,6 +246,11 @@ int LocalFileSource::height() { return _height; } +int LocalFileSource::bytesSize() { + ensureDimensionsKnown(); + return _bytes.size(); +} + void LocalFileSource::setInformation(int size, int width, int height) { ensureDimensionsKnown(); // First load _bytes. if (width && height) { @@ -456,6 +465,10 @@ int StorageSource::height() { return _location.height(); } +int StorageSource::bytesSize() { + return _size; +} + void StorageSource::setInformation(int size, int width, int height) { if (size) { _size = size; @@ -519,6 +532,10 @@ int WebCachedSource::height() { return _height; } +int WebCachedSource::bytesSize() { + return _size; +} + void WebCachedSource::setInformation(int size, int width, int height) { if (size) { _size = size; @@ -563,6 +580,10 @@ int GeoPointSource::height() { return _location.height * _location.scale; } +int GeoPointSource::bytesSize() { + return _size; +} + void GeoPointSource::setInformation(int size, int width, int height) { Expects(_location.scale != 0); @@ -705,6 +726,10 @@ int WebUrlSource::height() { return _height; } +int WebUrlSource::bytesSize() { + return _size; +} + void WebUrlSource::setInformation(int size, int width, int height) { if (size) { _size = size; diff --git a/Telegram/SourceFiles/ui/image/image_source.h b/Telegram/SourceFiles/ui/image/image_source.h index a025c8adff..e4afc44bf4 100644 --- a/Telegram/SourceFiles/ui/image/image_source.h +++ b/Telegram/SourceFiles/ui/image/image_source.h @@ -48,6 +48,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; QByteArray bytesForCache() override; @@ -100,6 +101,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; QByteArray bytesForCache() override; @@ -184,6 +186,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; protected: @@ -211,6 +214,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; protected: @@ -236,6 +240,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; protected: @@ -298,6 +303,7 @@ public: int width() override; int height() override; + int bytesSize() override; void setInformation(int size, int width, int height) override; protected: