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_types.h"
|
|
|
|
|
|
|
|
#include "data/data_document.h"
|
2019-01-25 14:37:28 +00:00
|
|
|
#include "data/data_file_origin.h"
|
|
|
|
#include "ui/image/image_source.h"
|
2018-05-21 21:31:46 +00:00
|
|
|
#include "ui/widgets/input_fields.h"
|
2018-08-27 11:35:58 +00:00
|
|
|
#include "storage/cache/storage_cache_types.h"
|
|
|
|
#include "base/openssl_help.h"
|
|
|
|
|
|
|
|
namespace Data {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kDocumentCacheTag = 0x0000000000000100ULL;
|
|
|
|
constexpr auto kDocumentCacheMask = 0x00000000000000FFULL;
|
2018-10-25 08:12:56 +00:00
|
|
|
constexpr auto kDocumentThumbCacheTag = 0x0000000000000200ULL;
|
|
|
|
constexpr auto kDocumentThumbCacheMask = 0x00000000000000FFULL;
|
2018-08-27 11:35:58 +00:00
|
|
|
constexpr auto kStorageCacheTag = 0x0000010000000000ULL;
|
|
|
|
constexpr auto kStorageCacheMask = 0x000000FFFFFFFFFFULL;
|
|
|
|
constexpr auto kWebDocumentCacheTag = 0x0000020000000000ULL;
|
|
|
|
constexpr auto kWebDocumentCacheMask = 0x000000FFFFFFFFFFULL;
|
|
|
|
constexpr auto kUrlCacheTag = 0x0000030000000000ULL;
|
|
|
|
constexpr auto kUrlCacheMask = 0x000000FFFFFFFFFFULL;
|
2018-10-08 18:14:05 +00:00
|
|
|
constexpr auto kGeoPointCacheTag = 0x0000040000000000ULL;
|
|
|
|
constexpr auto kGeoPointCacheMask = 0x000000FFFFFFFFFFULL;
|
2018-08-27 11:35:58 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
struct ReplyPreview::Data {
|
|
|
|
Data(std::unique_ptr<Images::Source> &&source, bool good);
|
|
|
|
|
|
|
|
Image image;
|
|
|
|
bool good = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
ReplyPreview::Data::Data(
|
|
|
|
std::unique_ptr<Images::Source> &&source,
|
|
|
|
bool good)
|
|
|
|
: image(std::move(source))
|
|
|
|
, good(good) {
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:35:58 +00:00
|
|
|
Storage::Cache::Key DocumentCacheKey(int32 dcId, uint64 id) {
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kDocumentCacheTag | (uint64(dcId) & Data::kDocumentCacheMask),
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-25 08:12:56 +00:00
|
|
|
Storage::Cache::Key DocumentThumbCacheKey(int32 dcId, uint64 id) {
|
|
|
|
const auto part = (uint64(dcId) & Data::kDocumentThumbCacheMask);
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kDocumentThumbCacheTag | part,
|
|
|
|
id
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:35:58 +00:00
|
|
|
Storage::Cache::Key StorageCacheKey(const StorageImageLocation &location) {
|
|
|
|
const auto dcId = uint64(location.dc()) & 0xFFULL;
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kStorageCacheTag | (dcId << 32) | uint32(location.local()),
|
|
|
|
location.volume()
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::Cache::Key WebDocumentCacheKey(const WebFileLocation &location) {
|
|
|
|
const auto dcId = uint64(location.dc()) & 0xFFULL;
|
2019-02-25 17:26:08 +00:00
|
|
|
const auto &url = location.url();
|
2018-08-27 11:35:58 +00:00
|
|
|
const auto hash = openssl::Sha256(bytes::make_span(url));
|
|
|
|
const auto bytes = bytes::make_span(hash);
|
|
|
|
const auto bytes1 = bytes.subspan(0, sizeof(uint32));
|
|
|
|
const auto bytes2 = bytes.subspan(sizeof(uint32), sizeof(uint64));
|
|
|
|
const auto part1 = *reinterpret_cast<const uint32*>(bytes1.data());
|
|
|
|
const auto part2 = *reinterpret_cast<const uint64*>(bytes2.data());
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kWebDocumentCacheTag | (dcId << 32) | part1,
|
|
|
|
part2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
Storage::Cache::Key UrlCacheKey(const QString &location) {
|
|
|
|
const auto url = location.toUtf8();
|
|
|
|
const auto hash = openssl::Sha256(bytes::make_span(url));
|
|
|
|
const auto bytes = bytes::make_span(hash);
|
|
|
|
const auto bytes1 = bytes.subspan(0, sizeof(uint32));
|
|
|
|
const auto bytes2 = bytes.subspan(sizeof(uint32), sizeof(uint64));
|
|
|
|
const auto bytes3 = bytes.subspan(
|
|
|
|
sizeof(uint32) + sizeof(uint64),
|
|
|
|
sizeof(uint16));
|
|
|
|
const auto part1 = *reinterpret_cast<const uint32*>(bytes1.data());
|
|
|
|
const auto part2 = *reinterpret_cast<const uint64*>(bytes2.data());
|
|
|
|
const auto part3 = *reinterpret_cast<const uint16*>(bytes3.data());
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kUrlCacheTag | (uint64(part3) << 32) | part1,
|
|
|
|
part2
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-10-08 18:14:05 +00:00
|
|
|
Storage::Cache::Key GeoPointCacheKey(const GeoPointLocation &location) {
|
|
|
|
const auto zoomscale = ((uint32(location.zoom) & 0x0FU) << 8)
|
|
|
|
| (uint32(location.scale) & 0x0FU);
|
|
|
|
const auto widthheight = ((uint32(location.width) & 0xFFFFU) << 16)
|
|
|
|
| (uint32(location.height) & 0xFFFFU);
|
|
|
|
return Storage::Cache::Key{
|
|
|
|
Data::kGeoPointCacheTag | (uint64(zoomscale) << 32) | widthheight,
|
|
|
|
(uint64(std::round(std::abs(location.lat + 360.) * 1000000)) << 32)
|
|
|
|
| uint64(std::round(std::abs(location.lon + 360.) * 1000000))
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-25 14:37:28 +00:00
|
|
|
ReplyPreview::ReplyPreview() = default;
|
|
|
|
|
|
|
|
ReplyPreview::ReplyPreview(ReplyPreview &&other) = default;
|
|
|
|
|
|
|
|
ReplyPreview &ReplyPreview::operator=(ReplyPreview &&other) = default;
|
|
|
|
|
|
|
|
ReplyPreview::~ReplyPreview() = default;
|
|
|
|
|
|
|
|
void ReplyPreview::prepare(
|
|
|
|
not_null<Image*> image,
|
|
|
|
FileOrigin origin,
|
|
|
|
Images::Options options) {
|
|
|
|
int w = image->width(), h = image->height();
|
|
|
|
if (w <= 0) w = 1;
|
|
|
|
if (h <= 0) h = 1;
|
|
|
|
auto thumbSize = (w > h)
|
|
|
|
? QSize(
|
|
|
|
w * st::msgReplyBarSize.height() / h,
|
|
|
|
st::msgReplyBarSize.height())
|
|
|
|
: QSize(
|
|
|
|
st::msgReplyBarSize.height(),
|
|
|
|
h * st::msgReplyBarSize.height() / w);
|
|
|
|
thumbSize *= cIntRetinaFactor();
|
|
|
|
const auto prepareOptions = Images::Option::Smooth
|
|
|
|
| Images::Option::TransparentBackground
|
|
|
|
| options;
|
|
|
|
auto outerSize = st::msgReplyBarSize.height();
|
|
|
|
auto bitmap = image->pixNoCache(
|
|
|
|
origin,
|
|
|
|
thumbSize.width(),
|
|
|
|
thumbSize.height(),
|
|
|
|
prepareOptions,
|
|
|
|
outerSize,
|
|
|
|
outerSize);
|
|
|
|
_data = std::make_unique<ReplyPreview::Data>(
|
|
|
|
std::make_unique<Images::ImageSource>(
|
|
|
|
bitmap.toImage(),
|
|
|
|
"PNG"),
|
|
|
|
((options & Images::Option::Blurred) == 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReplyPreview::clear() {
|
|
|
|
_data = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Image *ReplyPreview::image() const {
|
|
|
|
return _data ? &_data->image : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReplyPreview::good() const {
|
|
|
|
return !empty() && _data->good;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReplyPreview::empty() const {
|
|
|
|
return !_data;
|
|
|
|
}
|
|
|
|
|
2018-08-27 11:35:58 +00:00
|
|
|
} // namespace Data
|
2017-09-26 11:49:16 +00:00
|
|
|
|
2019-02-13 18:10:18 +00:00
|
|
|
AudioMsgId AudioMsgId::ForVideo() {
|
|
|
|
auto result = AudioMsgId();
|
|
|
|
result._playId = rand_value<uint32>();
|
|
|
|
result._type = Type::Video;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
void AudioMsgId::setTypeFromAudio() {
|
2017-12-10 10:26:58 +00:00
|
|
|
if (_audio->isVoiceMessage() || _audio->isVideoMessage()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_type = Type::Voice;
|
2017-12-10 10:26:58 +00:00
|
|
|
} else if (_audio->isVideoFile()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_type = Type::Video;
|
2017-12-10 10:26:58 +00:00
|
|
|
} else if (_audio->isAudioFile()) {
|
2017-09-26 11:49:16 +00:00
|
|
|
_type = Type::Song;
|
|
|
|
} else {
|
|
|
|
_type = Type::Unknown;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
void MessageCursor::fillFrom(not_null<const Ui::InputField*> field) {
|
|
|
|
const auto cursor = field->textCursor();
|
|
|
|
position = cursor.position();
|
|
|
|
anchor = cursor.anchor();
|
|
|
|
const auto top = field->scrollTop().current();
|
|
|
|
scroll = (top != field->scrollTopMax()) ? top : QFIXED_MAX;
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
|
|
|
|
2018-05-21 21:31:46 +00:00
|
|
|
void MessageCursor::applyTo(not_null<Ui::InputField*> field) {
|
|
|
|
auto cursor = field->textCursor();
|
2017-09-26 11:49:16 +00:00
|
|
|
cursor.setPosition(anchor, QTextCursor::MoveAnchor);
|
|
|
|
cursor.setPosition(position, QTextCursor::KeepAnchor);
|
2018-05-21 21:31:46 +00:00
|
|
|
field->setTextCursor(cursor);
|
|
|
|
field->scrollTo(scroll);
|
2017-09-26 11:49:16 +00:00
|
|
|
}
|
2017-12-25 14:17:00 +00:00
|
|
|
|
|
|
|
HistoryItem *FileClickHandler::getActionItem() const {
|
|
|
|
return context()
|
|
|
|
? App::histItemById(context())
|
|
|
|
: nullptr;
|
|
|
|
}
|
2018-12-17 08:16:06 +00:00
|
|
|
|
|
|
|
PeerId PeerFromMessage(const MTPmessage &message) {
|
|
|
|
return message.match([](const MTPDmessageEmpty &) {
|
|
|
|
return PeerId(0);
|
|
|
|
}, [](const auto &message) {
|
|
|
|
auto from_id = message.has_from_id() ? peerFromUser(message.vfrom_id) : 0;
|
|
|
|
auto to_id = peerFromMTP(message.vto_id);
|
|
|
|
auto out = message.is_out();
|
|
|
|
return (out || !peerIsUser(to_id)) ? to_id : from_id;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
MTPDmessage::Flags FlagsFromMessage(const MTPmessage &message) {
|
|
|
|
return message.match([](const MTPDmessageEmpty &) {
|
|
|
|
return MTPDmessage::Flags(0);
|
|
|
|
}, [](const MTPDmessage &message) {
|
|
|
|
return message.vflags.v;
|
|
|
|
}, [](const MTPDmessageService &message) {
|
|
|
|
return mtpCastFlags(message.vflags.v);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
MsgId IdFromMessage(const MTPmessage &message) {
|
|
|
|
return message.match([](const auto &message) {
|
|
|
|
return message.vid.v;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeId DateFromMessage(const MTPmessage &message) {
|
|
|
|
return message.match([](const MTPDmessageEmpty &) {
|
|
|
|
return TimeId(0);
|
|
|
|
}, [](const auto &message) {
|
|
|
|
return message.vdate.v;
|
|
|
|
});
|
|
|
|
}
|