2020-05-28 10:00:51 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
2020-06-08 17:24:36 +00:00
|
|
|
#include "data/stickers/data_stickers_set.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
|
|
|
|
#include "main/main_session.h"
|
|
|
|
#include "data/data_session.h"
|
|
|
|
#include "data/data_file_origin.h"
|
2022-07-13 15:05:29 +00:00
|
|
|
#include "data/data_document.h"
|
2022-07-22 11:39:28 +00:00
|
|
|
#include "data/stickers/data_stickers.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
#include "storage/file_download.h"
|
2020-05-29 15:10:25 +00:00
|
|
|
#include "ui/image/image.h"
|
2020-05-28 10:00:51 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
namespace Data {
|
2020-05-28 10:00:51 +00:00
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
StickersSetThumbnailView::StickersSetThumbnailView(
|
|
|
|
not_null<StickersSet*> owner)
|
|
|
|
: _owner(owner) {
|
2020-05-28 11:58:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
not_null<StickersSet*> StickersSetThumbnailView::owner() const {
|
2020-05-28 11:58:50 +00:00
|
|
|
return _owner;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void StickersSetThumbnailView::set(
|
2020-05-28 10:00:51 +00:00
|
|
|
not_null<Main::Session*> session,
|
|
|
|
QByteArray content) {
|
2021-08-11 15:40:17 +00:00
|
|
|
auto image = Images::Read({ .content = content }).image;
|
2020-05-28 10:00:51 +00:00
|
|
|
if (image.isNull()) {
|
|
|
|
_content = std::move(content);
|
|
|
|
} else {
|
2020-05-29 15:10:25 +00:00
|
|
|
_image = std::make_unique<Image>(std::move(image));
|
2020-05-28 10:00:51 +00:00
|
|
|
}
|
2020-07-01 08:03:34 +00:00
|
|
|
session->notifyDownloaderTaskFinished();
|
2020-05-28 10:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
Image *StickersSetThumbnailView::image() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _image.get();
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
QByteArray StickersSetThumbnailView::content() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _content;
|
|
|
|
}
|
|
|
|
|
2021-07-09 06:08:58 +00:00
|
|
|
StickersSetFlags ParseStickersSetFlags(const MTPDstickerSet &data) {
|
|
|
|
using Flag = StickersSetFlag;
|
|
|
|
return (data.is_archived() ? Flag::Archived : Flag())
|
|
|
|
| (data.is_official() ? Flag::Official : Flag())
|
|
|
|
| (data.is_masks() ? Flag::Masks : Flag())
|
2022-07-08 17:04:31 +00:00
|
|
|
| (data.is_emojis() ? Flag::Emoji : Flag())
|
2022-01-24 12:33:31 +00:00
|
|
|
| (data.vinstalled_date() ? Flag::Installed : Flag())
|
2023-10-27 19:40:39 +00:00
|
|
|
| (data.is_videos() ? Flag::Webm : Flag())
|
2023-12-22 02:06:10 +00:00
|
|
|
| (data.is_text_color() ? Flag::TextColor : Flag())
|
|
|
|
| (data.is_channel_emoji_status() ? Flag::ChannelStatus : Flag());
|
2021-07-09 06:08:58 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
StickersSet::StickersSet(
|
2020-05-28 10:00:51 +00:00
|
|
|
not_null<Data::Session*> owner,
|
|
|
|
uint64 id,
|
2021-08-25 08:15:05 +00:00
|
|
|
uint64 accessHash,
|
|
|
|
uint64 hash,
|
2020-05-28 10:00:51 +00:00
|
|
|
const QString &title,
|
|
|
|
const QString &shortName,
|
|
|
|
int count,
|
2021-07-09 06:08:58 +00:00
|
|
|
StickersSetFlags flags,
|
2020-05-28 10:00:51 +00:00
|
|
|
TimeId installDate)
|
|
|
|
: id(id)
|
2021-08-25 08:15:05 +00:00
|
|
|
, accessHash(accessHash)
|
|
|
|
, hash(hash)
|
2020-05-28 10:00:51 +00:00
|
|
|
, title(title)
|
|
|
|
, shortName(shortName)
|
|
|
|
, count(count)
|
|
|
|
, flags(flags)
|
|
|
|
, installDate(installDate)
|
|
|
|
, _owner(owner) {
|
|
|
|
}
|
|
|
|
|
2023-01-24 14:46:16 +00:00
|
|
|
StickersSet::~StickersSet() = default;
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
Data::Session &StickersSet::owner() const {
|
2020-05-28 11:58:50 +00:00
|
|
|
return *_owner;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
Main::Session &StickersSet::session() const {
|
2020-05-28 11:58:50 +00:00
|
|
|
return _owner->session();
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
MTPInputStickerSet StickersSet::mtpInput() const {
|
2021-08-25 08:15:05 +00:00
|
|
|
return (id && accessHash)
|
|
|
|
? MTP_inputStickerSetID(MTP_long(id), MTP_long(accessHash))
|
2020-05-28 10:00:51 +00:00
|
|
|
: MTP_inputStickerSetShortName(MTP_string(shortName));
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:42:57 +00:00
|
|
|
StickerSetIdentifier StickersSet::identifier() const {
|
|
|
|
return StickerSetIdentifier{
|
|
|
|
.id = id,
|
2021-08-25 08:15:05 +00:00
|
|
|
.accessHash = accessHash,
|
2021-07-08 16:42:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-07-22 11:39:28 +00:00
|
|
|
StickersType StickersSet::type() const {
|
|
|
|
return (flags & StickersSetFlag::Emoji)
|
|
|
|
? StickersType::Emoji
|
|
|
|
: (flags & StickersSetFlag::Masks)
|
|
|
|
? StickersType::Masks
|
|
|
|
: StickersType::Stickers;
|
|
|
|
}
|
|
|
|
|
2023-10-27 19:40:39 +00:00
|
|
|
bool StickersSet::textColor() const {
|
|
|
|
return flags & StickersSetFlag::TextColor;
|
|
|
|
}
|
|
|
|
|
2023-12-22 02:06:10 +00:00
|
|
|
bool StickersSet::channelStatus() const {
|
|
|
|
return flags & StickersSetFlag::ChannelStatus;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void StickersSet::setThumbnail(const ImageWithLocation &data) {
|
2020-05-28 10:00:51 +00:00
|
|
|
Data::UpdateCloudFile(
|
|
|
|
_thumbnail,
|
|
|
|
data,
|
|
|
|
_owner->cache(),
|
|
|
|
Data::kImageCacheTag,
|
2020-05-28 11:58:50 +00:00
|
|
|
[=](Data::FileOrigin origin) { loadThumbnail(); });
|
2020-05-28 10:00:51 +00:00
|
|
|
if (!data.bytes.isEmpty()) {
|
|
|
|
if (_thumbnail.loader) {
|
|
|
|
_thumbnail.loader->cancel();
|
|
|
|
}
|
|
|
|
if (const auto view = activeThumbnailView()) {
|
|
|
|
view->set(&_owner->session(), data.bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
bool StickersSet::hasThumbnail() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _thumbnail.location.valid();
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
bool StickersSet::thumbnailLoading() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return (_thumbnail.loader != nullptr);
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
bool StickersSet::thumbnailFailed() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return (_thumbnail.flags & Data::CloudFile::Flag::Failed);
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
void StickersSet::loadThumbnail() {
|
2020-05-28 10:00:51 +00:00
|
|
|
const auto autoLoading = false;
|
2020-06-08 15:17:33 +00:00
|
|
|
const auto finalCheck = [=] {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (const auto active = activeThumbnailView()) {
|
|
|
|
return !active->image() && active->content().isEmpty();
|
|
|
|
}
|
|
|
|
return true;
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
const auto done = [=](QByteArray result) {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (const auto active = activeThumbnailView()) {
|
|
|
|
active->set(&_owner->session(), std::move(result));
|
|
|
|
}
|
2020-06-08 15:17:33 +00:00
|
|
|
};
|
|
|
|
Data::LoadCloudFile(
|
|
|
|
&_owner->session(),
|
|
|
|
_thumbnail,
|
2021-08-25 08:15:05 +00:00
|
|
|
Data::FileOriginStickerSet(id, accessHash),
|
2020-06-08 15:17:33 +00:00
|
|
|
LoadFromCloudOrLocal,
|
|
|
|
autoLoading,
|
|
|
|
Data::kImageCacheTag,
|
|
|
|
finalCheck,
|
|
|
|
done);
|
2020-05-28 10:00:51 +00:00
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
const ImageLocation &StickersSet::thumbnailLocation() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _thumbnail.location;
|
|
|
|
}
|
|
|
|
|
2021-10-21 10:58:15 +00:00
|
|
|
Storage::Cache::Key StickersSet::thumbnailBigFileBaseCacheKey() const {
|
|
|
|
const auto &location = _thumbnail.location.file().data;
|
|
|
|
if (const auto storage = std::get_if<StorageFileLocation>(&location)) {
|
|
|
|
return storage->bigFileBaseCacheKey();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
int StickersSet::thumbnailByteSize() const {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _thumbnail.byteSize;
|
|
|
|
}
|
|
|
|
|
2022-07-13 15:05:29 +00:00
|
|
|
DocumentData *StickersSet::lookupThumbnailDocument() const {
|
|
|
|
if (thumbnailDocumentId) {
|
|
|
|
const auto i = ranges::find(
|
|
|
|
stickers,
|
|
|
|
thumbnailDocumentId,
|
|
|
|
&DocumentData::id);
|
|
|
|
if (i != stickers.end()) {
|
|
|
|
return *i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return !stickers.empty()
|
|
|
|
? stickers.front()
|
|
|
|
: !covers.empty()
|
|
|
|
? covers.front()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
std::shared_ptr<StickersSetThumbnailView> StickersSet::createThumbnailView() {
|
2020-05-28 10:00:51 +00:00
|
|
|
if (auto active = activeThumbnailView()) {
|
|
|
|
return active;
|
|
|
|
}
|
2020-06-08 17:24:36 +00:00
|
|
|
auto view = std::make_shared<StickersSetThumbnailView>(this);
|
2020-05-28 10:00:51 +00:00
|
|
|
_thumbnailView = view;
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2020-06-08 17:24:36 +00:00
|
|
|
std::shared_ptr<StickersSetThumbnailView> StickersSet::activeThumbnailView() {
|
2020-05-28 10:00:51 +00:00
|
|
|
return _thumbnailView.lock();
|
|
|
|
}
|
|
|
|
|
2021-07-08 16:42:57 +00:00
|
|
|
MTPInputStickerSet InputStickerSet(StickerSetIdentifier id) {
|
|
|
|
return !id
|
|
|
|
? MTP_inputStickerSetEmpty()
|
|
|
|
: id.id
|
|
|
|
? MTP_inputStickerSetID(MTP_long(id.id), MTP_long(id.accessHash))
|
|
|
|
: MTP_inputStickerSetShortName(MTP_string(id.shortName));
|
|
|
|
}
|
|
|
|
|
|
|
|
StickerSetIdentifier FromInputSet(const MTPInputStickerSet &id) {
|
|
|
|
return id.match([](const MTPDinputStickerSetID &data) {
|
|
|
|
return StickerSetIdentifier{
|
|
|
|
.id = data.vid().v,
|
|
|
|
.accessHash = data.vaccess_hash().v,
|
|
|
|
};
|
|
|
|
}, [](const MTPDinputStickerSetShortName &data) {
|
|
|
|
return StickerSetIdentifier{ .shortName = qs(data.vshort_name()) };
|
|
|
|
}, [](const auto &) {
|
|
|
|
return StickerSetIdentifier();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-28 10:00:51 +00:00
|
|
|
} // namespace Stickers
|