2017-09-26 11:49:16 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-26 11:49:16 +00:00
|
|
|
*/
|
|
|
|
#include "data/data_photo.h"
|
|
|
|
|
2018-01-04 10:22:53 +00:00
|
|
|
#include "data/data_session.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_file_origin.h"
|
2020-04-08 15:09:29 +00:00
|
|
|
#include "data/data_reply_preview.h"
|
2020-05-25 14:16:04 +00:00
|
|
|
#include "data/data_photo_media.h"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-05-04 13:38:49 +00:00
|
|
|
#include "main/main_session.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "mainwidget.h"
|
2020-05-25 14:16:04 +00:00
|
|
|
#include "storage/file_download.h"
|
2019-01-21 13:42:21 +00:00
|
|
|
#include "core/application.h"
|
2019-09-13 06:06:02 +00:00
|
|
|
#include "facades.h"
|
|
|
|
#include "app.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
namespace {
|
|
|
|
|
2020-05-26 10:13:32 +00:00
|
|
|
constexpr auto kPhotoSideLimit = 1280;
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
using Data::PhotoMedia;
|
|
|
|
using Data::PhotoSize;
|
|
|
|
using Data::PhotoSizeIndex;
|
|
|
|
using Data::kPhotoSizeCount;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
Data::Session &PhotoData::owner() const {
|
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
2019-07-24 11:45:24 +00:00
|
|
|
Main::Session &PhotoData::session() const {
|
2019-01-25 14:37:28 +00:00
|
|
|
return _owner->session();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
void PhotoData::automaticLoadSettingsChanged() {
|
|
|
|
const auto index = PhotoSizeIndex(PhotoSize::Large);
|
2020-05-26 11:59:45 +00:00
|
|
|
if (!(_images[index].flags & Data::CloudFile::Flag::Cancelled)) {
|
2020-05-25 14:16:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_images[index].loader = nullptr;
|
2020-05-26 11:59:45 +00:00
|
|
|
_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);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
bool PhotoData::loading() const {
|
|
|
|
return loading(PhotoSize::Large);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-26 10:13:32 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-26 10:13:32 +00:00
|
|
|
return (_images[validSizeIndex(size)].loader != nullptr);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
bool PhotoData::failed(PhotoSize size) const {
|
2020-05-26 11:59:45 +00:00
|
|
|
const auto flags = _images[validSizeIndex(size)].flags;
|
|
|
|
return (flags & Data::CloudFile::Flag::Failed);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
const ImageLocation &PhotoData::location(PhotoSize size) const {
|
2020-05-26 10:13:32 +00:00
|
|
|
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-05-26 10:13:32 +00:00
|
|
|
return _images[validSizeIndex(size)].byteSize;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::displayLoading() const {
|
2020-05-25 14:16:04 +00:00
|
|
|
const auto index = PhotoSizeIndex(PhotoSize::Large);
|
|
|
|
return _images[index].loader
|
|
|
|
? (!_images[index].loader->loadingLocal()
|
|
|
|
|| !_images[index].loader->autoLoading())
|
2017-12-25 14:17:00 +00:00
|
|
|
: (uploading() && !waitingForAlbum());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
2017-09-26 11:49:16 +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 snap(result, 0., 1.);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-05-25 14:16:04 +00:00
|
|
|
return 0.;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
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);
|
2020-05-26 11:59:45 +00:00
|
|
|
return (_images[index].flags & Data::CloudFile::Flag::Cancelled);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2017-12-25 14:17:00 +00:00
|
|
|
void PhotoData::setWaitingForAlbum() {
|
|
|
|
if (uploading()) {
|
|
|
|
uploadingData->waitingForAlbum = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::waitingForAlbum() const {
|
|
|
|
return uploading() && uploadingData->waitingForAlbum;
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
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;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::uploading() const {
|
2017-12-25 14:17:00 +00:00
|
|
|
return (uploadingData != nullptr);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 15:03:38 +00:00
|
|
|
Image *PhotoData::getReplyPreview(Data::FileOrigin origin) {
|
2020-04-08 15:09:29 +00:00
|
|
|
if (!_replyPreview) {
|
|
|
|
_replyPreview = std::make_unique<Data::ReplyPreview>(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2020-04-08 15:09:29 +00:00
|
|
|
return _replyPreview->image(origin);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-22 14:19:43 +00:00
|
|
|
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),
|
2019-03-22 14:19:43 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
void PhotoData::collectLocalData(not_null<PhotoData*> local) {
|
|
|
|
if (local == this) {
|
|
|
|
return;
|
|
|
|
}
|
2018-08-27 11:35:58 +00:00
|
|
|
|
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()) {
|
2020-05-20 12:28:18 +00:00
|
|
|
_owner->cache().copyIfEmpty(from, to);
|
2018-08-27 11:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
2020-05-25 14:16:04 +00:00
|
|
|
}
|
|
|
|
if (const auto localMedia = local->activeMediaView()) {
|
2020-05-26 14:32:38 +00:00
|
|
|
auto media = createMediaView();
|
2020-05-25 14:16:04 +00:00
|
|
|
media->collectLocalData(localMedia.get());
|
2020-05-26 14:32:38 +00:00
|
|
|
_owner->keepAlive(std::move(media));
|
2020-05-25 14:16:04 +00:00
|
|
|
}
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::isNull() const {
|
2020-05-25 14:16:04 +00:00
|
|
|
return !_images[PhotoSizeIndex(PhotoSize::Large)].location.valid();
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
void PhotoData::load(
|
|
|
|
PhotoSize size,
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
LoadFromCloudSetting fromCloud,
|
|
|
|
bool autoLoading) {
|
2020-05-26 10:13:32 +00:00
|
|
|
const auto index = validSizeIndex(size);
|
2020-05-27 09:01:25 +00:00
|
|
|
|
|
|
|
// Could've changed, if the requested size didn't have a location.
|
|
|
|
const auto loadingSize = static_cast<PhotoSize>(index);
|
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) {
|
2020-05-27 09:01:25 +00:00
|
|
|
if (const auto active = activeMediaView()) {
|
|
|
|
active->set(loadingSize, std::move(result));
|
2020-05-25 14:16:04 +00:00
|
|
|
}
|
2020-05-27 09:01:25 +00:00
|
|
|
if (loadingSize == PhotoSize::Large) {
|
|
|
|
_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-05-27 09:01:25 +00:00
|
|
|
if (loadingSize == 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-05-27 09:01:25 +00:00
|
|
|
if (loadingSize == PhotoSize::Large) {
|
|
|
|
_owner->photoLoadProgress(this);
|
2020-05-25 14:16:04 +00:00
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
Data::LoadCloudFile(
|
|
|
|
&session(),
|
|
|
|
_images[index],
|
|
|
|
origin,
|
|
|
|
fromCloud,
|
|
|
|
autoLoading,
|
|
|
|
Data::kImageCacheTag,
|
|
|
|
finalCheck,
|
|
|
|
done,
|
|
|
|
fail,
|
|
|
|
progress);
|
2019-01-25 14:37:28 +00:00
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
if (size == PhotoSize::Large) {
|
|
|
|
_owner->notifyPhotoLayoutChanged(this);
|
|
|
|
}
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
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;
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 14:16:04 +00:00
|
|
|
std::shared_ptr<PhotoMedia> PhotoData::activeMediaView() const {
|
|
|
|
return _media.lock();
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::updateImages(
|
2020-05-25 14:16:04 +00:00
|
|
|
const QByteArray &inlineThumbnailBytes,
|
|
|
|
const ImageWithLocation &small,
|
|
|
|
const ImageWithLocation &thumbnail,
|
|
|
|
const ImageWithLocation &large) {
|
|
|
|
if (!inlineThumbnailBytes.isEmpty()
|
|
|
|
&& _inlineThumbnailBytes.isEmpty()) {
|
|
|
|
_inlineThumbnailBytes = inlineThumbnailBytes;
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
2020-05-25 14:16:04 +00:00
|
|
|
const auto update = [&](PhotoSize size, const ImageWithLocation &data) {
|
2020-05-26 11:59:45 +00:00
|
|
|
Data::UpdateCloudFile(
|
|
|
|
_images[PhotoSizeIndex(size)],
|
|
|
|
data,
|
|
|
|
owner().cache(),
|
2020-05-27 09:01:25 +00:00
|
|
|
Data::kImageCacheTag,
|
2020-05-26 11:59:45 +00:00
|
|
|
[=](Data::FileOrigin origin) { load(size, origin); },
|
|
|
|
[=](QImage preloaded) {
|
|
|
|
if (const auto media = activeMediaView()) {
|
|
|
|
media->set(size, data.preloaded);
|
|
|
|
}
|
|
|
|
});
|
2019-01-25 14:37:28 +00:00
|
|
|
};
|
2020-05-25 14:16:04 +00:00
|
|
|
update(PhotoSize::Small, small);
|
|
|
|
update(PhotoSize::Thumbnail, thumbnail);
|
|
|
|
update(PhotoSize::Large, large);
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int PhotoData::width() const {
|
2020-05-25 14:16:04 +00:00
|
|
|
return _images[PhotoSizeIndex(PhotoSize::Large)].location.width();
|
2019-01-25 14:37:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int PhotoData::height() const {
|
2020-05-25 14:16:04 +00:00
|
|
|
return _images[PhotoSizeIndex(PhotoSize::Large)].location.height();
|
2018-08-27 11:35:58 +00:00
|
|
|
}
|
|
|
|
|
2020-05-04 13:38:49 +00:00
|
|
|
PhotoClickHandler::PhotoClickHandler(
|
|
|
|
not_null<PhotoData*> photo,
|
|
|
|
FullMsgId context,
|
|
|
|
PeerData *peer)
|
2020-06-09 09:36:40 +00:00
|
|
|
: FileClickHandler(&photo->session(), context)
|
2020-05-04 13:38:49 +00:00
|
|
|
, _photo(photo)
|
|
|
|
, _peer(peer) {
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void PhotoOpenClickHandler::onClickImpl() const {
|
2020-06-09 09:36:40 +00:00
|
|
|
Core::App().showPhoto(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoSaveClickHandler::onClickImpl() const {
|
2020-05-04 13:38:49 +00:00
|
|
|
const auto data = photo();
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
} else {
|
2020-05-25 14:16:04 +00:00
|
|
|
data->load(context());
|
2020-05-04 13:38:49 +00:00
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCancelClickHandler::onClickImpl() const {
|
2020-05-04 13:38:49 +00:00
|
|
|
const auto data = photo();
|
|
|
|
if (!data->date) {
|
|
|
|
return;
|
|
|
|
} else if (data->uploading()) {
|
2019-04-25 12:45:15 +00:00
|
|
|
if (const auto item = data->owner().message(context())) {
|
2020-06-12 14:09:04 +00:00
|
|
|
if (const auto m = App::main()) { // multi good
|
|
|
|
if (&m->session() == &data->session()) {
|
|
|
|
m->cancelUploadLayer(item);
|
|
|
|
}
|
|
|
|
}
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|