2016-04-04 21:09:46 +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-04 21:09:46 +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-04 21:09:46 +00:00
|
|
|
*/
|
|
|
|
#include "inline_bots/inline_bot_layout_item.h"
|
|
|
|
|
2017-09-26 11:49:16 +00:00
|
|
|
#include "data/data_photo.h"
|
|
|
|
#include "data/data_document.h"
|
2016-04-12 21:31:28 +00:00
|
|
|
#include "core/click_handler_types.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
#include "inline_bots/inline_bot_result.h"
|
|
|
|
#include "inline_bots/inline_bot_layout_internal.h"
|
2017-03-04 10:23:56 +00:00
|
|
|
#include "storage/localstorage.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
#include "mainwidget.h"
|
2017-12-05 08:43:18 +00:00
|
|
|
#include "ui/empty_userpic.h"
|
2016-04-04 21:09:46 +00:00
|
|
|
|
|
|
|
namespace InlineBots {
|
|
|
|
namespace Layout {
|
|
|
|
|
|
|
|
void ItemBase::setPosition(int32 position) {
|
|
|
|
_position = position;
|
|
|
|
}
|
|
|
|
|
|
|
|
int32 ItemBase::position() const {
|
|
|
|
return _position;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result *ItemBase::getResult() const {
|
|
|
|
return _result;
|
|
|
|
}
|
|
|
|
|
|
|
|
DocumentData *ItemBase::getDocument() const {
|
|
|
|
return _doc;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoData *ItemBase::getPhoto() const {
|
|
|
|
return _photo;
|
|
|
|
}
|
|
|
|
|
2016-04-06 15:32:05 +00:00
|
|
|
DocumentData *ItemBase::getPreviewDocument() const {
|
|
|
|
auto previewDocument = [this]() -> DocumentData* {
|
|
|
|
if (_doc) {
|
|
|
|
return _doc;
|
|
|
|
}
|
|
|
|
if (_result) {
|
|
|
|
return _result->_document;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
};
|
|
|
|
if (DocumentData *result = previewDocument()) {
|
|
|
|
if (result->sticker() || result->loaded()) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
PhotoData *ItemBase::getPreviewPhoto() const {
|
|
|
|
if (_photo) {
|
|
|
|
return _photo;
|
|
|
|
}
|
|
|
|
if (_result) {
|
|
|
|
return _result->_photo;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-04-04 21:09:46 +00:00
|
|
|
void ItemBase::preload() const {
|
|
|
|
if (_result) {
|
2016-04-05 20:24:27 +00:00
|
|
|
if (_result->_photo) {
|
|
|
|
_result->_photo->thumb->load();
|
|
|
|
} else if (_result->_document) {
|
|
|
|
_result->_document->thumb->load();
|
|
|
|
} else if (!_result->_thumb->isNull()) {
|
|
|
|
_result->_thumb->load();
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
} else if (_doc) {
|
|
|
|
_doc->thumb->load();
|
|
|
|
} else if (_photo) {
|
|
|
|
_photo->medium->load();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ItemBase::update() {
|
|
|
|
if (_position >= 0) {
|
2017-03-27 18:11:51 +00:00
|
|
|
context()->inlineItemRepaint(this);
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 18:11:51 +00:00
|
|
|
void ItemBase::layoutChanged() {
|
|
|
|
if (_position >= 0) {
|
|
|
|
context()->inlineItemLayoutChanged(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<ItemBase> ItemBase::createLayout(not_null<Context*> context, Result *result, bool forceThumb) {
|
2016-04-04 21:09:46 +00:00
|
|
|
using Type = Result::Type;
|
|
|
|
|
2016-04-05 20:24:27 +00:00
|
|
|
switch (result->_type) {
|
2017-03-27 18:11:51 +00:00
|
|
|
case Type::Photo: return std::make_unique<internal::Photo>(context, result); break;
|
2016-04-04 21:09:46 +00:00
|
|
|
case Type::Audio:
|
2017-03-27 18:11:51 +00:00
|
|
|
case Type::File: return std::make_unique<internal::File>(context, result); break;
|
|
|
|
case Type::Video: return std::make_unique<internal::Video>(context, result); break;
|
|
|
|
case Type::Sticker: return std::make_unique<internal::Sticker>(context, result); break;
|
|
|
|
case Type::Gif: return std::make_unique<internal::Gif>(context, result); break;
|
2016-04-04 21:09:46 +00:00
|
|
|
case Type::Article:
|
2016-04-11 07:43:40 +00:00
|
|
|
case Type::Geo:
|
2017-03-27 18:11:51 +00:00
|
|
|
case Type::Venue: return std::make_unique<internal::Article>(context, result, forceThumb); break;
|
|
|
|
case Type::Game: return std::make_unique<internal::Game>(context, result); break;
|
|
|
|
case Type::Contact: return std::make_unique<internal::Contact>(context, result); break;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
2017-08-13 16:14:00 +00:00
|
|
|
return nullptr;
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
std::unique_ptr<ItemBase> ItemBase::createLayoutGif(not_null<Context*> context, DocumentData *document) {
|
2017-03-27 18:11:51 +00:00
|
|
|
return std::make_unique<internal::Gif>(context, document, true);
|
2016-04-04 21:09:46 +00:00
|
|
|
}
|
|
|
|
|
2016-04-05 20:24:27 +00:00
|
|
|
DocumentData *ItemBase::getResultDocument() const {
|
|
|
|
return _result ? _result->_document : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
PhotoData *ItemBase::getResultPhoto() const {
|
|
|
|
return _result ? _result->_photo : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ImagePtr ItemBase::getResultThumb() const {
|
|
|
|
if (_result) {
|
|
|
|
if (_result->_photo && !_result->_photo->thumb->isNull()) {
|
|
|
|
return _result->_photo->thumb;
|
|
|
|
}
|
|
|
|
if (!_result->_thumb->isNull()) {
|
|
|
|
return _result->_thumb;
|
|
|
|
}
|
|
|
|
return _result->_locationThumb;
|
|
|
|
}
|
|
|
|
return ImagePtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
QPixmap ItemBase::getResultContactAvatar(int width, int height) const {
|
|
|
|
if (_result->_type == Result::Type::Contact) {
|
2017-12-05 08:43:18 +00:00
|
|
|
auto result = Ui::EmptyUserpic(
|
|
|
|
Data::PeerUserpicColor(qHash(_result->_id)),
|
2017-12-01 10:21:40 +00:00
|
|
|
_result->getLayoutTitle()
|
|
|
|
).generate(width);
|
2016-12-29 09:03:51 +00:00
|
|
|
if (result.height() != height * cIntRetinaFactor()) {
|
|
|
|
result = result.scaled(QSize(width, height) * cIntRetinaFactor(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
|
|
|
|
}
|
2017-02-21 14:37:53 +00:00
|
|
|
return result;
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
return QPixmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
int ItemBase::getResultDuration() const {
|
2018-03-04 20:04:13 +00:00
|
|
|
return 0;
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString ItemBase::getResultUrl() const {
|
|
|
|
return _result->_url;
|
|
|
|
}
|
|
|
|
|
|
|
|
ClickHandlerPtr ItemBase::getResultUrlHandler() const {
|
|
|
|
if (!_result->_url.isEmpty()) {
|
2017-12-18 09:07:18 +00:00
|
|
|
return std::make_shared<UrlClickHandler>(_result->_url);
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
return ClickHandlerPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClickHandlerPtr ItemBase::getResultContentUrlHandler() const {
|
|
|
|
if (!_result->_content_url.isEmpty()) {
|
2017-12-18 09:07:18 +00:00
|
|
|
return std::make_shared<UrlClickHandler>(_result->_content_url);
|
2016-04-05 20:24:27 +00:00
|
|
|
}
|
|
|
|
return ClickHandlerPtr();
|
|
|
|
}
|
|
|
|
|
|
|
|
QString ItemBase::getResultThumbLetter() const {
|
2016-08-30 05:24:16 +00:00
|
|
|
#ifndef OS_MAC_OLD
|
|
|
|
auto parts = _result->_url.splitRef('/');
|
|
|
|
#else // OS_MAC_OLD
|
|
|
|
auto parts = _result->_url.split('/');
|
|
|
|
#endif // OS_MAC_OLD
|
2016-04-05 20:24:27 +00:00
|
|
|
if (!parts.isEmpty()) {
|
2016-08-30 05:24:16 +00:00
|
|
|
auto domain = parts.at(0);
|
2016-04-05 20:24:27 +00:00
|
|
|
if (parts.size() > 2 && domain.endsWith(':') && parts.at(1).isEmpty()) { // http:// and others
|
|
|
|
domain = parts.at(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
parts = domain.split('@').back().split('.');
|
|
|
|
if (parts.size() > 1) {
|
|
|
|
return parts.at(parts.size() - 2).at(0).toUpper();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!_result->_title.isEmpty()) {
|
|
|
|
return _result->_title.at(0).toUpper();
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2016-04-10 18:18:26 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
NeverFreedPointer<DocumentItems> documentItemsMap;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
const DocumentItems *documentItems() {
|
|
|
|
return documentItemsMap.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace internal {
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void regDocumentItem(
|
|
|
|
not_null<const DocumentData*> document,
|
|
|
|
not_null<ItemBase*> item) {
|
2016-10-12 19:34:25 +00:00
|
|
|
documentItemsMap.createIfNull();
|
2016-04-10 18:18:26 +00:00
|
|
|
(*documentItemsMap)[document].insert(item);
|
|
|
|
}
|
|
|
|
|
2018-01-17 16:21:01 +00:00
|
|
|
void unregDocumentItem(
|
|
|
|
not_null<const DocumentData*> document,
|
|
|
|
not_null<ItemBase*> item) {
|
2016-04-10 18:18:26 +00:00
|
|
|
if (documentItemsMap) {
|
|
|
|
auto i = documentItemsMap->find(document);
|
|
|
|
if (i != documentItemsMap->cend()) {
|
2018-01-17 16:21:01 +00:00
|
|
|
if (i->second.remove(item) && i->second.empty()) {
|
2016-04-10 18:18:26 +00:00
|
|
|
documentItemsMap->erase(i);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 16:21:01 +00:00
|
|
|
if (documentItemsMap->empty()) {
|
|
|
|
documentItemsMap.clear();
|
|
|
|
}
|
2016-04-10 18:18:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2016-04-04 21:09:46 +00:00
|
|
|
} // namespace Layout
|
|
|
|
} // namespace InlineBots
|