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"
|
2018-10-23 09:44:42 +00:00
|
|
|
#include "ui/image/image.h"
|
2018-11-09 15:03:38 +00:00
|
|
|
#include "ui/image/image_source.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "mainwidget.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
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void PhotoData::automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
const HistoryItem *item) {
|
2019-01-25 14:37:28 +00:00
|
|
|
_large->automaticLoad(origin, item);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::automaticLoadSettingsChanged() {
|
2019-01-25 14:37:28 +00:00
|
|
|
_large->automaticLoadSettingsChanged();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void PhotoData::download(Data::FileOrigin origin) {
|
2019-01-25 14:37:28 +00:00
|
|
|
_large->loadEvenCancelled(origin);
|
|
|
|
_owner->notifyPhotoLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::loaded() const {
|
|
|
|
bool wasLoading = loading();
|
2019-01-25 14:37:28 +00:00
|
|
|
if (_large->loaded()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (wasLoading) {
|
2019-01-25 14:37:28 +00:00
|
|
|
_owner->notifyPhotoLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::loading() const {
|
2019-01-25 14:37:28 +00:00
|
|
|
return _large->loading();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::displayLoading() const {
|
2019-01-25 14:37:28 +00:00
|
|
|
return _large->loading()
|
|
|
|
? _large->displayLoading()
|
2017-12-25 14:17:00 +00:00
|
|
|
: (uploading() && !waitingForAlbum());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::cancel() {
|
2019-01-25 14:37:28 +00:00
|
|
|
_large->cancel();
|
|
|
|
_owner->notifyPhotoLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float64 PhotoData::progress() const {
|
|
|
|
if (uploading()) {
|
|
|
|
if (uploadingData->size > 0) {
|
|
|
|
return float64(uploadingData->offset) / uploadingData->size;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2019-01-25 14:37:28 +00:00
|
|
|
return _large->progress();
|
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 {
|
2019-01-25 14:37:28 +00:00
|
|
|
return _large->loadOffset();
|
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-10-23 12:57:43 +00:00
|
|
|
void PhotoData::unload() {
|
2019-01-25 14:37:28 +00:00
|
|
|
// Forget thumbnail only when image cache limit exceeds.
|
|
|
|
//_thumbnailInline->unload();
|
|
|
|
_thumbnailSmall->unload();
|
|
|
|
_thumbnail->unload();
|
|
|
|
_large->unload();
|
|
|
|
_replyPreview.clear();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 15:03:38 +00:00
|
|
|
Image *PhotoData::getReplyPreview(Data::FileOrigin origin) {
|
2019-01-25 14:37:28 +00:00
|
|
|
if (_replyPreview
|
|
|
|
&& (_replyPreview.good() || !_thumbnailSmall->loaded())) {
|
|
|
|
return _replyPreview.image();
|
|
|
|
}
|
|
|
|
if (_thumbnailSmall->isDelayedStorageImage()
|
|
|
|
&& !_large->isNull()
|
|
|
|
&& !_large->isDelayedStorageImage()
|
|
|
|
&& _large->loaded()) {
|
|
|
|
_replyPreview.prepare(
|
|
|
|
_large.get(),
|
|
|
|
origin,
|
|
|
|
Images::Option(0));
|
|
|
|
} else if (_thumbnailSmall->loaded()) {
|
|
|
|
_replyPreview.prepare(
|
|
|
|
_thumbnailSmall.get(),
|
|
|
|
origin,
|
|
|
|
Images::Option(0));
|
|
|
|
} else {
|
|
|
|
_thumbnailSmall->load(origin);
|
|
|
|
if (_thumbnailInline) {
|
|
|
|
_replyPreview.prepare(
|
|
|
|
_thumbnailInline.get(),
|
|
|
|
origin,
|
|
|
|
Images::Option::Blurred);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-25 14:37:28 +00:00
|
|
|
return _replyPreview.image();
|
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;
|
|
|
|
_thumbnailSmall->refreshFileReference(value);
|
|
|
|
_thumbnail->refreshFileReference(value);
|
|
|
|
_large->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
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
const auto copyImage = [&](const ImagePtr &src, const ImagePtr &dst) {
|
2018-08-27 11:35:58 +00:00
|
|
|
if (const auto from = src->cacheKey()) {
|
|
|
|
if (const auto to = dst->cacheKey()) {
|
2019-01-25 14:37:28 +00:00
|
|
|
_owner->cache().copyIfEmpty(*from, *to);
|
2018-08-27 11:35:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-01-25 14:37:28 +00:00
|
|
|
copyImage(local->_thumbnailSmall, _thumbnailSmall);
|
|
|
|
copyImage(local->_thumbnail, _thumbnail);
|
|
|
|
copyImage(local->_large, _large);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::isNull() const {
|
|
|
|
return _large->isNull();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::loadThumbnail(Data::FileOrigin origin) {
|
|
|
|
_thumbnail->load(origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::loadThumbnailSmall(Data::FileOrigin origin) {
|
|
|
|
_thumbnailSmall->load(origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *PhotoData::thumbnailInline() const {
|
|
|
|
return _thumbnailInline ? _thumbnailInline.get() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Image*> PhotoData::thumbnailSmall() const {
|
|
|
|
return _thumbnailSmall.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Image*> PhotoData::thumbnail() const {
|
|
|
|
return _thumbnail.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::load(Data::FileOrigin origin) {
|
|
|
|
_large->load(origin);
|
|
|
|
}
|
|
|
|
|
|
|
|
not_null<Image*> PhotoData::large() const {
|
|
|
|
return _large.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::updateImages(
|
|
|
|
ImagePtr thumbnailInline,
|
|
|
|
ImagePtr thumbnailSmall,
|
|
|
|
ImagePtr thumbnail,
|
|
|
|
ImagePtr large) {
|
|
|
|
if (!thumbnailSmall || !thumbnail || !large) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (thumbnailInline && !_thumbnailInline) {
|
|
|
|
_thumbnailInline = thumbnailInline;
|
|
|
|
}
|
|
|
|
const auto update = [](ImagePtr &was, ImagePtr now) {
|
|
|
|
if (!was) {
|
|
|
|
was = now;
|
|
|
|
} else if (was->isDelayedStorageImage()) {
|
2019-03-22 14:19:43 +00:00
|
|
|
if (const auto location = now->location(); location.valid()) {
|
2019-01-25 14:37:28 +00:00
|
|
|
was->setDelayedStorageLocation(
|
|
|
|
Data::FileOrigin(),
|
|
|
|
location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
update(_thumbnailSmall, thumbnailSmall);
|
|
|
|
update(_thumbnail, thumbnail);
|
|
|
|
update(_large, large);
|
|
|
|
}
|
|
|
|
|
|
|
|
int PhotoData::width() const {
|
|
|
|
return _large->width();
|
|
|
|
}
|
|
|
|
|
|
|
|
int PhotoData::height() const {
|
|
|
|
return _large->height();
|
2018-08-27 11:35:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void PhotoOpenClickHandler::onClickImpl() const {
|
2019-01-21 13:42:21 +00:00
|
|
|
Core::App().showPhoto(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoSaveClickHandler::onClickImpl() const {
|
|
|
|
auto data = photo();
|
|
|
|
if (!data->date) return;
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
data->download(context());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoCancelClickHandler::onClickImpl() const {
|
|
|
|
auto data = photo();
|
|
|
|
if (!data->date) return;
|
|
|
|
|
|
|
|
if (data->uploading()) {
|
2019-04-25 12:45:15 +00:00
|
|
|
if (const auto item = data->owner().message(context())) {
|
2018-01-11 13:07:29 +00:00
|
|
|
App::main()->cancelUploadLayer(item);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|