tdesktop/Telegram/SourceFiles/data/data_photo.cpp

496 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 "data/data_photo.h"
#include "data/data_session.h"
#include "data/data_file_origin.h"
#include "data/data_reply_preview.h"
2020-05-25 14:16:04 +00:00
#include "data/data_photo_media.h"
#include "ui/image/image.h"
#include "main/main_session.h"
#include "history/history.h"
#include "history/history_item.h"
2020-07-03 10:48:55 +00:00
#include "media/streaming/media_streaming_loader_local.h"
#include "media/streaming/media_streaming_loader_mtproto.h"
#include "mainwidget.h"
2020-05-25 14:16:04 +00:00
#include "storage/file_download.h"
#include "core/application.h"
#include "facades.h"
2020-05-25 14:16:04 +00:00
namespace {
constexpr auto kPhotoSideLimit = 1280;
2020-05-25 14:16:04 +00:00
using Data::PhotoMedia;
using Data::PhotoSize;
using Data::PhotoSizeIndex;
using Data::kPhotoSizeCount;
[[nodiscard]] QImage ValidatePhotoImage(
QImage image,
const Data::CloudFile &file) {
return (v::is<WebFileLocation>(file.location.file().data)
&& image.format() == QImage::Format_ARGB32)
? Images::Opaque(std::move(image))
: image;
}
2020-05-25 14:16:04 +00:00
} // namespace
PhotoData::PhotoData(not_null<Data::Session*> owner, PhotoId id)
: id(id)
, _owner(owner) {
2018-07-13 16:49:46 +00:00
}
2020-05-25 14:16:04 +00:00
PhotoData::~PhotoData() {
for (auto &image : _images) {
base::take(image.loader).reset();
}
base::take(_video.loader).reset();
2020-05-25 14:16:04 +00:00
}
Data::Session &PhotoData::owner() const {
return *_owner;
}
2019-07-24 11:45:24 +00:00
Main::Session &PhotoData::session() const {
return _owner->session();
}
2020-05-25 14:16:04 +00:00
void PhotoData::automaticLoadSettingsChanged() {
const auto index = PhotoSizeIndex(PhotoSize::Large);
if (!(_images[index].flags & Data::CloudFile::Flag::Cancelled)) {
2020-05-25 14:16:04 +00:00
return;
}
_images[index].loader = nullptr;
_images[index].flags &= ~Data::CloudFile::Flag::Cancelled;
2020-05-25 14:16:04 +00:00
}
void PhotoData::load(
2018-07-13 21:25:47 +00:00
Data::FileOrigin origin,
2020-05-25 14:16:04 +00:00
LoadFromCloudSetting fromCloud,
bool autoLoading) {
load(PhotoSize::Large, origin, fromCloud, autoLoading);
}
2020-05-25 14:16:04 +00:00
bool PhotoData::loading() const {
return loading(PhotoSize::Large);
}
int PhotoData::validSizeIndex(PhotoSize size) const {
const auto index = PhotoSizeIndex(size);
for (auto i = index; i != kPhotoSizeCount; ++i) {
if (_images[i].location.valid()) {
return i;
}
}
return PhotoSizeIndex(PhotoSize::Large);
}
2020-08-25 18:03:41 +00:00
int PhotoData::existingSizeIndex(PhotoSize size) const {
const auto index = PhotoSizeIndex(size);
for (auto i = index; i != kPhotoSizeCount; ++i) {
if (_images[i].location.valid() || _images[i].progressivePartSize) {
return i;
}
}
return PhotoSizeIndex(PhotoSize::Large);
}
bool PhotoData::hasExact(PhotoSize size) const {
return _images[PhotoSizeIndex(size)].location.valid();
}
2020-05-25 14:16:04 +00:00
bool PhotoData::loading(PhotoSize size) const {
2020-08-25 18:03:41 +00:00
const auto valid = validSizeIndex(size);
const auto existing = existingSizeIndex(size);
if (!_images[valid].loader) {
return false;
} else if (valid == existing) {
return true;
}
return (_images[valid].loader->loadSize()
2020-08-25 18:03:41 +00:00
>= _images[existing].progressivePartSize);
}
2020-05-25 14:16:04 +00:00
bool PhotoData::failed(PhotoSize size) const {
const auto flags = _images[validSizeIndex(size)].flags;
return (flags & Data::CloudFile::Flag::Failed);
}
void PhotoData::clearFailed(PhotoSize size) {
_images[validSizeIndex(size)].flags &= ~Data::CloudFile::Flag::Failed;
}
2020-05-25 14:16:04 +00:00
const ImageLocation &PhotoData::location(PhotoSize size) const {
return _images[validSizeIndex(size)].location;
}
int PhotoData::SideLimit() {
return kPhotoSideLimit;
}
std::optional<QSize> PhotoData::size(PhotoSize size) const {
const auto &provided = location(size);
const auto result = QSize{ provided.width(), provided.height() };
const auto limit = SideLimit();
if (result.isEmpty()) {
return std::nullopt;
} else if (result.width() <= limit && result.height() <= limit) {
return result;
}
const auto scaled = result.scaled(limit, limit, Qt::KeepAspectRatio);
return QSize(std::max(scaled.width(), 1), std::max(scaled.height(), 1));
2020-05-25 14:16:04 +00:00
}
int PhotoData::imageByteSize(PhotoSize size) const {
2020-08-25 18:03:41 +00:00
const auto existing = existingSizeIndex(size);
if (const auto result = _images[existing].progressivePartSize) {
return result;
}
return _images[validSizeIndex(size)].byteSize;
}
bool PhotoData::displayLoading() const {
2020-05-25 14:16:04 +00:00
const auto index = PhotoSizeIndex(PhotoSize::Large);
if (const auto loader = _images[index].loader.get()) {
return !loader->finished()
&& (!loader->loadingLocal() || !loader->autoLoading());
}
return (uploading() && !waitingForAlbum());
}
void PhotoData::cancel() {
2020-05-27 09:01:25 +00:00
if (loading()) {
_images[PhotoSizeIndex(PhotoSize::Large)].loader->cancel();
2020-05-25 14:16:04 +00:00
}
}
float64 PhotoData::progress() const {
if (uploading()) {
if (uploadingData->size > 0) {
2020-05-25 14:16:04 +00:00
const auto result = float64(uploadingData->offset)
/ uploadingData->size;
return std::clamp(result, 0., 1.);
}
2020-05-25 14:16:04 +00:00
return 0.;
}
2020-05-25 14:16:04 +00:00
const auto index = PhotoSizeIndex(PhotoSize::Large);
return loading() ? _images[index].loader->currentProgress() : 0.;
}
bool PhotoData::cancelled() const {
const auto index = PhotoSizeIndex(PhotoSize::Large);
return (_images[index].flags & Data::CloudFile::Flag::Cancelled);
}
void PhotoData::setWaitingForAlbum() {
if (uploading()) {
uploadingData->waitingForAlbum = true;
}
}
bool PhotoData::waitingForAlbum() const {
return uploading() && uploadingData->waitingForAlbum;
}
int32 PhotoData::loadOffset() const {
2020-05-25 14:16:04 +00:00
const auto index = PhotoSizeIndex(PhotoSize::Large);
return loading() ? _images[index].loader->currentOffset() : 0;
}
bool PhotoData::uploading() const {
return (uploadingData != nullptr);
}
Image *PhotoData::getReplyPreview(
Data::FileOrigin origin,
not_null<PeerData*> context) {
if (!_replyPreview) {
_replyPreview = std::make_unique<Data::ReplyPreview>(this);
}
return _replyPreview->image(origin, context);
}
Image *PhotoData::getReplyPreview(not_null<HistoryItem*> item) {
return getReplyPreview(item->fullId(), item->history()->peer);
}
bool PhotoData::replyPreviewLoaded() const {
if (!_replyPreview) {
return false;
}
return _replyPreview->loaded();
}
void PhotoData::setRemoteLocation(
int32 dc,
uint64 access,
const QByteArray &fileReference) {
_fileReference = fileReference;
if (_dc != dc || _access != access) {
_dc = dc;
_access = access;
}
}
2018-07-13 16:49:46 +00:00
MTPInputPhoto PhotoData::mtpInput() const {
return MTP_inputPhoto(
MTP_long(id),
MTP_long(_access),
MTP_bytes(_fileReference));
}
QByteArray PhotoData::fileReference() const {
return _fileReference;
}
void PhotoData::refreshFileReference(const QByteArray &value) {
_fileReference = value;
2020-05-25 14:16:04 +00:00
for (auto &image : _images) {
image.location.refreshFileReference(value);
}
2018-07-13 16:49:46 +00:00
}
void PhotoData::collectLocalData(not_null<PhotoData*> local) {
if (local == this) {
return;
}
2020-05-25 14:16:04 +00:00
for (auto i = 0; i != kPhotoSizeCount; ++i) {
if (const auto from = local->_images[i].location.file().cacheKey()) {
if (const auto to = _images[i].location.file().cacheKey()) {
_owner->cache().copyIfEmpty(from, to);
}
}
2020-05-25 14:16:04 +00:00
}
if (const auto localMedia = local->activeMediaView()) {
auto media = createMediaView();
2020-05-25 14:16:04 +00:00
media->collectLocalData(localMedia.get());
_owner->keepAlive(std::move(media));
2020-05-25 14:16:04 +00:00
}
}
bool PhotoData::isNull() const {
2020-05-25 14:16:04 +00:00
return !_images[PhotoSizeIndex(PhotoSize::Large)].location.valid();
}
2020-05-25 14:16:04 +00:00
void PhotoData::load(
PhotoSize size,
Data::FileOrigin origin,
LoadFromCloudSetting fromCloud,
bool autoLoading) {
2020-08-25 18:03:41 +00:00
const auto valid = validSizeIndex(size);
const auto existing = existingSizeIndex(size);
2020-05-27 09:01:25 +00:00
// Could've changed, if the requested size didn't have a location.
2020-08-25 18:03:41 +00:00
const auto validSize = static_cast<PhotoSize>(valid);
2020-06-08 15:17:33 +00:00
const auto finalCheck = [=] {
2020-05-27 09:01:25 +00:00
if (const auto active = activeMediaView()) {
return !active->image(size);
2020-05-25 14:16:04 +00:00
}
2020-05-27 09:01:25 +00:00
return true;
2020-06-08 15:17:33 +00:00
};
const auto done = [=](QImage result) {
Expects(_images[valid].loader != nullptr);
// Find out what progressive photo size have we loaded exactly.
auto goodFor = validSize;
const auto loadSize = _images[valid].loader->loadSize();
if (valid > 0 && _images[valid].byteSize > loadSize) {
for (auto i = valid; i != 0;) {
--i;
const auto required = _images[i].progressivePartSize;
if (required > 0 && required <= loadSize) {
goodFor = static_cast<PhotoSize>(i);
break;
}
}
}
2020-05-27 09:01:25 +00:00
if (const auto active = activeMediaView()) {
active->set(
validSize,
goodFor,
ValidatePhotoImage(std::move(result), _images[valid]));
2020-05-25 14:16:04 +00:00
}
if (validSize == PhotoSize::Large && goodFor == validSize) {
2020-05-27 09:01:25 +00:00
_owner->photoLoadDone(this);
2020-05-25 14:16:04 +00:00
}
2020-06-08 15:17:33 +00:00
};
const auto fail = [=](bool started) {
2020-08-25 18:03:41 +00:00
if (validSize == PhotoSize::Large) {
2020-05-25 14:16:04 +00:00
_owner->photoLoadFail(this, started);
}
2020-06-08 15:17:33 +00:00
};
const auto progress = [=] {
2020-08-25 18:03:41 +00:00
if (validSize == PhotoSize::Large) {
2020-05-27 09:01:25 +00:00
_owner->photoLoadProgress(this);
2020-05-25 14:16:04 +00:00
}
2020-06-08 15:17:33 +00:00
};
Data::LoadCloudFile(
&session(),
2020-08-25 18:03:41 +00:00
_images[valid],
2020-06-08 15:17:33 +00:00
origin,
fromCloud,
autoLoading,
Data::kImageCacheTag,
finalCheck,
done,
fail,
2020-08-25 18:03:41 +00:00
progress,
_images[existing].progressivePartSize);
2020-05-25 14:16:04 +00:00
if (size == PhotoSize::Large) {
_owner->notifyPhotoLayoutChanged(this);
}
}
2020-05-25 14:16:04 +00:00
std::shared_ptr<PhotoMedia> PhotoData::createMediaView() {
if (auto result = activeMediaView()) {
return result;
}
auto result = std::make_shared<PhotoMedia>(this);
_media = result;
return result;
}
2020-05-25 14:16:04 +00:00
std::shared_ptr<PhotoMedia> PhotoData::activeMediaView() const {
return _media.lock();
}
void PhotoData::updateImages(
2020-05-25 14:16:04 +00:00
const QByteArray &inlineThumbnailBytes,
const ImageWithLocation &small,
const ImageWithLocation &thumbnail,
const ImageWithLocation &large,
2020-07-03 10:48:55 +00:00
const ImageWithLocation &video,
crl::time videoStartTime) {
2020-05-25 14:16:04 +00:00
if (!inlineThumbnailBytes.isEmpty()
&& _inlineThumbnailBytes.isEmpty()) {
_inlineThumbnailBytes = inlineThumbnailBytes;
}
2020-05-25 14:16:04 +00:00
const auto update = [&](PhotoSize size, const ImageWithLocation &data) {
const auto index = PhotoSizeIndex(size);
Data::UpdateCloudFile(
_images[index],
data,
owner().cache(),
2020-05-27 09:01:25 +00:00
Data::kImageCacheTag,
[=](Data::FileOrigin origin) { load(size, origin); },
[=](QImage preloaded) {
if (const auto media = activeMediaView()) {
media->set(
size,
size,
ValidatePhotoImage(
std::move(preloaded),
_images[index]));
}
});
};
2020-05-25 14:16:04 +00:00
update(PhotoSize::Small, small);
update(PhotoSize::Thumbnail, thumbnail);
update(PhotoSize::Large, large);
2020-07-03 10:48:55 +00:00
if (video.location.valid()) {
_videoStartTime = videoStartTime;
}
Data::UpdateCloudFile(
_video,
video,
owner().cache(),
Data::kAnimationCacheTag,
[&](Data::FileOrigin origin) { loadVideo(origin); });
}
[[nodiscard]] bool PhotoData::hasAttachedStickers() const {
return _hasStickers;
}
void PhotoData::setHasAttachedStickers(bool value) {
_hasStickers = value;
}
int PhotoData::width() const {
2020-05-25 14:16:04 +00:00
return _images[PhotoSizeIndex(PhotoSize::Large)].location.width();
}
int PhotoData::height() const {
2020-05-25 14:16:04 +00:00
return _images[PhotoSizeIndex(PhotoSize::Large)].location.height();
}
bool PhotoData::hasVideo() const {
return _video.location.valid();
}
bool PhotoData::videoLoading() const {
return _video.loader != nullptr;
}
bool PhotoData::videoFailed() const {
return (_video.flags & Data::CloudFile::Flag::Failed);
}
void PhotoData::loadVideo(Data::FileOrigin origin) {
const auto autoLoading = false;
const auto finalCheck = [=] {
if (const auto active = activeMediaView()) {
return active->videoContent().isEmpty();
}
return true;
};
const auto done = [=](QByteArray result) {
if (const auto active = activeMediaView()) {
active->setVideo(std::move(result));
}
};
Data::LoadCloudFile(
&session(),
_video,
origin,
LoadFromCloudOrLocal,
autoLoading,
Data::kAnimationCacheTag,
finalCheck,
done);
}
const ImageLocation &PhotoData::videoLocation() const {
return _video.location;
}
int PhotoData::videoByteSize() const {
return _video.byteSize;
}
2020-07-03 10:48:55 +00:00
bool PhotoData::videoCanBePlayed() const {
return hasVideo() && !videoPlaybackFailed();
}
auto PhotoData::createStreamingLoader(
Data::FileOrigin origin,
bool forceRemoteLoader) const
-> std::unique_ptr<Media::Streaming::Loader> {
if (!hasVideo()) {
return nullptr;
}
if (!forceRemoteLoader) {
const auto media = activeMediaView();
if (media && !media->videoContent().isEmpty()) {
return Media::Streaming::MakeBytesLoader(media->videoContent());
}
}
return v::is<StorageFileLocation>(videoLocation().file().data)
2020-07-03 10:48:55 +00:00
? std::make_unique<Media::Streaming::LoaderMtproto>(
&session().downloader(),
2020-09-01 19:56:25 +00:00
v::get<StorageFileLocation>(videoLocation().file().data),
2020-07-03 10:48:55 +00:00
videoByteSize(),
origin)
: nullptr;
}