2
0
mirror of https://github.com/telegramdesktop/tdesktop synced 2025-08-25 11:47:29 +00:00
tdesktop/Telegram/SourceFiles/ui/chat/attach/attach_prepare.cpp

183 lines
4.7 KiB
C++
Raw Normal View History

/*
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 "ui/chat/attach/attach_prepare.h"
2020-10-17 12:51:55 +03:00
#include "ui/chat/attach/attach_send_files_way.h"
#include "core/mime_type.h"
namespace Ui {
namespace {
constexpr auto kMaxAlbumCount = 10;
} // namespace
PreparedFile::PreparedFile(const QString &path) : path(path) {
}
PreparedFile::PreparedFile(PreparedFile &&other) = default;
2020-10-17 12:51:55 +03:00
PreparedFile &PreparedFile::operator=(PreparedFile &&other) = default;
PreparedFile::~PreparedFile() = default;
PreparedList PreparedList::Reordered(
PreparedList &&list,
std::vector<int> order) {
Expects(list.error == PreparedList::Error::None);
Expects(list.files.size() == order.size());
auto result = PreparedList(list.error, list.errorData);
result.files.reserve(list.files.size());
for (auto index : order) {
result.files.push_back(std::move(list.files[index]));
}
return result;
}
void PreparedList::mergeToEnd(PreparedList &&other, bool cutToAlbumSize) {
if (error != Error::None) {
return;
}
if (other.error != Error::None) {
error = other.error;
errorData = other.errorData;
return;
}
files.reserve(std::min(
size_t(cutToAlbumSize ? kMaxAlbumCount : INT_MAX),
files.size() + other.files.size()));
for (auto &file : other.files) {
if (cutToAlbumSize && files.size() == kMaxAlbumCount) {
break;
}
files.push_back(std::move(file));
}
2020-10-16 10:52:55 +03:00
}
bool PreparedList::canBeSentInSlowmode() const {
return canBeSentInSlowmodeWith(PreparedList());
}
bool PreparedList::canBeSentInSlowmodeWith(const PreparedList &other) const {
if (!filesToProcess.empty() || !other.filesToProcess.empty()) {
2020-10-16 10:52:55 +03:00
return false;
} else if (files.size() + other.files.size() < 2) {
2020-10-16 10:52:55 +03:00
return true;
} else if (files.size() + other.files.size() > kMaxAlbumCount) {
2020-10-16 10:52:55 +03:00
return false;
}
2020-10-16 10:52:55 +03:00
auto &&all = ranges::view::concat(files, other.files);
const auto hasNonGrouping = ranges::contains(
all,
PreparedFile::AlbumType::None,
&PreparedFile::type);
2020-10-16 10:52:55 +03:00
const auto hasFiles = ranges::contains(
all,
2020-10-16 10:52:55 +03:00
PreparedFile::AlbumType::File,
&PreparedFile::type);
const auto hasVideos = ranges::contains(
all,
2020-10-16 10:52:55 +03:00
PreparedFile::AlbumType::Video,
&PreparedFile::type);
// File-s and Video-s never can be grouped.
return !hasNonGrouping && (!hasFiles || !hasVideos);
}
2020-10-17 12:51:55 +03:00
bool PreparedList::canAddCaption(bool sendingAlbum) const {
2020-10-16 10:52:55 +03:00
if (!filesToProcess.empty()
|| files.empty()
|| files.size() > kMaxAlbumCount) {
return false;
}
if (files.size() == 1) {
Assert(files.front().information != nullptr);
const auto isSticker = Core::IsMimeSticker(
files.front().information->filemime)
|| files.front().path.endsWith(
qstr(".tgs"),
Qt::CaseInsensitive);
2020-10-16 10:52:55 +03:00
return !isSticker;
2020-10-17 12:51:55 +03:00
} else if (!sendingAlbum) {
2020-10-16 10:52:55 +03:00
return false;
}
const auto hasFiles = ranges::contains(
files,
PreparedFile::AlbumType::File,
&PreparedFile::type);
2020-10-17 12:51:55 +03:00
const auto hasNotGrouped = ranges::contains(
files,
PreparedFile::AlbumType::None,
&PreparedFile::type);
return !hasFiles && !hasNotGrouped;
}
int MaxAlbumItems() {
return kMaxAlbumCount;
}
2020-10-13 20:15:52 +03:00
bool ValidateThumbDimensions(int width, int height) {
return (width > 0)
&& (height > 0)
&& (width < 20 * height)
&& (height < 20 * width);
}
2020-10-17 12:51:55 +03:00
std::vector<PreparedGroup> DivideByGroups(
PreparedList &&list,
SendFilesWay way,
bool slowmode) {
const auto sendImagesAsPhotos = way.sendImagesAsPhotos();
const auto groupFiles = way.groupFiles() || slowmode;
auto group = Ui::PreparedList();
// For groupType Type::Video means media album,
// Type::File means file album,
// Type::None means no grouping.
using Type = Ui::PreparedFile::AlbumType;
auto groupType = Type::None;
auto result = std::vector<PreparedGroup>();
auto pushGroup = [&] {
result.push_back(PreparedGroup{
.list = base::take(group),
.grouped = (groupType != Type::None)
});
};
for (auto i = 0; i != list.files.size(); ++i) {
auto &file = list.files[i];
const auto fileGroupType = (file.type == Type::Video)
? (groupFiles ? Type::Video : Type::None)
: (file.type == Type::Photo)
? ((groupFiles && sendImagesAsPhotos)
? Type::Video
: (groupFiles && !sendImagesAsPhotos)
? Type::File
: Type::None)
: (file.type == Type::File)
? (groupFiles ? Type::File : Type::None)
: Type::None;
if ((!group.files.empty() && groupType != fileGroupType)
|| ((groupType != Type::None)
&& (group.files.size() == Ui::MaxAlbumItems()))) {
pushGroup();
}
group.files.push_back(std::move(file));
groupType = fileGroupType;
}
if (!group.files.empty()) {
pushGroup();
}
return result;
}
} // namespace Ui