2016-04-09 05:57:55 +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.
|
2016-04-09 05:57:55 +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
|
2016-04-09 05:57:55 +00:00
|
|
|
*/
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/serialize_document.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/serialize_common.h"
|
2017-11-05 17:07:27 +00:00
|
|
|
#include "chat_helpers/stickers.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "data/data_session.h"
|
2018-10-11 15:54:57 +00:00
|
|
|
#include "ui/image.h"
|
2018-01-17 16:21:01 +00:00
|
|
|
#include "auth_session.h"
|
2016-04-09 05:57:55 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
enum StickerSetType {
|
|
|
|
StickerSetTypeEmpty = 0,
|
|
|
|
StickerSetTypeID = 1,
|
|
|
|
StickerSetTypeShortName = 2,
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace Serialize {
|
|
|
|
|
|
|
|
void Document::writeToStream(QDataStream &stream, DocumentData *document) {
|
2018-07-13 21:25:47 +00:00
|
|
|
const auto version = 0;
|
2016-04-09 05:57:55 +00:00
|
|
|
stream << quint64(document->id) << quint64(document->_access) << qint32(document->date);
|
2018-07-13 21:25:47 +00:00
|
|
|
stream << document->_fileReference << qint32(version);
|
2017-11-05 11:00:48 +00:00
|
|
|
stream << document->filename() << document->mimeString() << qint32(document->_dc) << qint32(document->size);
|
2016-04-09 05:57:55 +00:00
|
|
|
stream << qint32(document->dimensions.width()) << qint32(document->dimensions.height());
|
|
|
|
stream << qint32(document->type);
|
|
|
|
if (auto sticker = document->sticker()) {
|
|
|
|
stream << document->sticker()->alt;
|
|
|
|
switch (document->sticker()->set.type()) {
|
|
|
|
case mtpc_inputStickerSetID: {
|
|
|
|
stream << qint32(StickerSetTypeID);
|
|
|
|
} break;
|
|
|
|
case mtpc_inputStickerSetShortName: {
|
|
|
|
stream << qint32(StickerSetTypeShortName);
|
|
|
|
} break;
|
|
|
|
case mtpc_inputStickerSetEmpty:
|
|
|
|
default: {
|
|
|
|
stream << qint32(StickerSetTypeEmpty);
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
writeStorageImageLocation(stream, document->sticker()->loc);
|
|
|
|
} else {
|
|
|
|
stream << qint32(document->duration());
|
|
|
|
writeStorageImageLocation(stream, document->thumb->location());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
DocumentData *Document::readFromStreamHelper(int streamAppVersion, QDataStream &stream, const StickerSetInfo *info) {
|
2016-04-09 05:57:55 +00:00
|
|
|
quint64 id, access;
|
|
|
|
QString name, mime;
|
2016-07-08 13:56:53 +00:00
|
|
|
qint32 date, dc, size, width, height, type, version;
|
2018-07-13 16:49:46 +00:00
|
|
|
QByteArray fileReference;
|
2016-04-09 05:57:55 +00:00
|
|
|
stream >> id >> access >> date;
|
2016-07-25 19:54:40 +00:00
|
|
|
if (streamAppVersion >= 9061) {
|
2018-07-16 19:43:44 +00:00
|
|
|
if (streamAppVersion >= 1003013) {
|
2018-07-13 16:49:46 +00:00
|
|
|
stream >> fileReference;
|
|
|
|
}
|
2016-07-08 13:56:53 +00:00
|
|
|
stream >> version;
|
|
|
|
} else {
|
|
|
|
version = 0;
|
|
|
|
}
|
2016-04-09 05:57:55 +00:00
|
|
|
stream >> name >> mime >> dc >> size;
|
|
|
|
stream >> width >> height;
|
|
|
|
stream >> type;
|
|
|
|
|
|
|
|
QVector<MTPDocumentAttribute> attributes;
|
|
|
|
if (!name.isEmpty()) {
|
|
|
|
attributes.push_back(MTP_documentAttributeFilename(MTP_string(name)));
|
|
|
|
}
|
|
|
|
|
|
|
|
qint32 duration = -1;
|
|
|
|
StorageImageLocation thumb;
|
|
|
|
if (type == StickerDocument) {
|
|
|
|
QString alt;
|
|
|
|
qint32 typeOfSet;
|
|
|
|
stream >> alt >> typeOfSet;
|
|
|
|
|
2018-07-13 16:49:46 +00:00
|
|
|
thumb = readStorageImageLocation(streamAppVersion, stream);
|
2016-04-09 05:57:55 +00:00
|
|
|
|
|
|
|
if (typeOfSet == StickerSetTypeEmpty) {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetEmpty(), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} else if (info) {
|
2018-03-07 17:43:26 +00:00
|
|
|
if (info->setId == Stickers::DefaultSetId
|
|
|
|
|| info->setId == Stickers::CloudRecentSetId
|
|
|
|
|| info->setId == Stickers::FavedSetId
|
|
|
|
|| info->setId == Stickers::CustomSetId) {
|
2016-04-09 05:57:55 +00:00
|
|
|
typeOfSet = StickerSetTypeEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (typeOfSet) {
|
|
|
|
case StickerSetTypeID: {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetID(MTP_long(info->setId), MTP_long(info->accessHash)), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
|
|
|
case StickerSetTypeShortName: {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetShortName(MTP_string(info->shortName)), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
|
|
|
case StickerSetTypeEmpty:
|
|
|
|
default: {
|
2017-03-25 15:42:01 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeSticker(MTP_flags(0), MTP_string(alt), MTP_inputStickerSetEmpty(), MTPMaskCoords()));
|
2016-04-09 05:57:55 +00:00
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stream >> duration;
|
|
|
|
if (type == AnimatedDocument) {
|
|
|
|
attributes.push_back(MTP_documentAttributeAnimated());
|
|
|
|
}
|
2018-07-13 16:49:46 +00:00
|
|
|
thumb = readStorageImageLocation(streamAppVersion, stream);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
if (width > 0 && height > 0) {
|
|
|
|
if (duration >= 0) {
|
2017-03-23 16:11:35 +00:00
|
|
|
auto flags = MTPDdocumentAttributeVideo::Flags(0);
|
2017-04-02 10:25:54 +00:00
|
|
|
if (type == RoundVideoDocument) {
|
|
|
|
flags |= MTPDdocumentAttributeVideo::Flag::f_round_message;
|
|
|
|
}
|
2017-03-23 16:11:35 +00:00
|
|
|
attributes.push_back(MTP_documentAttributeVideo(MTP_flags(flags), MTP_int(duration), MTP_int(width), MTP_int(height)));
|
2016-04-09 05:57:55 +00:00
|
|
|
} else {
|
|
|
|
attributes.push_back(MTP_documentAttributeImageSize(MTP_int(width), MTP_int(height)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dc && !access) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
return Auth().data().document(
|
|
|
|
id,
|
|
|
|
access,
|
2018-07-13 16:49:46 +00:00
|
|
|
fileReference,
|
2018-01-17 16:21:01 +00:00
|
|
|
date,
|
|
|
|
attributes,
|
|
|
|
mime,
|
2018-10-23 09:08:50 +00:00
|
|
|
thumb.isNull() ? ImagePtr() : Images::Create(thumb),
|
2018-01-17 16:21:01 +00:00
|
|
|
dc,
|
|
|
|
size,
|
|
|
|
thumb);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
DocumentData *Document::readStickerFromStream(int streamAppVersion, QDataStream &stream, const StickerSetInfo &info) {
|
|
|
|
return readFromStreamHelper(streamAppVersion, stream, &info);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
DocumentData *Document::readFromStream(int streamAppVersion, QDataStream &stream) {
|
|
|
|
return readFromStreamHelper(streamAppVersion, stream, nullptr);
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int Document::sizeInStream(DocumentData *document) {
|
|
|
|
int result = 0;
|
|
|
|
|
2016-07-08 13:56:53 +00:00
|
|
|
// id + access + date + version
|
|
|
|
result += sizeof(quint64) + sizeof(quint64) + sizeof(qint32) + sizeof(qint32);
|
2016-04-09 05:57:55 +00:00
|
|
|
// + namelen + name + mimelen + mime + dc + size
|
2017-11-05 11:00:48 +00:00
|
|
|
result += stringSize(document->filename()) + stringSize(document->mimeString()) + sizeof(qint32) + sizeof(qint32);
|
2016-04-09 05:57:55 +00:00
|
|
|
// + width + height
|
|
|
|
result += sizeof(qint32) + sizeof(qint32);
|
|
|
|
// + type
|
|
|
|
result += sizeof(qint32);
|
|
|
|
|
|
|
|
if (auto sticker = document->sticker()) { // type == StickerDocument
|
|
|
|
// + altlen + alt + type-of-set
|
|
|
|
result += stringSize(sticker->alt) + sizeof(qint32);
|
2018-07-13 16:49:46 +00:00
|
|
|
// + sticker loc
|
|
|
|
result += Serialize::storageImageLocationSize(document->sticker()->loc);
|
2016-04-09 05:57:55 +00:00
|
|
|
} else {
|
|
|
|
// + duration
|
|
|
|
result += sizeof(qint32);
|
|
|
|
// + thumb loc
|
2018-07-13 16:49:46 +00:00
|
|
|
result += Serialize::storageImageLocationSize(document->thumb->location());
|
2016-04-09 05:57:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Serialize
|