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"
|
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"
|
|
|
|
#include "history/history_media_types.h"
|
2017-10-05 15:35:52 +00:00
|
|
|
#include "auth_session.h"
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "messenger.h"
|
|
|
|
|
2018-07-13 16:49:46 +00:00
|
|
|
PhotoData::PhotoData(const PhotoId &id)
|
|
|
|
: id(id) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoData::PhotoData(
|
|
|
|
const PhotoId &id,
|
|
|
|
const uint64 &access,
|
|
|
|
const QByteArray &fileReference,
|
|
|
|
TimeId date,
|
|
|
|
const ImagePtr &thumb,
|
|
|
|
const ImagePtr &medium,
|
|
|
|
const ImagePtr &full)
|
2017-09-26 11:49:16 +00:00
|
|
|
: id(id)
|
|
|
|
, access(access)
|
|
|
|
, date(date)
|
|
|
|
, thumb(thumb)
|
|
|
|
, medium(medium)
|
|
|
|
, full(full) {
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void PhotoData::automaticLoad(
|
|
|
|
Data::FileOrigin origin,
|
|
|
|
const HistoryItem *item) {
|
|
|
|
full->automaticLoad(origin, item);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::automaticLoadSettingsChanged() {
|
|
|
|
full->automaticLoadSettingsChanged();
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
void PhotoData::download(Data::FileOrigin origin) {
|
|
|
|
full->loadEvenCancelled(origin);
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().notifyPhotoLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::loaded() const {
|
|
|
|
bool wasLoading = loading();
|
|
|
|
if (full->loaded()) {
|
|
|
|
if (wasLoading) {
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().notifyPhotoLayoutChanged(this);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::loading() const {
|
|
|
|
return full->loading();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PhotoData::displayLoading() const {
|
2017-12-25 14:17:00 +00:00
|
|
|
return full->loading()
|
|
|
|
? full->displayLoading()
|
|
|
|
: (uploading() && !waitingForAlbum());
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::cancel() {
|
|
|
|
full->cancel();
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().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;
|
|
|
|
}
|
|
|
|
return full->progress();
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
return full->loadOffset();
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
// Forget thumb only when image cache limit exceeds.
|
|
|
|
//thumb->unload();
|
|
|
|
medium->unload();
|
|
|
|
full->unload();
|
2018-11-09 15:03:38 +00:00
|
|
|
_replyPreview = nullptr;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 15:03:38 +00:00
|
|
|
Image *PhotoData::getReplyPreview(Data::FileOrigin origin) {
|
|
|
|
if (!_replyPreview && !thumb->isNull()) {
|
2018-08-27 11:35:58 +00:00
|
|
|
const auto previewFromImage = [&](const ImagePtr &image) {
|
|
|
|
if (!image->loaded()) {
|
|
|
|
image->load(origin);
|
2018-11-09 15:03:38 +00:00
|
|
|
return std::unique_ptr<Image>();
|
2018-08-27 11:35:58 +00:00
|
|
|
}
|
|
|
|
int w = image->width(), h = image->height();
|
2017-09-26 11:49:16 +00:00
|
|
|
if (w <= 0) w = 1;
|
|
|
|
if (h <= 0) h = 1;
|
2018-11-09 15:03:38 +00:00
|
|
|
return std::make_unique<Image>(
|
|
|
|
std::make_unique<Images::ImageSource>(
|
2018-08-27 11:35:58 +00:00
|
|
|
(w > h
|
|
|
|
? image->pix(
|
|
|
|
origin,
|
|
|
|
w * st::msgReplyBarSize.height() / h,
|
|
|
|
st::msgReplyBarSize.height())
|
2018-10-12 16:41:51 +00:00
|
|
|
: image->pix(origin, st::msgReplyBarSize.height())
|
2018-11-09 15:03:38 +00:00
|
|
|
).toImage(),
|
|
|
|
"PNG"));
|
2018-08-27 11:35:58 +00:00
|
|
|
};
|
2018-10-11 16:07:08 +00:00
|
|
|
if (thumb->isDelayedStorageImage()
|
2018-08-27 11:35:58 +00:00
|
|
|
&& !full->isNull()
|
2018-10-11 16:07:08 +00:00
|
|
|
&& !full->isDelayedStorageImage()) {
|
2018-11-09 15:03:38 +00:00
|
|
|
_replyPreview = previewFromImage(full);
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2018-11-09 15:03:38 +00:00
|
|
|
_replyPreview = previewFromImage(thumb);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-11-09 15:03:38 +00:00
|
|
|
return _replyPreview.get();
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-07-13 16:49:46 +00:00
|
|
|
MTPInputPhoto PhotoData::mtpInput() const {
|
|
|
|
return MTP_inputPhoto(
|
|
|
|
MTP_long(id),
|
|
|
|
MTP_long(access),
|
|
|
|
MTP_bytes(fileReference));
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:35:58 +00:00
|
|
|
void PhotoData::collectLocalData(PhotoData *local) {
|
|
|
|
if (local == this) return;
|
|
|
|
|
|
|
|
const auto copyImage = [](const ImagePtr &src, const ImagePtr &dst) {
|
|
|
|
if (const auto from = src->cacheKey()) {
|
|
|
|
if (const auto to = dst->cacheKey()) {
|
|
|
|
Auth().data().cache().copyIfEmpty(*from, *to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
copyImage(local->thumb, thumb);
|
|
|
|
copyImage(local->medium, medium);
|
|
|
|
copyImage(local->full, full);
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void PhotoOpenClickHandler::onClickImpl() const {
|
2017-12-15 16:25:47 +00:00
|
|
|
Messenger::Instance().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()) {
|
2017-12-15 16:25:47 +00:00
|
|
|
if (const auto item = App::histItemById(context())) {
|
2018-01-11 13:07:29 +00:00
|
|
|
App::main()->cancelUploadLayer(item);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
data->cancel();
|
|
|
|
}
|
|
|
|
}
|