attach_prepare.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "ui/chat/attach/attach_prepare.h"
  8. #include "ui/rp_widget.h"
  9. #include "ui/widgets/popup_menu.h"
  10. #include "ui/chat/attach/attach_send_files_way.h"
  11. #include "ui/image/image_prepare.h"
  12. #include "ui/ui_utility.h"
  13. #include "core/mime_type.h"
  14. namespace Ui {
  15. namespace {
  16. constexpr auto kMaxAlbumCount = 10;
  17. } // namespace
  18. PreparedFile::PreparedFile(const QString &path) : path(path) {
  19. }
  20. PreparedFile::PreparedFile(PreparedFile &&other) = default;
  21. PreparedFile &PreparedFile::operator=(PreparedFile &&other) = default;
  22. PreparedFile::~PreparedFile() = default;
  23. bool PreparedFile::canBeInAlbumType(AlbumType album) const {
  24. return CanBeInAlbumType(type, album);
  25. }
  26. bool PreparedFile::isSticker() const {
  27. Expects(information != nullptr);
  28. return (type == PreparedFile::Type::Photo)
  29. && Core::IsMimeSticker(information->filemime);
  30. }
  31. bool PreparedFile::isVideoFile() const {
  32. Expects(information != nullptr);
  33. using Video = Ui::PreparedFileInformation::Video;
  34. return (type == PreparedFile::Type::Video)
  35. && v::is<Video>(information->media)
  36. && !v::get<Video>(information->media).isGifv;
  37. }
  38. bool PreparedFile::isGifv() const {
  39. Expects(information != nullptr);
  40. using Video = Ui::PreparedFileInformation::Video;
  41. return (type == PreparedFile::Type::Video)
  42. && v::is<Video>(information->media)
  43. && v::get<Video>(information->media).isGifv;
  44. }
  45. AlbumType PreparedFile::albumType(bool sendImagesAsPhotos) const {
  46. switch (type) {
  47. case Type::Photo:
  48. return sendImagesAsPhotos ? AlbumType::PhotoVideo : AlbumType::File;
  49. case Type::Video:
  50. return AlbumType::PhotoVideo;
  51. case Type::Music:
  52. return AlbumType::Music;
  53. case Type::File:
  54. return AlbumType::File;
  55. case Type::None:
  56. return AlbumType::None;
  57. }
  58. Unexpected("PreparedFile::type in PreparedFile::albumType().");
  59. }
  60. bool CanBeInAlbumType(PreparedFile::Type type, AlbumType album) {
  61. Expects(album != AlbumType::None);
  62. using Type = PreparedFile::Type;
  63. switch (album) {
  64. case AlbumType::PhotoVideo:
  65. return (type == Type::Photo) || (type == Type::Video);
  66. case AlbumType::Music:
  67. return (type == Type::Music);
  68. case AlbumType::File:
  69. return (type == Type::Photo) || (type == Type::File);
  70. }
  71. Unexpected("AlbumType in CanBeInAlbumType.");
  72. }
  73. bool InsertTextOnImageCancel(const QString &text) {
  74. return !text.isEmpty() && !text.startsWith(u"data:image"_q);
  75. }
  76. PreparedList PreparedList::Reordered(
  77. PreparedList &&list,
  78. std::vector<int> order) {
  79. Expects(list.error == PreparedList::Error::None);
  80. Expects(list.files.size() == order.size());
  81. auto result = PreparedList(list.error, list.errorData);
  82. result.files.reserve(list.files.size());
  83. for (auto index : order) {
  84. result.files.push_back(std::move(list.files[index]));
  85. }
  86. return result;
  87. }
  88. void PreparedList::mergeToEnd(PreparedList &&other, bool cutToAlbumSize) {
  89. if (error != Error::None) {
  90. return;
  91. }
  92. if (other.error != Error::None) {
  93. error = other.error;
  94. errorData = other.errorData;
  95. return;
  96. }
  97. files.reserve(std::min(
  98. size_t(cutToAlbumSize ? kMaxAlbumCount : INT_MAX),
  99. files.size() + other.files.size()));
  100. for (auto &file : other.files) {
  101. if (cutToAlbumSize && files.size() == kMaxAlbumCount) {
  102. break;
  103. }
  104. files.push_back(std::move(file));
  105. }
  106. }
  107. bool PreparedList::canBeSentInSlowmode() const {
  108. return canBeSentInSlowmodeWith(PreparedList());
  109. }
  110. bool PreparedList::canBeSentInSlowmodeWith(const PreparedList &other) const {
  111. if (!filesToProcess.empty() || !other.filesToProcess.empty()) {
  112. return false;
  113. } else if (files.size() + other.files.size() < 2) {
  114. return true;
  115. } else if (files.size() + other.files.size() > kMaxAlbumCount) {
  116. return false;
  117. }
  118. using Type = PreparedFile::Type;
  119. auto &&all = ranges::views::concat(files, other.files);
  120. const auto has = [&](Type type) {
  121. return ranges::contains(all, type, &PreparedFile::type);
  122. };
  123. const auto hasNonGrouping = has(Type::None);
  124. const auto hasPhotos = has(Type::Photo);
  125. const auto hasFiles = has(Type::File);
  126. const auto hasVideos = has(Type::Video);
  127. const auto hasMusic = has(Type::Music);
  128. // File-s and Video-s never can be grouped.
  129. // Music-s can be grouped only with themselves.
  130. if (hasNonGrouping) {
  131. return false;
  132. } else if (hasFiles) {
  133. return !hasMusic && !hasVideos;
  134. } else if (hasVideos) {
  135. return !hasMusic && !hasFiles;
  136. } else if (hasMusic) {
  137. return !hasVideos && !hasFiles && !hasPhotos;
  138. }
  139. return !hasNonGrouping && (!hasFiles || !hasVideos);
  140. }
  141. bool PreparedList::canAddCaption(bool sendingAlbum, bool compress) const {
  142. if (!filesToProcess.empty()
  143. || files.empty()
  144. || files.size() > kMaxAlbumCount) {
  145. return false;
  146. }
  147. if (files.size() == 1) {
  148. Assert(files.front().information != nullptr);
  149. const auto isSticker = (!compress
  150. && Core::IsMimeSticker(files.front().information->filemime))
  151. || files.front().path.endsWith(u".tgs"_q, Qt::CaseInsensitive);
  152. return !isSticker;
  153. } else if (!sendingAlbum) {
  154. return false;
  155. }
  156. const auto hasFiles = ranges::contains(
  157. files,
  158. PreparedFile::Type::File,
  159. &PreparedFile::type);
  160. const auto hasMusic = ranges::contains(
  161. files,
  162. PreparedFile::Type::Music,
  163. &PreparedFile::type);
  164. const auto hasNotGrouped = ranges::contains(
  165. files,
  166. PreparedFile::Type::None,
  167. &PreparedFile::type);
  168. return !hasFiles && !hasMusic && !hasNotGrouped;
  169. }
  170. bool PreparedList::canMoveCaption(bool sendingAlbum, bool compress) const {
  171. if (!canAddCaption(sendingAlbum, compress)) {
  172. return false;
  173. } else if (files.size() != 1) {
  174. return true;
  175. }
  176. const auto &file = files.front();
  177. return (file.type == PreparedFile::Type::Video)
  178. || (file.type == PreparedFile::Type::Photo && compress);
  179. }
  180. bool PreparedList::canChangePrice(bool sendingAlbum, bool compress) const {
  181. return canMoveCaption(sendingAlbum, compress);
  182. }
  183. bool PreparedList::hasGroupOption(bool slowmode) const {
  184. if (slowmode || files.size() < 2) {
  185. return false;
  186. }
  187. using Type = PreparedFile::Type;
  188. auto lastType = Type::None;
  189. for (const auto &file : files) {
  190. if ((file.type == lastType)
  191. || (file.type == Type::Video && lastType == Type::Photo)
  192. || (file.type == Type::Photo && lastType == Type::Video)
  193. || (file.type == Type::File && lastType == Type::Photo)
  194. || (file.type == Type::Photo && lastType == Type::File)) {
  195. if (lastType != Type::None) {
  196. return true;
  197. }
  198. }
  199. lastType = file.type;
  200. }
  201. return false;
  202. }
  203. bool PreparedList::hasSendImagesAsPhotosOption(bool slowmode) const {
  204. using Type = PreparedFile::Type;
  205. return slowmode
  206. ? ((files.size() == 1) && (files.front().type == Type::Photo))
  207. : ranges::contains(files, Type::Photo, &PreparedFile::type);
  208. }
  209. bool PreparedList::canHaveEditorHintLabel() const {
  210. for (const auto &file : files) {
  211. if ((file.type == PreparedFile::Type::Photo)
  212. && !Core::IsMimeSticker(file.information->filemime)) {
  213. return true;
  214. }
  215. }
  216. return false;
  217. }
  218. bool PreparedList::hasSticker() const {
  219. return ranges::any_of(files, &PreparedFile::isSticker);
  220. }
  221. bool PreparedList::hasSpoilerMenu(bool compress) const {
  222. const auto allAreVideo = !ranges::any_of(files, [](const auto &f) {
  223. using Type = Ui::PreparedFile::Type;
  224. return (f.type != Type::Video);
  225. });
  226. const auto allAreMedia = !ranges::any_of(files, [](const auto &f) {
  227. using Type = Ui::PreparedFile::Type;
  228. return (f.type != Type::Photo) && (f.type != Type::Video);
  229. });
  230. return allAreVideo || (allAreMedia && compress);
  231. }
  232. std::shared_ptr<PreparedBundle> PrepareFilesBundle(
  233. std::vector<PreparedGroup> groups,
  234. SendFilesWay way,
  235. TextWithTags caption,
  236. bool ctrlShiftEnter) {
  237. auto totalCount = 0;
  238. for (const auto &group : groups) {
  239. totalCount += group.list.files.size();
  240. }
  241. const auto sendComment = !caption.text.isEmpty()
  242. && (groups.size() != 1 || !groups.front().sentWithCaption());
  243. return std::make_shared<PreparedBundle>(PreparedBundle{
  244. .groups = std::move(groups),
  245. .way = way,
  246. .caption = std::move(caption),
  247. .totalCount = totalCount + (sendComment ? 1 : 0),
  248. .sendComment = sendComment,
  249. .ctrlShiftEnter = ctrlShiftEnter,
  250. });
  251. }
  252. int MaxAlbumItems() {
  253. return kMaxAlbumCount;
  254. }
  255. bool ValidateThumbDimensions(int width, int height) {
  256. return (width > 0)
  257. && (height > 0)
  258. && (width <= 20 * height)
  259. && (height <= 20 * width);
  260. }
  261. std::vector<PreparedGroup> DivideByGroups(
  262. PreparedList &&list,
  263. SendFilesWay way,
  264. bool slowmode) {
  265. const auto sendImagesAsPhotos = way.sendImagesAsPhotos();
  266. const auto groupFiles = way.groupFiles() || slowmode;
  267. auto group = Ui::PreparedList();
  268. using Type = Ui::PreparedFile::Type;
  269. auto groupType = AlbumType::None;
  270. auto result = std::vector<PreparedGroup>();
  271. auto pushGroup = [&] {
  272. const auto type = (group.files.size() > 1)
  273. ? groupType
  274. : AlbumType::None;
  275. result.push_back(PreparedGroup{
  276. .list = base::take(group),
  277. .type = type,
  278. });
  279. };
  280. for (auto i = 0; i != list.files.size(); ++i) {
  281. auto &file = list.files[i];
  282. const auto fileGroupType = (file.type == Type::Music)
  283. ? (groupFiles ? AlbumType::Music : AlbumType::None)
  284. : (file.type == Type::Video)
  285. ? (groupFiles ? AlbumType::PhotoVideo : AlbumType::None)
  286. : (file.type == Type::Photo)
  287. ? ((groupFiles && sendImagesAsPhotos)
  288. ? AlbumType::PhotoVideo
  289. : (groupFiles && !sendImagesAsPhotos)
  290. ? AlbumType::File
  291. : AlbumType::None)
  292. : (file.type == Type::File)
  293. ? (groupFiles ? AlbumType::File : AlbumType::None)
  294. : AlbumType::None;
  295. if ((!group.files.empty() && groupType != fileGroupType)
  296. || ((groupType != AlbumType::None)
  297. && (group.files.size() == Ui::MaxAlbumItems()))) {
  298. pushGroup();
  299. }
  300. group.files.push_back(std::move(file));
  301. groupType = fileGroupType;
  302. }
  303. if (!group.files.empty()) {
  304. pushGroup();
  305. }
  306. return result;
  307. }
  308. QPixmap PrepareSongCoverForThumbnail(QImage image, int size) {
  309. const auto scaledSize = image.size().scaled(
  310. size,
  311. size,
  312. Qt::KeepAspectRatioByExpanding);
  313. using Option = Images::Option;
  314. const auto ratio = style::DevicePixelRatio();
  315. return PixmapFromImage(Images::Prepare(
  316. std::move(image),
  317. scaledSize * ratio,
  318. {
  319. .colored = &st::songCoverOverlayFg,
  320. .options = Option::RoundCircle,
  321. .outer = { size, size },
  322. }));
  323. }
  324. QPixmap BlurredPreviewFromPixmap(QPixmap pixmap, RectParts corners) {
  325. const auto image = pixmap.toImage();
  326. const auto skip = st::roundRadiusLarge * image.devicePixelRatio();
  327. auto small = image.copy(
  328. skip,
  329. skip,
  330. image.width() - 2 * skip,
  331. image.height() - 2 * skip
  332. ).scaled(
  333. 40,
  334. 40,
  335. Qt::KeepAspectRatioByExpanding,
  336. Qt::SmoothTransformation);
  337. using namespace Images;
  338. return PixmapFromImage(Prepare(
  339. Blur(std::move(small), true),
  340. image.size(),
  341. { .options = RoundOptions(ImageRoundRadius::Large, corners) }));
  342. }
  343. } // namespace Ui