2018-01-26 15:40:11 +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
|
|
|
|
*/
|
|
|
|
#include "history/history_item_text.h"
|
|
|
|
|
|
|
|
#include "history/history_item.h"
|
|
|
|
#include "history/history_item_components.h"
|
|
|
|
#include "data/data_media_types.h"
|
|
|
|
#include "data/data_web_page.h"
|
2018-01-27 17:26:24 +00:00
|
|
|
#include "data/data_groups.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
#include "data/data_peer.h"
|
2018-01-26 15:40:11 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2020-10-10 09:15:37 +00:00
|
|
|
#include "ui/text/text_options.h"
|
2018-01-26 15:40:11 +00:00
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData WrapAsReply(
|
|
|
|
TextForMimeData &&text,
|
2018-01-26 15:40:11 +00:00
|
|
|
not_null<HistoryItem*> to) {
|
|
|
|
const auto name = to->author()->name;
|
2019-04-08 15:10:06 +00:00
|
|
|
auto result = TextForMimeData();
|
|
|
|
result.reserve(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_in_reply_to(tr::now).size()
|
2018-01-26 15:40:11 +00:00
|
|
|
+ name.size()
|
|
|
|
+ 4
|
2019-04-08 15:10:06 +00:00
|
|
|
+ text.expanded.size());
|
|
|
|
return result.append('['
|
2019-06-19 15:09:03 +00:00
|
|
|
).append(tr::lng_in_reply_to(tr::now)
|
2018-01-26 15:40:11 +00:00
|
|
|
).append(' '
|
|
|
|
).append(name
|
2019-04-08 15:10:06 +00:00
|
|
|
).append(qstr("]\n")
|
|
|
|
).append(std::move(text));
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData WrapAsForwarded(
|
|
|
|
TextForMimeData &&text,
|
2018-01-26 15:40:11 +00:00
|
|
|
not_null<HistoryMessageForwarded*> forwarded) {
|
2019-04-08 15:10:06 +00:00
|
|
|
auto info = forwarded->text.toTextForMimeData();
|
|
|
|
auto result = TextForMimeData();
|
|
|
|
result.reserve(
|
|
|
|
info.expanded.size() + 4 + text.expanded.size(),
|
|
|
|
info.rich.entities.size() + text.rich.entities.size());
|
|
|
|
return result.append('['
|
|
|
|
).append(std::move(info)
|
|
|
|
).append(qstr("]\n")
|
|
|
|
).append(std::move(text));
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData WrapAsItem(
|
2018-01-27 17:26:24 +00:00
|
|
|
not_null<HistoryItem*> item,
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData &&result) {
|
2018-01-27 17:26:24 +00:00
|
|
|
if (const auto reply = item->Get<HistoryMessageReply>()) {
|
|
|
|
if (const auto message = reply->replyToMsg) {
|
|
|
|
result = WrapAsReply(std::move(result), message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (const auto forwarded = item->Get<HistoryMessageForwarded>()) {
|
|
|
|
result = WrapAsForwarded(std::move(result), forwarded);
|
|
|
|
}
|
2019-03-26 14:00:47 +00:00
|
|
|
return std::move(result);
|
2018-01-27 17:26:24 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData HistoryItemText(not_null<HistoryItem*> item) {
|
2018-01-26 15:40:11 +00:00
|
|
|
const auto media = item->media();
|
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
auto mediaResult = media ? media->clipboardText() : TextForMimeData();
|
|
|
|
auto textResult = mediaResult.empty()
|
2018-01-27 17:26:24 +00:00
|
|
|
? item->clipboardText()
|
2019-04-08 15:10:06 +00:00
|
|
|
: TextForMimeData();
|
2018-01-26 15:40:11 +00:00
|
|
|
auto logEntryOriginalResult = [&] {
|
|
|
|
const auto entry = item->Get<HistoryMessageLogEntryOriginal>();
|
|
|
|
if (!entry) {
|
2019-04-08 15:10:06 +00:00
|
|
|
return TextForMimeData();
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|
|
|
|
const auto title = TextUtilities::SingleLine(entry->page->title.isEmpty()
|
|
|
|
? entry->page->author
|
|
|
|
: entry->page->title);
|
2019-04-08 15:10:06 +00:00
|
|
|
auto titleResult = TextForMimeData::Rich(
|
|
|
|
TextUtilities::ParseEntities(
|
|
|
|
title,
|
|
|
|
Ui::WebpageTextTitleOptions().flags));
|
|
|
|
auto descriptionResult = TextForMimeData::Rich(
|
|
|
|
base::duplicate(entry->page->description));
|
|
|
|
if (titleResult.empty()) {
|
2018-01-26 15:40:11 +00:00
|
|
|
return descriptionResult;
|
2019-04-08 15:10:06 +00:00
|
|
|
} else if (descriptionResult.empty()) {
|
2018-01-26 15:40:11 +00:00
|
|
|
return titleResult;
|
|
|
|
}
|
2019-04-08 15:10:06 +00:00
|
|
|
titleResult.append('\n').append(std::move(descriptionResult));
|
2018-01-26 15:40:11 +00:00
|
|
|
return titleResult;
|
|
|
|
}();
|
|
|
|
auto result = textResult;
|
2019-04-08 15:10:06 +00:00
|
|
|
if (result.empty()) {
|
2018-01-26 15:40:11 +00:00
|
|
|
result = std::move(mediaResult);
|
2019-04-08 15:10:06 +00:00
|
|
|
} else if (!mediaResult.empty()) {
|
|
|
|
result.append(qstr("\n\n")).append(std::move(mediaResult));
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|
2019-04-08 15:10:06 +00:00
|
|
|
if (result.empty()) {
|
2018-01-26 15:40:11 +00:00
|
|
|
result = std::move(logEntryOriginalResult);
|
2019-04-08 15:10:06 +00:00
|
|
|
} else if (!logEntryOriginalResult.empty()) {
|
|
|
|
result.append(qstr("\n\n")).append(std::move(logEntryOriginalResult));
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|
2018-01-27 17:26:24 +00:00
|
|
|
return WrapAsItem(item, std::move(result));
|
|
|
|
}
|
|
|
|
|
2019-04-08 15:10:06 +00:00
|
|
|
TextForMimeData HistoryGroupText(not_null<const Data::Group*> group) {
|
2018-01-27 17:26:24 +00:00
|
|
|
Expects(!group->items.empty());
|
|
|
|
|
2020-10-29 16:12:43 +00:00
|
|
|
const auto columnAlbum = [&] {
|
|
|
|
const auto item = group->items.front();
|
|
|
|
if (const auto media = item->media()) {
|
|
|
|
if (const auto document = media->document()) {
|
|
|
|
return !document->isVideoFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}();
|
|
|
|
const auto hasCaption = [](not_null<HistoryItem*> item) {
|
|
|
|
return !item->clipboardText().empty();
|
|
|
|
};
|
|
|
|
if (columnAlbum) {
|
|
|
|
const auto simple = !ranges::any_of(group->items, hasCaption);
|
|
|
|
if (!simple) {
|
|
|
|
auto result = TextForMimeData();
|
|
|
|
for (const auto &item : group->items) {
|
|
|
|
if (result.empty()) {
|
|
|
|
result = HistoryItemText(item);
|
|
|
|
} else {
|
|
|
|
result.append(qstr("\n\n")).append(HistoryItemText(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
2018-01-27 17:26:24 +00:00
|
|
|
auto caption = [&] {
|
2019-04-08 15:10:06 +00:00
|
|
|
auto &&nonempty = ranges::view::all(
|
|
|
|
group->items
|
2020-10-29 16:12:43 +00:00
|
|
|
) | ranges::view::filter(
|
|
|
|
hasCaption
|
|
|
|
) | ranges::view::take(2);
|
2019-04-08 15:10:06 +00:00
|
|
|
auto first = nonempty.begin();
|
|
|
|
auto end = nonempty.end();
|
|
|
|
if (first == end) {
|
|
|
|
return TextForMimeData();
|
2018-01-27 17:26:24 +00:00
|
|
|
}
|
2019-04-08 15:10:06 +00:00
|
|
|
auto result = (*first)->clipboardText();
|
|
|
|
return (++first == end) ? result : TextForMimeData();
|
2018-01-27 17:26:24 +00:00
|
|
|
}();
|
|
|
|
return WrapAsItem(group->items.back(), Data::WithCaptionClipboardText(
|
2019-06-19 15:09:03 +00:00
|
|
|
tr::lng_in_dlg_album(tr::now),
|
2018-01-27 17:26:24 +00:00
|
|
|
std::move(caption)));
|
2018-01-26 15:40:11 +00:00
|
|
|
}
|