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"
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void PhotoData::forget() {
|
|
|
|
thumb->forget();
|
|
|
|
replyPreview->forget();
|
|
|
|
medium->forget();
|
|
|
|
full->forget();
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:25:47 +00:00
|
|
|
ImagePtr PhotoData::makeReplyPreview(Data::FileOrigin origin) {
|
2017-09-26 11:49:16 +00:00
|
|
|
if (replyPreview->isNull() && !thumb->isNull()) {
|
2018-08-27 11:35:58 +00:00
|
|
|
const auto previewFromImage = [&](const ImagePtr &image) {
|
|
|
|
if (!image->loaded()) {
|
|
|
|
image->load(origin);
|
|
|
|
return ImagePtr();
|
|
|
|
}
|
|
|
|
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-08-27 11:35:58 +00:00
|
|
|
return ImagePtr(
|
|
|
|
(w > h
|
|
|
|
? image->pix(
|
|
|
|
origin,
|
|
|
|
w * st::msgReplyBarSize.height() / h,
|
|
|
|
st::msgReplyBarSize.height())
|
|
|
|
: image->pix(origin, st::msgReplyBarSize.height())),
|
|
|
|
"PNG");
|
|
|
|
};
|
|
|
|
if (thumb->toDelayedStorageImage()
|
|
|
|
&& !full->isNull()
|
|
|
|
&& !full->toDelayedStorageImage()) {
|
|
|
|
return previewFromImage(full);
|
2017-09-26 11:49:16 +00:00
|
|
|
} else {
|
2018-08-27 11:35:58 +00:00
|
|
|
return previewFromImage(thumb);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return replyPreview;
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|