tdesktop/Telegram/SourceFiles/ui/image/image_source.cpp

617 lines
12 KiB
C++
Raw Normal View History

/*
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 "ui/image/image_source.h"
#include "storage/cache/storage_cache_database.h"
#include "storage/file_download_mtproto.h"
#include "storage/file_download_web.h"
#include "data/data_session.h"
#include "data/data_file_origin.h"
#include "history/history_item.h"
#include "history/history.h"
2019-07-24 11:45:24 +00:00
#include "main/main_session.h"
#include "app.h"
2019-09-04 07:19:15 +00:00
#include <QtCore/QBuffer>
namespace Images {
ImageSource::ImageSource(QImage &&data, const QByteArray &format)
: _data(std::move(data))
2018-10-30 05:44:48 +00:00
, _format(format)
, _width(_data.width())
, _height(_data.height()) {
}
void ImageSource::load(Data::FileOrigin origin) {
2018-10-23 13:04:42 +00:00
if (_data.isNull() && !_bytes.isEmpty()) {
_data = App::readImage(_bytes, &_format, false);
}
}
void ImageSource::loadEvenCancelled(Data::FileOrigin origin) {
load(origin);
}
QImage ImageSource::takeLoaded() {
load({});
return _data;
}
void ImageSource::unload() {
2018-10-23 13:04:42 +00:00
if (_bytes.isEmpty() && !_data.isNull()) {
2018-10-29 13:32:34 +00:00
if (_format != "JPG") {
2018-10-23 13:04:42 +00:00
_format = "PNG";
}
2018-10-29 13:32:34 +00:00
{
QBuffer buffer(&_bytes);
_data.save(&buffer, _format);
}
Assert(!_bytes.isEmpty());
2018-10-23 13:04:42 +00:00
}
_data = QImage();
}
bool ImageSource::loading() {
return false;
}
bool ImageSource::displayLoading() {
return false;
}
void ImageSource::cancel() {
}
float64 ImageSource::progress() {
return 1.;
}
int ImageSource::loadOffset() {
return 0;
}
const StorageImageLocation &ImageSource::location() {
return StorageImageLocation::Invalid();
}
void ImageSource::refreshFileReference(const QByteArray &data) {
}
Storage::Cache::Key ImageSource::cacheKey() {
return Storage::Cache::Key();
}
void ImageSource::setDelayedStorageLocation(
const StorageImageLocation &location) {
}
void ImageSource::performDelayedLoad(Data::FileOrigin origin) {
}
void ImageSource::setImageBytes(const QByteArray &bytes) {
}
int ImageSource::width() {
2018-10-30 05:44:48 +00:00
return _width;
}
int ImageSource::height() {
2018-10-30 05:44:48 +00:00
return _height;
}
int ImageSource::bytesSize() {
return _bytes.size();
}
void ImageSource::setInformation(int size, int width, int height) {
2018-10-30 05:44:48 +00:00
if (width && height) {
_width = width;
_height = height;
}
}
QByteArray ImageSource::bytesForCache() {
auto result = QByteArray();
{
QBuffer buffer(&result);
if (!_data.save(&buffer, _format)) {
if (_data.save(&buffer, "PNG")) {
_format = "PNG";
}
}
}
return result;
}
LocalFileSource::LocalFileSource(
const QString &path,
const QByteArray &content,
const QByteArray &format,
QImage &&data)
: _path(path)
, _bytes(content)
, _format(format)
2018-10-30 06:17:34 +00:00
, _data(std::move(data))
, _width(_data.width())
, _height(_data.height()) {
}
void LocalFileSource::load(Data::FileOrigin origin) {
if (!_data.isNull()) {
return;
}
if (_bytes.isEmpty()) {
QFile f(_path);
if (f.size() <= App::kImageSizeLimit && f.open(QIODevice::ReadOnly)) {
_bytes = f.readAll();
}
if (_bytes.isEmpty()) {
_bytes = "(bad)";
}
}
if (_bytes != "(bad)") {
_data = App::readImage(_bytes, &_format, false, nullptr);
}
_width = std::max(_data.width(), 1);
_height = std::max(_data.height(), 1);
}
void LocalFileSource::loadEvenCancelled(Data::FileOrigin origin) {
load(origin);
}
QImage LocalFileSource::takeLoaded() {
return std::move(_data);
}
void LocalFileSource::unload() {
_data = QImage();
}
bool LocalFileSource::loading() {
return false;
}
bool LocalFileSource::displayLoading() {
return false;
}
void LocalFileSource::cancel() {
}
float64 LocalFileSource::progress() {
return 1.;
}
int LocalFileSource::loadOffset() {
return 0;
}
const StorageImageLocation &LocalFileSource::location() {
return StorageImageLocation::Invalid();
}
void LocalFileSource::refreshFileReference(const QByteArray &data) {
}
Storage::Cache::Key LocalFileSource::cacheKey() {
return Storage::Cache::Key();
}
void LocalFileSource::setDelayedStorageLocation(
const StorageImageLocation &location) {
}
void LocalFileSource::performDelayedLoad(Data::FileOrigin origin) {
}
void LocalFileSource::setImageBytes(const QByteArray &bytes) {
_bytes = bytes;
load({});
}
int LocalFileSource::width() {
ensureDimensionsKnown();
return _width;
}
int LocalFileSource::height() {
ensureDimensionsKnown();
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) {
_width = width;
_height = height;
}
}
void LocalFileSource::ensureDimensionsKnown() {
if (!_width || !_height) {
load({});
}
}
QByteArray LocalFileSource::bytesForCache() {
ensureDimensionsKnown();
return (_bytes == "(bad)") ? QByteArray() : _bytes;
}
QImage RemoteSource::takeLoaded() {
2019-04-30 14:32:49 +00:00
if (!_loader || !_loader->finished()) {
return QImage();
}
2019-07-01 14:35:20 +00:00
if (_loader->cancelled()) {
2019-04-30 14:32:49 +00:00
_cancelled = true;
destroyLoader();
return QImage();
}
2019-07-01 14:35:20 +00:00
auto data = _loader->imageData(shrinkBox());
if (data.isNull()) {
// Bad content in the image.
data = Image::Empty()->original();
}
setInformation(_loader->bytes().size(), data.width(), data.height());
2018-11-23 14:39:14 +00:00
destroyLoader();
return data;
}
2019-04-30 14:32:49 +00:00
void RemoteSource::destroyLoader() {
if (!_loader) {
return;
}
2019-04-30 14:32:49 +00:00
const auto loader = base::take(_loader);
2018-11-30 13:27:33 +00:00
if (cancelled()) {
2018-11-23 14:39:14 +00:00
loader->cancel();
}
}
void RemoteSource::loadLocal() {
2019-04-30 14:32:49 +00:00
if (_loader) {
2019-07-01 14:35:20 +00:00
return;
}
_loader = createLoader(Data::FileOrigin(), LoadFromLocalOnly, true);
2019-04-30 14:32:49 +00:00
if (_loader) {
_loader->start();
}
}
void RemoteSource::setImageBytes(const QByteArray &bytes) {
if (bytes.isEmpty()) {
return;
2019-04-30 14:32:49 +00:00
} else if (_loader) {
2018-12-05 11:50:41 +00:00
unload();
}
_loader = createLoader({}, LoadFromLocalOnly, true);
_loader->finishWithBytes(bytes);
const auto location = this->location();
if (location.valid()
&& !bytes.isEmpty()
&& bytes.size() <= Storage::kMaxFileInMemory) {
Auth().data().cache().putIfEmpty(
location.file().cacheKey(),
Storage::Cache::Database::TaggedValue(
base::duplicate(bytes),
Data::kImageCacheTag));
}
}
bool RemoteSource::loading() {
2019-04-30 14:32:49 +00:00
return (_loader != nullptr);
}
void RemoteSource::load(Data::FileOrigin origin) {
if (!_loader) {
_loader = createLoader(origin, LoadFromCloudOrLocal, false);
}
2019-04-30 14:32:49 +00:00
if (_loader) {
_loader->start();
}
}
2018-11-30 13:27:33 +00:00
bool RemoteSource::cancelled() const {
2019-04-30 14:32:49 +00:00
return _cancelled;
2018-11-30 13:27:33 +00:00
}
void RemoteSource::loadEvenCancelled(Data::FileOrigin origin) {
2019-04-30 14:32:49 +00:00
_cancelled = false;
return load(origin);
}
bool RemoteSource::displayLoading() {
2019-04-30 14:32:49 +00:00
return _loader && (!_loader->loadingLocal() || !_loader->autoLoading());
}
void RemoteSource::cancel() {
2019-04-30 14:32:49 +00:00
if (!_loader) {
return;
}
_cancelled = true;
destroyLoader();
}
void RemoteSource::unload() {
2019-04-30 14:32:49 +00:00
base::take(_loader);
}
float64 RemoteSource::progress() {
2019-04-30 14:32:49 +00:00
return _loader ? _loader->currentProgress() : 0.;
}
int RemoteSource::loadOffset() {
2019-04-30 14:32:49 +00:00
return _loader ? _loader->currentOffset() : 0;
}
RemoteSource::~RemoteSource() {
unload();
}
const StorageImageLocation &RemoteSource::location() {
return StorageImageLocation::Invalid();
}
void RemoteSource::refreshFileReference(const QByteArray &data) {
}
void RemoteSource::setDelayedStorageLocation(
const StorageImageLocation &location) {
}
void RemoteSource::performDelayedLoad(Data::FileOrigin origin) {
}
QByteArray RemoteSource::bytesForCache() {
return QByteArray();
}
StorageSource::StorageSource(const StorageImageLocation &location, int size)
: _location(location)
, _size(size) {
}
void StorageSource::refreshFileReference(const QByteArray &data) {
_location.refreshFileReference(data);
}
const StorageImageLocation &StorageSource::location() {
return _location;
}
Storage::Cache::Key StorageSource::cacheKey() {
return _location.valid()
? _location.file().cacheKey()
: Storage::Cache::Key();
}
int StorageSource::width() {
return _location.width();
}
int StorageSource::height() {
return _location.height();
}
int StorageSource::bytesSize() {
return _size;
}
void StorageSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;
}
if (width && height) {
_location.setSize(width, height);
}
}
QSize StorageSource::shrinkBox() const {
return QSize();
}
2019-04-30 14:32:49 +00:00
std::unique_ptr<FileLoader> StorageSource::createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
return _location.valid()
2019-04-30 14:32:49 +00:00
? std::make_unique<mtpFileLoader>(
_location.file(),
origin,
UnknownFileLocation,
QString(),
_size,
LoadToCacheAsWell,
fromCloud,
autoLoading,
Data::kImageCacheTag)
: nullptr;
}
WebCachedSource::WebCachedSource(
const WebFileLocation &location,
QSize box,
int size)
: _location(location)
, _box(box)
, _size(size) {
}
WebCachedSource::WebCachedSource(
const WebFileLocation &location,
int width,
int height,
int size)
: _location(location)
, _width(width)
, _height(height)
, _size(size) {
}
Storage::Cache::Key WebCachedSource::cacheKey() {
return _location.isNull()
? Storage::Cache::Key()
: Data::WebDocumentCacheKey(_location);
}
int WebCachedSource::width() {
return _width;
}
int WebCachedSource::height() {
return _height;
}
int WebCachedSource::bytesSize() {
return _size;
}
void WebCachedSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;
}
if (width && height) {
_width = width;
_height = height;
}
}
QSize WebCachedSource::shrinkBox() const {
return _box;
}
2019-04-30 14:32:49 +00:00
std::unique_ptr<FileLoader> WebCachedSource::createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
2019-04-30 14:32:49 +00:00
return !_location.isNull()
? std::make_unique<mtpFileLoader>(
_location,
_size,
fromCloud,
autoLoading,
2019-04-30 14:32:49 +00:00
Data::kImageCacheTag)
: nullptr;
}
GeoPointSource::GeoPointSource(const GeoPointLocation &location)
: _location(location) {
}
Storage::Cache::Key GeoPointSource::cacheKey() {
return Data::GeoPointCacheKey(_location);
}
int GeoPointSource::width() {
return _location.width * _location.scale;
}
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);
if (size) {
_size = size;
}
if (width && height) {
_location.width = width / _location.scale;
_location.height = height / _location.scale;
}
}
QSize GeoPointSource::shrinkBox() const {
return QSize();
}
2019-04-30 14:32:49 +00:00
std::unique_ptr<FileLoader> GeoPointSource::createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
2019-04-30 14:32:49 +00:00
return std::make_unique<mtpFileLoader>(
_location,
_size,
fromCloud,
autoLoading,
Data::kImageCacheTag);
}
WebUrlSource::WebUrlSource(const QString &url, QSize box)
: _url(url)
, _box(box) {
}
WebUrlSource::WebUrlSource(const QString &url, int width, int height)
: _url(url)
, _width(width)
, _height(height) {
}
Storage::Cache::Key WebUrlSource::cacheKey() {
return Data::UrlCacheKey(_url);
}
int WebUrlSource::width() {
return _width;
}
int WebUrlSource::height() {
return _height;
}
int WebUrlSource::bytesSize() {
return _size;
}
void WebUrlSource::setInformation(int size, int width, int height) {
if (size) {
_size = size;
}
if (width && height) {
_width = width;
_height = height;
}
}
QSize WebUrlSource::shrinkBox() const {
return _box;
}
2019-04-30 14:32:49 +00:00
std::unique_ptr<FileLoader> WebUrlSource::createLoader(
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
2019-04-30 14:32:49 +00:00
return std::make_unique<webFileLoader>(
_url,
QString(),
fromCloud,
autoLoading,
Data::kImageCacheTag);
}
} // namespace Images